Saving multiple objects¶
This is a transaction operation responsible for saving one or more new objects in the database. To add this operation to a Unit Of Work request, use the following operation in the request payload:
{
"isolationLevelEnum" : "REPEATABLE_READ" | "READ_COMMITTED" | "READ_UNCOMMITTED" | "SERIALZABLE",
"operations": [
{
"operationType": "CREATE_BULK",
"table": "TABLENAME",
"opResultId": "OPRESULT-ID",
"payload": [
{ },
{ }
]
}
]
}
where:
Argument | Description |
---|---|
"operationType" |
The value must be "CREATE_BULK" , this designates the operation as "saving multiple objects". The objects to save in the database are defined in the "payload" property. |
"table" |
The value must contain the name of the table where the objects will be saved. The table must exist in the database before the transaction is executed. |
"opResultId" |
This is an optional property. If present, must contain an Id for this operation. An operation Id is a free-form string that must be unique within the transaction. If the result of this operation is to be used as an input in another operation within the same transaction, the Id will be used to reference the result. |
"payload" |
The value is mandatory. Must contain a JSON array consisting of JSON objects which will be saved in a table identified with the "table" property. |
Return Value¶
This operation returns a JSON array of string objectId
values assigned to each object from the request after it is saved in the database. The array or any of its elements can be used in other operations of the same transaction. For more information see the Operation Result chapter of this guide.
Example¶
Consider the example below. The example shows request body for a transaction where three objects are saved in the Person
table:
Request:
URL:
POST https://xxxx.backendless.app/api/transaction/unit-of-work
Content-Type:application/json
{
"operations": [
{
"operationType": "CREATE_BULK",
"table": "Person",
"opResultId": "savePeopleOperation",
"payload": [
{ "name": "Joe", "age": 25 },
{ "name": "Mary", "age": 32 },
{ "name": "Bob", "age": 22 }
]
}
]
}
Response:
{
"success": true,
"error": null,
"results": {
"savePeopleOperation": {
"type": "CREATE_BULK",
"result": [
"8C098C2B-B4E3-85B2-FFFE-D1966093BE00",
"59F69F71-97AC-2AEC-FF19-19EE01C92C00",
"3DEF90B5-B42E-62B8-FF6B-457B85B1D400"
]
}
}
}
Codeless Reference¶
where:
Argument | Description |
---|---|
operation id |
Unique identifier of the current operation which can be used later on in other subsequent operation within the same transaction. Refer to the Operation Result topic for more information. |
table name |
Name of the data table where new records must be saved. |
objects |
A list containing objects to save in the database. Objects properties must match the names of the table columns. The objects must not have the objectId property. |
return operation reference |
Optional parameter. When this box is checked, after this operation is executed its reference is returned in the response as an object containing transaction details. |
Consider the structure of the data table called Person
:
To create a transaction and save new objects to the data table, you must use the following additional Codeless blocks:
1 Create Transaction
Codeless block is important, since it creates an empty object which is used as a placeholder for transaction information. Refer to the Transaction Isolation topic to learn more about the options of the isolation
property. This empty object must be saved to a variable.
2 All operations stored inside a transaction must have a specific order. The following Codeless block sets this order for different operation types. The transaction
property expects an empty object created with the Create Transaction
Codeless block. This mandatory empty object is used to store transaction information. In the context of this description, the variable that was set for the Create Transaction
Codeless block must be used in the transaction
property to specify the empty object.
This block expects different operation types, such as "Find"
, "Bulk Create"
, "Bulk Upsert"
, "Bulk Update"
and "Bulk Delete"
. This topic describes the "Bulk Create"
operation type used in conjunction with these additional Codeless blocks. The example below shortly highlights how this block accepts the "Bulk Create"
operation and also provides insight how the previous blocks are used to create the required logic structure:
As it was mentioned earlier, you need a variable(e.g. myTx
) that will store the empty object. Then this empty object myTx
must be passed to the Add Operations to Transaction
Codeless block, so that the logic can store the transaction details. And then inside this block you must declare operations; this example uses only one operation of the "Bulk Create"
type.
Now lets provide data to properties of the "Bulk Create" Operation
Codeless block. The table name
argument must reference the data table where an object is saved. The operation id
property must be set to the value of your choice, while the objects
argument expects a list containing multiple objects, whose properties must match the column names in the Person
data table.
3 To run the logic above you must use an additional Codeless block that initiates the operation. The transaction
property expects an object containing the transaction details(e.g. myTx
variable).
where:
Argument | Description |
---|---|
transaction |
Expects an object containing transaction details, this includes: operation type and data. |
return result |
When this box is checked, the operation is set to return an object containing the operation result. |
throw error |
Check this box to return an error in case if the operation cannot be executed. |
The following example combines all Codeless blocks described above into the logic that saves a single object to the data table called Person
:
This example contains an additional variable called opRef
(operation reference), that was added intentionally to highlight the operation reference returned after this Codeless logic runs:
Furthermore, the operation has saved new objects to the data table: