Always use an automatic _id, which is guaranteed to be a string

pull/2/head
Louis Chatriot 12 years ago
parent ddb7ffc74e
commit a87d89f277
  1. 7
      lib/datastore.js
  2. 2
      package.json
  3. 62
      test/db.test.js

@ -231,19 +231,22 @@ Datastore.prototype._insert = function (newDoc, cb) {
var callback = cb || function () {} var callback = cb || function () {}
, self = this , self = this
, persistableNewDoc , persistableNewDoc
, insertedDoc
; ;
// Ensure the document has the right format
try { try {
newDoc._id = newDoc._id || customUtils.uid(16); newDoc._id = customUtils.uid(16);
persistableNewDoc = model.serialize(newDoc); persistableNewDoc = model.serialize(newDoc);
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
insertedDoc = model.deserialize(persistableNewDoc);
fs.appendFile(self.filename, persistableNewDoc + '\n', 'utf8', function (err) { fs.appendFile(self.filename, persistableNewDoc + '\n', 'utf8', function (err) {
if (err) { return callback(err); } if (err) { return callback(err); }
var insertedDoc = model.deserialize(persistableNewDoc);
self.data.push(insertedDoc); self.data.push(insertedDoc);
self.addToIndexes(insertedDoc); self.addToIndexes(insertedDoc);
self.datafileSize += 1; self.datafileSize += 1;

@ -22,7 +22,7 @@
"dependencies": { "dependencies": {
"async": "~0.2.8", "async": "~0.2.8",
"underscore": "~1.4.4", "underscore": "~1.4.4",
"binary-search-tree": "~0.1.1" "binary-search-tree": "0.1.2"
}, },
"devDependencies": { "devDependencies": {
"chai": "1.0.x", "chai": "1.0.x",

@ -291,12 +291,12 @@ describe('Database', function () {
}); });
}); });
it('If an _id is already given when we insert a document, use it and not the default uid', function (done) { it('If an _id is already given when we insert a document, dont use it but use an automatic one', function (done) {
d.insert({ _id: 'test', stuff: true }, function (err, newDoc) { d.insert({ _id: 'test', stuff: true }, function (err, newDoc) {
if (err) { return done(err); } if (err) { return done(err); }
newDoc.stuff.should.equal(true); newDoc.stuff.should.equal(true);
newDoc._id.should.equal('test'); newDoc._id.should.not.equal('test');
done(); done();
}); });
@ -1109,15 +1109,15 @@ describe('Database', function () {
}); // ==== End of 'Remove' ==== // }); // ==== End of 'Remove' ==== //
describe.only('Using indexes', function () { describe('Using indexes', function () {
describe('ensureIndex', function () { describe('ensureIndex', function () {
it('ensureIndex can be called right after a loadDatabase and be initialized and filled correctly', function (done) { it('ensureIndex can be called right after a loadDatabase and be initialized and filled correctly', function (done) {
var now = new Date() var now = new Date()
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + , rawData = model.serialize({ _id: "aaa", z: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "bbb", z: "2", hello: 'world' }) + '\n' +
model.serialize({ _id: "3", nested: { today: now } }) model.serialize({ _id: "ccc", z: "3", nested: { today: now } })
; ;
d.data.length.should.equal(0); d.data.length.should.equal(0);
@ -1130,14 +1130,14 @@ describe('Database', function () {
assert.deepEqual(d.indexes, {}); assert.deepEqual(d.indexes, {});
d.ensureIndex({ fieldName: '_id' }); d.ensureIndex({ fieldName: 'z' });
d.indexes._id.fieldName.should.equal('_id'); d.indexes.z.fieldName.should.equal('z');
d.indexes._id.unique.should.equal(false); d.indexes.z.unique.should.equal(false);
d.indexes._id.sparse.should.equal(false); d.indexes.z.sparse.should.equal(false);
d.indexes._id.tree.getNumberOfKeys().should.equal(3); d.indexes.z.tree.getNumberOfKeys().should.equal(3);
d.indexes._id.tree.search('1')[0].should.equal(d.data[0]); d.indexes.z.tree.search('1')[0].should.equal(d.data[0]);
d.indexes._id.tree.search('2')[0].should.equal(d.data[1]); d.indexes.z.tree.search('2')[0].should.equal(d.data[1]);
d.indexes._id.tree.search('3')[0].should.equal(d.data[2]); d.indexes.z.tree.search('3')[0].should.equal(d.data[2]);
done(); done();
}); });
@ -1145,8 +1145,8 @@ describe('Database', function () {
}); });
it('ensureIndex can be called after the data set was modified and still be correct', function (done) { it('ensureIndex can be called after the data set was modified and still be correct', function (done) {
var rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + var rawData = model.serialize({ _id: "aaa", z: "1", a: 2, ages: [1, 5, 12] }) + '\n' +
model.serialize({ _id: "2", hello: 'world' }) model.serialize({ _id: "bbb", z: "2", hello: 'world' })
; ;
d.data.length.should.equal(0); d.data.length.should.equal(0);
@ -1159,26 +1159,26 @@ describe('Database', function () {
assert.deepEqual(d.indexes, {}); assert.deepEqual(d.indexes, {});
d.insert({ _id: "12", yes: 'yes' }, function () { d.insert({ z: "12", yes: 'yes' }, function (err, newDoc1) {
d.insert({ _id: "14", nope: 'nope' }, function () { d.insert({ z: "14", nope: 'nope' }, function (err, newDoc2) {
d.remove({ _id: "2" }, {}, function () { d.remove({ z: "2" }, {}, function () {
d.update({ _id: "1" }, { $set: { 'yes': 'yep' } }, {}, function () { d.update({ z: "1" }, { $set: { 'yes': 'yep' } }, {}, function () {
assert.deepEqual(d.indexes, {}); assert.deepEqual(d.indexes, {});
d.ensureIndex({ fieldName: '_id' }); d.ensureIndex({ fieldName: 'z' });
d.indexes._id.fieldName.should.equal('_id'); d.indexes.z.fieldName.should.equal('z');
d.indexes._id.unique.should.equal(false); d.indexes.z.unique.should.equal(false);
d.indexes._id.sparse.should.equal(false); d.indexes.z.sparse.should.equal(false);
d.indexes._id.tree.getNumberOfKeys().should.equal(3); d.indexes.z.tree.getNumberOfKeys().should.equal(3);
d.indexes._id.tree.search('1')[0].should.equal(d.data[0]); d.indexes.z.tree.search('1')[0].should.equal(d.data[0]);
assert.deepEqual(d.data[0], { _id: "1", a: 2, ages: [1, 5, 12], yes: 'yep' }); assert.deepEqual(d.data[0], { _id: "aaa", z: "1", a: 2, ages: [1, 5, 12], yes: 'yep' });
d.indexes._id.tree.search('12')[0].should.equal(d.data[1]); d.indexes.z.tree.search('12')[0].should.equal(d.data[1]);
assert.deepEqual(d.data[1], { _id: "12", yes: 'yes' }); assert.deepEqual(d.data[1], { _id: newDoc1._id, z: "12", yes: 'yes' });
d.indexes._id.tree.search('14')[0].should.equal(d.data[2]); d.indexes.z.tree.search('14')[0].should.equal(d.data[2]);
assert.deepEqual(d.data[2], { _id: "14", nope: 'nope' }); assert.deepEqual(d.data[2], { _id: newDoc2._id, z: "14", nope: 'nope' });
done(); done();
}); });

Loading…
Cancel
Save