Skip to content

Commit

Permalink
feat: add mongodb.dataType to property options
Browse files Browse the repository at this point in the history
Adds mongodb.dataType to model property options
  • Loading branch information
Hage Yaapa committed May 14, 2019
1 parent 5c9ed78 commit 9836d99
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1881,14 +1881,25 @@ MongoDB.prototype.isObjectIDProperty = function(model, prop, value, options) {
) {
return true;
} else if ('string' === typeof value) {
// values doesn't look like an ObjectID
if (!value.match(/^[0-9a-fA-F]{24}$/)) return false;

// value looks like an ObjectID, and it should be treated as one
// because `prop.mongodb.dataType` is set to 'ObjectID'
// this condition overrides the `strictObjectIDCoercion` setting
if (prop.id && prop.mongodb && prop.mongodb.dataType === 'ObjectID') return true;

// value looks like an ObjectID, determine its nature based on `strictObjectIDCoercion`
var settings = this._models[model] && this._models[model].settings;

options = options || {};
var strict =
(settings && settings.strictObjectIDCoercion) ||
this.settings.strictObjectIDCoercion ||
options.strictObjectIDCoercion;

if (strict) return false; // unless explicitly typed, don't coerce
return /^[0-9a-fA-F]{24}$/.test(value);
else return true;
} else {
return false;
}
Expand Down
179 changes: 179 additions & 0 deletions test/id.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,182 @@ describe('mongodb default id name', function() {
);
});
});

describe('strictObjectIDCoercion', function() {
var ObjectID = ds.connector.getDefaultIdType();
var objectIdLikeString = '7cd2ad46ffc580ba45d3cb1f';

context('set to false (default)', function() {
var User = ds.createModel(
'user1',
{
id: {type: String, id: true},
name: String,
}
);

beforeEach(function(done) {
User.deleteAll(done);
});

it('should find model with ObjectID id', function(done) {
User.create({name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(ds.ObjectID);
User.findById(user.id, function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});

it('should find model with ObjectID-like id', function(done) {
User.create({id: objectIdLikeString, name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(ds.ObjectID);
user.id.should.eql(ObjectID(objectIdLikeString));
User.findById(objectIdLikeString, function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});

it('should find model with string id', function(done) {
User.create({id: 'a', name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(String);
user.id.should.equal('a');
User.findById('a', function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});
});

context('set to true', function() {
var User = ds.createModel(
'user2',
{
id: {type: String, id: true},
name: String,
},
{strictObjectIDCoercion: true}
);

beforeEach(function(done) {
User.deleteAll(done);
});

it('should not find model with ObjectID id', function(done) {
User.create({name: 'John'}, function(err, user) {
user.id.should.not.be.an.instanceOf(ds.ObjectID);
User.findById(user.id, function(err, user) {
(user === null).should.be.true();
done(err, user);
});
});
});

it('should find model with ObjectID-like string id', function(done) {
User.create({id: objectIdLikeString, name: 'John'}, function(err, user) {
user.id.should.not.be.an.instanceOf(ds.ObjectID);
user.id.should.eql(objectIdLikeString);
User.findById(objectIdLikeString, function(err, user) {
user.should.not.be.empty();
done(err, user);
});
});
});

it('should find model with string id', function(done) {
User.create({id: 'a', name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(String);
user.id.should.equal('a');
User.findById('a', function(err, user) {
user.should.not.be.empty();
done(err, user);
});
});
});
});

context('set to true, id type set to `ObjectID`', function() {
var User = ds.createModel(
'user3',
{
id: {type: String, id: true, mongodb: {dataType: 'ObjectID'}},
name: String,
},
{strictObjectIDCoercion: true}
);

beforeEach(function(done) {
User.deleteAll(done);
});

it('should find model with ObjectID id', function(done) {
User.create({name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(ds.ObjectID);
User.findById(user.id, function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});

// This works by coercing string to ObjectID, overriding `strictObjectIDCoercion: true`
it('should find model with ObjectID-like id', function(done) {
User.create({id: objectIdLikeString, name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(ds.ObjectID);
user.id.should.eql(ObjectID(objectIdLikeString));
User.findById(objectIdLikeString, function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});

it('should find model with string id', function(done) {
User.create({id: 'a', name: 'John'}, function(err, user) {
user.id.should.be.an.instanceOf(String);
user.id.should.equal('a');
User.findById('a', function(err, user) {
user.should.not.be.null();
done(err, user);
});
});
});

it('should update model with ObjectID id', function(done) {
User.create({name: 'John'}, function(err, user) {
User.update({id: user.id, name: 'Jon'}, function(err) {
User.findById(user.id, function(err, user) {
user.name.should.equal('Jon');
done(err, user);
});
});
});
});

it('should update model with ObjectID-like id', function(done) {
User.create({id: objectIdLikeString, name: 'John'}, function(err, user) {
User.update({id: objectIdLikeString, name: 'Jon'}, function(err) {
User.findById(objectIdLikeString, function(err, user) {
user.name.should.equal('Jon');
done(err, user);
});
});
});
});

it('should update model with string id', function(done) {
User.create({id: 'a', name: 'John'}, function(err, user) {
User.update({id: 'a', name: 'Jon'}, function(err) {
User.findById('a', function(err, user) {
user.name.should.equal('Jon');
done(err, user);
});
});
});
});
});
});

0 comments on commit 9836d99

Please sign in to comment.