Browse Source

feat(nocodb): utc timezone handling for mysql

pull/5601/head
Wing-Kam Wong 2 years ago
parent
commit
06fc89a101
  1. 33
      packages/nocodb/src/lib/meta/NcMetaIOImpl.ts
  2. 35
      packages/nocodb/src/lib/utils/NcConfigFactory.ts

33
packages/nocodb/src/lib/meta/NcMetaIOImpl.ts

@ -1,5 +1,6 @@
import CryptoJS from 'crypto-js'; import CryptoJS from 'crypto-js';
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
import dayjs from 'dayjs';
import { XKnex } from '../db/sql-data-mapper'; import { XKnex } from '../db/sql-data-mapper';
import XcMigrationSource from '../migrations/XcMigrationSource'; import XcMigrationSource from '../migrations/XcMigrationSource';
import NcConnectionMgr from '../utils/common/NcConnectionMgr'; import NcConnectionMgr from '../utils/common/NcConnectionMgr';
@ -94,6 +95,20 @@ export default class NcMetaIOImpl extends NcMetaIO {
return (this.trx || this.connection) as any; return (this.trx || this.connection) as any;
} }
private isMySQL(): boolean {
return (
this.config?.meta?.db?.client === 'mysql' ||
this.config?.meta?.db?.client === 'mysql2'
);
}
private now(): any {
if (this.isMySQL()) {
return dayjs().utc().format('YYYY-MM-DD HH:mm:ss');
}
return dayjs().utc().toISOString();
}
public updateKnex(connectionConfig): void { public updateKnex(connectionConfig): void {
this.connection = XKnex(connectionConfig); this.connection = XKnex(connectionConfig);
} }
@ -107,6 +122,10 @@ export default class NcMetaIOImpl extends NcMetaIO {
migrationSource: new XcMigrationSourcev2(), migrationSource: new XcMigrationSourcev2(),
tableName: 'xc_knex_migrationsv2', tableName: 'xc_knex_migrationsv2',
}); });
if (this.isMySQL()) {
// set timezone
await this.connection.raw(`SET time_zone = '+00:00'`);
}
return true; return true;
} }
@ -238,8 +257,8 @@ export default class NcMetaIOImpl extends NcMetaIO {
return this.knexConnection(target).insert({ return this.knexConnection(target).insert({
db_alias: dbAlias, db_alias: dbAlias,
project_id, project_id,
created_at: this.knexConnection?.fn?.now(), created_at: this.now(),
updated_at: this.knexConnection?.fn?.now(), updated_at: this.now(),
...data, ...data,
}); });
} }
@ -260,8 +279,8 @@ export default class NcMetaIOImpl extends NcMetaIO {
if (project_id !== null) insertObj.project_id = project_id; if (project_id !== null) insertObj.project_id = project_id;
await this.knexConnection(target).insert({ await this.knexConnection(target).insert({
...insertObj, ...insertObj,
created_at: insertObj?.created_at || this.knexConnection?.fn?.now(), created_at: insertObj?.created_at || this.now(),
updated_at: insertObj?.updated_at || this.knexConnection?.fn?.now(), updated_at: insertObj?.updated_at || this.now(),
}); });
return insertObj; return insertObj;
} }
@ -410,7 +429,7 @@ export default class NcMetaIOImpl extends NcMetaIO {
delete data.created_at; delete data.created_at;
query.update({ ...data, updated_at: this.knexConnection?.fn?.now() }); query.update({ ...data, updated_at: this.now() });
if (typeof idOrCondition !== 'object') { if (typeof idOrCondition !== 'object') {
query.where('id', idOrCondition); query.where('id', idOrCondition);
} else if (idOrCondition) { } else if (idOrCondition) {
@ -530,8 +549,8 @@ export default class NcMetaIOImpl extends NcMetaIO {
// todo: check project name used or not // todo: check project name used or not
await this.knexConnection('nc_projects').insert({ await this.knexConnection('nc_projects').insert({
...project, ...project,
created_at: this.knexConnection?.fn?.now(), created_at: this.now(),
updated_at: this.knexConnection?.fn?.now(), updated_at: this.now(),
}); });
// todo // todo

35
packages/nocodb/src/lib/utils/NcConfigFactory.ts

@ -232,6 +232,13 @@ export default class NcConfigFactory implements NcConfig {
acquireConnectionTimeout: 600000, acquireConnectionTimeout: 600000,
} as any; } as any;
if (url.protocol.startsWith('mysql')) {
dbConfig.connection = {
...dbConfig.connection,
...this.mysqlConnectionTypeCastConfig,
};
}
if (process.env.NODE_TLS_REJECT_UNAUTHORIZED) { if (process.env.NODE_TLS_REJECT_UNAUTHORIZED) {
dbConfig.connection.ssl = true; dbConfig.connection.ssl = true;
} }
@ -344,6 +351,14 @@ export default class NcConfigFactory implements NcConfig {
} }
: {}), : {}),
}; };
if (url.protocol.startsWith('mysql')) {
dbConfig.connection = {
...dbConfig.connection,
...this.mysqlConnectionTypeCastConfig,
};
}
if (process.env.NODE_TLS_REJECT_UNAUTHORIZED) { if (process.env.NODE_TLS_REJECT_UNAUTHORIZED) {
dbConfig.connection.ssl = true; dbConfig.connection.ssl = true;
} }
@ -507,6 +522,13 @@ export default class NcConfigFactory implements NcConfig {
}; };
} }
if (dbConfig.client.startsWith('mysql')) {
dbConfig.connection = {
...dbConfig.connection,
...this.mysqlConnectionTypeCastConfig,
};
}
// todo: // todo:
const key = ''; const key = '';
Object.assign(dbConfig, { Object.assign(dbConfig, {
@ -739,6 +761,19 @@ export default class NcConfigFactory implements NcConfig {
return res; return res;
} }
private static mysqlConnectionTypeCastConfig = {
typeCast: function (field, next) {
if (
field.type === 'DATETIME' &&
(field.name === 'created_at' || field.name === 'updated_at')
) {
return new Date(field.string() + ' UTC');
}
return next();
},
timezone: '+00:00',
};
// public static initOneClickDeployment() { // public static initOneClickDeployment() {
// if (process.env.NC_ONE_CLICK) { // if (process.env.NC_ONE_CLICK) {
// const url = NcConfigFactory.extractXcUrlFromJdbc(process.env.DATABASE_URL); // const url = NcConfigFactory.extractXcUrlFromJdbc(process.env.DATABASE_URL);

Loading…
Cancel
Save