Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 970
Dinesh Avatar asked Mar 23 '23 02:03

Dinesh


1 Answers

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...

like image 130
flaky Avatar answered Apr 10 '23 16:04

flaky