Skip to content

Commit 3a23d90

Browse files
committed
doc: custom procedures
1 parent a2475ef commit 3a23d90

File tree

10 files changed

+201
-7
lines changed

10 files changed

+201
-7
lines changed

docs/modeling/custom-proc.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
sidebar_position: 12
3+
description: ZenStack custom procedures
4+
---
5+
6+
import ZModelVsPSL from '../_components/ZModelVsPSL';
7+
import PreviewFeature from '../_components/PreviewFeature'
8+
9+
# Custom Procedure
10+
11+
<PreviewFeature name="Custom procedure" />
12+
13+
<ZModelVsPSL>
14+
Custom procedure is a ZModel feature and doesn't exist in PSL.
15+
</ZModelVsPSL>
16+
17+
Custom procedures are like database stored procedures that allow you to define reusable routines encapsulating complex logic.
18+
19+
Use the `procedure` keyword to define a custom procedure in ZModel. Here's an example for a query procedure:
20+
21+
```zmodel title="schema.zmodel"
22+
procedure getUserFeeds(userId: Int, limit: Int?) : Post[]
23+
```
24+
25+
Mutation procedures (that write to the database) should be defined with `mutation procedure`:
26+
27+
```zmodel title="schema.zmodel"
28+
mutation procedure signUp(email: String) : User
29+
```
30+
31+
You can use all types supported by ZModel to define procedure parameters and return types, including:
32+
33+
- Primitive types like `Int`, `String`
34+
- Models
35+
- Enums
36+
- Custom types
37+
- Array of the types above
38+
39+
Parameter types can be marked optional with a `?` suffix. If a procedure doesn't return anything, use `Void` as the return type.
40+
41+
Custom procedures are implemented with TypeScript when constructing the ORM client, and can be invoked via the ORM client in backend code. See [Custom Procedures](../orm/custom-proc.md) in the ORM part for more details.
42+
43+
They are also accessible via Query-as-a-Service (via [RPC-style](../service/api-handler/rpc.md#endpoints) or [RESTful-style](../service/api-handler/rest.md#calling-custom-procedures) API), plus consumable via Client SDKs like [TanStack Query Client](../service/client-sdk/tanstack-query/).

docs/modeling/plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 12
2+
sidebar_position: 13
33
description: ZenStack plugins
44
---
55

docs/orm/custom-proc.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
sidebar_position: 13
3+
description: ORM custom procedures
4+
---
5+
6+
import PreviewFeature from '../_components/PreviewFeature'
7+
8+
# Custom Procedures
9+
10+
<PreviewFeature name="Custom procedure" />
11+
12+
:::info
13+
Please refer to the [Modeling](../modeling/custom-proc.md) part for how to define custom procedures in ZModel.
14+
:::
15+
16+
The ORM's CRUD API is very flexible and powerful, but in real-world applications you'll often find the need to encapsulate complex logic into more high-level and reusable operations. For example, in a collaborative app, after creating new users, you may want to automatically create a default workspace for them and assign some initial roles.
17+
18+
A conventional approach is to implement a `signUp` API route that orchestrates these steps. However, since the operation is still very much database-centric, it's more natural to have the encapsulation at the ORM level. This is where custom procedures come in. They are type-safe procedures defined in ZModel and implemented with TypeScript, and can be invoked via the ORM client just like the built-in CRUD methods.
19+
20+
## Implementing custom procedures
21+
22+
Suppose you have the following custom procedures defined in ZModel:
23+
24+
```zmodel title="schema.zmodel"
25+
// get blog post feeds for a given user
26+
procedure getUserFeeds(userId: Int, limit: Int?) : Post[]
27+
28+
// sign up a new user
29+
mutation procedure signUp(email: String) : User
30+
```
31+
32+
:::info
33+
Query procedures and mutation procedures currently don't have any semantic differences at the ORM level. However, in the future they may behave differently, for example, when features like cached queries are introduced.
34+
:::
35+
36+
When you construct a `ZenStackClient`, you must provide an implementation for each procedure:
37+
38+
```ts title="db.ts"
39+
const db = new ZenStackClient({
40+
...
41+
procedures: {
42+
getUserFeeds: ({ client, args }) => {
43+
return client.post.findMany({
44+
where: { authorId: args.userId },
45+
orderBy: { createdAt: 'desc' },
46+
take: args.limit,
47+
});
48+
},
49+
50+
signUp: ({ client, args }) => {
51+
return client.user.create({
52+
data: {
53+
email: args.email,
54+
memberships: {
55+
create: {
56+
role: 'OWNER',
57+
workspace: {
58+
create: { name: 'Default Workspace' },
59+
},
60+
},
61+
}
62+
}
63+
});
64+
},
65+
},
66+
});
67+
```
68+
69+
The implementation callbacks are provided with a context argument with the following fields:
70+
71+
- `client`: an instance of `ZenStackClient` used to invoke the procedure.
72+
- `args`: an object that contains the procedure arguments.
73+
74+
At runtime, before passing the args to the callbacks, ZenStack verifies that they conform to the types defined in ZModel. You can implement additional validations in the implementation if needed. ZenStack doesn't verify the return values. It's your responsibility to ensure they match the declared return types.
75+
76+
## Calling custom procedures
77+
78+
The custom procedures methods are grouped under the `$procs` property of the client instance. You must provide arguments as an object under the `args` key:
79+
80+
```ts
81+
const user = await db.$procs.signUp({
82+
args: { email: 'alice@example.com' }
83+
});
84+
85+
const feeds = await db.$procs.getUserFeeds({
86+
args: { userId: user.id, limit: 20 }
87+
});
88+
```
89+
90+
## Error handling
91+
92+
The `ZenStackClient` always throws an `ORMError` to the caller when an error occurs. To follow this protocol, custom procedure implementations should ensure other types of errors are caught and wrapped into `ORMError` and re-thrown. See [Error Handling](./errors.md) for more details.

docs/orm/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 14
2+
sidebar_position: 15
33
description: Setup logging
44
---
55

docs/orm/plugins/_category_.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
position: 13
1+
position: 14
22
label: Plugins
33
collapsible: true
44
collapsed: true

docs/recipe/plugin-dev.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 5
2+
sidebar_position: 6
33
description: Plugin development guide
44
---
55

docs/service/api-handler/rest.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The factory function accepts an options object with the following fields:
5050

5151
- externalIdMapping
5252

53-
Optional. An `Record<string, string>` value that provides a mapping from model names (as defined in ZModel) to unique constraint name. This is useful when you for example want to expose natural keys in place of a surrogate keys:
53+
Optional. An `Record<string, string>` value that provides a mapping from model names (as defined in ZModel) to the model's unique field name. This is useful when you for example want to expose natural keys in place of a surrogate keys:
5454

5555
```ts
5656
// Expose tags by unique name and not by ID, ie. /tag/blue intead of /tag/id
@@ -98,6 +98,10 @@ model Comment {
9898
post Post @relation(fields: [postId], references: [id])
9999
postId Int
100100
}
101+
102+
procedure getUserFeeds(userId: Int, limit: Int?) : Post[]
103+
104+
mutation procedure signUp(email: String) : User
101105
```
102106

103107
### Listing resources
@@ -835,6 +839,47 @@ PATCH /:type/:id/relationships/:relationship
835839
}
836840
```
837841

842+
### Calling custom procedures
843+
844+
Custom procedures can be invoked with the special `$procs` resource type.
845+
846+
Use `GET` for query procedures and pass the arguments as a URL encoded object in the `args` query parameter:
847+
848+
```ts
849+
GET /$procs/:procName?args=<encoded arguments>
850+
```
851+
852+
Use `POST` for mutation procedures and pass the arguments in the request body:
853+
854+
```ts
855+
POST /$procs/:procName
856+
{
857+
"args": { ... }
858+
}
859+
```
860+
861+
#### Status codes
862+
863+
- 200: The request was successful and the response body contains the custom procedure's return value.
864+
- 400: Invalid custom procedure name or arguments.
865+
- 500: An error occurred while executing the custom procedure.
866+
867+
#### Examples
868+
869+
```ts
870+
// for arguments `{"userId":1,"limit":10}
871+
GET /$procs/getUserFeeds?args=%7B%22userId%22%3A1%2Climit%3A10%7D
872+
```
873+
874+
```ts
875+
POST /$procs/signUp
876+
{
877+
"args": {
878+
"email": "alice@zenstack.dev"
879+
}
880+
}
881+
```
882+
838883
## Compound ID Fields
839884
840885
ZModel allows a model to have compound ID fields, e.g.:

docs/service/api-handler/rpc.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,18 @@ The following part explains how the `meta` information is included for different
181181
182182
_Http method:_ `DELETE`
183183
184-
- **[model]/check**
184+
- **[$procs]/[custom-procedure-name]**
185+
186+
Invoking a query custom procedure. E.g., `/$procs/getUserFeeds?q=<encoded args>`.
185187
186188
_Http method:_ `GET`
187189
190+
- **[$procs]/[mutation-custom-procedure-name]**
191+
192+
Invoking a mutation custom procedure. E.g., `/$procs/signUp`.
193+
194+
_Http method:_ `POST`
195+
188196
## HTTP Status Code and Error Responses
189197
190198
### Status code

docs/service/client-sdk/tanstack-query/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ Here's a quick example of using infinite query to load a list of posts with infi
521521
522522
</Tabs>
523523
524+
## Custom Procedures
525+
526+
[Custom procedures](../../../modeling/custom-proc.md) are grouped under the `$procs` property on the client returned by `useClientQueries`. Query procedures are mapped to query hooks, while mutation procedures are mapped to mutation hooks.
527+
528+
There's no automatic query invalidation or optimistic update support for custom procedures, since their semantics are unknown to the system. You need to implement such behavior manually as needed.
529+
524530
## Advanced Topics
525531
526532
### Query Invalidation

src/lib/prism-zmodel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Prism.languages.zmodel = Prism.languages.extend('clike', {
22
function: /@@*[A-Za-z_]\w*/,
33
keyword:
4-
/\b(?:datasource|enum|generator|model|type|abstract|import|extends|attribute|function|view|plugin|proc|with|true|false|null)\b/,
4+
/\b(?:datasource|enum|generator|model|type|abstract|import|extends|attribute|function|view|plugin|procedure|mutation|with|true|false|null)\b/,
55
entity: /\b(?:Int|String|Boolean|DateTime|Float|Decimal|BigInt|Bytes|Json|Unsupported)\b/,
66
'type-class-name': /(\b()\s+)[\w.\\]+/,
77
});

0 commit comments

Comments
 (0)