|
| 1 | +--- |
| 2 | +title: Gel auth extension |
| 3 | +slug: gel-ext-auth |
| 4 | +--- |
| 5 | +import Prerequisites from "@mdx/Prerequisites.astro"; |
| 6 | +import Callout from "@mdx/Callout.astro"; |
| 7 | +import Npx from "@mdx/Npx.astro"; |
| 8 | + |
| 9 | +<Prerequisites> |
| 10 | +- Get started with [Gel](/docs/get-started-gel) |
| 11 | +- Using [drizzle-kit pull](/docs/drizzle-kit-pull) |
| 12 | +</Prerequisites> |
| 13 | + |
| 14 | +#### Step 1 - Define Gel auth schema |
| 15 | + |
| 16 | +In `dbschema/default.esdl` file add a Gel schema with an auth extension |
| 17 | + |
| 18 | +```esdl |
| 19 | +using extension auth; |
| 20 | +
|
| 21 | +module default { |
| 22 | + global current_user := ( |
| 23 | + assert_single(( |
| 24 | + select User { id, username, email } |
| 25 | + filter .identity = global ext::auth::ClientTokenIdentity |
| 26 | + )) |
| 27 | + ); |
| 28 | +
|
| 29 | + type User { |
| 30 | + required identity: ext::auth::Identity; |
| 31 | + required username: str; |
| 32 | + required email: str; |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +#### Step 2 - Push Gel schema to the database |
| 38 | + |
| 39 | +Generate Gel migration file: |
| 40 | +```bash |
| 41 | +gel migration create |
| 42 | +``` |
| 43 | + |
| 44 | +Apply Gel migrations to the database |
| 45 | +```bash |
| 46 | +gel migration apply |
| 47 | +``` |
| 48 | + |
| 49 | +#### Step 3 - Setup Drizzle config file |
| 50 | + |
| 51 | +**Drizzle config** - a configuration file that is used by [Drizzle Kit](/docs/kit-overview) and contains all the information about your database connection, migration folder and schema files. |
| 52 | + |
| 53 | +Create a `drizzle.config.ts` file in the root of your project and add the following content: |
| 54 | + |
| 55 | +```typescript copy filename="drizzle.config.ts" |
| 56 | +import { defineConfig } from 'drizzle-kit'; |
| 57 | + |
| 58 | +export default defineConfig({ |
| 59 | + dialect: 'gel', |
| 60 | + // Enable auth schema for drizzle-kit |
| 61 | + schemaFilter: ['ext::auth', 'public'] |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +#### Step 4 - Pull Gel types to Drizzle schema |
| 66 | + |
| 67 | +Pull your database schema: |
| 68 | +<Npx> |
| 69 | +drizzle-kit pull |
| 70 | +</Npx> |
| 71 | + |
| 72 | +Here is an example of the generated schema.ts file: |
| 73 | + |
| 74 | +<Callout type="warning"> |
| 75 | +You'll get more than just the `Identity` table from `ext::auth`. Drizzle will pull in all the |
| 76 | +`auth` tables you can use. The example below showcases just one of them. |
| 77 | +</Callout> |
| 78 | + |
| 79 | +```ts |
| 80 | +import { gelTable, uniqueIndex, uuid, text, gelSchema, timestamptz, foreignKey } from "drizzle-orm/gel-core" |
| 81 | +import { sql } from "drizzle-orm" |
| 82 | + |
| 83 | +export const extauth = gelSchema('ext::auth'); |
| 84 | + |
| 85 | +export const identityInExtauth = extauth.table('Identity', { |
| 86 | + id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(), |
| 87 | + createdAt: timestamptz('created_at').default(sql`(clock_timestamp())`).notNull(), |
| 88 | + issuer: text().notNull(), |
| 89 | + modifiedAt: timestamptz('modified_at').notNull(), |
| 90 | + subject: text().notNull(), |
| 91 | +}, (table) => [ |
| 92 | + uniqueIndex('6bc2dd19-bce4-5810-bb1b-7007afe97a11;schemaconstr').using( |
| 93 | + 'btree', |
| 94 | + table.id.asc().nullsLast().op('uuid_ops'), |
| 95 | + ), |
| 96 | +]); |
| 97 | + |
| 98 | +export const user = gelTable('User', { |
| 99 | + id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(), |
| 100 | + email: text().notNull(), |
| 101 | + identityId: uuid('identity_id').notNull(), |
| 102 | + username: text().notNull(), |
| 103 | +}, (table) => [ |
| 104 | + uniqueIndex('d504514c-26a7-11f0-b836-81aa188c0abe;schemaconstr').using( |
| 105 | + 'btree', |
| 106 | + table.id.asc().nullsLast().op('uuid_ops'), |
| 107 | + ), |
| 108 | + foreignKey({ |
| 109 | + columns: [table.identityId], |
| 110 | + foreignColumns: [identityInExtauth.id], |
| 111 | + name: 'User_fk_identity', |
| 112 | + }), |
| 113 | +]); |
| 114 | +``` |
| 115 | + |
| 116 | +🎉 Now you can use the `auth` tables in your queries! |
0 commit comments