From 1534392c621718ef14af0923eddc2ca2a6247158 Mon Sep 17 00:00:00 2001 From: biniam Date: Tue, 16 Apr 2019 10:50:54 -0400 Subject: [PATCH 1/2] fix: coerce date array properties --- lib/list.js | 10 +++++++-- test/datatype.test.js | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/list.js b/lib/list.js index 038e4323..eccd003f 100644 --- a/lib/list.js +++ b/lib/list.js @@ -67,8 +67,14 @@ function List(items, itemType, parent) { if (isClass(this.itemType)) { return new this.itemType(item); } else { - if (Array.isArray(item)) return item; - else return this.itemType(item); + if (Array.isArray(item)) { + return item; + } else if (this.itemType === Date) { + if (item === null) return null; + return new Date(item); + } else { + return this.itemType(item); + } } }; diff --git a/test/datatype.test.js b/test/datatype.test.js index cb7168ee..241cc470 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -52,6 +52,54 @@ describe('datatypes', function() { }); Account.definition.properties.item.type.should.not.equal(String); }); + it('should resolve array prop with connector specific metadata', function() { + const model = db.define('test', { + randomReview: { + type: [String], + mongodb: { + dataType: 'Decimal128', + }, + }, + }); + model.definition.properties.randomReview.type.should.deepEqual(Array(String)); + model.definition.properties.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'}); + }); + + it('should coerce array of dates from string', async () => { + const dateArrayModel = db.define('dateArrayModel', { + bunchOfDates: [Date], + bunchOfOtherDates: { + type: [Date], + }, + }); + const dateVal = new Date('2019-02-21T12:00:00').toISOString(); + const created = await dateArrayModel.create({ + bunchOfDates: [dateVal, + dateVal, + dateVal], + bunchOfOtherDates: [dateVal, + dateVal, + dateVal], + }); + created.bunchOfDates[0].should.be.an.instanceOf(Date); + created.bunchOfDates[0].should.deepEqual(new Date(dateVal)); + created.bunchOfOtherDates[0].should.be.an.instanceOf(Date); + created.bunchOfOtherDates[0].should.deepEqual(new Date(dateVal)); + }); + + it('should coerce array of numbers from string', async () => { + const numArrayModel = db.define('numArrayModel', { + bunchOfNums: [Number], + }); + const dateVal = new Date('2019-02-21T12:00:00').toISOString(); + const created = await numArrayModel.create({ + bunchOfNums: ['1', + '2', + '3'], + }); + created.bunchOfNums[0].should.be.an.instanceOf(Number); + created.bunchOfNums[0].should.equal(1); + }); it('should return 400 when property of type array is set to string value', function(done) { From 48af738ec4028eee2de9d3604f5b3bb73a498643 Mon Sep 17 00:00:00 2001 From: biniam Date: Tue, 16 Apr 2019 10:51:44 -0400 Subject: [PATCH 2/2] fix: coerce primitive properties on update --- lib/dao.js | 1 + test/basic-querying.test.js | 34 ++++++++++++++++++++++++++++++++++ test/manipulation.test.js | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 24bd6682..516b21c4 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -2301,6 +2301,7 @@ DataAccessObject.updateAll = function(where, data, options, cb) { if (!(data instanceof Model)) { try { inst = new Model(data, {applyDefaultValues: false}); + ctx.data = inst.toObject(true); } catch (err) { return cb(err); } diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 3e79be45..0fb605d2 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -1055,6 +1055,40 @@ describe('basic-querying', function() { }); }); + describe('updateAll', function() { + it('coerces primitive datatypes on update', async () => { + const numAndDateModel = db.define('numAndDateModel', { + dateProp: Date, + dateArray: [Date], + numProp: Number, + numArray: [Number], + }); + const createDate = new Date('2019-02-21T12:00:00').toISOString(); + const createData = { + dateProp: createDate, + dateArray: [createDate, createDate], + numProp: '1', + numArray: ['1', '2'], + }; + const updateDate = new Date('2019-04-15T12:00:00').toISOString(); + const updateData = { + dateProp: updateDate, + dateArray: [updateDate, updateDate], + numProp: '3', + numArray: ['3', '4'], + }; + const created = await numAndDateModel.create(createData); + const updated = await numAndDateModel.updateAll({id: created.id}, updateData); + const found = await numAndDateModel.findById(created.id); + found.dateProp.should.deepEqual(new Date(updateDate)); + found.dateArray[0].should.deepEqual(new Date(updateDate)); + found.dateArray[1].should.deepEqual(new Date(updateDate)); + found.numProp.should.equal(3); + found.numArray[0].should.equal(3); + found.numArray[1].should.equal(4); + }); + }); + context('regexp operator', function() { const invalidDataTypes = [0, true, {}, [], Function, null]; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index f5808eaa..1d416bcb 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -2366,7 +2366,7 @@ describe('manipulation', function() { it('should not coerce invalid values provided in where conditions', function(done) { Person.update({name: 'Brett Boe'}, {dob: 'notadate'}, function(err) { should.exist(err); - err.message.should.equal('Invalid date: notadate'); + err.message.should.equal('Invalid date: Invalid Date'); done(); }); });