Skip to content

Commit

Permalink
docs: upsert for rest api handler (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 27, 2024
1 parent 3ea5f10 commit 9f5bf83
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/reference/server-adapters/api-handlers/rest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,44 @@ Relationships can also be manipulated directly. See [Manipulating Relationships]
```
### Upserting a resource
JSON:API didn't specify a convention for "upsert" operations. ZenStack uses a variation of the "create" operation to represent "upsert", and uses the request meta to indicate the intention. See details in [examples](#examples-10).
```
POST /:type
```
#### Status codes
- 201: The request was successful and the resource was created.
- 200: The request was successful and the resource was updated.
- 400: The request was malformed.
- 403: The request was forbidden.
- 404: The requested resource type does not exist.
#### Examples
```json
POST /user
{
"data": {
"type": "user",
"attributes": {
"id": 1,
"name": "Emily",
"email": "[email protected]"
}
},
"meta": {
"operation": "upsert",
"matchFields": ["id"],
}
}
```
The `meta.operation` field must be "upsert", and the `meta.matchFields` field must be an array of field names that are used to determine if the resource already exists. If an existing resource is found, "update" operation is conducted, otherwise "create". The `meta.matchFields` fields must be unique fields, and they must have corresponding entries in `data.attributes`.
### Deleting a resource
A resource can be deleted using the following endpoint:
Expand Down

0 comments on commit 9f5bf83

Please sign in to comment.