Skip to content

Commit a9d76ec

Browse files
committed
complete filter section
1 parent 2c526ff commit a9d76ec

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

versioned_docs/version-3.x/orm/api/filter.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,88 @@ sidebar_position: 3
33
description: how to filter entities
44
---
55

6+
import GithubCodeBlock from '@site/src/components/GithubCodeBlock';
67
import StackBlitzGithubEmbed from '@site/src/components/StackBlitzGithubEmbed';
78

89
# Filter
910

11+
Filtering is an important topic because it's involved in many ORM operations, for example when you find records, selecting relations, and updating or deleting multiple records.
12+
13+
Throughout this section all samples are based on the following ZModel schema:
14+
15+
<GithubCodeBlock repoPath="zenstackhq/v3-doc-orm-filter" file="zenstack/schema.zmodel" />
16+
17+
## Basic filters
18+
19+
You can filter on scalar fields with values or operators as supported by the field type. The following filter operators are available.
20+
21+
- `equals` `not`: all scalar fields.
22+
- `in` `notIn`: all scalar fields
23+
- `contains` `startsWith` `endsWith`: String fields
24+
- `lt` `lte` `gt` `gte`: String, Int, BigInt, Float, Decimal, and Date fields
25+
26+
A filter object can contain multiple field filters, and they are combined with `AND` semantic. You can also use the `AND`, `OR`, and `NOT` logical operators to combine filter objects to form a complex filter.
27+
28+
<StackBlitzGithubEmbed repoPath="zenstackhq/v3-doc-orm-filter" openFile="basic.ts" startScript="generate,basic" />
29+
30+
## Relation filters
31+
32+
Filters can be defined on conditions over relations. For one-to-one relations, you can filter on their fields directly. For one-to-many relations, use the "some", "every", or "none" operators to build a condition over a list of records.
33+
34+
<StackBlitzGithubEmbed repoPath="zenstackhq/v3-doc-orm-filter" openFile="relation.ts" startScript="generate,relation" />
35+
36+
## List filters
37+
38+
List fields allow extra filter operators to filter on the list content:
39+
40+
- `has`: checks if the list contains a specific value.
41+
- `hasEvery`: checks if the list contains all values in a given array.
42+
- `hasSome`: checks if the list contains at least one value in a given array.
43+
- `isEmpty`: checks if the list is empty.
44+
45+
:::info
46+
List type is only supported for PostgreSQL.
47+
:::
48+
49+
```zmodel
50+
model Post {
51+
...
52+
topics String[]
53+
}
54+
```
55+
56+
```ts
57+
await db.post.findMany({
58+
where: { topics: { has: 'webdev' } }
59+
});
60+
61+
await db.post.findMany({
62+
where: { topics: { hasSome: ['webdev', 'typescript'] } }
63+
});
64+
65+
await db.post.findMany({
66+
where: { topics: { hasEvery: ['webdev', 'typescript'] } }
67+
});
68+
69+
await db.post.findMany({
70+
where: { topics: { hasNone: ['webdev', 'typescript'] } }
71+
});
72+
73+
await db.post.findMany({
74+
where: { topics: { isEmpty: true } }
75+
});
76+
```
77+
78+
## Json filters
79+
80+
:::info WORK IN PROGRESS
81+
Filtering on Json fields is work in progress and will be available soon.
82+
:::
83+
84+
## Query builder filters
85+
86+
ZenStack v3 is implemented on top of [Kysely](https://kysely.dev/), and it leverages Kysely's powerful query builder API to extend the filtering capabilities. You can use the `$expr` operator to define a boolean expression that can express almost everything that can be expressed in SQL.
87+
88+
The `$expr` operator can be used together with other filter operators, so you can keep most of your filters simple and only reach to the query builder level for complicated components.
89+
90+
<StackBlitzGithubEmbed repoPath="zenstackhq/v3-doc-orm-filter" openFile="query-builder.ts" startScript="generate,query-builder" />

0 commit comments

Comments
 (0)