also looking at this
fix(client): orWhere now groups, so an earlier where keeps applying (#1083)
#1088
13 files
+1310
-450
| @@ -112,7 +112,12 @@ const adults = await db | ||
| 112 | 112 | .where(['age', '>=', 18]) |
| 113 | 113 | .execute() |
| 114 | 114 | |
| 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. | |
| 116 | 121 | const eligibleUsers = await db |
| 117 | 122 | .selectFrom('users') |
| 118 | 123 | .where({ active: true }) |
| @@ -121,6 +126,16 @@ const eligibleUsers = await db | ||
| 121 | 126 | .orWhere(['role', '=', 'admin']) |
| 122 | 127 | .execute() |
| 123 | 128 | |
| 129 | // "Everything above, OR an admin": | |
| 130 | const 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 | ||
| 124 | 139 | // Dynamic condition building |
| 125 | 140 | async function searchUsers(filters: { name?: string, role?: string, minAge?: number }) { |
| 126 | 141 | let query = db.selectFrom('users') |
| @@ -246,7 +261,7 @@ const averyPrefs = await db | ||
| 246 | 261 | |
| 247 | 262 | - **Use indexes**: Ensure filtered columns have appropriate database indexes |
| 248 | 263 | - **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` | |
| 250 | 265 | - **Dynamic filters**: Build conditions conditionally based on user input or application state |
| 251 | 266 | - **JSON performance**: Be mindful that JSON operations may require special indexes |
| 252 | 267 | |