Skip to content

Commit

Permalink
Allow user to specify custom _id. Closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwrobinson committed Jan 19, 2016
1 parent da35432 commit 767d163
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/clients/mongoclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MongoClient extends DatabaseClient {
return resolve(result.insertedId);
});
} else {
db.updateOne({ _id: id }, { $set: values }, function(error, result) {
db.updateOne({ _id: id }, { $set: values }, { upsert: true }, function(error, result) {
if (error) return reject(error);
return resolve();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/clients/nedbclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NeDbClient extends DatabaseClient {
return resolve(result._id);
});
} else {
db.update({ _id: id }, { $set: values }, function(error, result) {
db.update({ _id: id }, { $set: values }, { upsert: true }, function(error, result) {
if (error) return reject(error);
return resolve(result);
});
Expand Down
28 changes: 27 additions & 1 deletion test/mongoclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var expect = require('chai').expect;
var ObjectId = require('mongodb').ObjectId;
var connect = require('../index').connect;
var Document = require('../index').Document;
var validateId = require('./util').validateId;
Expand Down Expand Up @@ -30,7 +31,32 @@ describe('MongoClient', function() {

after(function(done) {
done();
});
});

describe('id', function() {
it('should allow custom _id values', function(done) {
class School extends Document {
constructor() {
super();

this.name = String;
}
}

var school = School.create();
school._id = new ObjectId('1234567890abcdef12345678');
school.name = 'Springfield Elementary';

school.save().then(function() {
validateId(school);
expect(school._id.toString()).to.be.equal('1234567890abcdef12345678');
return School.loadOne();
}).then(function(s) {
validateId(s);
expect(s._id.toString()).to.be.equal('1234567890abcdef12345678');
}).then(done, done);
});
});

describe('query', function() {
class User extends Document {
Expand Down
25 changes: 25 additions & 0 deletions test/nedbclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ describe('NeDbClient', function() {
});
});*/

describe('id', function() {
it('should allow custom _id values', function(done) {
class School extends Document {
constructor() {
super();

this.name = String;
}
}

var school = School.create();
school._id = '1234567890abcdef';
school.name = 'South Park Elementary';

school.save().then(function() {
validateId(school);
expect(school._id).to.be.equal('1234567890abcdef');
return School.loadOne();
}).then(function(s) {
validateId(s);
expect(s._id).to.be.equal('1234567890abcdef');
}).then(done, done);
});
});

describe('indexes', function() {
it('should reject documents with duplicate values in unique-indexed fields', function(done) {
class User extends Document {
Expand Down

0 comments on commit 767d163

Please sign in to comment.