From 03f68398e618cecdcd3a9f8c919cad1cbba097da Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 7 Dec 2013 22:19:55 -0800 Subject: [PATCH] Can remove an index --- lib/persistence.js | 2 ++ test/db.test.js | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/persistence.js b/lib/persistence.js index 147f65f..4f28f9d 100644 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -217,6 +217,8 @@ Persistence.treatRawData = function (rawData) { } } else if (doc.$$indexCreated && doc.$$indexCreated.fieldName != undefined) { indexes[doc.$$indexCreated.fieldName] = doc.$$indexCreated; + } else if (typeof doc.$$indexRemoved === "string") { + delete indexes[doc.$$indexRemoved]; } } catch (e) { } diff --git a/test/db.test.js b/test/db.test.js index 5667d17..a346b2a 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1291,7 +1291,7 @@ describe('Database', function () { describe('Using indexes', function () { - describe('ensureIndex and index initialization in database loading', function () { + describe.only('ensureIndex and index initialization in database loading', function () { it('ensureIndex can be called right after a loadDatabase and be initialized and filled correctly', function (done) { var now = new Date() @@ -1523,9 +1523,27 @@ describe('Database', function () { }); }); }); + + it('Can remove an index', function (done) { + d.ensureIndex({ fieldName: 'e' }, function (err) { + assert.isNull(err); + + Object.keys(d.indexes).length.should.equal(2); + assert.isNotNull(d.indexes.e); + + d.removeIndex("e", function (err) { + assert.isNull(err); + Object.keys(d.indexes).length.should.equal(1); + assert.isUndefined(d.indexes.e); + + done(); + }); + }); + }); }); // ==== End of 'ensureIndex and index initialization in database loading' ==== // + describe('Indexing newly inserted documents', function () { it('Newly inserted documents are indexed', function (done) { @@ -2006,10 +2024,10 @@ describe('Database', function () { }); }); - }); // ==== End of 'Removing indexes upon document update' ==== // + }); // ==== End of 'Updating indexes upon document remove' ==== // - describe.only('Persisting indexes', function () { + describe.skip('Persisting indexes', function () { it('Indexes are persisted to a separate file and recreated upon reload', function (done) { var persDb = "workspace/persistIndexes.db"