Drizzle | Gel 인증 확장
This guide assumes familiarity with:

단계 1 - Gel 인증 스키마 정의

dbschema/default.esdl 파일에 auth 확장이 포함된 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에서 사용되는 설정 파일로, 데이터베이스 연결, 마이그레이션 폴더, 스키마 파일에 대한 모든 정보를 포함합니다.

프로젝트 루트에 drizzle.config.ts 파일을 생성하고 다음 내용을 추가합니다:

drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  dialect: 'gel',
  // drizzle-kit에서 auth 스키마 활성화
  schemaFilter: ['ext::auth', 'public']
});

단계 4 - Gel 타입을 Drizzle 스키마로 풀

데이터베이스 스키마를 풀합니다:

npm
yarn
pnpm
bun
npx drizzle-kit pull

다음은 생성된 schema.ts 파일의 예시입니다:

IMPORTANT

ext::auth에서 Identity 테이블뿐만 아니라 더 많은 테이블을 가져옵니다. Drizzle은 사용할 수 있는 모든 auth 테이블을 풀합니다. 아래 예시는 그 중 하나만 보여줍니다.

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 테이블을 사용할 수 있습니다!