From 58d2df802344e2ed5f2ba049bdfb57332635f0bf Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 2 Apr 2024 20:53:48 +0800 Subject: [PATCH] feat: impl Date/timestamp on update --- core/dal-decorator/src/decorator/Column.ts | 2 + core/dal-runtime/src/SqlGenerator.ts | 16 +++++- core/dal-runtime/test/SqlGenerator.test.ts | 14 +++++ .../fixtures/modules/dal/AutoUpdateTime.ts | 51 +++++++++++++++++++ plugin/dal/README.md | 6 +++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts diff --git a/core/dal-decorator/src/decorator/Column.ts b/core/dal-decorator/src/decorator/Column.ts index bfed3f63..8790fb15 100644 --- a/core/dal-decorator/src/decorator/Column.ts +++ b/core/dal-decorator/src/decorator/Column.ts @@ -84,11 +84,13 @@ export interface DateParams extends IColumnTypeParams { export interface DateTimeParams extends IColumnTypeParams { type: ColumnType.DATETIME; precision?: number; + autoUpdate?: boolean; } export interface TimestampParams extends IColumnTypeParams { type: ColumnType.TIMESTAMP; precision?: number; + autoUpdate?: boolean; } export interface TimeParams extends IColumnTypeParams { diff --git a/core/dal-runtime/src/SqlGenerator.ts b/core/dal-runtime/src/SqlGenerator.ts index 9be0a844..11947e9d 100644 --- a/core/dal-runtime/src/SqlGenerator.ts +++ b/core/dal-runtime/src/SqlGenerator.ts @@ -126,7 +126,21 @@ export class SqlGenerator { break; } case ColumnType.DATETIME: - case ColumnType.TIMESTAMP: + case ColumnType.TIMESTAMP: { + if (columnType.precision) { + sqls.push(`${columnType.type}(${columnType.precision})`); + } else { + sqls.push(columnType.type); + } + if (columnType.autoUpdate) { + if (columnType.precision) { + sqls.push(`ON UPDATE CURRENT_TIMESTAMP(${columnType.precision})`); + } else { + sqls.push('ON UPDATE CURRENT_TIMESTAMP'); + } + } + break; + } case ColumnType.TIME: { if (columnType.precision) { sqls.push(`${columnType.type}(${columnType.precision})`); diff --git a/core/dal-runtime/test/SqlGenerator.test.ts b/core/dal-runtime/test/SqlGenerator.test.ts index 4d3ccc9a..8f1ba217 100644 --- a/core/dal-runtime/test/SqlGenerator.test.ts +++ b/core/dal-runtime/test/SqlGenerator.test.ts @@ -2,6 +2,7 @@ import assert from 'node:assert'; import { Foo } from './fixtures/modules/dal/Foo'; import { SqlGenerator } from '../src/SqlGenerator'; import { TableModel } from '@eggjs/dal-decorator'; +import { AutoUpdateTime } from './fixtures/modules/dal/AutoUpdateTime'; describe('test/SqlGenerator.test.ts', () => { it('generator should work', () => { @@ -53,4 +54,17 @@ describe('test/SqlGenerator.test.ts', () => { ' UNIQUE KEY uk_name_col1 (name,col1) USING BTREE COMMENT \'index comment\\n\'\n' + ') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';'); }); + + it('generator auto update should work', () => { + const generator = new SqlGenerator(); + const autoUpdateTimeTableModel = TableModel.build(AutoUpdateTime); + const sql = generator.generate(autoUpdateTimeTableModel); + assert.equal(sql, 'CREATE TABLE IF NOT EXISTS auto_update_times (\n' + + ' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' + + ' date DATETIME ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' + + ' date_2 DATETIME(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY,\n' + + ' date_3 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' + + ' date_4 TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY\n' + + ') ;'); + }); }); diff --git a/core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts b/core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts new file mode 100644 index 00000000..bf75cf73 --- /dev/null +++ b/core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts @@ -0,0 +1,51 @@ +import { + Column, + ColumnType, + Table, +} from '@eggjs/dal-decorator'; + +@Table() +export class AutoUpdateTime { + @Column({ + type: ColumnType.INT, + }, { + primaryKey: true, + autoIncrement: true, + comment: 'the primary key', + }) + id: number; + + @Column({ + type: ColumnType.DATETIME, + autoUpdate: true, + }, { + uniqueKey: true, + }) + date: Date; + + @Column({ + type: ColumnType.DATETIME, + precision: 3, + autoUpdate: true, + }, { + uniqueKey: true, + }) + date2: Date; + + @Column({ + type: ColumnType.TIMESTAMP, + autoUpdate: true, + }, { + uniqueKey: true, + }) + date3: Date; + + @Column({ + type: ColumnType.TIMESTAMP, + precision: 3, + autoUpdate: true, + }, { + uniqueKey: true, + }) + date4: Date; +} diff --git a/plugin/dal/README.md b/plugin/dal/README.md index 7b2ac030..8f4a8b67 100644 --- a/plugin/dal/README.md +++ b/plugin/dal/README.md @@ -355,12 +355,18 @@ export interface DateParams { export interface DateTimeParams { type: ColumnType.DATETIME; precision?: number; + // 自动添加 ON UPDATE CURRENT_TIMESTAMP + // 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision) + autoUpdate?: boolean; } // Timestamp 类型,对应 js 中的 Date export interface TimestampParams { type: ColumnType.TIMESTAMP; precision?: number; + // 自动添加 ON UPDATE CURRENT_TIMESTAMP + // 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision) + autoUpdate?: boolean; } // Times 类型,对应 js 中的 string