diff --git a/.auri/$8h9k9jzr.md b/.auri/$8h9k9jzr.md new file mode 100644 index 000000000..17f228427 --- /dev/null +++ b/.auri/$8h9k9jzr.md @@ -0,0 +1,6 @@ +--- +package: "@lucia-auth/adapter-prisma" # package name +type: "major" # "major", "minor", "patch" +--- + +Update `prisma()` params \ No newline at end of file diff --git a/documentation-v2/content/main/2.database-adapters/prisma.md b/documentation-v2/content/main/2.database-adapters/prisma.md index f9921867b..3ceaceb3c 100644 --- a/documentation-v2/content/main/2.database-adapters/prisma.md +++ b/documentation-v2/content/main/2.database-adapters/prisma.md @@ -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; ``` ##### 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 @@ -68,12 +58,9 @@ 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 {} }) // ... }); @@ -81,7 +68,7 @@ const auth = lucia({ ## 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 { @@ -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]) @@ -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]) diff --git a/packages/adapter-prisma/package.json b/packages/adapter-prisma/package.json index e5c53e7f6..bd10dcb25 100644 --- a/packages/adapter-prisma/package.json +++ b/packages/adapter-prisma/package.json @@ -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" } diff --git a/packages/adapter-prisma/src/prisma.ts b/packages/adapter-prisma/src/prisma.ts index 7c073b864..a9729cc6b 100644 --- a/packages/adapter-prisma/src/prisma.ts +++ b/packages/adapter-prisma/src/prisma.ts @@ -18,17 +18,14 @@ 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 => { const getModels = () => { - if (!options) { + if (!modelNames) { return { User: client["user"] as SmartPrismaModel, Session: (client["session"] as SmartPrismaModel) ?? null, @@ -36,17 +33,16 @@ export const prismaAdapter = <_PrismaClient extends PrismaClient>( }; } return { - User: client[options.modelNames.user] as SmartPrismaModel, - Session: options.modelNames.session + User: client[modelNames.user] as SmartPrismaModel, + Session: modelNames.session ? (client[ - options.modelNames.session + modelNames.session ] as SmartPrismaModel) : null, - Key: client[options.modelNames.key] as SmartPrismaModel + Key: client[modelNames.key] as SmartPrismaModel }; }; const { User, Session, Key } = getModels(); - const userRelationKey = options?.userRelationKey ?? "user"; return (LuciaError) => { return { @@ -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 - ]; } }; }; diff --git a/packages/adapter-sqlite/test/main.db b/packages/adapter-sqlite/test/main.db index 1fe474821..6220e4342 100644 Binary files a/packages/adapter-sqlite/test/main.db and b/packages/adapter-sqlite/test/main.db differ