Skip to content

Commit

Permalink
fix: prisma update failing on multiple where clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru committed Oct 14, 2024
1 parent ef26279 commit 675e774
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/nextjs/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { authMiddleware } from "better-auth/next-js";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";

export default authMiddleware({
customRedirect: async (session, request) => {
Expand Down
18 changes: 14 additions & 4 deletions packages/better-auth/src/adapters/mongodb-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const mongodbAdapter = (mongo: Db) => {
.toArray();

const result = res[0];

if (!result) {
return null;
}
Expand All @@ -108,16 +109,25 @@ export const mongodbAdapter = (mongo: Db) => {
const { model, where, update } = data;
const wheres = whereConvertor(where);

const res = await db.collection(model).findOneAndUpdate(
if (where.length === 1) {
const res = await db.collection(model).findOneAndUpdate(
//@ts-expect-error
wheres,
{
$set: update,
},
{ returnDocument: "after" },
);
return removeMongoId(res);
}
const res = await db.collection(model).updateMany(
//@ts-expect-error
wheres,
{
$set: update,
},
{ returnDocument: "after" },
);

return removeMongoId(res);
return {};
},
async delete(data) {
const { model, where } = data;
Expand Down
9 changes: 7 additions & 2 deletions packages/better-auth/src/adapters/prisma-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ export const prismaAdapter = (
async update(data) {
const { model, where, update } = data;
const whereClause = whereConvertor(where);

return await db[model].update({
if (where.length === 1) {
return await db[model].update({
where: whereClause,
data: update,
});
}
return await db[model].updateMany({
where: whereClause,
data: update,
});
Expand Down
32 changes: 32 additions & 0 deletions packages/better-auth/src/adapters/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ export async function runAdapterTest(opts: AdapterTestOptions) {
expect(res.length).toBe(1);
});

test("should update with multiple where", async () => {
await adapter.update({
model: "user",
where: [
{
field: "name",
value: user.name,
},
{
field: "email",
value: user.email,
},
],
update: {
email: "updated@email.com",
},
});
const updatedUser = await adapter.findOne<User>({
model: "user",
where: [
{
field: "email",
value: "updated@email.com",
},
],
});
expect(updatedUser).toMatchObject({
name: user.name,
email: "updated@email.com",
});
});

test("delete model", async () => {
await adapter.delete({
model: "user",
Expand Down
6 changes: 5 additions & 1 deletion packages/better-auth/src/types/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { BetterAuthOptions } from "./options";
*/
export type Where = {
operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte"; //eq by default
value: string;
value: string | number | boolean;
field: string;
connector?: "AND" | "OR"; //AND by default
};
Expand Down Expand Up @@ -35,6 +35,10 @@ export interface Adapter {
};
offset?: number;
}) => Promise<T[]>;
/**
* ⚠︎ Update may not return the updated data
* if multiple where clauses are provided
*/
update: <T>(data: {
model: string;
where: Where[];
Expand Down

0 comments on commit 675e774

Please sign in to comment.