From 99fb91d7006e411ddd71769547be04432aa19e7c Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Mon, 8 Jul 2013 14:16:54 +0200 Subject: [PATCH] Updated readme and bumped version --- README.md | 37 ++++++++++++++++++++++++++++--------- lib/datastore.js | 7 +++++++ package.json | 2 +- test/db.test.js | 2 +- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 36158aa..8f910a0 100644 --- a/README.md +++ b/README.md @@ -33,29 +33,48 @@ It's a subset of MongoDB's API (the most used operations). The current API will 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. -* `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. * `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` - see below). 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. + +You need to call `loadDatabase` if you use a persistent datastore +without the `autoload` option. This function fetches the data from +datafile and prepares the database. **This is important!** 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. ```javascript -// In-memory only datastore +// In-memory only datastore (no need to load the database) var Datastore = require('nedb') , db = new Datastore(); -// Persistent datastore + +// 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 +}); + + +// Persistent datastore with automatic loading +var Datastore = require('nedb') + , db = new Datastore({ filename: 'path/to/datafile', autoload: true }); +// You can issue commands right away + + // 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' }); -db.loadDatabase(function (err) { // Callback is optional - // err is the error, if any -}); - - // Of course you can create multiple datastores if you need several // collections. For example: db = {}; @@ -373,7 +392,7 @@ db.insert({ somefield: 'nedb' }, function (err) { ### Speed NeDB is not intended to be a replacement of large-scale databases such as MongoDB, and as such was not designed for speed. That said, it is still pretty fast on the expected datasets, especially if you use indexing. On my machine (3 years old, no SSD), with a collection containing 10,000 documents, with indexing: * Insert: **5,950 ops/s** -* Find: **41,320 ops/s** +* Find: **25,440 ops/s** * Update: **4,490 ops/s** * Remove: **6,620 ops/s** diff --git a/lib/datastore.js b/lib/datastore.js index 9f647d1..9f1f4c1 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -14,6 +14,7 @@ var fs = require('fs') * Create a new collection * @param {String} options.filename Optional, datastore will be in-memory only if not provided * @param {Boolean} options.inMemoryOnly Optional, default to false + * @param {Boolean} options.autoload Optional, defaults to false * @param {Boolean} options.pipeline DEPRECATED, doesn't have any effect anymore * @param {Boolean} options.nodeWebkitAppName Optional, specify the name of your NW app if you want options.filename to be relative to the directory where * Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion) @@ -29,6 +30,7 @@ function Datastore (options) { options = options || {}; filename = options.filename; this.inMemoryOnly = options.inMemoryOnly || false; + this.autoload = options.autoload || false; } // Determine whether in memory or persistent @@ -44,7 +46,10 @@ function Datastore (options) { this.filename = customUtils.getNWAppFilename(options.nodeWebkitAppName, this.filename); } + // This new executor is ready if we don't use persistence + // If we do, it will only be ready once loadDatabase is called this.executor = new Executor(); + if (this.inMemoryOnly) { this.executor.ready = true; } // We keep internally the number of lines in the datafile // This will be used when/if I implement autocompacting when the datafile grows too big @@ -56,6 +61,8 @@ function Datastore (options) { // binary is always well-balanced this.indexes = {}; this.indexes._id = new Index({ fieldName: '_id', unique: true }); + + if (this.autoload) { this.loadDatabase(); } } diff --git a/package.json b/package.json index acd1941..37311e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nedb", - "version": "0.7.13", + "version": "0.7.14", "author": { "name": "tldr.io", "email": "hello@tldr.io" diff --git a/test/db.test.js b/test/db.test.js index 6027ef6..6f65c87 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -300,7 +300,7 @@ describe('Database', function () { describe('Insert', function () { - it.only('Able to insert a document in the database, setting an _id if none provided, and retrieve it even after a reload', function (done) { + it('Able to insert a document in the database, setting an _id if none provided, and retrieve it even after a reload', function (done) { d.find({}, function (err, docs) { docs.length.should.equal(0);