admin管理员组文章数量:1322205
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?
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?
Share Improve this question edited Apr 26, 2017 at 14:34 Tolga Ozses 3551 gold badge4 silver badges29 bronze badges asked Aug 7, 2015 at 9:54 Jerell FoxJerell Fox 431 silver badge4 bronze badges 6- 1 in addition to @sosdoc 's answer i will say that there is a ready-made cordova file-system adapter here: github./cosmith/loki-cordova-fs-adapter – Joe Minichino Commented Aug 8, 2015 at 5:30
- it's pretty cool ! I thanks a lot ! :) I'm trying cordova-file-api at the moment. – Jerell Fox Commented Aug 8, 2015 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 Commented Aug 11, 2015 at 12:03
- give us a star on github if you liked Loki ;) – Joe Minichino Commented Aug 11, 2015 at 18:45
- Sure ! I like LokiJS, – Jerell Fox Commented Aug 11, 2015 at 21:24
2 Answers
Reset to default 6LokiJS 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!
You can use this plugin for read/write access to the filesystem.
本文标签: javascriptLokiJS with cordovaStack Overflow
版权声明:本文标题:javascript - LokiJS with cordova - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742107563a2421092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论