Skip to content

Creating loading the database

Louis Chatriot edited this page Jul 10, 2013 · 2 revisions

You can use NeDB as an in-memory only datastore or as a persistent datastore. One datastore is the equivalent of a MongoDB collection. The constructor is used as follows new Datastore(options) where options is an object with the following fields:

  • filename (optional): path to the file where the data is persisted. If left blank, the datastore is automatically considered in-memory only.
  • inMemoryOnly (optional, defaults to false): as the name implies.
  • autoload (optional, defaults to false): if used, the database will automatically be loaded from the datafile upon creation (you don't need to call loadDatabase). Any command issued before load is finished is buffered and will be executed when load is done.
  • nodeWebkitAppName (optional): if you are using NeDB from whithin a Node Webkit app, specify its name (the same one you use in the package.json) in this field and the filename will be relative to the directory Node Webkit uses to store the rest of the application's data (local storage etc.). It works on Linux, OS X and Windows.

If you use a persistent datastore without the autoload option, you need to call loadDatabase manually. This function fetches the data from datafile and prepares the database. Don't forget it! If you use a persistent datastore, no command (insert, find, update, remove) will be executed before loadDatabase is called, so make sure to call it yourself or use the autoload option.

// Type 1: In-memory only datastore (no need to load the database)
var Datastore = require('nedb')
  , db = new Datastore();


// Type 2: Persistent datastore with manual loading
var Datastore = require('nedb')
  , db = new Datastore({ filename: 'path/to/datafile' });
db.loadDatabase(function (err) {    // Callback is optional
  // Now commands will be executed
});


// Type 3: Persistent datastore with automatic loading
var Datastore = require('nedb')
  , db = new Datastore({ filename: 'path/to/datafile', autoload: true });
// You can issue commands right away


// Type 4: Persistent datastore for a Node Webkit app called 'nwtest'
// For example on Linux, the datafile will be ~/.config/nwtest/nedb-data/something.db
var Datastore = require('nedb')
  , db = new Datastore({ filename: 'something.db', nodeWebkitAppName: 'nwtest' });


// Of course you can create multiple datastores if you need several
// collections. In this case it's usually a good idea to use autoload for all collections.
db = {};
db.users = new Datastore('path/to/users.db');
db.robots = new Datastore('path/to/robots.db');

// You need to load each database (here we do it asynchronously)
db.users.loadDatabase();
db.robots.loadDatabase();
Clone this wiki locally