diff --git a/lib/data-types.js b/lib/data-types.js index 66ab5b9af0ee..371652be60f9 100644 --- a/lib/data-types.js +++ b/lib/data-types.js @@ -469,7 +469,9 @@ class DATE extends ABSTRACT { return momentTz(date); } _stringify(date, options) { - date = this._applyTimezone(date, options); + if (!moment.isMoment(date)) { + date = this._applyTimezone(date, options); + } // Z here means current timezone, _not_ UTC return date.format('YYYY-MM-DD HH:mm:ss.SSS Z'); } diff --git a/lib/dialects/db2/data-types.js b/lib/dialects/db2/data-types.js index b2855b694634..a908a9498e30 100644 --- a/lib/dialects/db2/data-types.js +++ b/lib/dialects/db2/data-types.js @@ -1,6 +1,7 @@ 'use strict'; -const moment = require('moment-timezone'); +const momentTz = require('moment-timezone'); +const moment = require('moment'); module.exports = BaseTypes => { const warn = BaseTypes.ABSTRACT.warn.bind(undefined, @@ -194,7 +195,10 @@ module.exports = BaseTypes => { return `TIMESTAMP${ this._length ? `(${ this._length })` : ''}`; } _stringify(date, options) { - date = this._applyTimezone(date, options); + if (!moment.isMoment(date)) { + date = this._applyTimezone(date, options); + } + if (this._length > 0) { let msec = '.'; for ( let i = 0; i < this._length && i < 6; i++ ) { @@ -211,14 +215,14 @@ module.exports = BaseTypes => { if (value === null) { return value; } - value = new Date(moment.utc(value)); + value = new Date(momentTz.utc(value)); return value; } } class DATEONLY extends BaseTypes.DATEONLY { static parse(value) { - return moment(value).format('YYYY-MM-DD'); + return momentTz(value).format('YYYY-MM-DD'); } } diff --git a/lib/dialects/mariadb/data-types.js b/lib/dialects/mariadb/data-types.js index c163f206701f..202ebe5868b7 100644 --- a/lib/dialects/mariadb/data-types.js +++ b/lib/dialects/mariadb/data-types.js @@ -2,7 +2,8 @@ const wkx = require('wkx'); const _ = require('lodash'); -const moment = require('moment-timezone'); +const momentTz = require('moment-timezone'); +const moment = require('moment'); module.exports = BaseTypes => { BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://mariadb.com/kb/en/library/resultset/#field-types'; @@ -54,20 +55,19 @@ module.exports = BaseTypes => { return this._length ? `DATETIME(${this._length})` : 'DATETIME'; } _stringify(date, options) { - if (_.isDate(date)) { + if (!moment.isMoment(date)) { date = this._applyTimezone(date, options); - return date.format('YYYY-MM-DD HH:mm:ss.SSS'); } - return date; + return date.format('YYYY-MM-DD HH:mm:ss.SSS'); } static parse(value, options) { value = value.string(); if (value === null) { return value; } - if (moment.tz.zone(options.timezone)) { - value = moment.tz(value, options.timezone).toDate(); + if (momentTz.tz.zone(options.timezone)) { + value = momentTz.tz(value, options.timezone).toDate(); } else { value = new Date(`${value} ${options.timezone}`); diff --git a/lib/dialects/mysql/data-types.js b/lib/dialects/mysql/data-types.js index 017b74c130dd..3190c695ccc0 100644 --- a/lib/dialects/mysql/data-types.js +++ b/lib/dialects/mysql/data-types.js @@ -2,7 +2,9 @@ const wkx = require('wkx'); const _ = require('lodash'); -const moment = require('moment-timezone'); +const momentTz = require('moment-timezone'); +const moment = require('moment'); + module.exports = BaseTypes => { BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html'; @@ -53,24 +55,22 @@ module.exports = BaseTypes => { return this._length ? `DATETIME(${this._length})` : 'DATETIME'; } _stringify(date, options) { - if (_.isDate(date)) { + if (!moment.isMoment(date)) { date = this._applyTimezone(date, options); - // Fractional DATETIMEs only supported on MySQL 5.6.4+ - if (this._length) { - return date.format('YYYY-MM-DD HH:mm:ss.SSS'); - } - return date.format('YYYY-MM-DD HH:mm:ss'); } - - return date; + // Fractional DATETIMEs only supported on MySQL 5.6.4+ + if (this._length) { + return date.format('YYYY-MM-DD HH:mm:ss.SSS'); + } + return date.format('YYYY-MM-DD HH:mm:ss'); } static parse(value, options) { value = value.string(); if (value === null) { return value; } - if (moment.tz.zone(options.timezone)) { - value = moment.tz(value, options.timezone).toDate(); + if (momentTz.tz.zone(options.timezone)) { + value = momentTz.tz(value, options.timezone).toDate(); } else { value = new Date(`${value} ${options.timezone}`); diff --git a/lib/dialects/postgres/connection-manager.js b/lib/dialects/postgres/connection-manager.js index 2d12b11176fa..f60b67df9d4e 100644 --- a/lib/dialects/postgres/connection-manager.js +++ b/lib/dialects/postgres/connection-manager.js @@ -7,7 +7,7 @@ const debug = logger.debugContext('connection:pg'); const sequelizeErrors = require('../../errors'); const semver = require('semver'); const dataTypes = require('../../data-types'); -const moment = require('moment-timezone'); +const momentTz = require('moment-timezone'); const { promisify } = require('util'); class ConnectionManager extends AbstractConnectionManager { @@ -224,7 +224,7 @@ class ConnectionManager extends AbstractConnectionManager { } if (!this.sequelize.config.keepDefaultTimezone) { - const isZone = !!moment.tz.zone(this.sequelize.options.timezone); + const isZone = !!momentTz.tz.zone(this.sequelize.options.timezone); if (isZone) { query += `SET TIME ZONE '${this.sequelize.options.timezone}';`; } else { diff --git a/lib/dialects/snowflake/data-types.js b/lib/dialects/snowflake/data-types.js index 8d424d0abde2..0cfe20a33c4d 100644 --- a/lib/dialects/snowflake/data-types.js +++ b/lib/dialects/snowflake/data-types.js @@ -1,6 +1,8 @@ 'use strict'; -const moment = require('moment-timezone'); +const momentTz = require('moment-timezone'); +const moment = require('moment'); + module.exports = BaseTypes => { BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.snowflake.com/doc/refman/5.7/en/data-types.html'; @@ -40,7 +42,9 @@ module.exports = BaseTypes => { return 'TIMESTAMP'; } _stringify(date, options) { - date = this._applyTimezone(date, options); + if (!moment.isMoment(date)) { + date = this._applyTimezone(date, options); + } if (this._length) { return date.format('YYYY-MM-DD HH:mm:ss.SSS'); } @@ -51,8 +55,8 @@ module.exports = BaseTypes => { if (value === null) { return value; } - if (moment.tz.zone(options.timezone)) { - value = moment.tz(value, options.timezone).toDate(); + if (momentTz.tz.zone(options.timezone)) { + value = momentTz.tz(value, options.timezone).toDate(); } else { value = new Date(`${value} ${options.timezone}`); diff --git a/test/integration/model/findAll.test.js b/test/integration/model/findAll.test.js index eae031ed1384..48f3035d3052 100644 --- a/test/integration/model/findAll.test.js +++ b/test/integration/model/findAll.test.js @@ -387,6 +387,19 @@ describe(Support.getTestDialectTeaser('Model'), () => { expect(users[0].intVal).to.equal(10); }); + it('should be able to find a row using greater than or equal to logic with moment dates', async function() { + const users = await this.User.findAll({ + where: { + theDate: { + [Op.gte]: moment('2013-01-09') + } + } + }); + + expect(users[0].username).to.equal('boo2'); + expect(users[0].intVal).to.equal(10); + }); + it('should be able to find a row using greater than or equal to', async function() { const user = await this.User.findOne({ where: {