Skip to content

Upsert a single object

Description

With this operation, you can either update an existing object or insert a new one into the database. To update an existing object, the operation locates it using its objectId value. If the object cannot be found, the operation inserts a new object into the data table with the specified objectId.

Method

POST

Endpoint URL

Important

Make sure to replace xxxx in the domain name in the request specification below to the one assigned to your application.

https://xxxx.backendless.app/api/transaction/unit-of-work

Request Headers

Content-Type:application/json

where:

Argument                Description
Content-Type Must be set to application/json. This header is mandatory.

Request Body

The request body must be a JSON object containing the "operationType" property with the "UPSERT" value and also other properties showcased below:

{  
  "operations": [  
    {  
      "operationType": "UPSERT",  
      "table": "Person",  
      "opResultId": "upsertOperation",  
      "payload":  
        { "name": "Peter", "email": "peter@yourmail.com", "age": 55 }  
    }  
  ]  
}

where:

Argument                Description
operationType The name of the operation to execute. In this context it must be set to "UPSERT".
table Name of the table where the object will be upserted.
opResultId Sets the unique identifier for the operation result. This identifier will be returned in the response along with additional data related to the operation.
payload The value of this property must be an object that will be upserted in the data table.

Response Body

This operation returnsthe object that was upserted into the database. The returned value can be used in other operations of the same transaction.

Example

In the example below, the "objectId" value is utilized to search the "Person" data table for an existing object. If the object is found, it is updated with new "age" and "email" values. If the object does not already exist, the operation inserts a new object into the data table with the specified "objectId" and other associated values.

curl --location --request POST 'https://xxxx.backendless.app/api/transaction/unit-of-work' \  
--header 'Content-Type: application/json' \  
--data-raw '{  
  "operations": [  
    {  
      "operationType": "UPSERT",  
      "table": "Person",  
      "opResultId": "upsertOperation",  
      "payload":  
        { "email": "alice@yourmail.com", "age": 20, "objectId":"CB3C9021-E08E-4DB1-AB98-CEC00F83476F" }  
    }  
  ]  
}'

Codeless Reference

data_service_transactions_upsert_1

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 the operation must apply changes.
object The object must contain the objectId property identifying an existing object in the database that must be updated, or a new object that must be inserted into the data table.
return result When this box is checked, the operation reference is returned alongside with an updated or a new inserted object.

Returns an object containing the operation result and also a record that was updated or inserted into the data table.

Consider the object stored in the data table called Person:

data_service_transactions_upsert_0

To create a transaction and upsert a new object in 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.

data_transactions_save_one_object_6_additional

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.

data_transactions_save_one_object_7_additional

This block expects different operation types, such as "Find", "Create", "Upsert", "Update" and "Delete".  This topic describes the "Upsert" operation type used in conjunction with these additional Codeless blocks. The example below highlights how this block accepts the "Upsert" operation and also provides insight how the previous blocks are used to create the required logic structure:

data_service_transactions_upsert_2

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 "Upsert" type.

Now lets provide data to properties of the "Upsert" Operation Codeless block. The table name argument must reference the data table where an object is upserted. The operation id property must be set to the value of your choice, while the object argument expects an object, whose properties must match the column names in the Person data table.

You must also specify the value in the objectId property, so that the operation could find the object that must be updated or insert a new object in case the specified one doesn't exist.

data_service_transactions_upsert_3

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).

data_transactions_save_one_object_5_additional

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 upserts a single object in the data table called "Person":

data_service_transactions_upsert_4

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:

data_service_transactions_upsert_5

Furthermore, the operation has upserted an existing object in the data table:

data_service_transactions_upsert_6