- 开始使用 PostgreSQL、 MySQL 和 SQLite
- 了解 PostgreSQL、MySQL 和 SQLite 的列数据类型。
- sql 运算符
PostgreSQL
要在 PostgreSQL 中将当前时间戳设置为默认值,可以使用 defaultNow()
方法或 sql
操作符配合 now()
函数,后者返回带有时区的当前日期和时间。
import { sql } from 'drizzle-orm';
import { timestamp, pgTable, serial } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
timestamp1: timestamp('timestamp1').notNull().defaultNow(),
timestamp2: timestamp('timestamp2', { mode: 'string' })
.notNull()
.default(sql`now()`),
});
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"timestamp1" timestamp DEFAULT now() NOT NULL,
"timestamp2" timestamp DEFAULT now() NOT NULL
);
mode
选项定义了如何在应用程序中处理值。string
模式的值在应用程序中被视为 string
,但在数据库中以时间戳形式存储。
// Data stored in the database
+----+----------------------------+----------------------------+
| id | timestamp1 | timestamp2 |
+----+----------------------------+----------------------------+
| 1 | 2024-04-11 14:14:28.038697 | 2024-04-11 14:14:28.038697 |
+----+----------------------------+----------------------------+
// Data returned by the application
[
{
id: 1,
timestamp1: 2024-04-11T14:14:28.038Z, // Date object
timestamp2: '2024-04-11 14:14:28.038697' // string
}
]
要在 PostgreSQL 中将 Unix 时间戳设置为默认值,可以使用 sql
操作符配合 extract(epoch from now())
函数,后者返回自 1970-01-01 00:00:00 UTC
以来的秒数。
import { sql } from 'drizzle-orm';
import { integer, pgTable, serial } from 'drizzle-orm/pg-core'
export const users = pgTable('users', {
id: serial('id').primaryKey(),
timestamp: integer('timestamp')
.notNull()
.default(sql`extract(epoch from now())`),
});
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"timestamp" integer DEFAULT extract(epoch from now()) NOT NULL
);
// Data stored in the database
+----+------------+
| id | timestamp |
+----+------------+
| 1 | 1712846784 |
+----+------------+
// Data returned by the application
[
{
id: 1,
timestamp: 1712846784 // number
}
]
MySQL
要在 MySQL 中将当前时间戳设置为默认值,可以使用 defaultNow()
方法或 sql
操作符配合 now()
函数,后者返回当前日期和时间 (YYYY-MM-DD HH-MM-SS)
。
import { sql } from 'drizzle-orm';
import { mysqlTable, serial, timestamp } from 'drizzle-orm/mysql-core';
export const users = mysqlTable('users', {
id: serial('id').primaryKey(),
timestamp1: timestamp('timestamp1').notNull().defaultNow(),
timestamp2: timestamp('timestamp2', { mode: 'string' })
.notNull()
.default(sql`now()`),
timestamp3: timestamp('timestamp3', { fsp: 3 }) // fractional seconds part
.notNull()
.default(sql`now(3)`),
});
CREATE TABLE `users` (
`id` serial AUTO_INCREMENT NOT NULL,
`timestamp1` timestamp NOT NULL DEFAULT now(),
`timestamp2` timestamp NOT NULL DEFAULT now(),
`timestamp3` timestamp(3) NOT NULL DEFAULT now(3),
CONSTRAINT `users_id` PRIMARY KEY(`id`)
);
fsp
选项定义了时间戳中包含的毫秒(小数秒)位数。默认值为 0
。mode
选项定义了如何在应用程序中处理值。string
模式的值在应用程序中被视为 string
,但在数据库中以时间戳形式存储。
// Data stored in the database
+----+---------------------+---------------------+-------------------------+
| id | timestamp1 | timestamp2 | timestamp3 |
+----+---------------------+---------------------+-------------------------+
| 1 | 2024-04-11 15:24:53 | 2024-04-11 15:24:53 | 2024-04-11 15:24:53.236 |
+----+---------------------+---------------------+-------------------------+
// Data returned by the application
[
{
id: 1,
timestamp1: 2024-04-11T15:24:53.000Z, // Date object
timestamp2: '2024-04-11 15:24:53', // string
timestamp3: 2024-04-11T15:24:53.236Z // Date object
}
]
要在 MySQL 中将 Unix 时间戳设置为默认值,可以使用 sql
操作符配合 unix_timestamp()
函数,后者返回自 1970-01-01 00:00:00 UTC
以来的秒数。
import { sql } from 'drizzle-orm';
import { mysqlTable, serial, int } from 'drizzle-orm/mysql-core';
export const users = mysqlTable('users', {
id: serial('id').primaryKey(),
timestamp: int('timestamp')
.notNull()
.default(sql`(unix_timestamp())`),
});
CREATE TABLE `users` (
`id` serial AUTO_INCREMENT NOT NULL,
`timestamp` int NOT NULL DEFAULT (unix_timestamp()),
CONSTRAINT `users_id` PRIMARY KEY(`id`)
);
// Data stored in the database
+----+------------+
| id | timestamp |
+----+------------+
| 1 | 1712847986 |
+----+------------+
// Data returned by the application
[
{
id: 1,
timestamp: 1712847986 // number
}
]
SQLite
要在 SQLite 中将当前时间戳设置为默认值,可以使用 sql
操作符配合 current_timestamp
常量,后者返回当前 UTC 日期和时间的文本表示 (YYYY-MM-DD HH:MM:SS)
。
import { sql } from 'drizzle-orm';
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
export const users = sqliteTable('users', {
id: integer('id').primaryKey(),
timestamp: text('timestamp')
.notNull()
.default(sql`(current_timestamp)`),
});
CREATE TABLE `users` (
`id` integer PRIMARY KEY NOT NULL,
`timestamp` text DEFAULT (current_timestamp) NOT NULL
);
// Data stored in the database
+----+---------------------+
| id | timestamp |
+----+---------------------+
| 1 | 2024-04-11 15:40:43 |
+----+---------------------+
// Data returned by the application
[
{
id: 1,
timestamp: '2024-04-11 15:40:43' // string
}
]
要在 SQLite 中将 Unix 时间戳设置为默认值,可以使用 sql
操作符配合 unixepoch()
函数,后者返回自 1970-01-01 00:00:00 UTC
以来的秒数。
import { sql } from 'drizzle-orm';
import { integer, sqliteTable } from 'drizzle-orm/sqlite-core';
export const users = sqliteTable('users', {
id: integer('id').primaryKey(),
timestamp1: integer('timestamp1', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
timestamp2: integer('timestamp2', { mode: 'timestamp_ms' })
.notNull()
.default(sql`(unixepoch() * 1000)`),
timestamp3: integer('timestamp3', { mode: 'number' })
.notNull()
.default(sql`(unixepoch())`),
});
CREATE TABLE `users` (
`id` integer PRIMARY KEY NOT NULL,
`timestamp1` integer DEFAULT (unixepoch()) NOT NULL,
`timestamp2` integer DEFAULT (unixepoch() * 1000) NOT NULL,
`timestamp3` integer DEFAULT (unixepoch()) NOT NULL
);
mode
选项定义了如何在应用程序中处理值。在应用程序中,timestamp
和 timestamp_ms
模式的值被视为 Date
对象,但在数据库中以整数形式存储。它们的不同之处在于,timestamp
处理秒,而 timestamp_ms
处理毫秒。
// Data stored in the database
+------------+------------+---------------+------------+
| id | timestamp1 | timestamp2 | timestamp3 |
+------------+------------+---------------+------------+
| 1 | 1712835640 | 1712835640000 | 1712835640 |
+------------+------------+---------------+------------+
// Data returned by the application
[
{
id: 1,
timestamp1: 2024-04-11T11:40:40.000Z, // Date object
timestamp2: 2024-04-11T11:40:40.000Z, // Date object
timestamp3: 1712835640 // number
}
]