LokiJS with a corridor

I am currently working on an application using Onsen UI + Cordova and trying to use LokiJS as my serverless database. According to the LokiJS docs:

If nothing is specified, Loki tries to use the default methods (fs in Node, localStorage in the browser and cordova).

Does this mean that I cannot write the * .json database file to the file system, i.e. Sdcard or internal storage?

+4
source share
2 answers

LokiJS allows you to save the database that you use as the JSON file, as you can see from the save function docs:

Saves db according to persistence adapter specified in Loki constructor. If nothing is specified, Loki tries to default methods (fs in Node, localStorage in the browser and cordova).

This means that by default, localStorage is used in a cord that will already be stored on the file system.

Then you can use the Loki load() function to load a specific database name.

Note that this is not explicitly required , you can simply create a new database in Loki:

 var db = new loki('db_name.json', { autosave: true, autosaveInterval: 60 * 1000, //every 60 seconds autoload: true }); 

If you specify the autoload and autosave , you don’t need to process anything yourself, so the data will be saved automatically using localStorage on the cord.

In addition, if you want to do something differently, that is, not to use localStorage, you can create your own adapter for saving to the file system or even to the server or cloud storage . This requires you, of course, to write an Adapter, you can see an example in Loki gitHub here (this is an example of a jQuery AJAX adapter)

EDIT: As @Joe Minichino noted, there is a ready-made Cordova FS adapter for Loki , it should work right out of the box, check it out!

+6
source

You can use this plugin for read / write access to the file system.

+2
source

All Articles