Installation
Setup
1. Define Database Schema
You can either use the pre-built schema from the adapter or define your own schema:Important Schema Requirements:
- Use
texttype for IDs with$defaultFnfor auto-generation emailandphonemust be nullable and unique- Foreign keys should use
onDelete: "cascade"for automatic cleanup - The
verification_typeenum uses underscores instead of hyphens - Add indexes on frequently queried fields (
userId,token)
2. Create Drizzle Config
Create adrizzle.config.ts file in your project root:
drizzle.config.ts
3. Generate and Run Migrations
Generate migration files and apply them to your database:4. Create Database Connection
Set up your Drizzle database connection:lib/db/index.ts
5. Configure Arraf Auth
Use the Drizzle adapter in your Arraf Auth configuration:lib/auth.ts
Usage with Next.js
API Reference
drizzleAdapter
Creates a database adapter for Drizzle ORM.An instance of the Drizzle database client configured with postgres-js.
A database adapter that implements all required methods for Arraf Auth.
Exported Schema Types
The adapter exports TypeScript types for all table records:Schema Field Mapping
The adapter handles automatic conversion between Drizzle’s enum format and Arraf Auth’s type format:| Arraf Auth Type | Drizzle Enum |
|---|---|
phone-otp | phone_otp |
email-otp | email_otp |
email-verification | email_verification |
password-reset | password_reset |
phone-change | phone_change |
The adapter automatically converts between hyphenated and underscored formats using internal helper functions.
Advanced Usage
Custom ID Generation
The default schema uses a simple ID generation function. For production, consider using a more robust solution:Connection Pooling
For better performance in production, configure connection pooling:Using with Neon or Supabase
Querying User Data
You can use Drizzle’s query API alongside the adapter:Migration from Prisma
If you’re migrating from Prisma to Drizzle:-
Generate Drizzle schema from your Prisma database:
- Compare the generated schema with the required Arraf Auth schema
- Update your auth configuration to use the Drizzle adapter
Troubleshooting
Error: relation does not exist
Make sure you’ve run the migrations:Error: enum type does not exist
Ensure theverification_type enum is created in your database. Run:
Type errors with database client
Make sure you’re using the correct Drizzle database type:Next Steps
- Learn about the DatabaseAdapter interface
- Explore session management
- Set up OAuth providers