Skip to content

Commit

Permalink
feat(adapters): support Prisma's strictUndefinedChecks (nextauthjs#…
Browse files Browse the repository at this point in the history
…11952)

* Fix nextauthjs#11944 - Removed explicit undefined values before passing to Prisma

* Pull out stripUndefined to module scope

* chore: upgrade dependencies

* feat(adapters): support Prisma's `strictUndefinedChecks`

* downgrade accelerate

https://www.answeroverflow.com/m/1290391123633770589

* syntax

* syntax

---------

Co-authored-by: Peter Goldstein <peter.goldstein@hearst.com>
  • Loading branch information
balazsorban44 and petergoldstein authored Oct 2, 2024
1 parent e2f3d51 commit 56a9564
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 124 deletions.
8 changes: 4 additions & 4 deletions packages/adapter-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"@prisma/client": ">=2.26.0 || >=3 || >=4 || >=5"
},
"devDependencies": {
"@prisma/client": "^5.9.1",
"@prisma/extension-accelerate": "^0.6.3",
"mongodb": "^4.17.0",
"prisma": "^5.9.1"
"@prisma/client": "^5.20.0",
"@prisma/extension-accelerate": "1.1.0",
"mongodb": "^6.9.0",
"prisma": "^5.20.0"
}
}
1 change: 1 addition & 0 deletions packages/adapter-prisma/prisma/custom.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ datasource db {

generator client {
provider = "prisma-client-js"
previewFeatures = ["strictUndefinedChecks"]
}

model User {
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-prisma/prisma/mongodb.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ datasource db {

generator client {
provider = "prisma-client-js"
previewFeatures = ["strictUndefinedChecks"]
}

model Account {
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-prisma/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ datasource db {

generator client {
provider = "prisma-client-js"
previewFeatures = ["strictUndefinedChecks"]
}

model User {
Expand Down
34 changes: 22 additions & 12 deletions packages/adapter-prisma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export function PrismaAdapter(
const p = prisma as PrismaClient
return {
// We need to let Prisma generate the ID because our default UUID is incompatible with MongoDB
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createUser: ({ id, ...data }) => {
return p.user.create({ data })
},
createUser: ({ id, ...data }) => p.user.create(stripUndefined(data)),
getUser: (id) => p.user.findUnique({ where: { id } }),
getUserByEmail: (email) => p.user.findUnique({ where: { email } }),
async getUserByAccount(provider_providerAccountId) {
Expand All @@ -43,7 +40,10 @@ export function PrismaAdapter(
return (account?.user as AdapterUser) ?? null
},
updateUser: ({ id, ...data }) =>
p.user.update({ where: { id }, data }) as Promise<AdapterUser>,
p.user.update({
where: { id },
...stripUndefined(data),
}) as Promise<AdapterUser>,
deleteUser: (id) =>
p.user.delete({ where: { id } }) as Promise<AdapterUser>,
linkAccount: (data) =>
Expand All @@ -61,13 +61,18 @@ export function PrismaAdapter(
const { user, ...session } = userAndSession
return { user, session } as { user: AdapterUser; session: AdapterSession }
},
createSession: (data) => p.session.create({ data }),
createSession: (data) => p.session.create(stripUndefined(data)),
updateSession: (data) =>
p.session.update({ where: { sessionToken: data.sessionToken }, data }),
p.session.update({
where: { sessionToken: data.sessionToken },
...stripUndefined(data),
}),
deleteSession: (sessionToken) =>
p.session.delete({ where: { sessionToken } }),
async createVerificationToken(data) {
const verificationToken = await p.verificationToken.create({ data })
const verificationToken = await p.verificationToken.create(
stripUndefined(data)
)
// @ts-expect-errors // MongoDB needs an ID, but we don't
if (verificationToken.id) delete verificationToken.id
return verificationToken
Expand All @@ -93,10 +98,8 @@ export function PrismaAdapter(
where: { providerAccountId, provider },
}) as Promise<AdapterAccount | null>
},
async createAuthenticator(authenticator) {
return p.authenticator.create({
data: authenticator,
})
async createAuthenticator(data) {
return p.authenticator.create(stripUndefined(data))
},
async getAuthenticator(credentialID) {
return p.authenticator.findUnique({
Expand All @@ -116,3 +119,10 @@ export function PrismaAdapter(
},
}
}

/** @see https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/null-and-undefined */
function stripUndefined<T>(obj: T) {
const data = {} as T
for (const key in obj) if (obj[key] !== undefined) data[key] = obj[key]
return { data }
}
Loading

0 comments on commit 56a9564

Please sign in to comment.