Skip to content

Commit e5c7b8d

Browse files
authored
feat: impl Date/timestamp on update (#203)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced an `autoUpdate` property for `DateTime` and `Timestamp` columns to enable automatic updating of timestamps. - **Documentation** - Updated documentation to include details about the new `autoUpdate` property for time-related columns. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 9fd585d commit e5c7b8d

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

core/dal-decorator/src/decorator/Column.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ export interface DateParams extends IColumnTypeParams {
8484
export interface DateTimeParams extends IColumnTypeParams {
8585
type: ColumnType.DATETIME;
8686
precision?: number;
87+
autoUpdate?: boolean;
8788
}
8889

8990
export interface TimestampParams extends IColumnTypeParams {
9091
type: ColumnType.TIMESTAMP;
9192
precision?: number;
93+
autoUpdate?: boolean;
9294
}
9395

9496
export interface TimeParams extends IColumnTypeParams {

core/dal-runtime/src/SqlGenerator.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,21 @@ export class SqlGenerator {
126126
break;
127127
}
128128
case ColumnType.DATETIME:
129-
case ColumnType.TIMESTAMP:
129+
case ColumnType.TIMESTAMP: {
130+
if (columnType.precision) {
131+
sqls.push(`${columnType.type}(${columnType.precision})`);
132+
} else {
133+
sqls.push(columnType.type);
134+
}
135+
if (columnType.autoUpdate) {
136+
if (columnType.precision) {
137+
sqls.push(`ON UPDATE CURRENT_TIMESTAMP(${columnType.precision})`);
138+
} else {
139+
sqls.push('ON UPDATE CURRENT_TIMESTAMP');
140+
}
141+
}
142+
break;
143+
}
130144
case ColumnType.TIME: {
131145
if (columnType.precision) {
132146
sqls.push(`${columnType.type}(${columnType.precision})`);

core/dal-runtime/test/SqlGenerator.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'node:assert';
22
import { Foo } from './fixtures/modules/dal/Foo';
33
import { SqlGenerator } from '../src/SqlGenerator';
44
import { TableModel } from '@eggjs/dal-decorator';
5+
import { AutoUpdateTime } from './fixtures/modules/dal/AutoUpdateTime';
56

67
describe('test/SqlGenerator.test.ts', () => {
78
it('generator should work', () => {
@@ -53,4 +54,17 @@ describe('test/SqlGenerator.test.ts', () => {
5354
' UNIQUE KEY uk_name_col1 (name,col1) USING BTREE COMMENT \'index comment\\n\'\n' +
5455
') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';');
5556
});
57+
58+
it('generator auto update should work', () => {
59+
const generator = new SqlGenerator();
60+
const autoUpdateTimeTableModel = TableModel.build(AutoUpdateTime);
61+
const sql = generator.generate(autoUpdateTimeTableModel);
62+
assert.equal(sql, 'CREATE TABLE IF NOT EXISTS auto_update_times (\n' +
63+
' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' +
64+
' date DATETIME ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' +
65+
' date_2 DATETIME(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY,\n' +
66+
' date_3 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL UNIQUE KEY,\n' +
67+
' date_4 TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL UNIQUE KEY\n' +
68+
') ;');
69+
});
5670
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
Column,
3+
ColumnType,
4+
Table,
5+
} from '@eggjs/dal-decorator';
6+
7+
@Table()
8+
export class AutoUpdateTime {
9+
@Column({
10+
type: ColumnType.INT,
11+
}, {
12+
primaryKey: true,
13+
autoIncrement: true,
14+
comment: 'the primary key',
15+
})
16+
id: number;
17+
18+
@Column({
19+
type: ColumnType.DATETIME,
20+
autoUpdate: true,
21+
}, {
22+
uniqueKey: true,
23+
})
24+
date: Date;
25+
26+
@Column({
27+
type: ColumnType.DATETIME,
28+
precision: 3,
29+
autoUpdate: true,
30+
}, {
31+
uniqueKey: true,
32+
})
33+
date2: Date;
34+
35+
@Column({
36+
type: ColumnType.TIMESTAMP,
37+
autoUpdate: true,
38+
}, {
39+
uniqueKey: true,
40+
})
41+
date3: Date;
42+
43+
@Column({
44+
type: ColumnType.TIMESTAMP,
45+
precision: 3,
46+
autoUpdate: true,
47+
}, {
48+
uniqueKey: true,
49+
})
50+
date4: Date;
51+
}

plugin/dal/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,18 @@ export interface DateParams {
355355
export interface DateTimeParams {
356356
type: ColumnType.DATETIME;
357357
precision?: number;
358+
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
359+
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
360+
autoUpdate?: boolean;
358361
}
359362
360363
// Timestamp 类型,对应 js 中的 Date
361364
export interface TimestampParams {
362365
type: ColumnType.TIMESTAMP;
363366
precision?: number;
367+
// 自动添加 ON UPDATE CURRENT_TIMESTAMP
368+
// 如果有精度则为 ON UPDATE CURRENT_TIMESTAMP(precision)
369+
autoUpdate?: boolean;
364370
}
365371
366372
// Times 类型,对应 js 中的 string

0 commit comments

Comments
 (0)