diff --git a/lib/collection.js b/lib/collection.js index 73d0b03..f320c5d 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -99,7 +99,9 @@ export default class Collection { } } else { if (options && options.upsert) { - data._id = UUID().replace(/-/g, ''); + if ( !("_id" in data) ) { + data._id = UUID().replace(/-/g, ''); + } collection.push(data); ret.updated = 0; ret.inserted = 1; diff --git a/test/diskdb_test.js b/test/diskdb_test.js index 5dd28aa..e698d4c 100644 --- a/test/diskdb_test.js +++ b/test/diskdb_test.js @@ -41,6 +41,11 @@ var dbPath = 'test/testdb', title: 'diskDB rocks', published: 'yesterday' }, + article4 = { + _id : 'pre_generated_id', + title: 'diskDB rocks', + published: 'yesterday' + }, //nested objects articleComments = { title: 'diskDB rocks', @@ -375,7 +380,7 @@ exports.update = { 'upsert': false }; - test.expect(4); + test.expect(6); //save one record diskdb.articles.save(article); // before update @@ -394,6 +399,11 @@ exports.update = { // should insert test.equal(diskdb.articles.update(query, article2, options).inserted, 1, 'Should return the inserted objects count'); + // should insert with pre-generated _id + var previous_id = article4._id; + test.equal(diskdb.articles.update({_id: article4._id}, article4, options).inserted, 1, 'Should return the inserted objects count'); + test.equal(article4._id, previous_id, 'Should have the pre-generated _id'); + //change options query = { published: 'yesterday' @@ -405,7 +415,7 @@ exports.update = { }; // should update 2 record - test.equal(diskdb.articles.update(query, article, options).updated, 2, 'Should return the updated objects count'); + test.equal(diskdb.articles.update(query, article, options).updated, 3, 'Should return the updated objects count'); test.done(); }, };