Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: date strings should be parsed with system time zone considered #365

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
"node": ">= 12.0.0"
},
"dependencies": {
"dayjs": "^1.10.3",
"debug": "^3.1.0",
"deep-equal": "^2.0.5",
"heredoc": "^1.3.1",
"pluralize": "^7.0.0",
"reflect-metadata": "^0.1.13",
"sqlstring": "^2.3.0",
"strftime": "^0.10.0",
"validator": "^13.5.2"
},
"devDependencies": {
Expand All @@ -65,7 +65,6 @@
"@journeyapps/sqlcipher": "^5.2.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.1",
"dayjs": "^1.10.3",
"eslint": "^7.20.0",
"eslint-plugin-no-only-tests": "^3.0.0",
"expect.js": "^0.3.1",
Expand Down
8 changes: 4 additions & 4 deletions src/data_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Raw from './raw';
import util from 'util';
import dayjs from 'dayjs';
const invokableFunc = require('./utils/invokable');

export enum LENGTH_VARIANTS {
Expand Down Expand Up @@ -316,14 +317,13 @@ class DATE extends DataType {
// @deprecated
// vaguely standard date formats such as 2021-10-15 15:50:02,548
if (typeof value === 'string' && rDateFormat.test(value)) {
// 2021-10-15 15:50:02,548 => 2021-10-15T15:50:02,548,
// 2021-10-15 15:50:02 => 2021-10-15T15:50:02.000
value = new Date(`${value.replace(' ', 'T').replace(',', '.')}`);
// 2021-10-15 15:50:02,548 => 2021-10-15 15:50:02.548,
value = dayjs(`${value.replace(',', '.')}`).toDate();
}

// 1634611135776
// '2021-10-15T08:38:43.877Z'
if (!(value instanceof Date)) value = new Date((value as string));
if (!(value instanceof Date)) value = dayjs((value as string)).toDate();
if (isNaN((value as any))) throw new Error(util.format('invalid date: %s', originValue));

return this._round(value);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/mysql/spellbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MySQLSpellBook extends Spellbook {
const { columnAttributes, primaryColumn } = Model;

if (Array.isArray(updateOnDuplicate) && updateOnDuplicate.length) {
columns = updateOnDuplicate.map(column => (columnAttributes[column] && columnAttributes[column].columnName ) || column)
columns = updateOnDuplicate.map(column => (columnAttributes[column] && columnAttributes[column].columnName) || column)
.filter(column => column !== primaryColumn);
} else if (!columns.length) {
columns = Object.values(columnAttributes).map(attribute => attribute.columnName).filter(column => column !== primaryColumn);
Expand Down Expand Up @@ -80,7 +80,7 @@ class MySQLSpellBook extends Spellbook {

/**
* DELETE ... ORDER BY ...LIMIT
* @param {Spell} spell
* @param {Spell} spell
*/
formatDelete(spell) {
const result = super.formatDelete(spell);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/sqlite/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const strftime = require('strftime');
const dayjs = require('dayjs');
const { performance } = require('perf_hooks');

const AbstractDriver = require('../abstract');
Expand Down Expand Up @@ -46,7 +46,7 @@ class SqliteDriver extends AbstractDriver {
if (values) {
values = values.map(entry => {
if (entry instanceof Date) {
return strftime('%Y-%m-%d %H:%M:%S.%L %:z', entry);
return dayjs(entry).format('YYYY-MM-DD HH:mm:ss.SSS Z');
}
return entry;
});
Expand Down
4 changes: 2 additions & 2 deletions src/migrations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const dayjs = require('dayjs');
const fs = require('fs').promises;
const path = require('path');
const strftime = require('strftime');

async function loadTasks(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
Expand Down Expand Up @@ -61,7 +61,7 @@ async function rollback(step = 1) {

async function createMigrationFile(name) {
const { migrations: dir } = this.options;
const timestamp = strftime('%Y%m%d%H%M%S');
const timestamp = dayjs().format('YYYYMMDDHHmmss');
const fpath = path.join(dir, `${timestamp}-${name}.js`);
await fs.writeFile(fpath, `'use strict';

Expand Down
8 changes: 4 additions & 4 deletions test/integration/custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class MySpellbook extends SqliteDriver.Spellbook {
obj[escapeId(Model.unalias(key))] = spell.sets[key];
return obj;
}, values);
let whereArgs = [];

const whereArgs = [];
let whereClause = '';
if (whereConditions.length > 0) {
for (const condition of whereConditions) collectLiteral(spell, condition, whereArgs);
Expand All @@ -77,7 +77,7 @@ class MySpellbook extends SqliteDriver.Spellbook {
const { Model, whereConditions } = spell;
const { escapeId } = Model.driver;
const table = escapeId(spell.table.value);
let whereArgs = [];
const whereArgs = [];
let whereClause = '';
if (whereConditions.length > 0) {
for (const condition of whereConditions) collectLiteral(spell, condition, whereArgs);
Expand All @@ -94,7 +94,7 @@ class MySpellbook extends SqliteDriver.Spellbook {
const { Model, sets } = spell;
const { escapeId } = Model.driver;
const table = escapeId(spell.table.value);
let values = {};
const values = {};

const { shardingKey } = Model;
if (shardingKey && sets[shardingKey] == null) {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/mysql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert').strict;
const path = require('path');
const strftime = require('strftime');
const dayjs = require('dayjs');

const { connect } = require('../..');

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('=> Data types (mysql)', function() {
birthday: new Date(2021, 5, 26),
sex: 'M',
});
assert.equal(strftime('%Y-%m-%d', user.birthday), '2021-06-26');
assert.equal(dayjs(user.birthday).format('YYYY-MM-DD'), '2021-06-26');
});

it('CHAR', async function() {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/suite/data_types.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const assert = require('assert').strict;
const strftime = require('strftime');
const dayjs = require('dayjs');

const { Bone, DataTypes } = require('../../..');
const { INTEGER, STRING, DATE, DATEONLY, TEXT, BOOLEAN, JSON, JSONB, BIGINT } = DataTypes;
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('=> Data types - DATE', function() {
});

await assert.doesNotReject(async function() {
const result = await Note.where({ createdAt: strftime('%Y-%m-%d %H:%M:%S,%L', date) });
const result = await Note.where({ createdAt: dayjs(date).format('YYYY-MM-DD HH:mm:ss,SSS') });
assert.equal(result.length, 1);
});

Expand Down Expand Up @@ -319,7 +319,7 @@ describe('=> Data types - DATEONLY', function() {
const date = new Date('2021-10-15T08:38:43.877Z');
const note = await Note.create({ createdAt: date });
await note.reload();
assert.equal(strftime('%Y-%m-%d', note.createdAt), '2021-10-15');
assert.equal(dayjs(note.createdAt).format('YYYY-MM-DD'), '2021-10-15');

await assert.doesNotReject(async function() {
const result = await Note.where({ createdAt: date });
Expand Down
4 changes: 2 additions & 2 deletions test/integration/suite/migrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const assert = require('assert').strict;
const fs = require('fs').promises;
const path = require('path');
const strftime = require('strftime');
const dayjs = require('dayjs');

const Realm = require('../../..');
const Logger = require('../../../src/drivers/abstract/logger');
Expand All @@ -15,7 +15,7 @@ const migrations = path.join(__dirname, '../migrations');
// class Topic extends Realm.Bone {}

async function createTopics() {
const name = `${strftime('%Y%m%d%H%M%S')}-create-topics.js`;
const name = `${dayjs().format('YYYYMMDDHHmmss')}-create-topics.js`;
const fpath = path.join(migrations, name);
await fs.writeFile(fpath, `'use strict';

Expand Down
4 changes: 2 additions & 2 deletions test/unit/drivers/sqlite/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert').strict;
const path = require('path');
const strftime = require('strftime');
const dayjs = require('dayjs');

const { heresql } = require('../../../../src/utils/string');
const SqliteDriver = require('../../../../src/drivers/sqlite');
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('=> SQLite driver.query()', () => {
} = await driver.query(heresql(`
SELECT datetime(created_at, 'localtime') AS created_at FROM notes
`));
assert.equal(created_at, strftime('%Y-%m-%d %H:%M:%S', createdAt));
assert.equal(created_at, dayjs(createdAt).format('YYYY-MM-DD HH:mm:ss'));
});

it('should handle boolean correctly', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/expr_formatter.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const assert = require('assert').strict;
const strftime = require('strftime');
const dayjs = require('dayjs');
const { connect, Bone } = require('../..');

describe('=> formatExpr', function() {
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('=> formatExpr', function() {
it('should cast date with precision set in database', async function() {
// users.birthday stores DATE without specific hours or so
const today = new Date();
const formatted = strftime('%Y-%m-%d', today);
const formatted = dayjs(today).format('YYYY-MM-DD');
assert.equal(
User.where({ birthday: today }).toSqlString(),
"SELECT * FROM `users` WHERE `birthday` = '" + formatted + " 00:00:00.000'"
Expand Down
11 changes: 9 additions & 2 deletions test/unit/spell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ describe('=> Spell', function() {
);
});

it('where date', function() {
assert.equal(
Post.where('createdAt > ?', '2022-11-03').toString(),
"SELECT * FROM `articles` WHERE `gmt_create` > '2022-11-03 00:00:00.000' AND `gmt_deleted` IS NULL"
);
});

it('where compound string conditions', function() {
assert.equal(
Post.where('title like "Arch%" or (title = "New Post" || title = "Skeleton King")').toString(),
Expand Down Expand Up @@ -623,8 +630,8 @@ describe('=> Spell', function() {

it('order by string with multiple condition', () => {
assert.equal(
Post.order('id asc, gmt_created desc').toString(),
'SELECT * FROM `articles` WHERE `gmt_deleted` IS NULL ORDER BY `id`, `gmt_created` DESC'
Post.order('id asc, gmt_create desc').toString(),
'SELECT * FROM `articles` WHERE `gmt_deleted` IS NULL ORDER BY `id`, `gmt_create` DESC'
);
});

Expand Down