Skip to content

Commit

Permalink
Merge pull request #50 from flyvictor/fix-specific-id-creation
Browse files Browse the repository at this point in the history
Fallback to resource.id when cannot read resource[pk]
  • Loading branch information
jesuscript committed Jul 18, 2014
2 parents 451b746 + 94cf92c commit c99366c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,11 @@ adapter._serialize = function (model, resource) {
}
}

//TODO: This may cause WEB-2618 issue. Test for /[0-9a-f]{24}/ before casting
resource[pk] = pkType(resource[pk]);
resource[pk] = pkType(resource[pk] || resource.id);
if (!resource[pk]){
//If failed to cast - generate ObjectId from provided .id
resource._id = mongoose.Types.ObjectId(resource.id.toString());
}

delete resource.id;
}
Expand Down
28 changes: 28 additions & 0 deletions test/fortune-mongodb/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ module.exports = function(options){
ids = options.ids;
});

describe('Creation', function(){
it('should be able to create document with provided id', function(done){
var doc = {
id: '123456789012345678901234'
};
adapter.create('pet', doc).then(function(){
var model = adapter.model('pet');
model.findOne({_id: '123456789012345678901234'}).exec(function(err, doc){
should.not.exist(err);
should.exist(doc);
done();
});
});
});
it('should be able to cast provided id to proper type', function(done){
var doc = {
id: '123456789012345678901234'
};
adapter.create('person', doc).then(function(){
var model = adapter.model('person');
model.findOne({email: '123456789012345678901234'}).exec(function(err, doc){
should.not.exist(err);
should.exist(doc);
done();
});
});
});
});
describe('Relationships', function(){
describe('synchronizing many-to-many', function(){
it('should keep in sync many-to-many relationship', function(done){
Expand Down

0 comments on commit c99366c

Please sign in to comment.