Drizzle
gau
has a Drizzle ORM adapter for SQLite and PostgreSQL. The SQLite 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-kitTerminal window npm i drizzle-orm pgnpm i -D drizzle-kitTerminal window pnpm add drizzle-orm pgpnpm add -D drizzle-kitTerminal window yarn add drizzle-orm pgyarn add -D drizzle-kitTerminal window bun add drizzle-orm pgbun add -D drizzle-kitTerminal window ni drizzle-orm pgni -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 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.$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.$inferInsertschema.ts import { boolean, integer, pgTable, primaryKey, text, timestamp } from 'drizzle-orm/pg-core'export const Users = pgTable('users', {id: text().primaryKey(),name: text(),email: text().unique(),emailVerified: boolean('email_verified'),image: text(),createdAt: timestamp('created_at').defaultNow(),updatedAt: timestamp('updated_at').defaultNow(),})export type SelectUser = typeof Users.$inferSelectexport type InsertUser = typeof Users.$inferInsertexport const Accounts = pgTable('accounts',{userId: text().notNull().references(() => Users.id, { onDelete: 'cascade' }),type: text().notNull(),provider: text().notNull(),providerAccountId: text().notNull(),refreshToken: text(),accessToken: text(),expiresAt: integer('expires_at'),tokenType: text(),scope: text(),idToken: text(),sessionState: text('session_state'),createdAt: timestamp('created_at').defaultNow(),},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: './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: './schema.ts',dialect: 'postgresql',dbCredentials: {url: process.env.DATABASE_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.
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' })db.ts import { drizzle } from 'drizzle-orm/node-postgres'import { Pool } from 'pg'import * as schema from './schema'const pool = new Pool({connectionString: process.env.DATABASE_URL!,})export const db = drizzle(pool, { schema }) -
Integrate with
Section titled “Integrate with createAuth”createAuth
Finally, pass the Drizzle client and schema tables to the
DrizzleAdapter
in yourcreateAuth
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