mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-07 04:19:22 +00:00
fix: prisma update failing on multiple where clauses
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { authMiddleware } from "better-auth/next-js";
|
import { authMiddleware } from "better-auth/next-js";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/lib/auth";
|
||||||
|
|
||||||
export default authMiddleware({
|
export default authMiddleware({
|
||||||
customRedirect: async (session, request) => {
|
customRedirect: async (session, request) => {
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ export const mongodbAdapter = (mongo: Db) => {
|
|||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
const result = res[0];
|
const result = res[0];
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -108,16 +109,25 @@ export const mongodbAdapter = (mongo: Db) => {
|
|||||||
const { model, where, update } = data;
|
const { model, where, update } = data;
|
||||||
const wheres = whereConvertor(where);
|
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
|
//@ts-expect-error
|
||||||
wheres,
|
wheres,
|
||||||
{
|
{
|
||||||
$set: update,
|
$set: update,
|
||||||
},
|
},
|
||||||
{ returnDocument: "after" },
|
|
||||||
);
|
);
|
||||||
|
return {};
|
||||||
return removeMongoId(res);
|
|
||||||
},
|
},
|
||||||
async delete(data) {
|
async delete(data) {
|
||||||
const { model, where } = data;
|
const { model, where } = data;
|
||||||
|
|||||||
@@ -112,8 +112,13 @@ export const prismaAdapter = (
|
|||||||
async update(data) {
|
async update(data) {
|
||||||
const { model, where, update } = data;
|
const { model, where, update } = data;
|
||||||
const whereClause = whereConvertor(where);
|
const whereClause = whereConvertor(where);
|
||||||
|
if (where.length === 1) {
|
||||||
return await db[model].update({
|
return await db[model].update({
|
||||||
|
where: whereClause,
|
||||||
|
data: update,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return await db[model].updateMany({
|
||||||
where: whereClause,
|
where: whereClause,
|
||||||
data: update,
|
data: update,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -178,6 +178,38 @@ export async function runAdapterTest(opts: AdapterTestOptions) {
|
|||||||
expect(res.length).toBe(1);
|
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 () => {
|
test("delete model", async () => {
|
||||||
await adapter.delete({
|
await adapter.delete({
|
||||||
model: "user",
|
model: "user",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { BetterAuthOptions } from "./options";
|
|||||||
*/
|
*/
|
||||||
export type Where = {
|
export type Where = {
|
||||||
operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte"; //eq by default
|
operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte"; //eq by default
|
||||||
value: string;
|
value: string | number | boolean;
|
||||||
field: string;
|
field: string;
|
||||||
connector?: "AND" | "OR"; //AND by default
|
connector?: "AND" | "OR"; //AND by default
|
||||||
};
|
};
|
||||||
@@ -35,6 +35,10 @@ export interface Adapter {
|
|||||||
};
|
};
|
||||||
offset?: number;
|
offset?: number;
|
||||||
}) => Promise<T[]>;
|
}) => Promise<T[]>;
|
||||||
|
/**
|
||||||
|
* ⚠︎ Update may not return the updated data
|
||||||
|
* if multiple where clauses are provided
|
||||||
|
*/
|
||||||
update: <T>(data: {
|
update: <T>(data: {
|
||||||
model: string;
|
model: string;
|
||||||
where: Where[];
|
where: Where[];
|
||||||
|
|||||||
Reference in New Issue
Block a user