您将从 ext::auth
中获得不仅仅是 Identity
表。Drizzle 将拉取所有您可以使用的 auth
表。下面的示例只展示了其中一个。
Drizzle | Gel 认证扩展
本指南假定您熟悉
- 使用 Gel 开始
- 使用 drizzle-kit pull
步骤 1 - 定义 Gel 认证模式
在 dbschema/default.esdl
文件中添加一个带有认证扩展的 Gel 模式
using extension auth;
module default {
global current_user := (
assert_single((
select User { id, username, email }
filter .identity = global ext::auth::ClientTokenIdentity
))
);
type User {
required identity: ext::auth::Identity;
required username: str;
required email: str;
}
}
步骤 2 - 将 Gel 模式推送到数据库
生成 Gel 迁移文件
gel migration create
将 Gel 迁移应用到数据库
gel migration apply
步骤 3 - 设置 Drizzle 配置文件
Drizzle config - 由 Drizzle Kit 使用的配置文件,包含有关你的数据库连接、迁移文件夹和 schema 文件的所有信息。
在项目的根目录中创建 drizzle.config.ts
文件并添加以下内容
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
dialect: 'gel',
// Enable auth schema for drizzle-kit
schemaFilter: ['ext::auth', 'public']
});
步骤 4 - 将 Gel 类型拉取到 Drizzle 模式
拉取您的数据库模式
npm
yarn
pnpm
bun
npx drizzle-kit pull
以下是生成的 schema.ts 文件示例
重要提示
import { gelTable, uniqueIndex, uuid, text, gelSchema, timestamptz, foreignKey } from "drizzle-orm/gel-core"
import { sql } from "drizzle-orm"
export const extauth = gelSchema('ext::auth');
export const identityInExtauth = extauth.table('Identity', {
id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
createdAt: timestamptz('created_at').default(sql`(clock_timestamp())`).notNull(),
issuer: text().notNull(),
modifiedAt: timestamptz('modified_at').notNull(),
subject: text().notNull(),
}, (table) => [
uniqueIndex('6bc2dd19-bce4-5810-bb1b-7007afe97a11;schemaconstr').using(
'btree',
table.id.asc().nullsLast().op('uuid_ops'),
),
]);
export const user = gelTable('User', {
id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),
email: text().notNull(),
identityId: uuid('identity_id').notNull(),
username: text().notNull(),
}, (table) => [
uniqueIndex('d504514c-26a7-11f0-b836-81aa188c0abe;schemaconstr').using(
'btree',
table.id.asc().nullsLast().op('uuid_ops'),
),
foreignKey({
columns: [table.identityId],
foreignColumns: [identityInExtauth.id],
name: 'User_fk_identity',
}),
]);
🎉 现在您可以在查询中使用 auth
表了!