Added support for custom _id-s

As per issue #154, if the document to be inserted
already has an _id property, that _id will be used
instead of an automatically generated one.

I updated tests, browser builds and the README.
pull/2/head
Paolo Scanferla 11 years ago
parent 7b5171f6bd
commit 26d6ec2bfd
  1. 2
      README.md
  2. 23
      browser-version/out/nedb.js
  3. 4
      browser-version/out/nedb.min.js
  4. 2
      lib/datastore.js
  5. 4
      test/db.test.js

@ -112,7 +112,7 @@ The native types are `String`, `Number`, `Boolean`, `Date` and `null`. You can a
arrays and subdocuments (objects). If a field is `undefined`, it will not be saved (this is different from
MongoDB which transforms `undefined` in `null`, something I find counter-intuitive).
An `_id` field will be automatically generated by NeDB. It's a 16-characters alphanumerical string that cannot be modified once it has been generated. Unlike with MongoDB, you cannot specify it (that shouldn't be a problem anyway).
If the document does not contain an `_id` field, NeDB will automatically generated one for you (a 16-characters alphanumerical string). The `_id` of a document, once set, cannot be modified.
Field names cannot begin by '$' or contain a '.'.

@ -561,7 +561,8 @@ process.nextTick = (function () {
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
if (ev.source === window && ev.data === 'process-tick') {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
@ -683,13 +684,23 @@ Cursor.prototype._exec = function(callback) {
if (this._sort) {
keys = Object.keys(this._sort);
// Going backwards so that the first sort is the last that gets applied
for (i = keys.length - 1; i >= 0; i -= 1) {
// Sorting
var criteria = [];
for (i = 0; i < keys.length; i++) {
key = keys[i];
criteria.push({ key: key, direction: self._sort[key] });
}
res.sort(function(a, b) {
return self._sort[key] * model.compareThings(model.getDotValue(a, key), model.getDotValue(b, key));
});
var criterion, compare, i;
for (i = 0; i < criteria.length; i++) {
criterion = criteria[i];
compare = criterion.direction * model.compareThings(model.getDotValue(a, criterion.key), model.getDotValue(b, criterion.key));
if (compare !== 0) {
return compare;
}
}
return 0;
});
// Applying limit and skip
var limit = this._limit || res.length
@ -1096,7 +1107,7 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) {
preparedDoc = [];
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); });
} else {
newDoc._id = customUtils.uid(16);
newDoc._id = newDoc._id || customUtils.uid(16);
preparedDoc = model.deepCopy(newDoc);
model.checkObject(preparedDoc);
}

File diff suppressed because one or more lines are too long

@ -300,7 +300,7 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) {
preparedDoc = [];
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); });
} else {
newDoc._id = customUtils.uid(16);
newDoc._id = newDoc._id || customUtils.uid(16);
preparedDoc = model.deepCopy(newDoc);
model.checkObject(preparedDoc);
}

@ -198,12 +198,12 @@ describe('Database', function () {
});
});
it('If an _id is already given when we insert a document, dont use it but use an automatic one', function (done) {
it('If an _id is already given when we insert a document, use that instead of generating a random one', function (done) {
d.insert({ _id: 'test', stuff: true }, function (err, newDoc) {
if (err) { return done(err); }
newDoc.stuff.should.equal(true);
newDoc._id.should.not.equal('test');
newDoc._id.should.equal('test');
done();
});

Loading…
Cancel
Save