Skip to content

Commit

Permalink
fix(data-types): moment object throwing error (sequelize#13818)
Browse files Browse the repository at this point in the history
* fix(data-types): moment object throwing error

* fix: typo

* fix: revert with consistent variable naming

* test(moment): add moment support test case

Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
Co-authored-by: Sascha Depold <sascha@depold.com>
  • Loading branch information
3 people authored and aliatsis committed Jun 2, 2022
1 parent b8ef47b commit 1741ec2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 28 deletions.
4 changes: 3 additions & 1 deletion lib/data-types.js
Expand Up @@ -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');
}
Expand Down
12 changes: 8 additions & 4 deletions 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,
Expand Down Expand Up @@ -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++ ) {
Expand All @@ -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');
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/dialects/mariadb/data-types.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
Expand Down
22 changes: 11 additions & 11 deletions lib/dialects/mysql/data-types.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/dialects/postgres/connection-manager.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions 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';

Expand Down Expand Up @@ -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');
}
Expand All @@ -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}`);
Expand Down
13 changes: 13 additions & 0 deletions test/integration/model/findAll.test.js
Expand Up @@ -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: {
Expand Down

0 comments on commit 1741ec2

Please sign in to comment.