Dinesh
Dinesh

Reputation: 1868

How to achieve persistent storage in NeDB?

I tried NeDB in node-webkit it's working fine on in memory data but not able to store in persistent storage.

Upvotes: 4

Views: 5997

Answers (2)

flaky
flaky

Reputation: 7404

definitely no node-webkit or nedb expert but this is how I did it and it worked.

As already mentioned by mvanderw in the comments, definitely make sure to check the autoload option.

This is for example my configuration for a simple node-webkit/ angular todo app:

var Datastore = require('nedb'),                                                                                                                                              
    path = require('path'),
    db = new Datastore({ filename: path.join(require('nw.gui').App.dataPath, 'todo.db'), autoload: true });

When I restart the app, all todos are still there and I'm ready to go.

Hope this helps

Edit: Example as requested by Scott

var Datastore = require('nedb'), 
path = require('path'),
db = new Datastore({
  filename:path.join(require('nw.gui').App.dataPath, 'todo.db'),
  autoload: true
}); 

var todoServices = angular.module('todoServices', []);

todoServices.factory('Todo', function($q) {
  return { 
    getAll: function(){ 
      var defer = $q.defer();
      db.find({ 
        //...some criteria
      },
      function(err, docs) {
        defer.resolve(docs);
      });
      return defer.promise;
    }, //...moar code
  }
});

Something like this...

Upvotes: 6

roland
roland

Reputation: 7775

C:\Users\Dinesh\AppData\Local\FrameLess\nedb-data is where node-webkit uncompressed the app; it means that your app has been packaged as .nw, which is a smarter zip file. As a consequence it must be unzipped in a tmp folder to execute, which is why you found your db in AppData.

In your developer tool you can always know which folder your app has been unzipped into by typing: process.env. Your TMPDIR property is where your db sits in (TMPDIR is a Mac OS X property, it might be named differently on Windows).

You can execute your nw app without packaging it. Basically you can drag your folder onto nw.exe; or typing nw.exe yourAppFolder; or package your app as .nw, as you did.

Read the following guideline once again: https://github.com/rogerwang/node-webkit/wiki/How-to-run-apps

Upvotes: 1

Related Questions