How to achieve persistent storage in NeDB?

我只是一个虾纸丫 提交于 2019-12-09 06:47:45

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/19315550/how-to-achieve-persistent-storage-in-nedb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!