Perform several operations in a single API call.
A batch operation performs several API calls in a single call.
Batch API Calls Count
When performing a batch operation, the actual number of API calls is the number of calls inside the batch operation. For example, if a batch operation updates 5 objects, it will count as 5 platform API calls.
To execute a batch operation send the following request:
curl -X POST \\
-H "Content-Type: application/json" \\
-H "X-Appery-Database-Id: <database_id>" \\
[-H "X-Appery-Session-Token: <session_token>" \\]
-d "<operations_array>" \\
https://api.appery.io/rest/1/db/batch
operations_array is a JSON array of objects containing batch operations description. For example:
[
{
"method": "<method>",
"collection": "<collection_name>",
"_id": "<object_id>",
"data": `<object_data>`
},
{
"method": "<method>",
"collection": "<collection_name>",
"_id": "<object_id>",
"data": `<object_data>`
},
...
]
Parameer | Description | Required |
---|---|---|
method |
GET, POST, PUT or DELETE. |
Yes. |
collection_name |
Database collection name. |
Yes |
object_id |
Object ID to perform the operation on. |
Required for GET, PUT or DELETE. |
object_data |
Object data for the operation. |
Required for POST or PUT. |
The database returns HTTP 200 OK status and JSON array representing results for the operations requested.
If an operation fails on a specific object, operations on other objects will continue.
The position of the operation result item is the same as the position of the operation description in the request. For example:
[
{
"<operation_status>": `<operation_result>`
},
{
"<operation_status>": `<operation_result>`
},
...
]
<HTMLBlock>{`
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr>
<th style="border: 1px solid #ddd; padding: 8px;">Parameter</th>
<th style="border: 1px solid #ddd; padding: 8px;">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><p>operation_status</p>
</td>
<td style="border: 1px solid #ddd; padding: 8px;"><p>success or error.</p>
</td>
</tr>
<tr>
<td style="border: 1px solid #ddd; padding: 8px;"><p>operation_result</p>
</td>
<td style="border: 1px solid #ddd; padding: 8px;"><p>JSON object representing operation response which is usually returned by a direct service invocation (see Collections REST API for more details), including error responses.</p>
</td>
</tr>
</tbody>
</table>
`}</HTMLBlock>
## Example
For collection **Labels** with one custom column name (string):
<Image
align="#e7e7e7"
alt="labels1.png"
border={false}
caption="Sample collection."
src="https://files.readme.io/a33b1c0-labels1.png"
width="654"
/>
Batch operation has the following format:
```curl
curl -X POST \\
-H "Content-Type: application/json" \\
-H "X-Appery-Database-Id: 58a59358975a4fa00e0ff50f" \\
https://api.appery.io/rest/1/db/batch
-d
"[
{
"method": "post",
"collection": "Labels",
"data": {"name": "nick"}
},
{
"method": "put",
"collection": "Labels",
"_id": "58a59386975a4fa00e0ff513",
"data": {"name": "max updated"}
},
{
"method": "delete",
"collection": "Labels",
"_id": "58a59381975a4fa00e0ff512"
},
{
"method": "get",
"collection": "Labels",
"_id": "58a59386975a4fa00e0ff513"
},
{
"method": "put",
"collection": "Labels",
"_id": "000000000000000000000000",
"data": {"name": "unexisting label with _id 000000000000000000000000"}
}
]"
[
{
"success": {
"_id": "58a5962d0a975a799b278abd",
"_createdAt": "2017-02-16 12:08:13.327"
}
},
{
"success": {
"_updatedAt": "2017-02-16 12:08:13.337"
}
},
{
"success": {}
},
{
"success": {
"_id": "58a59386975a4fa00e0ff513",
"name": "max updated",
"_createdAt": "2017-02-16 11:56:54.725",
"_updatedAt": "2017-02-16 12:08:13.337"
}
},
{
"error": {
"code": "DBSU205",
"description": "Collection 'Labels' doesn't contain object with id 000000000000000000000000."
}
}
]