From 6b968288f727bbc7344512a0b2d09cf3f02e760c Mon Sep 17 00:00:00 2001 From: Duy Luong Date: Thu, 29 Mar 2018 10:18:40 +0700 Subject: [PATCH] fix(Date): fix convert date and timezone --- src/LucidMongo/Model/Base.js | 4 +- src/LucidMongo/QueryBuilder/index.js | 3 +- test/unit/field-format.spec.js | 82 ++++++++++++++++++++++++---- test/unit/lucid.spec.js | 4 +- 4 files changed, 78 insertions(+), 15 deletions(-) diff --git a/src/LucidMongo/Model/Base.js b/src/LucidMongo/Model/Base.js index 351a1de..35dcea6 100644 --- a/src/LucidMongo/Model/Base.js +++ b/src/LucidMongo/Model/Base.js @@ -177,7 +177,7 @@ class BaseModel { * @return {String} */ static formatDates (key, value) { - return moment.isMoment(value) ? value.toDate() : moment(value).toDate() + return moment.isMoment(value) ? value.toDate() : moment.utc(value).toDate() } /** @@ -339,7 +339,7 @@ class BaseModel { * @static */ static parseDates (key, value) { - return moment.isMoment(value) || !value ? value : moment(value) + return (moment.isMoment(value) || !value) ? value : moment.utc(value) } /** diff --git a/src/LucidMongo/QueryBuilder/index.js b/src/LucidMongo/QueryBuilder/index.js index 8995b28..d589522 100644 --- a/src/LucidMongo/QueryBuilder/index.js +++ b/src/LucidMongo/QueryBuilder/index.js @@ -696,8 +696,9 @@ class QueryBuilder { queryObject[key][subKey] = this.Model.formatField(key, queryObject[key][subKey]) } } + } else { + queryObject[key] = this.Model.formatField(key, queryObject[key]) } - queryObject[key] = this.Model.formatField(key, queryObject[key]) } this.query.where(queryObject) } else if (_.isFunction(arguments[0])) { diff --git a/test/unit/field-format.spec.js b/test/unit/field-format.spec.js index 3c3563f..1589466 100644 --- a/test/unit/field-format.spec.js +++ b/test/unit/field-format.spec.js @@ -67,7 +67,7 @@ test.group('Field date format', (group) => { const user = new User() user.last_login = '2018-01-01' assert.equal(moment.isMoment(user.$attributes.last_login), true) - assert.equal(moment('2018-01-01').isSame(user.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(user.last_login), true) }) test('Should parse the date field when assign by constructor', async (assert) => { @@ -81,7 +81,7 @@ test.group('Field date format', (group) => { last_login: '2018-01-01' }) assert.equal(moment.isMoment(user.$attributes.last_login), true) - assert.equal(moment('2018-01-01').isSame(user.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(user.last_login), true) }) test('Should parse the date field when fill', async (assert) => { @@ -96,7 +96,7 @@ test.group('Field date format', (group) => { last_login: '2018-01-01' }) assert.equal(moment.isMoment(user.$attributes.last_login), true) - assert.equal(moment('2018-01-01').isSame(user.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(user.last_login), true) }) test('Should store date field as date', async (assert) => { @@ -110,10 +110,10 @@ test.group('Field date format', (group) => { last_login: '2018-01-01' }) assert.equal(moment.isMoment(user.$attributes.last_login), true) - assert.equal(moment('2018-01-01').isSame(user.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(user.last_login), true) const newUser = await ioc.use('Database').collection('users').findOne() assert.instanceOf(newUser.last_login, Date) - assert.equal(moment('2018-01-01').isSame(newUser.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(newUser.last_login), true) }) test('Should update date field as date', async (assert) => { @@ -130,10 +130,10 @@ test.group('Field date format', (group) => { user.last_login = '2018-01-02' await user.save() assert.equal(moment.isMoment(user.$attributes.last_login), true) - assert.equal(moment('2018-01-02').isSame(user.last_login), true) + assert.equal(moment.utc('2018-01-02').isSame(user.last_login), true) const newUser = await ioc.use('Database').collection('users').findOne() assert.instanceOf(newUser.last_login, Date) - assert.equal(moment('2018-01-02').isSame(newUser.last_login), true) + assert.equal(moment.utc('2018-01-02').isSame(newUser.last_login), true) }) test('Should convert date field as moment after fetch from database', async (assert) => { @@ -153,10 +153,10 @@ test.group('Field date format', (group) => { ]) const users = await User.all() assert.equal(moment.isMoment(users.first().$attributes.last_login), true) - assert.equal(moment('2018-01-01').isSame(users.first().last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(users.first().last_login), true) }) - test('Should convert date params as date when build query', async (assert) => { + test('Should convert string params as date when build query 2 params', async (assert) => { class User extends Model { static get dates () { return ['last_login'] @@ -165,7 +165,55 @@ test.group('Field date format', (group) => { User._bootIfNotBooted() const query = User.where('last_login', '2018-01-01') assert.instanceOf(query.query._conditions.last_login, Date) - assert.equal(moment('2018-01-01').isSame(query.query._conditions.last_login), true) + assert.equal(moment.utc('2018-01-01').isSame(query.query._conditions.last_login), true) + }) + + test('Should convert string params as date when build query object', async (assert) => { + class User extends Model { + static get dates () { + return ['last_login'] + } + } + User._bootIfNotBooted() + const query = User.where({ 'last_login': { $lt: '2018-01-01' } }) + assert.instanceOf(query.query._conditions.last_login.$lt, Date) + assert.equal(moment.utc('2018-01-01').isSame(query.query._conditions.last_login.$lt), true) + }) + + test('Should convert string params as date when build query with chain method', async (assert) => { + class User extends Model { + static get dates () { + return ['last_login'] + } + } + User._bootIfNotBooted() + const query = User.where('last_login').lt('2018-01-01') + assert.instanceOf(query.query._conditions.last_login.$lt, Date) + assert.equal(moment.utc('2018-01-01').isSame(query.query._conditions.last_login.$lt), true) + }) + + test('Should convert moment params as date when build query', async (assert) => { + class User extends Model { + static get dates () { + return ['last_login'] + } + } + User._bootIfNotBooted() + const query = User.where({ 'last_login': { $gte: moment('2018-01-01') } }) + assert.instanceOf(query.query._conditions.last_login.$gte, Date) + assert.equal(moment('2018-01-01').isSame(query.query._conditions.last_login.$gte), true) + }) + + test('Should keep date params when build query', async (assert) => { + class User extends Model { + static get dates () { + return ['last_login'] + } + } + User._bootIfNotBooted() + const query = User.where({ 'last_login': { $gte: new Date('2018-01-01') } }) + assert.instanceOf(query.query._conditions.last_login.$gte, Date) + assert.equal(moment.utc('2018-01-01').isSame(query.query._conditions.last_login.$gte), true) }) }) @@ -690,4 +738,18 @@ test.group('Field ObjectID format', (group) => { assert.equal(String(query.query._conditions.group_id.$in[0]), '5a40077430f075256427a147') assert.equal(String(query.query._conditions.group_id.$in[1]), '5a40077430f075256427a148') }) + + test('Should convert array of string params as ObjectID when build query object', async (assert) => { + class User extends Model { + static get objectIDs () { + return ['_id', 'group_id'] + } + } + User._bootIfNotBooted() + const query = User.where({ 'group_id': { $in: ['5a40077430f075256427a147', '5a40077430f075256427a148'] } }) + assert.instanceOf(query.query._conditions.group_id.$in[0], ObjectID) + assert.instanceOf(query.query._conditions.group_id.$in[1], ObjectID) + assert.equal(String(query.query._conditions.group_id.$in[0]), '5a40077430f075256427a147') + assert.equal(String(query.query._conditions.group_id.$in[1]), '5a40077430f075256427a148') + }) }) diff --git a/test/unit/lucid.spec.js b/test/unit/lucid.spec.js index 7d16b7f..78941a1 100644 --- a/test/unit/lucid.spec.js +++ b/test/unit/lucid.spec.js @@ -1450,7 +1450,7 @@ test.group('Model', (group) => { test('should update sub attribute', async (assert) => { class User extends Model { - static get dates () { return [...super.dates, 'birthDate', 'joinDate'] } + static get dates () { return super.dates.concat(['birthDate', 'joinDate']) } } User._bootIfNotBooted() @@ -1475,7 +1475,7 @@ test.group('Model', (group) => { city: 'hnn' }, age: 20, - birthDate: moment('2001-12-12') + birthDate: moment.utc('2001-12-12') }) }) })