4

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?

Tolga Ozses
  • 348
  • 4
  • 26
Jerell Fox
  • 43
  • 1
  • 4
  • 1
    in addition to @sosdoc 's answer i will say that there is a ready-made cordova file-system adapter here: https://github.com/cosmith/loki-cordova-fs-adapter – Joe Minichino Aug 08 '15 at 05:30
  • it's pretty cool ! I thanks a lot ! :) I'm trying cordova-file-api at the moment. – Jerell Fox Aug 08 '15 at 11:33
  • problem solved ! thanks for all of your answers ! :) but I have no idea how to use the adapter, so, I try to make a specific one operator to deal with cordova file api + json file + part of lokiJS function. now I can operate create, read, update and delete with lokiJS+file api ! thank you all ! :D – Jerell Fox Aug 11 '15 at 12:03
  • give us a star on github if you liked Loki ;) – Joe Minichino Aug 11 '15 at 18:45
  • Sure ! I like LokiJS, – Jerell Fox Aug 11 '15 at 21:24
  • hi i use loki for a Inonoc app but when I press f5 i lost all data, i mean it doesnt persit at all. Any suggestion? – SAMUEL OSPINA Feb 21 '16 at 18:47

2 Answers2

6

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!

Vale
  • 1,912
  • 16
  • 22
  • wow ! All I need is save all the data to an Independent json file. create/read/load/change using LokiJS, and Yes, in my app's feature, I will needs to transfer it to somewhere else, like cloud storage or private server. – Jerell Fox Aug 07 '15 at 10:14
  • @JerellFox you can do that by using `save()`, though you still need an adapter for saving the db's json somewhere else, if you look at the jquery ajax sample you can see it's pretty simple to implement one yourself. – Vale Aug 07 '15 at 10:18
  • Really ! I'm enormously grateful for your help. :) I will study it. ! :) – Jerell Fox Aug 07 '15 at 10:26
  • @JerellFox look at the samples, you just need to implement a `saveDatabase` and `loadDatabase` for an adapter, you could also have multiple adapters for saving on the file system and/or on a server (the jquery ajax one can already do that for you). Also, remember to upvote any helpful answers you get, it's the best way of saying thanks! – Vale Aug 07 '15 at 10:29
  • I wish I can do that for you. ;P but seems I have not enough reputation to do that. so sorry about it. :( – Jerell Fox Aug 07 '15 at 10:58
2

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

Tolga Ozses
  • 348
  • 4
  • 26