From 6cb3bcc0031bb0b550addd6fac8a86578ea36ee4 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 30 May 2013 14:01:56 +0200 Subject: [PATCH] If one unique constraint is violated, the data doesn't change --- lib/datastore.js | 4 ++- test/db.test.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 519869c..0b6b4d4 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -244,11 +244,13 @@ Datastore.prototype._insert = function (newDoc, cb) { insertedDoc = model.deserialize(persistableNewDoc); + // Insert in all indexes (also serves to ensure uniqueness) + try { self.addToIndexes(insertedDoc); } catch (e) { return callback(e); } + fs.appendFile(self.filename, persistableNewDoc + '\n', 'utf8', function (err) { if (err) { return callback(err); } self.data.push(insertedDoc); - self.addToIndexes(insertedDoc); self.datafileSize += 1; return callback(null, model.deepCopy(insertedDoc)); }); diff --git a/test/db.test.js b/test/db.test.js index d797030..9d357f1 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1220,6 +1220,14 @@ describe('Database', function () { }); }); + it.skip('Can initialize multiple indexes', function (done) { + done(); + }); + + it.skip('If a unique constraint is not respected, database loading will not work and no data will be inserted', function (done) { + done(); + }); + }); // ==== End of 'ensureIndex' ==== // describe('Indexing newly inserted documents', function () { @@ -1241,6 +1249,28 @@ describe('Database', function () { }); }); + it('If multiple indexes are defined, the document is inserted in all of them', function (done) { + d.ensureIndex({ fieldName: 'z' }); + d.ensureIndex({ fieldName: 'ya' }); + d.indexes.z.tree.getNumberOfKeys().should.equal(0); + + d.insert({ a: 2, z: 'yes', ya: 'indeed' }, function (err, newDoc) { + d.indexes.z.tree.getNumberOfKeys().should.equal(1); + d.indexes.ya.tree.getNumberOfKeys().should.equal(1); + assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc]); + assert.deepEqual(d.indexes.ya.getMatching('indeed'), [newDoc]); + + d.insert({ a: 5, z: 'nope', ya: 'sure' }, function (err, newDoc2) { + d.indexes.z.tree.getNumberOfKeys().should.equal(2); + d.indexes.ya.tree.getNumberOfKeys().should.equal(2); + assert.deepEqual(d.indexes.z.getMatching('nope'), [newDoc2]); + assert.deepEqual(d.indexes.ya.getMatching('sure'), [newDoc2]); + + done(); + }); + }); + }); + it('Can insert two docs at the same key for a non unique index', function (done) { d.ensureIndex({ fieldName: 'z' }); d.indexes.z.tree.getNumberOfKeys().should.equal(0); @@ -1258,7 +1288,39 @@ describe('Database', function () { }); }); - }); // ==== End of '' ==== // + it('If the index has a unique constraint, an error is thrown if it is violated', function (done) { + d.ensureIndex({ fieldName: 'z', unique: true }); + d.indexes.z.tree.getNumberOfKeys().should.equal(0); + + d.insert({ a: 2, z: 'yes' }, function (err, newDoc) { + d.indexes.z.tree.getNumberOfKeys().should.equal(1); + assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc]); + + d.insert({ a: 5, z: 'yes' }, function (err) { + err.errorType.should.equal('uniqueViolated'); + err.key.should.equal('yes'); + + // Index didn't change + d.indexes.z.tree.getNumberOfKeys().should.equal(1); + assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc]); + + // Data didn't change + assert.deepEqual(d.data, [newDoc]); + d.loadDatabase(function () { + d.data.length.should.equal(1); + assert.deepEqual(d.data[0], newDoc); + + done(); + }); + }); + }); + }); + + it.skip('If the index has a unique constraint, others cannot be modified when it raises an error', function (done) { + done(); + }); + + }); // ==== End of 'Indexing newly inserted documents' ==== //