Skip to content

Drizzle

gau has a Drizzle ORM adapter for SQLite and PostgreSQL. The SQLite examples use Turso as the database provider.


  1. Terminal window
    npm i drizzle-orm @libsql/client
    npm i -D drizzle-kit
  2. This is the complete schema for gau. Depending on your OAuth provider you may be able to omit some columns.

    schema.ts
    import { integer, primaryKey, sqliteTable, text } from 'drizzle-orm/sqlite-core'
    export const Users = sqliteTable('users', {
    id: text().primaryKey().$defaultFn(() => crypto.randomUUID()),
    name: text(),
    email: text().unique(),
    emailVerified: integer({ mode: 'boolean' }),
    image: text(),
    createdAt: integer({ mode: 'timestamp' }).$defaultFn(() => new Date()),
    updatedAt: integer({ mode: 'timestamp' }).$defaultFn(() => new Date()),
    })
    export type SelectUser = typeof Users.$inferSelect
    export type InsertUser = typeof Users.$inferInsert
    export const Accounts = sqliteTable(
    'accounts',
    {
    userId: text()
    .notNull()
    .references(() => Users.id, { onDelete: 'cascade' }),
    type: text().notNull(),
    provider: text().notNull(),
    providerAccountId: text().notNull(),
    refreshToken: text(),
    accessToken: text(),
    expiresAt: integer(),
    tokenType: text(),
    scope: text(),
    idToken: text(),
    sessionState: text('session_state'),
    createdAt: integer('created_at', { mode: 'timestamp' }).$defaultFn(() => new Date()),
    },
    account => [
    primaryKey({
    columns: [account.provider, account.providerAccountId],
    }),
    ],
    )
    export type SelectAccount = typeof Accounts.$inferSelect
    export type InsertAccount = typeof Accounts.$inferInsert
  3. Configure the schema path and database credentials for migrations.

    drizzle.config.ts
    import { defineConfig } from 'drizzle-kit'
    export default defineConfig({
    schema: './schema.ts',
    dialect: 'turso',
    dbCredentials: {
    authToken: process.env.TURSO_AUTH_TOKEN!,
    url: process.env.TURSO_DB_URL!,
    },
    })

    Now you can run npx drizzle-kit push to apply your schema to the database.

  4. Initialize the Drizzle client with your database connection.

    db.ts
    import { createClient } from '@libsql/client'
    import { drizzle } from 'drizzle-orm/libsql'
    import * as schema from './schema'
    const client = createClient({
    url: process.env.TURSO_DB_URL!,
    authToken: process.env.TURSO_AUTH_TOKEN!,
    })
    export const db = drizzle(client, { schema, casing: 'snake_case' })
  5. Finally, pass the Drizzle client and schema tables to the DrizzleAdapter in your createAuth configuration.

    auth.ts
    import { DrizzleAdapter } from '@rttnd/gau/adapters/drizzle'
    import { createAuth } from '@rttnd/gau/core'
    import { GitHub } from '@rttnd/gau/oauth'
    import { db } from './db'
    import { Accounts, Users } from './schema'
    export const auth = createAuth({
    adapter: DrizzleAdapter(db, Users, Accounts),
    providers: [
    GitHub({
    clientId: process.env.GITHUB_CLIENT_ID!,
    clientSecret: process.env.GITHUB_CLIENT_SECRET!,
    }),
    ],
    jwt: {
    secret: process.env.AUTH_SECRET!,
    },
    })
    export type Auth = typeof auth