I am currently working on an app by using Onsen UI+Cordova and trying to use LokiJS as my server-less database. As per LokiJS's documents:
If nothing is specified, Loki tries default methods (fs in Node, localStorage in browser and cordova).
Does that mean I can't write the database file *.json to filesystem ie. SDCard or internal storage somewhere?
LokiJS allows you to persist a database you're using as a JSON file, as you can see from the docs of the save
function:
Saves the db, according to the persistence adapter you specified in the Loki constructor. If nothing is specified, Loki tries default methods (fs in Node, localStorage in browser and cordova).
This means that by default localStorage is used in cordova, which will already be persisted to the file system.
You can then use Loki's load()
function to load a specific DB name.
Note that you don't need to do this explicitly, you can just create a new DB in Loki by doing:
var db = new loki('db_name.json', {
autosave: true,
autosaveInterval: 60 * 1000, //every 60 seconds
autoload: true
});
If you specify autoload
and autosave
options you don't need to handle anything by yourself, so data will be persisted automatically using localStorage on cordova.
Also, if you want to do things differently, i.e. not use localStorage, you can create your own Adapter for saving on the file system or even on a server or cloud storage. This requires you to write an Adapter of course, you can see an example in Loki's gitHub here (it's a jquery AJAX adapter sample)
EDIT: As @Joe Minichino pointed out, there is a ready-made Cordova FS adapter for Loki, should work right out of the box, check it out!