typebox-legacy

安装依赖

npm
yarn
pnpm
bun
npm i drizzle-orm@rc @sinclair/typebox

选择 schema

定义从数据库查询出的数据形状,可用于校验 API 响应。

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createSelectSchema } from 'drizzle-orm/typebox-legacy';
import { Value } from '@sinclair/typebox/value';

const users = cockroachTable('users', {
  id: int4().primaryKey().generatedAlwaysAsIdentity(),
  name: text().notNull(),
  age: int4().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 } = Value.Parse(userSelectSchema, rows[0]); // 错误:上面的查询未返回 `age`

const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = Value.Parse(userSelectSchema, rows[0]); // 将成功解析

也支持视图和枚举。

import { cockroachEnum, cockroachView } from 'drizzle-orm/cockroach-core';
import { createSelectSchema } from 'drizzle-orm/typebox-legacy';
import { Value } from '@sinclair/typebox/value';

const roles = cockroachEnum('roles', ['admin', 'basic']);
const rolesSchema = createSelectSchema(roles);
const parsed: 'admin' | 'basic' = Value.Parse(rolesSchema, ...);

const usersView = cockroachView('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 } = Value.Parse(usersViewSchema, ...);

插入 schema

定义要插入到数据库中的数据形状,可用于校验 API 请求。

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createInsertSchema } from 'drizzle-orm/typebox-legacy';
import { Value } from '@sinclair/typebox/value';

const users = cockroachTable('users', {
  id: int4().primaryKey().generatedAlwaysAsIdentity(),
  name: text().notNull(),
  age: int4().notNull()
});

const userInsertSchema = createInsertSchema(users);

const user = { name: 'John' };
const parsed: { name: string, age: number } = Value.Parse(userInsertSchema, user); // 错误:未定义 `age`

const user = { name: 'Jane', age: 30 };
const parsed: { name: string, age: number } = Value.Parse(userInsertSchema, user); // 将成功解析
await db.insert(users).values(parsed);

更新 schema

定义要在数据库中更新的数据形状,可用于校验 API 请求。

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createUpdateSchema } from 'drizzle-orm/typebox-legacy';
import { Value } from '@sinclair/typebox/value';

const users = cockroachTable('users', {
  id: int4().primaryKey().generatedAlwaysAsIdentity(),
  name: text().notNull(),
  age: int4().notNull()
});

const userUpdateSchema = createUpdateSchema(users);

const user = { age: 35 };
const parsed: { name?: string | undefined, age?: number | undefined } = Value.Parse(userUpdateSchema, user); // 将成功解析
await db.update(users).set(parsed).where(eq(users.name, 'Jane'));

细化

每个 create schema 函数都接受一个额外的可选参数,可用于扩展、修改或完全覆盖某个字段的 schema。定义回调函数会进行扩展或修改,而提供 Typebox schema 则会覆盖它。

import { int4, jsonb, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createSelectSchema } from 'drizzle-orm/typebox-legacy';
import { Type } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';

const users = cockroachTable('users', {
  id: int4().primaryKey().generatedAlwaysAsIdentity(),
  name: text().notNull(),
  bio: text(),
  preferences: jsonb()
});

const userSelectSchema = createSelectSchema(users, {
  name: (schema) => Type.String({ ...schema, maxLength: 20 }), // 扩展 schema
  bio: (schema) => Type.String({ ...schema, maxLength: 1000 }), // 在变为可为 null/可选之前扩展 schema
  preferences: Type.Object({ theme: Type.String() }) // 覆盖该字段,包括其可空性
});

const parsed: {
  id: number;
  name: string,
  bio: string | null;
  preferences: {
    theme: string;
  };
} = Value.Parse(userSelectSchema, ...);

工厂函数

对于更高级的用例,可以使用 createSchemaFactory 函数。

用例:使用扩展的 Typebox 实例

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createSchemaFactory } from 'drizzle-orm/typebox';
import { t } from 'elysia'; // 扩展的 Typebox 实例

const users = cockroachTable('users', {
  id: int4().primaryKey().generatedAlwaysAsIdentity(),
  name: text().notNull(),
  age: int4().notNull()
});

const { createInsertSchema } = createSchemaFactory({ typeboxInstance: t });

const userInsertSchema = createInsertSchema(users, {
  // 现在我们可以使用扩展后的实例
  name: (schema) => t.Number({ ...schema }, { error: '`name` 必须是字符串' })
});

数据类型参考

CockroachDB 数据类型映射遵循 CockroachDB 列构建器。请参阅 CockroachDB 数据类型 页面以获取完整的列参考。