valibot
安装依赖
npm
yarn
pnpm
bun
npm i drizzle-orm@rc valibot
选择 schema
定义从数据库查询的数据形状,可用于验证 API 响应。
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
age: int().notNull()
});
const userSelectSchema = createSelectSchema(users);
const rows = await db.select({ id: users.id, name: users.name }).from(users).limit(1);
const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // 错误:上面的查询未返回 `age`
const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // 将成功解析也支持视图。
import { mysqlView } from 'drizzle-orm/mysql-core';
import { gt } from 'drizzle-orm';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const usersView = mysqlView('users_view').as((qb) => qb.select().from(users).where(gt(users.age, 18)));
const usersViewSchema = createSelectSchema(usersView);
const parsed: { id: number; name: string; age: number } = parse(usersViewSchema, ...);插入 schema
定义要插入到数据库中的数据形状,可用于验证 API 请求。
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createInsertSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
age: int().notNull()
});
const userInsertSchema = createInsertSchema(users);
const user = { name: 'John' };
const parsed: { id?: number | undefined, name: string, age: number } = parse(userInsertSchema, user); // 错误:未定义 `age`
const user = { name: 'Jane', age: 30 };
const parsed: { id?: number | undefined, name: string, age: number } = parse(userInsertSchema, user); // 将成功解析
await db.insert(users).values(parsed);更新 schema
定义要在数据库中更新的数据形状,可用于验证 API 请求。
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createUpdateSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
import { eq } from "drizzle-orm";
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
age: int().notNull()
});
const userUpdateSchema = createUpdateSchema(users);
const user = { age: 35 };
const parsed: {
id?: number | undefined;
name?: string | undefined;
age?: number | undefined;
} = parse(userUpdateSchema, user); // 将成功解析
await db.update(users).set(parsed).where(eq(users.name, 'Jane'));精炼
每个 create schema 函数都接受一个额外的可选参数,可用于扩展、修改或完全覆盖某个字段的 schema。定义回调函数会扩展或修改 schema,而提供一个 Valibot schema 则会覆盖它。
import { int, json, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse, pipe, maxLength, object, string } from 'valibot';
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
bio: text(),
preferences: json()
});
const userSelectSchema = createSelectSchema(users, {
name: (schema) => pipe(schema, maxLength(20)), // 扩展 schema
bio: (schema) => pipe(schema, maxLength(1000)), // 在变为可空/可选之前先扩展 schema
preferences: object({ theme: string() }) // 覆盖该字段,包括其可空性
});
const parsed: {
id: number;
name: string,
bio: string | null;
preferences: {
theme: string;
};
} = parse(userSelectSchema, ...);数据类型参考
mysql.boolean();
// Schema
boolean();mysql.mysqlEnum('name', ['val1', 'val2']);
// Schema
enum({ val1: "val1", val2: "val2" });mysql.date({ mode: 'date' });
mysql.datetime({ mode: 'date' });
mysql.timestamp({ mode: 'date' });
// Schema
date();mysql.binary();
mysql.date({ mode: 'string' });
mysql.datetime({ mode: 'string' });
mysql.decimal();
mysql.time();
mysql.timestamp({ mode: 'string' });
mysql.varbinary();
// Schema
string();mysql.tinyblob({ mode: 'string' });
mysql.tinyblob();
pipe(string(), maxLength(255))mysql.blob({ mode: 'string' });
mysql.blob();
pipe(string(), maxLength(65_535))mysql.mediumblob({ mode: 'string' });
mysql.mediumblob();
pipe(string(), maxLength(16_777_215))mysql.longblob({ mode: 'string' });
mysql.longblob();
pipe(string(), maxLength(4_294_967_295))mysql.char({ length: ... });
// Schema
pipe(string(), length(length));mysql.varchar({ length: ... });
// Schema
pipe(string(), maxLength(length));binary({ length: ... });
// Schema
pipe(string(), regex(/^[01]*$/), maxLength(length))mysql.varbinary({ length: ... });
// Schema
pipe(string(), regex(/^[01]*$/), maxLength(length))mysql.tinytext();
// Schema
pipe(string(), maxLength(255));mysql.text();
// Schema
pipe(string(), maxLength(65_535));mysql.mediumtext();
// Schema
pipe(string(), maxLength(16_777_215));mysql.longtext();
// Schema
pipe(string(), maxLength(4_294_967_295));mysql.tinytext({ enum: ... });
mysql.mediumtext({ enum: ... });
mysql.text({ enum: ... });
mysql.longtext({ enum: ... });
mysql.char({ enum: ... });
mysql.varchar({ enum: ... });
// Schema
enum(enum);mysql.tinyint();
// Schema
pipe(number(), minValue(-128), maxValue(127), int()); // 8 位整数下限和上限mysql.tinyint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(255), int()); // 无符号 8 位整数下限和上限mysql.smallint();
// Schema
pipe(number(), minValue(-32_768), maxValue(32_767), int()); // 16 位整数下限和上限mysql.smallint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(65_535), int()); // 无符号 16 位整数下限和上限mysql.float();
// Schema
pipe(number(), minValue(-8_388_608), maxValue(8_388_607)); // 24 位整数下限和上限mysql.mediumint();
// Schema
pipe(number(), minValue(-8_388_608), maxValue(8_388_607), int()); // 24 位整数下限和上限mysql.float({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(16_777_215)); // 无符号 24 位整数下限和上限mysql.mediumint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(16_777_215), int()); // 无符号 24 位整数下限和上限mysql.int();
// Schema
pipe(number(), minValue(-2_147_483_648), maxValue(2_147_483_647), int()); // 32 位整数下限和上限mysql.int({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(4_294_967_295), int()); // 无符号 32 位整数下限和上限mysql.double();
mysql.real();
// Schema
pipe(number(), minValue(-140_737_488_355_328), maxValue(140_737_488_355_327)); // 48 位整数下限和上限mysql.double({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(281_474_976_710_655)); // 无符号 48 位整数下限和上限mysql.decimal({ mode: 'number' });
// Schema
pipe(number(), minValue(-9_007_199_254_740_991), maxValue(9_007_199_254_740_991))mysql.decimal({ mode: 'bigint' });
// Schema
pipe(bigint(), minValue(-9_223_372_036_854_775_808n), maxValue(9_223_372_036_854_775_807n))mysql.decimal({ mode: 'number', unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991))mysql.decimal({ mode: 'bigint', unsigned: true });
// Schema
pipe(bigint(), minValue(0), maxValue(18_446_744_073_709_551_615n))mysql.bigint({ mode: 'number' });
// Schema
pipe(number(), minValue(-9_007_199_254_740_991), maxValue(9_007_199_254_740_991), int()); // Javascript 最小和最大安全整数mysql.bigint({ mode: 'number', unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()); // Javascript 最小和最大安全整数mysql.bigint({ mode: 'string' });
// Schema
pipe(
string(),
regex(/^-?\d+$/),
transform((v) => BigInt(v)),
minValue(-9_223_372_036_854_775_808n),
maxValue(9_223_372_036_854_775_807n),
transform((v) => v.toString()),
)mysql.bigint({ mode: 'string', unsigned: true });
// Schema
pipe(
string(),
regex(/^\d+$/),
transform((v) => BigInt(v)),
minValue(0n),
maxValue(9_223_372_036_854_775_807n),
transform((v) => v.toString()),
);mysql.bigint({ mode: 'bigint' });
// Schema
pipe(bigint(), minValue(-9_223_372_036_854_775_808n), maxValue(9_223_372_036_854_775_807n)); // 64 位整数下限和上限mysql.bigint({ mode: 'bigint', unsigned: true });
// Schema
pipe(bigint(), minValue(0n), maxValue(18_446_744_073_709_551_615n)); // 无符号 64 位整数下限和上限mysql.serial();
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()); // Javascript 最大安全整数mysql.year();
// Schema
pipe(number(), minValue(1_901), maxValue(2_155), int());mysql.json();
// Schema
union([union([string(), number(), boolean(), null_()]), array(any()), record(string(), any())]);