Skip to content

Saving a single object

This is a transaction operation responsible for saving a new object 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",
      "table": "TABLENAME",
      "opResultId": "OPRESULT-ID",
      "payload": {
        "key1":"value",
        "key2": value
      }
    }
  ]
}

where:

Argument                Description
"operationType" The value must be "CREATE", this designates the operation as "saving a single object". The object to save in the database is defined in the "payload" property.
"table" The value must contain the name of the table where the object 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 object which will be saved in a table identified with the "table" property.

As you can see from the method signatures, there are two approaches for storing a new object in the database: 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 objectToSave argument in both signatures must conform the rules defined in the Data Object section of this guide.

Return Value

This operation returns the object saved in the database. The object will have the objectId property with the value assigned by the server. The object or any of its properties can be used in other operations of the same transaction. For more information see the Operation Result chapter of this guide.

Example

Consider the following example, it demonstrates saving an object into 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",
      "table": "Person",
      "opResultId": "createPerson",
      "payload": {
        "name":"Joe",
        "age":25
      }
    }
  ]
}

Response:

{
    "success": true,
    "error": null,
    "results": {
        "createPerson": {
            "type": "CREATE",
            "result": {
                "name": "Joe",
                "age": 25.0,
                "___class": "Person",
                "objectId": "0A7DDC5D-C9D3-8D1C-FF15-F232F2B92500",
                "created": 1586020674000,
                "updated": null,
                "ownerId": null
            }
        }
    }
}