From 0a1d4cac78bbd2b6ffd368dfb19200b20a55b6d7 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 7 Dec 2013 22:07:58 -0800 Subject: [PATCH] Created function to remove an index --- lib/datastore.js | 17 +++++++++++++++++ lib/model.js | 2 +- test/db.test.js | 4 ++-- test/model.test.js | 5 ++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 2d17736..8e432a8 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -118,6 +118,23 @@ Datastore.prototype.ensureIndex = function (options, cb) { }; +/** + * 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 */ diff --git a/lib/model.js b/lib/model.js index 09fb77e..ff4c4b7 100644 --- a/lib/model.js +++ b/lib/model.js @@ -26,7 +26,7 @@ var dateToJSON = function () { return { $$date: this.getTime() }; } * But you really need to want it to trigger such behaviour, even when warned not to use '$' at the beginning of the field names... */ function checkKey (k, v) { - if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true) && !(k === '$$indexCreated')) { + if (k[0] === '$' && !(k === '$$date' && typeof v === 'number') && !(k === '$$deleted' && v === true) && !(k === '$$indexCreated') && !(k === '$$indexRemoved')) { throw 'Field names cannot begin with the $ character'; } diff --git a/test/db.test.js b/test/db.test.js index 50b9e43..5667d17 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -2131,9 +2131,9 @@ describe('Database', function () { db.indexes.planet.getAll().length.should.equal(3); db.indexes.bloup.getAll().length.should.equal(0); db.indexes.planet.unique.should.equal(true); - db.indexes.planet.sparse.should.equal(false); + db.indexes.planet.sparse.should.equal(false); db.indexes.bloup.unique.should.equal(false); - db.indexes.bloup.sparse.should.equal(true); + db.indexes.bloup.sparse.should.equal(true); done(); }); diff --git a/test/model.test.js b/test/model.test.js index 63462a6..aae3bfb 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -112,12 +112,13 @@ describe('Model', function () { c.test[2].again.should.equal('yes'); }); - it('Reject field names beginning with a $ sign or containing a dot, except the three 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' } , a2 = { "with.dot": 'totest' } , e1 = { $$date: 4321 } , e2 = { $$deleted: true } , e3 = { $$indexCreated: "indexName" } + , e4 = { $$indexRemoved: "indexName" } , b; // Normal cases @@ -127,6 +128,8 @@ describe('Model', function () { // Edge cases b = model.serialize(e1); b = model.serialize(e2); + b = model.serialize(e3); + b = model.serialize(e4); }); it('Can serialize string fields with a new line without breaking the DB', function (done) {