Prisma
Store keys in PostgreSQL through Prisma
PrismaStore keeps keys in a model in your Prisma schema, through the Prisma Client your app already has. Use it with PostgreSQL: its owner and tag queries use Prisma’s PostgreSQL JSON filters (path: ['ownerId'], array_contains), and keypal tests it only on PostgreSQL. For MySQL, MariaDB or SQLite, use Drizzle or Kysely.
Install
npm install keypal @prisma/clientpnpm add keypal @prisma/clientyarn add keypal @prisma/clientbun add keypal @prisma/clientnub add keypal @prisma/clientaube add keypal @prisma/clientkeypal works with Prisma 6.
Model
Add a model with id, a unique keyHash, and a Json field for every other value:
model ApiKey {
id String @id
keyHash String @unique
metadata Json
@@map("api_keys")
}
keyHash must be @unique: keypal finds keys with findUnique on it. Then run prisma migrate dev (or prisma db push) and prisma generate.
Setup
import { PrismaClient } from '@prisma/client'
import { createKeys } from 'keypal'
import { PrismaStore } from 'keypal/prisma'
const prisma = new PrismaClient()
const keys = createKeys({
prefix: 'sk_',
storage: new PrismaStore({ prisma, model: 'apiKey' }),
})
model is the property on the client, which is the model name in camelCase: model ApiKey is prisma.apiKey, so pass 'apiKey'. keypal throws at startup if the client has no such model.
Options
| Option | Type | Description |
|---|---|---|
prisma |
PrismaClient |
Your Prisma Client |
model |
string | The client property for the API key model |
auditLogModel |
string | Enables audit log storage |
schema |
object | Field names; see below |
debugLogs |
boolean | Logs every storage call |
Field names
If your model names the fields differently, map them with schema.apiKeyColumns. The names are Prisma field names, not database column names:
const store = new PrismaStore({
prisma,
model: 'apiKey',
schema: { apiKeyColumns: { keyHash: 'hash', metadata: 'data' } },
})
schema.flattenMetadata (one field per value instead of the Json field) exists for Prisma too, but keypal doesn’t test it with Prisma. Prefer the Json field.
Audit logs
Add a log model, pass auditLogModel, and turn on auditLogs to store audit logs:
model ApiKeyLog {
id String @id
keyId String
ownerId String
action String
timestamp String
data Json?
@@index([ownerId])
@@map("api_key_logs")
}
const keys = createKeys({
prefix: 'sk_',
auditLogs: true,
storage: new PrismaStore({ prisma, model: 'apiKey', auditLogModel: 'apiKeyLog' }),
})
timestamp holds an ISO string, which sorts and compares correctly as text. Rename fields with schema.auditLogColumns.
Behavior
- Updates compare and set: keypal writes the new
metadataonly if the stored value hasn’t changed since it read it, and retries up to five times before throwing. A backgroundlastUsedAtwrite can’t undo a concurrent revoke. - If the database rejects JSON equality filters, keypal falls back to a plain read-then-write update for the rest of the process. Two updates at the same moment can then overwrite each other.
- Deleting a missing key is a no-op, not an error.
- Caching saves nothing here; leave
cacheoff. See Caching.