Prisma Adapter
The official database adapter using Prisma ORM.
Install
bash
# Prisma 5
pnpm add @authcore/prisma-adapter @prisma/client
pnpm add -D prisma
# Prisma 6 (also requires a driver adapter for your database)
pnpm add @authcore/prisma-adapter @prisma/client @prisma/adapter-pg pg
pnpm add -D prisma @types/pgSetup
ts
import { PrismaClient } from '@prisma/client'
import { prismaAdapter } from '@authcore/prisma-adapter'
// Prisma 5 (default prisma-client-js generator):
const prisma = new PrismaClient()
// Prisma 6 (new prisma-client generator — requires an explicit driver adapter):
import { PrismaPg } from '@prisma/adapter-pg'
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! })
const prisma = new PrismaClient({ adapter })
const config = {
db: prismaAdapter(prisma),
// ...
}Custom generator output: If your
schema.prismauses a customoutputpath (common in Prisma 6 monorepo setups), the import path changes — e.g.from '../generated/prisma/client'instead offrom '@prisma/client'. The adapter itself is not affected.
Schema
Add the AuthCore models to your Prisma schema:
prisma
model User {
id String @id @default(uuid())
email String @unique
passwordHash String
emailVerified Boolean @default(false)
role String @default("user")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
tokens Token[]
}
model Token {
id String @id @default(uuid())
userId String
type String
token String @unique
expiresAt DateTime
createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([type, expiresAt])
}Note:
typeis stored as a plainStringrather than a Prisma enum. The adapter's internaltoCoreTokenTypevalidator enforces that only valid values (EMAIL_VERIFICATION,PASSWORD_RESET,SESSION,INVITATION) are accepted at runtime.
Then sync with your database:
bash
npx prisma db push
# or for production:
npx prisma migrate dev --name initCustom Adapter
To use a different database, implement the DatabaseAdapter interface from @authcore/core:
ts
import type { DatabaseAdapter } from '@authcore/core'
export function myAdapter(client: MyClient): DatabaseAdapter {
return {
findUserByEmail: async (email) => { /* ... */ },
findUserById: async (id) => { /* ... */ },
createUser: async (data) => { /* ... */ },
updateUser: async (id, data) => { /* ... */ },
createToken: async (data) => { /* ... */ },
findToken: async (token, type) => { /* ... */ },
deleteToken: async (id) => { /* ... */ },
deleteExpiredTokens: async () => { /* ... */ },
}
}