Skip to content

Commit

Permalink
fix: allow arrays to be stored in type ObjecId
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Jun 30, 2020
1 parent c025814 commit f2cef0f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/mongodb.js
Expand Up @@ -2151,12 +2151,12 @@ function coercePropertyValue(modelCtor, propValue, propDef, setValue) {
if (Array.isArray(propValue)) {
propValue = propValue.map(val => {
if (isObjectIDProperty(modelCtor, propDef, val)) {
return coercedValue = ObjectID(propValue);
return coercedValue = ObjectID(val);
} else {
throw new Error(`${val} is not an ObjectID string`);
}
});
return setValue(coercedValue);
return setValue(propValue);
} else if (isObjectIDProperty(modelCtor, propDef, propValue)) {
coercedValue = ObjectID(propValue);
return setValue(coercedValue);
Expand Down
9 changes: 7 additions & 2 deletions test/objectid.test.js
Expand Up @@ -10,6 +10,7 @@ require('./init.js');
let Book, Chapter;
const ds = global.getDataSource();
const objectIDLikeString = '7cd2ad46ffc580ba45d3cb1f';
const objectIDLikeString2 = '7cd2ad46ffc580ba45d3cb1e';

describe('ObjectID', function() {
before(function() {
Expand Down Expand Up @@ -131,11 +132,15 @@ describe('ObjectID', function() {
it('should properly save an array of ObjectIDs', async () => {
await Article.create({
xid: objectIDLikeString,
xidArr: [objectIDLikeString],
xidArr: [objectIDLikeString, objectIDLikeString2],
title: 'arrayOfObjectID',
});
const found = await Article.findOne({where: {title: 'arrayOfObjectID'}});
found.xidArr.should.be.an.Array().which.containDeep([new ds.ObjectID(objectIDLikeString)]);
// the type of the returned array is actually string even it's stored as ObjectIds in the db as expected
found.xidArr.should.be.an.Array().which.containDeep([
new ds.ObjectID(objectIDLikeString),
new ds.ObjectID(objectIDLikeString2),
]);
});

it('handles auto-generated PK properties defined in LB4 style', async () => {
Expand Down

0 comments on commit f2cef0f

Please sign in to comment.