本章节文档中的所有示例均未使用数据库列名别名,列名均由 TypeScript 键名生成。
如果您愿意,可以在列名中使用数据库别名,也可以使用 casing 参数为 Drizzle 定义映射策略。
您可在此处了解更多内容 here
我们对所有类型均有原生支持,如果这些还不能满足您的需求,请随时创建 自定义类型。
本章节文档中的所有示例均未使用数据库列名别名,列名均由 TypeScript 键名生成。
如果您愿意,可以在列名中使用数据库别名,也可以使用 casing 参数为 Drizzle 定义映射策略。
您可在此处了解更多内容 here
有符号整数,根据值的大小,存储在 0、1、2、3、4、6 或 8 字节中。
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int()
});CREATE TABLE `table` (
`int` int
);import { tinyint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
tinyint: tinyint()
});CREATE TABLE `table` (
`tinyint` tinyint
);import { smallint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
smallint: smallint()
});CREATE TABLE `table` (
`smallint` smallint
);import { mediumint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
mediumint: mediumint()
});CREATE TABLE `table` (
`mediumint` mediumint
);import { bigint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
bigint: bigint({ mode: 'number' })
bigintUnsigned: bigint({ mode: 'number', unsigned: true })
});
bigint('...', { mode: 'number' | 'bigint' });
// 也可以为 bigint 指定 unsigned 选项
bigint('...', { mode: 'number' | 'bigint', unsigned: true })CREATE TABLE `table` (
`bigint` bigint,
`bigintUnsigned` bigint unsigned
);我们省略了 bigint(M) 中的 M 配置,因为它表示数字类型的显示宽度。
import { real, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
real: real()
});CREATE TABLE `table` (
`real` real
);import { real, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
realPrecision: real({ precision: 1,}),
realPrecisionScale: real({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`realPrecision` real(1),
`realPrecisionScale` real(1, 1)
);import { decimal, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
decimal: decimal(),
decimalNum: decimal({ scale: 30, mode: 'number' }),
decimalBig: decimal({ scale: 30, mode: 'bigint' }),
});CREATE TABLE `table` (
`decimal` decimal,
`decimalNum` decimal(30),
`decimalBig` decimal(30)
);import { decimal, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
decimalPrecision: decimal({ precision: 1,}),
decimalPrecisionScale: decimal({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`decimalPrecision` decimal(1),
`decimalPrecisionScale` decimal(1, 1)
);import { double, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
double: double('double')
});CREATE TABLE `table` (
`double` double
);import { double, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
doublePrecision: double({ precision: 1,}),
doublePrecisionScale: double({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`doublePrecision` double(1),
`doublePrecisionScale` double(1, 1)
);import { float, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
float: float()
});CREATE TABLE `table` (
`float` float
);SERIAL 是 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE 的别名。
import { serial, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
serial: serial()
});CREATE TABLE `table` (
`serial` serial AUTO_INCREMENT
);import { binary, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
binary: binary()
});CREATE TABLE `table` (
`binary` binary
);import { varbinary, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
varbinary: varbinary({ length: 2}),
});CREATE TABLE `table` (
`varbinary` varbinary(2)
);import { char, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
char: char(),
});CREATE TABLE `table` (
`char` char
);您可以定义 { enum: ["value1", "value2"] } 配置以推断 insert 和 select 类型,但它 不会 在运行时检查值。
import { varchar, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
varchar: varchar({ length: 2 }),
});
// 推断为文本类型: "value1" | "value2" | null
varchar: varchar({ length: 6, enum: ["value1", "value2"] })CREATE TABLE `table` (
`varchar` varchar(2)
);您可以定义 { enum: ["value1", "value2"] } 配置以推断 insert 和 select 类型,但它 不会 在运行时检查值。
import { text, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
text: text(),
});
// 推断为文本类型: "value1" | "value2" | null
text: text({ enum: ["value1", "value2"] });CREATE TABLE `table` (
`text` text
);import { boolean, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
boolean: boolean(),
});CREATE TABLE `table` (
`boolean` boolean
);import { boolean, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
date: date(),
});CREATE TABLE `table` (
`date` date
);import { datetime, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
datetime: datetime(),
});
datetime('...', { mode: 'date' | "string"}),CREATE TABLE `table` (
`datetime` datetime
);import { time, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
time: time(),
});CREATE TABLE `table` (
`time` time
);import { year, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
year: year(),
});CREATE TABLE `table` (
`year` year
);import { timestamp, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
timestamp: timestamp(),
});
timestamp('...', { mode: 'date' | "string"}),CREATE TABLE `table` (
`timestamp` timestamp
);import { timestamp, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
timestamp: timestamp().defaultNow(),
});CREATE TABLE `table` (
`timestamp` timestamp DEFAULT (now())
);import { json, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
json: json(),
});
CREATE TABLE `table` (
`json` json
);您可以指定 .$type<..>() 来推断 json 对象类型,但它 不会 在运行时校验值。
此功能为默认值、插入和查询的类型提供编译时保护。
// 推断为 { foo: string }
json: json().$type<{ foo: string }>();
// 推断为 string[]
json: json().$type<string[]>();
// 无法编译
json: json().$type<string[]>().default({});import { singlestoreEnum, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
popularity: singlestoreEnum(['unknown', 'known', 'popular']),
});CREATE TABLE `table` (
`popularity` enum('unknown','known','popular')
);每个列构建器都拥有 .$type() 方法,允许您自定义列的数据类型。这在使用未知类型或标记类型时非常有用。
type UserId = number & { __brand: 'user_id' };
type Data = {
foo: string;
bar: number;
};
const users = singlestoreTable('users', {
id: int().$type<UserId>().primaryKey(),
jsonField: json().$type<Data>(),
});NOT NULL 约束规定表中对应列不可包含 NULL 值。
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().notNull(),
});CREATE TABLE `table` (
`int` int NOT NULL
);DEFAULT 子句指定在 INSERT 操作时若用户未提供明确值,列默认使用的值。
若定义时未显式指定 DEFAULT 子句,默认值为 NULL。
显式的 DEFAULT 子句允许指定默认值为 NULL、字符串常量、二进制常量、带符号数字或任意括号括起的常量表达式。
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().default(3),
});CREATE TABLE `table` (
`int` int DEFAULT 3
);使用 $default() 或 $defaultFn() 可在运行时生成默认值,并在所有插入查询中使用这些值。它们是同一函数的不同别名。
这些函数帮助您使用 uuid、cuid、cuid2 等多种实现。
注意:此值不影响 drizzle-kit 行为,仅在 drizzle-orm 运行时使用。
import { varchar, singlestoreTable } from "drizzle-orm/singlestore-core";
import { createId } from '@paralleldrive/cuid2';
const table = singlestoreTable('table', {
id: varchar({ length: 128 }).$defaultFn(() => createId()),
});使用 $onUpdate() 或 $onUpdateFn() 可在更新行时生成动态值。这两个是同一函数的不同别名。
该函数将在行更新时调用,若未提供该列值,使用函数返回值作为值。
如果未提供默认值($default 或 $defaultFn),该函数在插入时也会被调用,使用返回值作为列值。
注意:此值不影响 drizzle-kit 行为,仅在 drizzle-orm 运行时使用。
import { text, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
alwaysNull: text().$type<string | null>().$onUpdate(() => null),
});import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().primaryKey(),
});CREATE TABLE `table` (
`int` int PRIMARY KEY NOT NULL
);import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().autoincrement(),
});CREATE TABLE `table` (
`int` int AUTO_INCREMENT
);