Api key plugin with Prisma and express server

Hi everyone, Has anyone gotten the api key plugin working with prisma in a separate express.js server? i've implemented the ApiKey schema like so:
model ApiKey {
id String @id @default(cuid())
name String?
start String?
prefix String?
key String
userId String @map("user_id")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
refillInterval Int? @map("refill_interval")
refillAmount Int? @map("refill_amount")
lastRefillAt DateTime? @map("last_refill_at")
enabled Boolean @default(true)
rateLimitEnabled Boolean @default(false) @map("rate_limit_enabled")
rateLimitTimeWindow Int? @map("rate_limit_time_window")
rateLimitMax Int? @map("rate_limit_max")
requestCount Int @default(0) @map("request_count")
remaining Int?
lastRequest DateTime? @map("last_request")
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
permissions String?
metadata Json?

@@index([userId])
@@map("api_key")
}
model ApiKey {
id String @id @default(cuid())
name String?
start String?
prefix String?
key String
userId String @map("user_id")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
refillInterval Int? @map("refill_interval")
refillAmount Int? @map("refill_amount")
lastRefillAt DateTime? @map("last_refill_at")
enabled Boolean @default(true)
rateLimitEnabled Boolean @default(false) @map("rate_limit_enabled")
rateLimitTimeWindow Int? @map("rate_limit_time_window")
rateLimitMax Int? @map("rate_limit_max")
requestCount Int @default(0) @map("request_count")
remaining Int?
lastRequest DateTime? @map("last_request")
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
permissions String?
metadata Json?

@@index([userId])
@@map("api_key")
}
But i keep getting this error when i try to create new api key from my client: ```` file:///Users/user/dev/project/repo_name/backend/node_modules/.pnpm/[email protected]/node_modules/better-auth/dist/shared/better-auth.rSYJCd3o.mjs:96 return schema[model].modelName !== model ? schema[model].modelName : config.usePlural ? ${model}s` : model; ^ TypeError: Cannot read properties of undefined (reading 'modelName') ````
4 Replies
discquist
discquistOP4w ago
And for reference here is how i configured my plugin server-side:
plugins: [
apiKey({
schema: {
apikey: {
modelName: "apiKey",
fields: {
userId: "user_id",
refillInterval: "refill_interval",
refillAmount: "refill_amount",
lastRefillAt: "last_refill_at",
rateLimitEnabled: "rate_limit_enabled",
rateLimitTimeWindow: "rate_limit_time_window",
rateLimitMax: "rate_limit_max",
requestCount: "request_count",
lastRequest: "last_request",
expiresAt: "expires_at",
createdAt: "created_at",
updatedAt: "updated_at",
},
},
},
}),
],
plugins: [
apiKey({
schema: {
apikey: {
modelName: "apiKey",
fields: {
userId: "user_id",
refillInterval: "refill_interval",
refillAmount: "refill_amount",
lastRefillAt: "last_refill_at",
rateLimitEnabled: "rate_limit_enabled",
rateLimitTimeWindow: "rate_limit_time_window",
rateLimitMax: "rate_limit_max",
requestCount: "request_count",
lastRequest: "last_request",
expiresAt: "expires_at",
createdAt: "created_at",
updatedAt: "updated_at",
},
},
},
}),
],
Ping
Ping4w ago
Just a side note, it's aside to your issue, but metadata should be a string, Better Auth doesn't have built-in functionality to store JSON fields yet
Ping
Ping4w ago
discquist
discquistOP4w ago
Hi! Thanks for the quick investigation, was pulling my hair a little bit. Rolled back to rolling my own api keys for now but will give this another try as soon as the PR is merged.

Did you find this page helpful?