ReviewOS

also looking at this

stacks/bun-query-builder

fix(client): orWhere now groups, so an earlier where keeps applying (#1083)

#1088
Merged glennmichael123 wants to merge fix/or-where-precedence into main
13 files +1310 -450
docs/features/builder.mdmodified+17-2
Changes to docs/features/builder.md
@@ -112,7 +112,12 @@ const adults = await db
112112 .where(['age', '>=', 18])
113113 .execute()
114114
115// Complex conditions with multiple filters
115// Complex conditions with multiple filters.
116//
117// A chained `orWhere` groups with the term immediately before it, so this is
118// active AND age >= 18 AND (country IN (...) OR role = 'admin')
119// If you mean "all three filters, OR an admin", make the conjunction explicit
120// with whereGroup — see the note under Logical grouping below.
116121const eligibleUsers = await db
117122 .selectFrom('users')
118123 .where({ active: true })
@@ -121,6 +126,16 @@ const eligibleUsers = await db
121126 .orWhere(['role', '=', 'admin'])
122127 .execute()
123128
129// "Everything above, OR an admin":
130const eligibleOrAdmin = await db
131 .selectFrom('users')
132 .whereGroup(qb => qb
133 .where({ active: true })
134 .andWhere(['age', '>=', 18])
135 .andWhere(['country', 'in', ['US', 'CA', 'GB']]))
136 .orWhere(['role', '=', 'admin'])
137 .execute()
138
124139// Dynamic condition building
125140async function searchUsers(filters: { name?: string, role?: string, minAge?: number }) {
126141 let query = db.selectFrom('users')
@@ -246,7 +261,7 @@ const averyPrefs = await db
246261
247262- **Use indexes**: Ensure filtered columns have appropriate database indexes
248263- **Parameterized queries**: The builder automatically parameterizes values for SQL injection safety
249- **Logical grouping**: Use `orWhere` and `andWhere` to create clear logical groups
264- **Logical grouping**: A chained `orWhere` groups with the term before it — `.where(a).where(b).orWhere(c)` is `a AND (b OR c)`. Use `whereGroup(cb)` / `orWhereGroup(cb)` when you need the other reading, `(a AND b) OR c`
250265- **Dynamic filters**: Build conditions conditionally based on user input or application state
251266- **JSON performance**: Be mindful that JSON operations may require special indexes
252267