Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the pre-generated id when inserting on an updated/upsert operation #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions test/diskdb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -375,7 +380,7 @@ exports.update = {
'upsert': false
};

test.expect(4);
test.expect(6);
//save one record
diskdb.articles.save(article);
// before update
Expand All @@ -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'
Expand All @@ -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();
},
};
Expand Down