fix(drizzle-adapter): handle all operators in multiple where conditions (#5311)

Co-authored-by: Maxwell <145994855+ping-maxwell@users.noreply.github.com>
This commit is contained in:
KinfeMichael Tariku
2025-10-15 13:42:04 +03:00
committed by GitHub
parent 6f23188a36
commit 58ffd0bc92
2 changed files with 149 additions and 0 deletions

View File

@@ -253,12 +253,76 @@ export const drizzleAdapter = (db: DB, config: DrizzleAdapterConfig) => {
}
return notInArray(schemaModel[field], w.value);
}
if (w.operator === "contains") {
return like(schemaModel[field], `%${w.value}%`);
}
if (w.operator === "starts_with") {
return like(schemaModel[field], `${w.value}%`);
}
if (w.operator === "ends_with") {
return like(schemaModel[field], `%${w.value}`);
}
if (w.operator === "lt") {
return lt(schemaModel[field], w.value);
}
if (w.operator === "lte") {
return lte(schemaModel[field], w.value);
}
if (w.operator === "gt") {
return gt(schemaModel[field], w.value);
}
if (w.operator === "gte") {
return gte(schemaModel[field], w.value);
}
if (w.operator === "ne") {
return ne(schemaModel[field], w.value);
}
return eq(schemaModel[field], w.value);
}),
);
const orClause = or(
...orGroup.map((w) => {
const field = getFieldName({ model, field: w.field });
if (w.operator === "in") {
if (!Array.isArray(w.value)) {
throw new BetterAuthError(
`The value for the field "${w.field}" must be an array when using the "in" operator.`,
);
}
return inArray(schemaModel[field], w.value);
}
if (w.operator === "not_in") {
if (!Array.isArray(w.value)) {
throw new BetterAuthError(
`The value for the field "${w.field}" must be an array when using the "not_in" operator.`,
);
}
return notInArray(schemaModel[field], w.value);
}
if (w.operator === "contains") {
return like(schemaModel[field], `%${w.value}%`);
}
if (w.operator === "starts_with") {
return like(schemaModel[field], `${w.value}%`);
}
if (w.operator === "ends_with") {
return like(schemaModel[field], `%${w.value}`);
}
if (w.operator === "lt") {
return lt(schemaModel[field], w.value);
}
if (w.operator === "lte") {
return lte(schemaModel[field], w.value);
}
if (w.operator === "gt") {
return gt(schemaModel[field], w.value);
}
if (w.operator === "gte") {
return gte(schemaModel[field], w.value);
}
if (w.operator === "ne") {
return ne(schemaModel[field], w.value);
}
return eq(schemaModel[field], w.value);
}),
);

View File

@@ -398,6 +398,91 @@ export const getNormalTestSuiteTests = ({
});
expect(sortModels(result)).toEqual(sortModels(users));
},
"findMany - should handle multiple where conditions with different operators":
async () => {
const testData = [
{ name: "john doe", email: "john@example.com" },
{ name: "jane smith", email: "jane@gmail.com" },
];
const createdUsers: User[] = [];
for (const data of testData) {
const user = await adapter.create({
model: "user",
data: {
...generate("user"),
...data,
},
forceAllowId: true,
});
createdUsers.push(user as User);
}
const result = await adapter.findMany<User>({
model: "user",
where: [
{
field: "email",
value: "john@example.com",
operator: "eq",
connector: "AND",
},
{
field: "name",
value: "john",
operator: "contains",
connector: "AND",
},
],
});
expect(result.length).toBe(1);
expect(result[0]!.email).toBe("john@example.com");
expect(result[0]!.name).toBe("john doe");
const result2 = await adapter.findMany<User>({
model: "user",
where: [
{
field: "email",
value: "gmail",
operator: "contains",
connector: "AND",
},
{
field: "name",
value: "jane",
operator: "contains",
connector: "AND",
},
],
});
expect(result2.length).toBe(1);
expect(result2[0]!.email).toBe("jane@gmail.com");
expect(result2[0]!.name).toBe("jane smith");
const result3 = await adapter.findMany<User>({
model: "user",
where: [
{
field: "email",
value: "john",
operator: "starts_with",
connector: "AND",
},
{
field: "name",
value: "john",
operator: "contains",
connector: "AND",
},
],
});
expect(result3.length).toBe(1);
expect(result3[0]!.email).toBe("john@example.com");
expect(result3[0]!.name).toBe("john doe");
},
"findMany - should find many models with contains operator (using symbol)":
async () => {
const users = (await insertRandom("user", 3)).map((x) => x[0]);