Skip to content

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.

The method argument is a collection containing objects which will be stored in the database. As you can see from the method signatures, there are two approaches for storing new objects: class-based and map-based approaches. The former uses a class defined in your program. The signature defines that class as type E. In this case, the name of the table where the object will be stored is the same as the name of the class. The objectsToSave argument in both signatures is a collection of objects to save. Each object must conform to the rules defined in the Data Object section of this guide.

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://api.backendless.com/APP-ID/REST-API-KEY/transaction/unit-of-work
Request header:
Content-Type:application/json
Request body:
{
  "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"
            ]
        }
    }
}