Update record
The update record API endpoint allows you to update an existing record in a schema. To update a record, you need to send a PUT request to the /api/content/{schema}/{id} endpoint with the update data in the request body.
Supported operations
The update record API supports a variety of update operations, includes:
- Update a single field.
- Updating
o2oando2mrelation fields. - Adding and removing items from
m2mrelation fields. - Clearing the value of a field.
- Performing expressions-based updates.
Update data
The update data is a JSON object that contains the field data to update or the update operations to perform.
Simple field update
To update a single field, you can provide the field name and the new value in the update data.
Example
{
"name": "Updated Post",
"priority": 2,
"category": { "id": 3 },
"tags": [{ "id": 4 }, { "id": 5 }]
}A simple field update contains the schema field names as keys and the new values as values. The new values can be one of the following:
- A simple value to update the field. For example, the above update data updates the
namefield toUpdated Postand thepriorityfield to2. - An object containing the new value for the relation field. For example, the above updates the
categoryfield to the category withid: 3and thetagsfield to the tags withid: 4andid: 5.
Operation-based update
To perform an operation-based update, you can use the operation as the key and the operation data as the value in the update data.
Supported operations:
$set$clear$add$expr
Example
{
"$set": {
"name": "Updated Post",
"priority": 2,
"category": { "id": 3 }
},
"$clear": {
"tags": true
},
"$add": {
"tags": [{ "id": 4 }, { "id": 5 }]
},
"$expr": {
"views": "views + 1",
"description": "CONCAT(description, ' Updated')"
}
}$set operation
The $set operation allows you to update the field with the new value.
Example
{
"$set": {
"name": "Updated Post",
"priority": 2,
"category": { "id": 3 },
"tags": [{ "id": 4 }, { "id": 5 }]
}
}The $set property accepts an object with:
- The field names as keys: The field can be a simple field or a relation field.
- The new values as values: The new values can be simple values or objects for relation fields.
- A simple value:
- Set the
namefield toUpdated Post. - Set the
priorityfield to2.
- Set the
- A single relation field (single entity):
- Set the
categoryfield to{ "id": 3 }.
- Set the
- Multiple relation fields (array of entity):
- Set the
tagsfield to[{ "id": 4 }, { "id": 5 }].
- Set the
- A simple value:
$clear operation
The $clear operation allows you to clear the value of a field, including relation fields.
Example
{
"$clear": {
"description": true,
"category": true,
"tags": [{ "id": 4 }]
}
}The $clear property accepts an object with the field names as keys and the values as one of the following:
true: Clear the field value.- Clear a simple field: Set the
descriptionfield tonull. - Clear all the relation fields: Set the
categoryto empty.
- Clear a simple field: Set the
- An array of objects: Clear the specific items from the
o2morm2mrelation field.- Remove the
[ { "id": 4 } ]from thetagsrelation field.
- Remove the
$add operation
The $add operation allows you to increment a numeric field or add new items to the o2m or m2m relation fields.
Example
{
"$add": {
"views": 1,
"tags": [{ "id": 4 }, { "id": 5 }]
}
}The $add property accepts an object with the field names as keys and the values as one of the following:
- A numeric value: Increment the
viewsfield by1. - An array of objects: Add new items to the
o2morm2mrelation fields.
$expr operation
The $expr operation allows you to perform expressions-based updates on the fields.
Example
{
"$expr": {
"views": "views + 1",
"description": "CONCAT(description, ' Updated')"
}
}The $expr property accepts an object with the field names as keys and the values as the expressions to update the field.
The values of the $expr property must be a valid SQL expression that can be evaluated by the database.
Example
To update a user record with ID 1 with the new data:
POST /api/content/post
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en;q=0.9,ja;q=0.8
Authorization: Bearer <jwt token>
Cache-Control: max-age=0
Connection: keep-alive
Content-Type: application/json;charset=utf-8
Cookie: token=<jwt token>
Host: localhost:8000
Referer: http://localhost:8000/dash/
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36{
"name": "John Doe",
"age": 30,
"room": { "id": 2 },
"pets": [ { "id": 2 }, { "id": 3 } ],
"groups": [ { "id": 4 }, { "id": 5 } ],
"$set": {
"bio": "Hello World",
"address": "123 Main St",
"department": { "id": 2 },
"roles": [ { "id": 2 }, { "id": 3 } ],
"projects": [ { "id": 4 }, { "id": 5 } ]
},
"$clear": {
"bio": true,
"address": true,
"room": true,
"roles": true,
"projects": true,
"pets": [ { "id": 2 }, { "id": 3 } ],
"groups": [ { "id": 4 }, { "id": 5 } ]
},
"$add": {
"pets": [ { "id": 2 }, { "id": 3 } ],
"groups": [ { "id": 4 }, { "id": 5 } ],
"age": 1,
"salary": 1000
},
"$expr": {
"bio": "LOWER(`bio`)",
"address": "CONCAT(`address`, ' ', `city`, ' ', `state`, ' ', `zip`)"
}
}