Comparing version 0.3.2 to 0.3.3
@@ -190,3 +190,3 @@ /** | ||
* @param {Object} query | ||
* @param {Object} newDoc Will replace the former docs | ||
* @param {Object} updateQuery | ||
* @param {Object} options Optional options | ||
@@ -197,3 +197,3 @@ * options.multi If true, can update multiple documents (defaults to false) | ||
*/ | ||
Datastore.prototype.update = function (query, newDoc, options, cb) { | ||
Datastore.prototype.update = function (query, updateQuery, options, cb) { | ||
var callback | ||
@@ -219,3 +219,5 @@ , self = this | ||
} else { | ||
return self.insert(model.modify({}, newDoc), function (err) { | ||
// The upserted document is the query (since for now queries have the same structure as | ||
// documents), modified by the updateQuery | ||
return self.insert(model.modify(query, updateQuery), function (err) { | ||
if (err) { return callback(err); } | ||
@@ -232,3 +234,3 @@ return callback(null, 1, true); | ||
numReplaced += 1; | ||
newData.push(model.modify(d, newDoc)); | ||
newData.push(model.modify(d, updateQuery)); | ||
} else { | ||
@@ -235,0 +237,0 @@ newData.push(d); |
{ | ||
"name": "nedb", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "tldr.io", |
@@ -1,13 +0,18 @@ | ||
# NE DB (Node Embedded DataBase) | ||
# NeDB (Node Embedded DataBase) | ||
Embedded persistent database for Node.js, with no dependency (except npm modules of course). The API is the same as MongoDB. | ||
<img src="http://i.imgur.com/GdeQBmc.png" style="width: 25%; height: 25%; float: left;"> | ||
**It's still experimental!** I'm still stabilizing the code. The API will not change though. Don't hesitate to file issues/PRs if you find bugs. | ||
Embedded persistent database for Node.js, with no dependency (except npm | ||
modules of course). You can think of it as a SQLite for Node.js, which | ||
can be installed and used in less than 30 seconds. The API is a subset of MongoDB's. | ||
## Why? | ||
I needed to store data from another project (<a href="https://github.com/louischatriot/braindead-ci" target="_blank">Braindead CI</a>). I needed the datastore to be standalone (i.e. no dependency except other Node modules) so that people can install the software using a simple `npm install`. I couldn't find one without bugs and a clean API so I made this one. | ||
## Installation, tests | ||
It will be published as an npm module once it is finished. To launch tests: `npm test`. | ||
Module name on npm is `nedb`. | ||
```javascript | ||
npm install nedb --save // Put latest version in your package.json | ||
make test // You'll need the dev dependencies to test it | ||
``` | ||
## API | ||
@@ -131,22 +136,59 @@ It's a subset of MongoDB's API (the most used operations). The current API will not change, but I will add operations as they are needed. | ||
}); | ||
// If you upsert with a modifier, the upserted doc is the query modified by the modifier | ||
db.update({ planet: 'Pluton' }, { $set: { inhabited: false } }, { upsert: true }, function () { | ||
// A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection | ||
}); | ||
``` | ||
### Removing documents | ||
`db.remove(query, options, callback)` will remove all documents matching `query` according to `options` | ||
* `query` is the same as the ones used for finding and updating | ||
* `options` only one option for now: `multi` which allows the removal of multiple documents if set to true. Default is false | ||
* `callback` is optional, signature: err, numRemoved | ||
```javascript | ||
// Let's use the same example collection as in the "finding document" part | ||
// Remove one document from the collection | ||
// options set to {} since the default for multi is false | ||
db.remove({ _id: 'id2' }, {}, function (err, numRemoved) { | ||
// numRemoved = 1 | ||
}); | ||
// Remove multiple documents | ||
db.remove({ system: 'solar' }, { multi: true }, function (err, numRemoved) { | ||
// numRemoved = 3 | ||
// All planets from the solar system were removed | ||
}); | ||
``` | ||
## Performance | ||
### Speed | ||
It is pretty fast on the kind of datasets it was designed for (10,000 documents or less). On my machine (3 years old, no SSD), with a collection with 10,000 documents: | ||
It is pretty fast on the kind of datasets it is designed for (10,000 | ||
documents max). On my machine (3 years old, no SSD), with a collection | ||
containing 10,000 documents: | ||
* An insert takes 0.1ms | ||
* A read takes 5.7ms | ||
* An update takes 62ms | ||
* A deletion takes 61ms | ||
* An update takes 58ms | ||
* A deletion takes 57ms | ||
Read, update and deletion times are pretty much non impacted by the number of concerned documents. Inserts, updates and deletions are non-blocking. Read will be soon, too (but they are so fast it is not so important anyway). | ||
You can run the simple benchmarks I use by executing the scripts in the `benchmarks` folder. They all take an optional parameter which is the size of the dataset to use (default is 10,000). | ||
### Memory footprint | ||
For now, a copy of the whole database is kept in memory. For the kind of datasets expected this should not be too much (max 20MB) but I am planning on stopping using that method to free RAM and make it completely asynchronous. | ||
A copy of the whole database is kept in memory. This is not much on the | ||
expected (20MB for 10,000 2K documents). If requested, I'll introduce an | ||
option to not use this cache to decrease memory footprint (at the cost | ||
of a lower speed). | ||
## Use in other services | ||
* <a href="https://github.com/louischatriot/connect-nedb-session" | ||
target="_blank">connect-nedb-session</a> is a session store for | ||
Connect and Express, backed by nedb | ||
* I'm planning on making an export tool to get all your data in an nedb | ||
database in a Mongo database | ||
## License | ||
@@ -153,0 +195,0 @@ |
@@ -477,4 +477,5 @@ var Datastore = require('../lib/datastore') | ||
docs.length.should.equal(1); | ||
Object.keys(docs[0]).length.should.equal(2); | ||
Object.keys(docs[0]).length.should.equal(3); | ||
docs[0].hello.should.equal('world'); | ||
docs[0].bloup.should.equal('blap'); | ||
assert.isDefined(docs[0]._id); | ||
@@ -481,0 +482,0 @@ |
62217
1445
217