Comparing version 0.9.1 to 0.9.2
@@ -55,3 +55,3 @@ var customUtils = require('./customUtils') | ||
this.indexes._id = new Index({ fieldName: '_id', unique: true }); | ||
if (this.autoload) { this.loadDatabase(); } | ||
@@ -115,3 +115,6 @@ } | ||
return callback(null); | ||
this.persistence.persistNewState([{ $$indexCreated: options }], function (err) { | ||
if (err) { return callback(err); } | ||
return callback(null); | ||
}); | ||
}; | ||
@@ -121,2 +124,19 @@ | ||
/** | ||
* Remove an index | ||
* @param {String} fieldName | ||
* @param {Function} cb Optional callback, signature: err | ||
*/ | ||
Datastore.prototype.removeIndex = function (fieldName, cb) { | ||
var callback = cb || function () {}; | ||
delete this.indexes[fieldName]; | ||
this.persistence.persistNewState([{ $$indexRemoved: fieldName }], function (err) { | ||
if (err) { return callback(err); } | ||
return callback(null); | ||
}); | ||
}; | ||
/** | ||
* Add one or several document(s) to all indexes | ||
@@ -123,0 +143,0 @@ */ |
@@ -29,3 +29,3 @@ /** | ||
function checkKey (k, v) { | ||
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true)) { | ||
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true) && !(k === '$$indexCreated') && !(k === '$$indexRemoved')) { | ||
throw 'Field names cannot begin with the $ character'; | ||
@@ -32,0 +32,0 @@ } |
@@ -14,2 +14,3 @@ /** | ||
, customUtils = require('./customUtils') | ||
, Index = require('./indexes') | ||
; | ||
@@ -107,3 +108,8 @@ | ||
}); | ||
Object.keys(this.db.indexes).forEach(function (fieldName) { | ||
if (fieldName != "_id") { // The special _id index is managed by datastore.js, the others need to be persisted | ||
toPersist += model.serialize({ $$indexCreated: { fieldName: fieldName, unique: self.db.indexes[fieldName].unique, sparse: self.db.indexes[fieldName].sparse }}) + '\n'; | ||
} | ||
}); | ||
async.waterfall([ | ||
@@ -198,4 +204,6 @@ async.apply(customUtils.ensureFileDoesntExist, self.tempFilename) | ||
, dataById = {} | ||
, res = [] | ||
, i; | ||
, tdata = [] | ||
, i | ||
, indexes = {} | ||
; | ||
@@ -213,2 +221,6 @@ for (i = 0; i < data.length; i += 1) { | ||
} | ||
} else if (doc.$$indexCreated && doc.$$indexCreated.fieldName != undefined) { | ||
indexes[doc.$$indexCreated.fieldName] = doc.$$indexCreated; | ||
} else if (typeof doc.$$indexRemoved === "string") { | ||
delete indexes[doc.$$indexRemoved]; | ||
} | ||
@@ -220,6 +232,6 @@ } catch (e) { | ||
Object.keys(dataById).forEach(function (k) { | ||
res.push(dataById[k]); | ||
tdata.push(dataById[k]); | ||
}); | ||
return res; | ||
return { data: tdata, indexes: indexes }; | ||
}; | ||
@@ -254,2 +266,5 @@ | ||
* Load the database | ||
* 1) Create all indexes | ||
* 2) Insert all data | ||
* 3) Compact the database | ||
* This means pulling data out of the data file or creating it if it doesn't exist | ||
@@ -278,4 +293,10 @@ * Also, all data is persisted right away, which has the effect of compacting the database file | ||
// Recreate all indexes in the datafile | ||
Object.keys(treatedData.indexes).forEach(function (key) { | ||
self.db.indexes[key] = new Index(treatedData.indexes[key]); | ||
}); | ||
// Fill cached database (i.e. all indexes) with data | ||
try { | ||
self.db.resetIndexes(treatedData); | ||
self.db.resetIndexes(treatedData.data); | ||
} catch (e) { | ||
@@ -282,0 +303,0 @@ self.db.resetIndexes(); // Rollback any index which didn't fail |
{ | ||
"name": "nedb", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"author": { | ||
@@ -17,6 +17,6 @@ "name": "tldr.io", | ||
], | ||
"homepage": "https://github.com/louischatriot/node-embedded-db", | ||
"homepage": "https://github.com/louischatriot/nedb", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:louischatriot/node-embedded-db.git" | ||
"url": "git@github.com:louischatriot/nedb.git" | ||
}, | ||
@@ -23,0 +23,0 @@ "dependencies": { |
@@ -449,3 +449,6 @@ # NeDB (Node embedded database) | ||
You can remove a previously created index with `datastore.removeIndex(fieldName, cb)`. | ||
If your datastore is persistent, the indexes you created are persisted in the datafile, when you load the database a second time they are automatically created for you. No need to remove any `ensureIndex` though, if it is called on a database that already has the index, nothing happens. | ||
```javascript | ||
@@ -474,2 +477,6 @@ db.ensureIndex({ fieldName: 'somefield' }, function (err) { | ||
}); | ||
// Remove index on field somefield | ||
db.removeIndex('somefield', function (err) { | ||
}); | ||
``` | ||
@@ -525,2 +532,3 @@ | ||
Connect and Express, backed by nedb | ||
* If you mostly use NeDB for logging purposes and don't want the memory footprint of your application to grow too large, you can use <a href="https://github.com/louischatriot/nedb-logger" target="_blank">NeDB Logger</a> to insert documents in a NeDB-readable database | ||
* If you've outgrown NeDB, switching to MongoDB won't be too hard as it is the same API. Use <a href="https://github.com/louischatriot/nedb-to-mongodb" target="_blank">this utility</a> to transfer the data from a NeDB database to a MongoDB collection | ||
@@ -527,0 +535,0 @@ |
@@ -115,3 +115,3 @@ var model = require('../lib/model') | ||
it('Reject field names beginning with a $ sign or containing a dot, except the two edge cases', function () { | ||
it('Reject field names beginning with a $ sign or containing a dot, except the four edge cases', function () { | ||
var a1 = { $something: 'totest' } | ||
@@ -121,2 +121,4 @@ , a2 = { "with.dot": 'totest' } | ||
, e2 = { $$deleted: true } | ||
, e3 = { $$indexCreated: "indexName" } | ||
, e4 = { $$indexRemoved: "indexName" } | ||
, b; | ||
@@ -131,2 +133,4 @@ | ||
b = model.serialize(e2); | ||
b = model.serialize(e3); | ||
b = model.serialize(e4); | ||
}); | ||
@@ -133,0 +137,0 @@ |
@@ -49,3 +49,3 @@ var should = require('chai').should() | ||
model.serialize({ _id: "3", nested: { today: now } }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -65,3 +65,3 @@ | ||
model.serialize({ _id: "3", nested: { today: now } }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -80,3 +80,3 @@ | ||
model.serialize({ nested: { today: now } }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -95,3 +95,3 @@ | ||
model.serialize({ _id: "1", nested: { today: now } }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -111,3 +111,3 @@ | ||
model.serialize({ _id: "3", today: now }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -126,3 +126,3 @@ | ||
model.serialize({ _id: "3", today: now }) | ||
, treatedData = Persistence.treatRawData(rawData) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
; | ||
@@ -135,3 +135,21 @@ | ||
}); | ||
it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () { | ||
var now = new Date() | ||
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + | ||
model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' + | ||
model.serialize({ _id: "3", today: now }) | ||
, treatedData = Persistence.treatRawData(rawData).data | ||
, indexes = Persistence.treatRawData(rawData).indexes | ||
; | ||
Object.keys(indexes).length.should.equal(1); | ||
assert.deepEqual(indexes.test, { fieldName: "test", unique: true }); | ||
treatedData.sort(function (a, b) { return a._id - b._id; }); | ||
treatedData.length.should.equal(2); | ||
_.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); | ||
_.isEqual(treatedData[1], { _id: "3", today: now }).should.equal(true); | ||
}); | ||
it('Compact database on load', function (done) { | ||
@@ -607,4 +625,4 @@ d.insert({ a: 2 }, function () { | ||
}); | ||
}); // ==== End of 'Prevent dataloss when persisting data' ==== | ||
}); |
Sorry, the diff of this file is too big to display
658410
15562
558