Skip to content

Commit aeae046

Browse files
Austinaustinwoon
authored andcommitted
feat: default to current timestamp
Co-authored-by: Austin Woon Quan <43132101+austinwoon@users.noreply.github.com>
1 parent 2ac68f0 commit aeae046

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/schema/column-definition-builder.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { GeneratedNode } from '../operation-node/generated-node.js'
1515
import { DefaultValueNode } from '../operation-node/default-value-node.js'
1616
import { parseOnModifyForeignAction } from '../parser/on-modify-action-parser.js'
1717
import { Expression } from '../expression/expression.js'
18+
import { RawNode } from '../operation-node/raw-node.js'
1819

1920
export class ColumnDefinitionBuilder implements OperationNodeSource {
2021
readonly #node: ColumnDefinitionNode
@@ -371,6 +372,29 @@ export class ColumnDefinitionBuilder implements OperationNodeSource {
371372
)
372373
}
373374

375+
/**
376+
* Adds `DEFAULT CURRENT_TIMESTAMP` for the column.
377+
*
378+
* ### Examples
379+
*
380+
* ```ts
381+
* db.schema.createTable('test')
382+
* .addColumn('created_at', 'datetime', (col) =>
383+
* col.defaultToCurrentTimestamp(),
384+
* )
385+
* .execute()
386+
* ```
387+
*/
388+
defaultToCurrentTimestamp(): ColumnDefinitionBuilder {
389+
return new ColumnDefinitionBuilder(
390+
ColumnDefinitionNode.cloneWith(this.#node, {
391+
defaultTo: DefaultValueNode.create(
392+
RawNode.createWithSql('current_timestamp'),
393+
),
394+
}),
395+
)
396+
}
397+
374398
/**
375399
* Adds a check constraint for the column.
376400
*

test/node/src/schema.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,53 @@ for (const dialect of DIALECTS) {
484484
throw new Error(`Unknown dialect: ${dialect}`)
485485
}
486486

487+
it('should create table with default timestamp column', async () => {
488+
if (dialect !== 'postgres') {
489+
const builder = ctx.db.schema
490+
.createTable('test')
491+
.addColumn('created_at', 'datetime', (col) =>
492+
col.defaultToCurrentTimestamp(),
493+
)
494+
495+
testSql(builder, dialect, {
496+
postgres: NOT_SUPPORTED,
497+
mysql: {
498+
sql: 'create table `test` (`created_at` datetime default current_timestamp)',
499+
parameters: [],
500+
},
501+
mssql: {
502+
sql: 'create table "test" ("created_at" datetime default current_timestamp)',
503+
parameters: [],
504+
},
505+
sqlite: {
506+
sql: 'create table "test" ("created_at" datetime default current_timestamp)',
507+
parameters: [],
508+
},
509+
})
510+
511+
await builder.execute()
512+
} else {
513+
const builder = ctx.db.schema
514+
.createTable('test')
515+
.addColumn('created_at', 'timestamptz', (col) =>
516+
col.defaultToCurrentTimestamp(),
517+
)
518+
.addColumn('data', 'varchar')
519+
520+
testSql(builder, dialect, {
521+
postgres: {
522+
sql: 'create table "test" ("created_at" timestamptz default current_timestamp, "data" varchar)',
523+
parameters: [],
524+
},
525+
mysql: NOT_SUPPORTED,
526+
mssql: NOT_SUPPORTED,
527+
sqlite: NOT_SUPPORTED,
528+
})
529+
530+
await builder.execute()
531+
}
532+
})
533+
487534
it('should create a table with a unique constraints', async () => {
488535
const builder = ctx.db.schema
489536
.createTable('test')

0 commit comments

Comments
 (0)