Drizzle
gau
currently only supports the Drizzle ORM adapter and SQLite. The examples use Turso as the database provider.
-
Install Dependencies
Section titled “Install Dependencies”Terminal window npm i drizzle-orm @libsql/clientnpm i -D drizzle-kitTerminal window pnpm add drizzle-orm @libsql/clientpnpm add -D drizzle-kitTerminal window yarn add drizzle-orm @libsql/clientyarn add -D drizzle-kitTerminal window bun add drizzle-orm @libsql/clientbun add -D drizzle-kitTerminal window ni drizzle-orm @libsql/clientni -D drizzle-kit -
Define the Database Schema
Section titled “Define the Database Schema”This is the complete schema for
gau
, depending on your OAuth provider you may be able to omit some of the columns, each provider lists its required columns.src/lib/server/db/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.$inferSelectexport type InsertUser = typeof Users.$inferInsertexport 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.$inferSelectexport type InsertAccount = typeof Accounts.$inferInsertsrc/db/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.$inferSelectexport type InsertUser = typeof Users.$inferInsertexport 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(),createdAt: integer({ mode: 'timestamp' }).$defaultFn(() => new Date()),}, account => [primaryKey({columns: [account.provider, account.providerAccountId],}),])export type SelectAccount = typeof Accounts.$inferSelectexport type InsertAccount = typeof Accounts.$inferInsert -
Configure DrizzleKit
Section titled “Configure DrizzleKit”Configure the schema path and database credentials for migrations.
drizzle.config.ts import { defineConfig } from 'drizzle-kit'export default defineConfig({schema: './src/lib/server/db/schema.ts',dialect: 'turso',dbCredentials: {authToken: process.env.TURSO_AUTH_TOKEN!,url: process.env.TURSO_DB_URL!,},})drizzle.config.ts import { defineConfig } from 'drizzle-kit'export default defineConfig({schema: './src/db/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. -
Set up the Database Client
Section titled “Set up the Database Client”Initialize the Drizzle client with your database connection.
src/lib/server/db/index.ts import { env } from '$env/dynamic/private'import { createClient } from '@libsql/client'import { drizzle } from 'drizzle-orm/libsql'import * as schema from './schema'const client = createClient({url: env.TURSO_DB_URL!,authToken: env.TURSO_AUTH_TOKEN!,})export const db = drizzle(client, { schema, casing: 'snake_case' })src/db/index.ts import process from 'node:process'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' }) -
Integrate with
Section titled “Integrate with createAuth”createAuth
Finally, pass the Drizzle client and schema tables to the
DrizzleAdapter
in yourcreateAuth
configuration.src/lib/server/auth.ts import { env } from '$env/dynamic/private'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 './db/schema'export const auth = createAuth({adapter: DrizzleAdapter(db, Users, Accounts),providers: [GitHub({clientId: env.GITHUB_CLIENT_ID!,clientSecret: env.GITHUB_CLIENT_SECRET!,}),],jwt: {secret: env.AUTH_SECRET,},})export type Auth = typeof authsrc/server/auth.ts import process from 'node:process'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 './db/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