Skip to content

Commit

Permalink
Simplify Prisma adapter (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Jul 22, 2023
1 parent 805b22e commit 5cfe534
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 65 deletions.
6 changes: 6 additions & 0 deletions .auri/$8h9k9jzr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/adapter-prisma" # package name
type: "major" # "major", "minor", "patch"
---

Update `prisma()` params
47 changes: 16 additions & 31 deletions documentation-v2/content/main/2.database-adapters/prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,25 @@ import { prisma } from "@lucia-auth/adapter-prisma";
```ts
const prisma: (
client: PrismaClient,
options?: {
modelNames: {
user: string;
key: string;
session: string | null;
};
userRelationKey: string;
modelNames?: {
user: string;
key: string;
session: string | null;
}
) => InitializeAdapter<Adapter>;
```

##### Parameters

| name | type | description | optional |
| ---------------------------- | ---------------- | ---------------------------------------------------- | :------: |
| `client` | `PrismaClient` | The Prisma client | |
| `options` | | ||
| `options.modelNames.user` | `string` | | |
| `options.modelNames.key` | `string` | | |
| `options.modelNames.session` | `string \| null` | Can be `null` when using alongside a session adapter | |
| `options.userRelationKey` | `string` | | |
| name | type | description | optional |
| -------------------- | ---------------- | ---------------------------------------------------- | :------: |
| `client` | `PrismaClient` | The Prisma client | |
| `modelNames` | | ||
| `modelNames.user` | `string` | | |
| `modelNames.key` | `string` | | |
| `modelNames.session` | `string \| null` | Can be `null` when using alongside a session adapter | |

When `options` is undefined, the adapter uses predefined adapter configs, and as such, your Prisma schema must match exactly the one listed below this page. You can still add columns to the user and session table.

The values for the `modelNames` params of the adapter config is the `camelCase` version of your `PascalCase` model names defined in your schema (sounds confusing but the TS auto-complete should help you). The `userRelationKey` is key that represents foreign key relations (`user_relation_key` in the example):

```prisma
user_relation_key User @relation(references: [id], fields: [user_id], onDelete: Cascade)
```
The values for the `modelNames` params is the `camelCase` version of your `PascalCase` model names defined in your schema (sounds confusing but the TS auto-complete should help you). When it's undefined, the adapter uses predefined model names (see below).

## Installation

Expand All @@ -68,20 +58,17 @@ const auth = lucia({
// default values
const auth = lucia({
adapter: prisma(client, {
modelNames: {
user: "user",
key: "key",
session: "session"
},
userRelationKey: "user"
user: "user", // model User {}
key: "key", // model Key {}
session: "session" // model Session {}
})
// ...
});
```

## Prisma schema

You can add additional columns to the user model to store user attributes, and to the session model to store session attributes. **Your schema must exactly match this if you're `options` params is undefined** (you can still add columns for attributes).
You can add additional columns to the user model to store user attributes, and to the session model to store session attributes. If you change the model names, pass the new names to the adapter config.

```prisma
model User {
Expand All @@ -96,7 +83,6 @@ model Session {
user_id String
active_expires BigInt
idle_expires BigInt
// pass this key to `userRelationKey`
user User @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@index([user_id])
Expand All @@ -106,7 +92,6 @@ model Key {
id String @id @unique
hashed_password String?
user_id String
// pass this key to `userRelationKey`
user User @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@index([user_id])
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"devDependencies": {
"lucia": "latest",
"@lucia-auth/adapter-test": "latest",
"@prisma/client": "^4.9.0",
"@prisma/client": "^5.0.0",
"prisma": "^4.9.0",
"tsx": "^3.12.6"
}
Expand Down
42 changes: 9 additions & 33 deletions packages/adapter-prisma/src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ type ExtractModelNames<_PrismaClient extends PrismaClient> = Exclude<

export const prismaAdapter = <_PrismaClient extends PrismaClient>(
client: _PrismaClient,
options?: {
modelNames: {
user: ExtractModelNames<_PrismaClient>;
session: ExtractModelNames<_PrismaClient> | null;
key: ExtractModelNames<_PrismaClient>;
};
userRelationKey: string;
modelNames?: {
user: ExtractModelNames<_PrismaClient>;
session: ExtractModelNames<_PrismaClient> | null;
key: ExtractModelNames<_PrismaClient>;
}
): InitializeAdapter<Adapter> => {
const getModels = () => {
if (!options) {
if (!modelNames) {
return {
User: client["user"] as SmartPrismaModel<UserSchema>,
Session: (client["session"] as SmartPrismaModel<SessionSchema>) ?? null,
Key: client["key"] as SmartPrismaModel<KeySchema>
};
}
return {
User: client[options.modelNames.user] as SmartPrismaModel<UserSchema>,
Session: options.modelNames.session
User: client[modelNames.user] as SmartPrismaModel<UserSchema>,
Session: modelNames.session
? (client[
options.modelNames.session
modelNames.session
] as SmartPrismaModel<SessionSchema>)
: null,
Key: client[options.modelNames.key] as SmartPrismaModel<KeySchema>
Key: client[modelNames.key] as SmartPrismaModel<KeySchema>
};
};
const { User, Session, Key } = getModels();
const userRelationKey = options?.userRelationKey ?? "user";

return (LuciaError) => {
return {
Expand Down Expand Up @@ -219,26 +215,6 @@ export const prismaAdapter = <_PrismaClient extends PrismaClient>(
id: userId
}
});
},

getSessionAndUser: async (sessionId) => {
if (!Session) {
throw new Error("Session table not defined");
}
const result = await Session.findUnique({
where: {
id: sessionId
},
include: {
[userRelationKey]: true
}
});
if (!result) return [null, null];
const { [userRelationKey]: userResult, ...sessionResult } = result;
return [
transformPrismaSession(sessionResult as PrismaSession),
userResult as UserSchema
];
}
};
};
Expand Down
Binary file modified packages/adapter-sqlite/test/main.db
Binary file not shown.

0 comments on commit 5cfe534

Please sign in to comment.