[PR #7272] fix(drizzle-adapter):and or connector #32790

Open
opened 2026-04-17 23:31:29 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7272
Author: @Ridhim-RR
Created: 1/11/2026
Status: 🔄 Open

Base: mainHead: fix/drizzle-adapter-mixed-and-or-connectors


📝 Commits (10+)

  • 39c5852 fix(drizzle-adapter):and or connector
  • 549aed6 chore(lint): format issue
  • 8fbe853 Update packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts
  • 30ccd4f Update packages/better-auth/src/adapters/tests/basic.ts
  • ad262da chore(reafactor): code refactor
  • 0401e98 fix(in-memory): update logic in in-memory-adapter
  • 1c32141 chore(format): format
  • 7653978 Merge branch 'canary' into fix/drizzle-adapter-mixed-and-or-connectors
  • 2ad2dfc Apply suggestions from code review
  • d47f8b6 Merge branch 'canary' into fix/drizzle-adapter-mixed-and-or-connectors

📊 Changes

166 files changed (+19664 additions, -7 deletions)

View changed files

📝 e2e/adapter/test/adapter-factory/basic.ts (+77 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-mysql-schema-1.ts (+100 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-1.ts (+93 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-10.ts (+112 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-11.ts (+112 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-12.ts (+101 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-13.ts (+93 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-14.ts (+241 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-15.ts (+106 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-16.ts (+105 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-17.ts (+93 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-18.ts (+94 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-19.ts (+100 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-2.ts (+93 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-20.ts (+115 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-21.ts (+108 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-22.ts (+108 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-23.ts (+100 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-24.ts (+100 -0)
packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-25.ts (+101 -0)

...and 80 more files

📄 Description

Closes #7271

Description

Fixes a bug in the Drizzle adapter where queries with mixed AND/OR connectors would drop one of the connector groups, leading to incorrect query results.

Problem

The convertWhereClause function in the Drizzle adapter was building both andClause and orClause but only returning the first clause via clause[0], effectively ignoring the second group when both AND and OR connectors were present.

Solution

  • Modified convertWhereClause to combine both AND and OR clauses using and(andClause, orClause) when both groups exist
  • Added comprehensive test case to verify mixed AND/OR connector behavior across all adapters

Changes

  • packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts: Fixed logic in convertWhereClause to properly combine AND/OR groups
  • packages/better-auth/src/adapters/tests/basic.ts: Added test case "findMany - should support mixed AND/OR conditions" to ensure all adapters handle this scenario

Test Case

The test creates 5 users and queries for:

"findMany - should support mixed AND/OR conditions": async () => {
			const { id: _ignored, ...template } = await generate("user");
			const user1 = await adapter.create<User>({
				model: "user",
				data: {
					...template,
					email: "admin@company.com",
					name: "Admin User",
				},
			});
			const user2 = await adapter.create<User>({
				model: "user",
				data: {
					...template,
					email: "john@company.com",
					name: "John Doe",
				},
			});
			const user3 = await adapter.create<User>({
				model: "user",
				data: {
					...template,
					email: "admin@external.com",
					name: "External Admin",
				},
			});

			const user4 = await adapter.create<User>({
				model: "user",
				data: {
					...template,
					email: "jane@other.com",
					name: "Jane Smith",
				},
			});
			const user5 = await adapter.create<User>({
				model: "user",
				data: {
					...template,
					email: "other@company.com",
					name: "Other Person",
				},
			});
			// Find users where:
			// (email contains "company.com") AND (name contains "Admin" OR name contains "John")
			// Should match user1 (admin@company.com + Admin User) and user2 (john@company.com + John Doe)
			// Should NOT match user3 (different domain), user4 (different domain), or user5 (domain matches but name doesn't)
			const result = await adapter.findMany<User>({
				model: "user",
				where: [
					{
						field: "email",
						value: "company.com",
						operator: "contains",
					},
					{
						field: "name",
						value: "Admin",
						operator: "contains",
						connector: "OR",
					},
					{
						field: "name",
						value: "John",
						operator: "contains",
						connector: "OR",
					},
				],
			});
			expect(result.length).toBe(2);
			const sortIds = (ids: string[]) =>
				ids.toSorted((a, b) => a.localeCompare(b));
			expect(sortIds(result.map((u) => u.id))).toEqual(
				sortIds([user1.id, user2.id]),
			);
		},

Summary by cubic

Fixes mixed AND/OR handling in the Drizzle and Memory adapters. Queries like (A) AND (B OR C) now return the correct rows, with tests covering this across adapters.

  • Bug Fixes
    • Drizzle: combine AND/OR with and(andClause, orClause) when both exist; return a single clause when only one exists.
    • Memory: group OR conditions and AND them with base filters in where evaluation.

Written for commit 8926ea87eb. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7272 **Author:** [@Ridhim-RR](https://github.com/Ridhim-RR) **Created:** 1/11/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/drizzle-adapter-mixed-and-or-connectors` --- ### 📝 Commits (10+) - [`39c5852`](https://github.com/better-auth/better-auth/commit/39c5852ad675fd0fe06a8338dbd66048a5b88c78) fix(drizzle-adapter):and or connector - [`549aed6`](https://github.com/better-auth/better-auth/commit/549aed636a67dfd4b4b942820021a0f3fdd5fa3a) chore(lint): format issue - [`8fbe853`](https://github.com/better-auth/better-auth/commit/8fbe85385976124aac37062ea2e6372a84d4dce0) Update packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts - [`30ccd4f`](https://github.com/better-auth/better-auth/commit/30ccd4fa36ba4b03e216975234e685d40f24d511) Update packages/better-auth/src/adapters/tests/basic.ts - [`ad262da`](https://github.com/better-auth/better-auth/commit/ad262da3c29dfc5d4efbef37ff85b6c206521d1a) chore(reafactor): code refactor - [`0401e98`](https://github.com/better-auth/better-auth/commit/0401e98d1616cbf345b9e445fd5f1719b37d248a) fix(in-memory): update logic in in-memory-adapter - [`1c32141`](https://github.com/better-auth/better-auth/commit/1c3214172b035d62731f970718f1792f9e50dfb8) chore(format): format - [`7653978`](https://github.com/better-auth/better-auth/commit/76539786b331f47dc14d71bbc8a2a866b4003a81) Merge branch 'canary' into fix/drizzle-adapter-mixed-and-or-connectors - [`2ad2dfc`](https://github.com/better-auth/better-auth/commit/2ad2dfc58f1728563f53236d6d846943e52384b3) Apply suggestions from code review - [`d47f8b6`](https://github.com/better-auth/better-auth/commit/d47f8b64d6bc5adf3f2e04ddb737badf596b6800) Merge branch 'canary' into fix/drizzle-adapter-mixed-and-or-connectors ### 📊 Changes **166 files changed** (+19664 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `e2e/adapter/test/adapter-factory/basic.ts` (+77 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-mysql-schema-1.ts` (+100 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-1.ts` (+93 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-10.ts` (+112 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-11.ts` (+112 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-12.ts` (+101 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-13.ts` (+93 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-14.ts` (+241 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-15.ts` (+106 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-16.ts` (+105 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-17.ts` (+93 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-18.ts` (+94 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-19.ts` (+100 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-2.ts` (+93 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-20.ts` (+115 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-21.ts` (+108 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-22.ts` (+108 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-23.ts` (+100 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-24.ts` (+100 -0) ➕ `packages/better-auth/src/adapters/drizzle-adapter/test/.tmp/generated-pg-schema-25.ts` (+101 -0) _...and 80 more files_ </details> ### 📄 Description Closes #7271 ## Description Fixes a bug in the Drizzle adapter where queries with mixed AND/OR connectors would drop one of the connector groups, leading to incorrect query results. ## Problem The `convertWhereClause` function in the Drizzle adapter was building both `andClause` and `orClause` but only returning the first clause via `clause[0]`, effectively ignoring the second group when both AND and OR connectors were present. ## Solution - Modified `convertWhereClause` to combine both AND and OR clauses using `and(andClause, orClause)` when both groups exist - Added comprehensive test case to verify mixed AND/OR connector behavior across all adapters ## Changes - **packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts**: Fixed logic in `convertWhereClause` to properly combine AND/OR groups - **packages/better-auth/src/adapters/tests/basic.ts**: Added test case `"findMany - should support mixed AND/OR conditions"` to ensure all adapters handle this scenario ## Test Case The test creates 5 users and queries for: ``` "findMany - should support mixed AND/OR conditions": async () => { const { id: _ignored, ...template } = await generate("user"); const user1 = await adapter.create<User>({ model: "user", data: { ...template, email: "admin@company.com", name: "Admin User", }, }); const user2 = await adapter.create<User>({ model: "user", data: { ...template, email: "john@company.com", name: "John Doe", }, }); const user3 = await adapter.create<User>({ model: "user", data: { ...template, email: "admin@external.com", name: "External Admin", }, }); const user4 = await adapter.create<User>({ model: "user", data: { ...template, email: "jane@other.com", name: "Jane Smith", }, }); const user5 = await adapter.create<User>({ model: "user", data: { ...template, email: "other@company.com", name: "Other Person", }, }); // Find users where: // (email contains "company.com") AND (name contains "Admin" OR name contains "John") // Should match user1 (admin@company.com + Admin User) and user2 (john@company.com + John Doe) // Should NOT match user3 (different domain), user4 (different domain), or user5 (domain matches but name doesn't) const result = await adapter.findMany<User>({ model: "user", where: [ { field: "email", value: "company.com", operator: "contains", }, { field: "name", value: "Admin", operator: "contains", connector: "OR", }, { field: "name", value: "John", operator: "contains", connector: "OR", }, ], }); expect(result.length).toBe(2); const sortIds = (ids: string[]) => ids.toSorted((a, b) => a.localeCompare(b)); expect(sortIds(result.map((u) => u.id))).toEqual( sortIds([user1.id, user2.id]), ); }, ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes mixed AND/OR handling in the Drizzle and Memory adapters. Queries like (A) AND (B OR C) now return the correct rows, with tests covering this across adapters. - **Bug Fixes** - Drizzle: combine AND/OR with and(andClause, orClause) when both exist; return a single clause when only one exists. - Memory: group OR conditions and AND them with base filters in where evaluation. <sup>Written for commit 8926ea87eb4dad58db632415c3b7bd076ee952d8. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 23:31:29 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#32790