Skip to content

Commit

Permalink
Update Prisma docs on relation names (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Feb 16, 2024
1 parent 80340b6 commit 8a5d745
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/pages/database/prisma.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ npm install @lucia-auth/adapter-prisma

## Schema

While Lucia does not enforce model names, the relation name (`user`) in the session model must be the camel-case version of the user model name. For example, if the user model was named `AuthUser`, the relation must be named `Session.authUser`.

```prisma
model User {
id String @id
2 changes: 1 addition & 1 deletion packages/adapter-prisma/package.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
"scripts": {
"build": "shx rm -rf ./dist/* && tsc",
"test": "tsx tests/prisma.ts",
"test-setup": "prisma db push",
"test-setup": "prisma db push && prisma generate",
"auri.build": "pnpm build"
},
"keywords": [
9 changes: 4 additions & 5 deletions packages/adapter-prisma/src/index.ts
Original file line number Diff line number Diff line change
@@ -39,10 +39,7 @@ export class PrismaAdapter<_PrismaClient extends PrismaClient> implements Adapte
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const userModelKey = this.userModel.name[0].toLowerCase() + this.userModel.name.slice(1);
const result = await this.sessionModel.findUnique<{
// this is a lie to make TS shut up
user: UserSchema;
}>({
const result = await this.sessionModel.findUnique({
where: {
id: sessionId
},
@@ -51,7 +48,9 @@ export class PrismaAdapter<_PrismaClient extends PrismaClient> implements Adapte
}
});
if (!result) return [null, null];
const userResult: UserSchema = result[userModelKey as "user"];
const userResult: UserSchema = result[
userModelKey as keyof typeof result
] as any as UserSchema;
delete result[userModelKey as keyof typeof result];
return [transformIntoDatabaseSession(result), transformIntoDatabaseUser(userResult)];
}
8 changes: 8 additions & 0 deletions packages/adapter-prisma/tests/prisma.ts
Original file line number Diff line number Diff line change
@@ -20,3 +20,11 @@ await client.session.deleteMany();
await client.user.deleteMany();

process.exit(0);

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}

0 comments on commit 8a5d745

Please sign in to comment.