You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import StackBlitzGithub from '@site/src/components/StackBlitzGithub';
7
+
8
+
# Omitting Fields
9
+
10
+
The previous sections have shown how you can use `select` and `omit` clauses to control the fields returned in the query results. There may be scenarios where you want to exclude certain fields (e.g., `password`) for most queries. ZenStack provides a mechanism to configure field omission in a hierarchical manner.
11
+
12
+
:::info
13
+
Please note that the omit settings only affect the ORM query APIs, but not the [Query Builder APIs](../query-builder). With query builder you'll need to explicitly specify the fields to select.
14
+
:::
15
+
16
+
## Schema-Level Omit
17
+
18
+
You can use the `@omit` attribute in ZModel to mark fields to be omitted. Such fields will be omitted by default for all `ZenStackClient` instances and all queries.
19
+
20
+
```zmodel
21
+
model User {
22
+
id Int @id
23
+
name String
24
+
email String @unique
25
+
password String @omit
26
+
}
27
+
```
28
+
29
+
## Client-Level Omit
30
+
31
+
When creating a `ZenStackClient`, you can customize what fields to omit by specifying the `omit` option.
32
+
33
+
```ts
34
+
const db =newZenStackClient<Schema>({
35
+
...
36
+
omit: {
37
+
User: { email: true }
38
+
},
39
+
});
40
+
```
41
+
42
+
You can also use the `$setOptions` API to create a shallow clone of an existing client with different options:
43
+
44
+
```ts
45
+
const db =newZenStackClient<Schema>({ ... });
46
+
const dbNoEmail =db.$setOptions({
47
+
...db.$options,
48
+
omit: {
49
+
User: { email: true }
50
+
},
51
+
});
52
+
```
53
+
54
+
## Query-Level Omit
55
+
56
+
Finally, at query time you can use the `omit` clause for per-query control.
57
+
58
+
```ts
59
+
const users =awaitdb.user.findMany({
60
+
omit: { name: true },
61
+
});
62
+
```
63
+
64
+
## Precedence and Overrides
65
+
66
+
The precedence of omit settings is: query-level > client-level > schema-level. A higher precedence setting can override a lower one and re-include a field by negating the omit flag. E.g., at query time you can re-include the `password` field that was omitted at schema level:
67
+
68
+
```ts
69
+
const users =awaitdb.user.findMany({
70
+
omit: { password: false },
71
+
});
72
+
```
73
+
74
+
There might be scenarios where you don't want the query-level override feature. For example, when using `ZenStackClient` with the [Query as a Service](../../service), you may want to have certain fields always omitted for security reasons. In such cases, use the `allowQueryTimeOmitOverride` option to disable query-time overrides:
75
+
76
+
```ts
77
+
const db =newZenStackClient<Schema>({
78
+
...
79
+
allowQueryTimeOmitOverride: false,
80
+
});
81
+
```
82
+
83
+
When this option set to `false`, any attempt to negate an omission at query time will trigger a validation error.
84
+
85
+
```ts
86
+
// This will throw an error because query-time override is disabled.
0 commit comments