cant change an _id

pull/2/head
Louis Chatriot 12 years ago
parent ee33eff4db
commit 411f88c733
  1. 24
      lib/indexes.js
  2. 1
      lib/model.js
  3. 15
      test/db.test.js

@ -0,0 +1,24 @@
var BinarySearchTree = require('binary-search-tree').BinarySearchTree;
/**
* Create a new index
* @param {String} options.fieldName On which field should the index apply
* @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)
*/
function Index (options) {
this.fieldName = options.fieldName;
this.datastore = options.datastore;
this.unique = options.unique || false;
this.sparse = options.unique || false;
if (this.sparse) { this.fieldUndefined = []; } // Will store all elements for which the indexed field is not defined
}
// Interface
module.exports = Index;

@ -226,6 +226,7 @@ function modify (obj, updateQuery) {
// Check result is valid and return it // Check result is valid and return it
checkObject(newDoc); checkObject(newDoc);
if (obj._id !== newDoc._id) { throw "You can't change a document's _id"; }
return newDoc; return newDoc;
}; };

@ -817,7 +817,7 @@ describe('Database', function () {
it('Cant change the _id of a document', function (done) { it('Cant change the _id of a document', function (done) {
d.insert({ a: 2 }, function (err, newDoc) { d.insert({ a: 2 }, function (err, newDoc) {
d.update({ a: 2 }, { _id: 'nope' }, {}, function (err) { d.update({ a: 2 }, { a: 2, _id: 'nope' }, {}, function (err) {
assert.isDefined(err); assert.isDefined(err);
d.find({}, function (err, docs) { d.find({}, function (err, docs) {
@ -826,7 +826,18 @@ describe('Database', function () {
docs[0].a.should.equal(2); docs[0].a.should.equal(2);
docs[0]._id.should.equal(newDoc._id); docs[0]._id.should.equal(newDoc._id);
done(); d.update({ a: 2 }, { $set: { _id: 'nope' } }, {}, function (err) {
assert.isDefined(err);
d.find({}, function (err, docs) {
docs.length.should.equal(1);
Object.keys(docs[0]).length.should.equal(2);
docs[0].a.should.equal(2);
docs[0]._id.should.equal(newDoc._id);
done();
});
});
}); });
}); });
}); });

Loading…
Cancel
Save