Search code examples
node.jsnode-webkitnedb

How ensure default data in NeDB?


I'm trying to use NeDB as storage for my data in node-webkit application. I have the single collection named config.db:

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

When user opens node-webkit application first time my config.db should have default data like:

{
   color: "red",
   font: 'bold'
   ...
}

Does NeDB have option for providing default data if there are no yet? Or What it the best way to save it if config.db is empty (in case if user opens node-webkit application first time)?


Solution

  • As far as I know NeDB does not have an option to create initial data.

    I think the easiest way to achieve this is to simply query whether there is data. If counting documents returns 0, obviously the initial data have not yet been saved, so you should do this now.

    If you include this check in the startup code of your application, it will automatically initialize the data on first run, and afterwards simply do nothing.