Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LokiJS with cordova

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?

like image 985
Jerell Fox Avatar asked Feb 10 '23 09:02

Jerell Fox


2 Answers

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!

like image 148
Sosdoc Avatar answered Feb 11 '23 23:02

Sosdoc


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

like image 31
Tolga Ozses Avatar answered Feb 11 '23 22:02

Tolga Ozses