diff --git a/lib/indexes.js b/lib/indexes.js index de361b8..8022fac 100644 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -19,6 +19,10 @@ function checkValueEquality (a, b) { * @param {Datastore} options.datastore Datastore on which the index is created * @param {Boolean} options.unique Optional, enforce a unique constraint (default: false) * @param {Boolean} options.sparse Optional, allow a sparse index (we can have documents for which fieldName is undefined) (default: false) + * TODO: for now the sparse option doesn't work fully + * don't use it. I will implement it in the future + * in the meantime you can use non-unique, non-sparse indexes + * for approx. the same behaviour */ function Index (options) { this.fieldName = options.fieldName; diff --git a/test/db.test.js b/test/db.test.js index a43f541..88fb032 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1109,7 +1109,7 @@ describe('Database', function () { }); // ==== End of 'Remove' ==== // - describe('Using indexes', function () { + describe.only('Using indexes', function () { describe('ensureIndex', function () { @@ -1220,7 +1220,45 @@ describe('Database', function () { }); }); - }); + }); // ==== End of 'ensureIndex' ==== // + + describe('Indexing newly inserted documents', function () { + + it('Newly inserted documents are indexed', function (done) { + d.ensureIndex({ fieldName: 'z' }); + 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: 'nope' }, function (err, newDoc) { + d.indexes.z.tree.getNumberOfKeys().should.equal(2); + assert.deepEqual(d.indexes.z.getMatching('nope'), [newDoc]); + + 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); + + 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, newDoc2) { + d.indexes.z.tree.getNumberOfKeys().should.equal(1); + assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc, newDoc2]); + + done(); + }); + }); + }); + + }); // ==== End of '' ==== //