本部分文档中的所有示例均不使用数据库列名别名,列名是从 TypeScript 键生成的。
如果需要,您可以在列名中使用数据库别名,也可以使用 casing
参数为 Drizzle 定义映射策略。
您可以在此处了解更多信息。
根据官方 SQLite 文档,存储在 SQLite 数据库中(或由数据库引擎操作)的每个值都具有以下存储类型之一:NULL
、INTEGER
、REAL
、TEXT
和 BLOB
。
我们对所有这些类型都提供了原生支持,如果这还不够,您可以随意创建 自定义类型。
本部分文档中的所有示例均不使用数据库列名别名,列名是从 TypeScript 键生成的。
如果需要,您可以在列名中使用数据库别名,也可以使用 casing
参数为 Drizzle 定义映射策略。
您可以在此处了解更多信息。
一种有符号整数,根据值的量级,以 0
、1
、2
、3
、4
、6
或 8
字节存储。
import { integer, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
id: integer()
});
// you can customize integer mode to be number, boolean, timestamp, timestamp_ms
integer({ mode: 'number' })
integer({ mode: 'boolean' })
integer({ mode: 'timestamp_ms' })
integer({ mode: 'timestamp' }) // Date
CREATE TABLE `table` (
`id` integer
);
// to make integer primary key auto increment
integer({ mode: 'number' }).primaryKey({ autoIncrement: true })
CREATE TABLE `table` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL
);
一个浮点值,以 8 字节 IEEE
浮点数存储。
import { real, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
real: real()
});
CREATE TABLE `table` (
`real` real
);
一个文本字符串,使用数据库编码(UTF-8
、UTF-16BE
或 UTF-16LE
)存储。
您可以定义 { enum: ["value1", "value2"] }
配置来推断 insert
和 select
类型,但它不会检查运行时值。
import { text, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
text: text()
});
// will be inferred as text: "value1" | "value2" | null
text({ enum: ["value1", "value2"] })
text({ mode: 'json' })
text({ mode: 'json' }).$type<{ foo: string }>()
CREATE TABLE `table` (
`text` text
);
一个数据 Blob,完全按照输入时的样子存储。
建议使用 text('', { mode: 'json' })
而不是 blob('', { mode: 'json' })
,因为它支持 JSON 函数。
目前,所有 JSON 函数在参数为 BLOB 类型时都会抛出错误,因为 BLOB 被保留用于未来的增强功能,届时 BLOB 将存储 JSON 的二进制编码。
import { blob, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
blob: blob()
});
blob()
blob({ mode: 'buffer' })
blob({ mode: 'bigint' })
blob({ mode: 'json' })
blob({ mode: 'json' }).$type<{ foo: string }>()
CREATE TABLE `table` (
`blob` blob
);
您可以为 Blob 推断指定 .$type<..>()
,它不会检查运行时值。它为默认值、插入和选择 schema 提供编译时保护。
// will be inferred as { foo: string }
json: blob({ mode: 'json' }).$type<{ foo: string }>();
// will be inferred as string[]
json: blob({ mode: 'json' }).$type<string[]>();
// won't compile
json: blob({ mode: 'json' }).$type<string[]>().default({});
SQLite 没有原生的 boolean
数据类型,但您可以将 integer
列指定为 boolean
模式。这允许您在代码中操作布尔值,Drizzle 会将它们作为 0 和 1 的整数值存储在数据库中。
import { integer, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
id: integer({ mode: 'boolean' })
});
CREATE TABLE `table` (
`id` integer
);
由于 SQLite 中没有 bigint
数据类型,Drizzle 为 blob
列提供了一种特殊的 bigint
模式。此模式允许您在代码中使用 BigInt 实例,Drizzle 会将它们作为 Blob 值存储在数据库中。
import { blob, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
id: blob({ mode: 'bigint' })
});
CREATE TABLE `table` (
`id` blob
);
import { blob, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
numeric: numeric(),
numericNum: numeric({ mode: 'number' }),
numericBig: numeric({ mode: 'bigint' }),
});
CREATE TABLE `table` (
`numeric` numeric,
`numericNum` numeric,
`numericBig` numeric
);
每个列构建器都有一个 .$type()
方法,允许您自定义列的数据类型。这对于未知类型或品牌类型(branded types)等情况非常有用。
type UserId = number & { __brand: 'user_id' };
type Data = {
foo: string;
bar: number;
};
const users = sqliteTable('users', {
id: integer().$type<UserId>().primaryKey(),
jsonField: blob().$type<Data>(),
});
NOT NULL
约束规定关联的列不能包含 NULL
值。
const table = sqliteTable('table', {
numInt: integer().notNull()
});
CREATE TABLE table (
`numInt` integer NOT NULL
);
DEFAULT
子句指定了当用户在执行 INSERT
操作时未显式提供值时,列要使用的默认值。如果列定义没有显式的 DEFAULT
子句,则列的默认值为 NULL
。
显式的 DEFAULT
子句可以指定默认值为 NULL
、字符串常量、BLOB 常量、带符号数字,或任何用括号括起来的常量表达式。
import { sql } from "drizzle-orm";
import { integer, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
int1: integer().default(42),
int2: integer().default(sql`(abs(42))`)
});
CREATE TABLE `table` (
`int1` integer DEFAULT 42,
`int2` integer DEFAULT (abs(42))
);
默认值也可以是特殊的不区分大小写的关键字之一:CURRENT_TIME
、CURRENT_DATE
或 CURRENT_TIMESTAMP
。
import { sql } from "drizzle-orm";
import { text, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable("table", {
time: text().default(sql`(CURRENT_TIME)`),
date: text().default(sql`(CURRENT_DATE)`),
timestamp: text().default(sql`(CURRENT_TIMESTAMP)`),
});
CREATE TABLE `table` (
`time` text DEFAULT (CURRENT_TIME),
`date` text DEFAULT (CURRENT_DATE),
`timestamp` text DEFAULT (CURRENT_TIMESTAMP)
);
当使用 $default()
或 $defaultFn()
时(它们只是同一函数的不同别名),您可以在运行时生成默认值,并将这些值用于所有插入查询。这些函数可以帮助您利用各种实现,例如 uuid
、cuid
、cuid2
等。
注意:此值不影响 drizzle-kit
的行为,它仅在 drizzle-orm
中于运行时使用。
import { text, sqliteTable } from "drizzle-orm/sqlite-core";
import { createId } from '@paralleldrive/cuid2';
const table = sqliteTable('table', {
id: text().$defaultFn(() => createId()),
});
当使用 $onUpdate()
或 $onUpdateFn()
时(它们只是同一函数的不同别名),您可以在运行时生成默认值,并将这些值用于所有更新查询。
为列添加动态更新值。当行更新时,将调用此函数,如果未提供任何值,则返回的值将用作列值。如果未提供默认值(或 $defaultFn),则在插入行时也会调用该函数,返回的值将用作列值。
注意:此值不影响 drizzle-kit
的行为,它仅在 drizzle-orm
中于运行时使用。
import { text, sqliteTable } from "drizzle-orm/sqlite-core";
const table = sqliteTable('table', {
alwaysNull: text().$type<string | null>().$onUpdate(() => null),
});