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

feat: impl Date/timestamp on update #203

Merged
merged 1 commit into from
Apr 2, 2024
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
2 changes: 2 additions & 0 deletions core/dal-decorator/src/decorator/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion core/dal-runtime/src/SqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
Expand Down
14 changes: 14 additions & 0 deletions core/dal-runtime/test/SqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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' +
') ;');
});
});
51 changes: 51 additions & 0 deletions core/dal-runtime/test/fixtures/modules/dal/AutoUpdateTime.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions plugin/dal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading