Skip to content

Commit

Permalink
fix conditional filters guide
Browse files Browse the repository at this point in the history
  • Loading branch information
realmikesolo committed Mar 2, 2024
1 parent 4eae00b commit b4fb88d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/content/guides/queries/conditional-filters-in-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ await searchPosts(filters);
```ts copy
import { AnyColumn, ... } from 'drizzle-orm';

const db = drizzle(...);

const lengthLt = (column: AnyColumn, value: number) => {
return sql`length(${column}) < ${value}`;
};
Expand Down Expand Up @@ -134,8 +136,8 @@ select * from posts where length(title) < 8;
It is possible because `.where()` method and `and()`, `or()` operators accept SQL expression or undefined which will be ignored. In Drizzle all operators return SQL expression under the hood:

```js
// example of gt operator
const gt = (left, right) => {
return sql`${left} > ${bindIfParam(right, left)}`; // bindIfParam is internal function for binding parameters
// example of lt operator
const lt = (left, right) => {
return sql`${left} < ${bindIfParam(right, left)}`; // bindIfParam is internal function for binding parameters
};
```

0 comments on commit b4fb88d

Please sign in to comment.