LokiJS: Can't get persistent data in Cordova (using cordova-fs-adapter)

When I serve the app using the Phonegap Desktop app, Chrome pops up a dialog asking me to allow storing files on this device. So, that’s promising… and everything appears to work as expected, but the data isn’t persistent. If I refresh the page, all data is lost. This is also the case when I build the app using Phonegap Build and test on my iPhone. If I close the app, data is lost.

I’ve noticed an FS adapter console error: Unable to resolve local file system URL, but I’m not sure how to fix that.

Here’s how I’m creating the database:

var loki = require('lokijs'),
    products,
    prices;

var LokiCordovaFSAdapter = require("loki-cordova-fs-adapter");

var adapter = new LokiCordovaFSAdapter({"prefix": "loki"}),
    db = new loki('pricelog_db', {
        autosave: true,
        autosaveInterval: 1000,
        adapter: adapter
    });

Which is all in a store.js file, which is then imported into my main js file (this is a Vue app). I’m confused about the whole device ready thing, and wonder if this is related to that? Sorry if I’m leaving out any relevant info or have made some glaring mistake. But any advice would be appreciated, thanks!

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 28 (1 by maintainers)

Most upvoted comments

Progress!

Works flawlessly on iOS! Data persists after closing the app and even restart. Cordova docs recommend adding <preference name="iosPersistentFileLocation" value="Library" /> to the config.xml, similar to how you have the location specified for Android, But even without that, seems to work fine…

On Windows, we’re back where we started, with an error on getFile

_getFile: {
            value: function _getFile(name, handleSuccess, handleError) {
                var _this = this;

                window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
                    var fileName = _this.options.prefix + "__" + name;
                    dir.getFile(fileName, { create: true }, handleSuccess, handleError);
                }, function (err) {
                    throw new LokiCordovaFSAdapterError("Unable to resolve local file system URL" + JSON.stringify(err));
                });
            }
        },

I’m seeing conflicting things on whether or not you can use cordova.file.dataDirectory on the browser platform. If you check the docs on where to store files you’ll notice “browser” isn’t listed for any of those locations. Some Stack posts say you have to manually write out the path. I’ve seen a few others that say dataDirectory should work. I haven’t been able to get anything to work…

If that is indeed the problem, perhaps the adapter should be extended to allow changing the path (if that isn’t an option already?). Otherwise the actual adapter has to be edited for each platform. iOS has a similar concern, dataDirectory doesn’t back up or sync, for that you have to use cordova.file.syncedDataDirectory. But if you change that in the adapter, I believe you’d have to change it back for Android.

But for now, I took a page from @obeliskos’s earlier dev work-around, using indexed adapter until I’m ready to build.

// BROWSER PLATFORM
var LokiIndexedAdapter = require("lokijs/src/loki-indexed-adapter.js");

And then within the deviceReady listener

  // var adapter = new LokiCordovaFs({ prefix: 'todos' });
  // BROWSER PLATFORM
  var adapter = new LokiIndexedAdapter("HelloWorld");

I also removed the saves from the add and delete handlers, and added autosave. Still works… Can’t believe my eyes lol

@Kanaye thanks for that example… since that is a known ‘working’ project (and when i run it with platform ‘browser’ i still get the same error/exception), that must mean the error we have been dealing with stems from browser/chrome sandboxing as @chasebank has pointed out.

@Kanaye’s project ran unmodified with cordova platform osx 😃

I have installed visual studio 2017 community (with visual studio cordova development components selected during vs install) on windows 10. Kanaye’s project ran unmodified when running the /platform/windows solution through Visual Studio 2017 debugger as a UWP application.

I managed to get chasebank’s example to run its /platform/windows solution through visual studio ide/debugger as well. @chasebank : at a minimum, I had to fix your autocallback syntax (should be just ‘autoloadCallback: databaseInitialize’) and remove the extra manual call to databaseInitialize and your example ran fresh from your github repo clone.

You can decide which development workflow you prefer (platform browser or platform windows + visual studio or some other workflow). If you go the visual studio route, after making changes to the index.js and running npm run build to browserify it, i still had to ‘cordova prepare’ it to update the ‘plaform/windows’ copy of it for running within visual studio. You will also probably want to turn on develop mode in window 10 settings so that you can run your UWP app unsigned.

I have created a simple todo test app using only lokijs, the cordova-fs-adapter and the dom api. I couldn’t test on a windows or ios device but it works fine on android. @chasebank can you test it on ios/windows. You should only need to clone/download the repository and run npm i --save-dev and cordova add <your platform>.