Skip to content

Commit

Permalink
fix(Date): fix convert date and timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Duy Luong committed Mar 29, 2018
1 parent 250933e commit 6b96828
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/LucidMongo/Model/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down Expand Up @@ -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)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/LucidMongo/QueryBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
82 changes: 72 additions & 10 deletions test/unit/field-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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']
Expand All @@ -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)
})
})

Expand Down Expand Up @@ -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')
})
})
4 changes: 2 additions & 2 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -1475,7 +1475,7 @@ test.group('Model', (group) => {
city: 'hnn'
},
age: 20,
birthDate: moment('2001-12-12')
birthDate: moment.utc('2001-12-12')
})
})
})
Expand Down

0 comments on commit 6b96828

Please sign in to comment.