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 instance, use the following methods:

// custom class approach
unitOfWork.create( object: object );

// untyped objects approach
unitOfWork.create( tableName: string, object: object );

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

The operation returns an OpResult object which represents the object stored in the database as a result of this operation. The OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). It is also possible to use OpResult to "extract" the value of a property of the saved object and use it in other operations. For more information see the Operation Result chapter of this guide.

Example

Consider the following example, it demonstrates creating an object and adding it to be saved in a transaction:

const unitOfWork = new Backendless.UnitOfWork();

const person = {
   name: "Joe",
   age: 25
}
//add OpResult
const addPersonResult = unitOfWork.create( "Person", person );

// add other operations
// ...

// use unitOfWork.execute() to run the transaction

Person class:

function Person(args) {
    args = args || {};
    this.name = args.name || "";
    this.age = args.age || null;
}
Code to add a "create" operation to a transaction:
const unitOfWork = new Backendless.UnitOfWork();

const person = new Person( {
    name: "Joe",
    age: 25
});

const addPersonResult = unitOfWork.create(person);
// add other operations
// ...

// use unitOfWork.execute() to run the transaction

Codeless Reference

data_transactions_save_one_object

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 a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object 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:

data_transactions_save_one_object_1

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

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 "Create" operation type used in conjunction with these additional Codeless blocks. The example below highlights how this block accepts the "Create" operation and also provides insight how the previous blocks are used to create the required logic structure:

data_transactions_save_one_object_8_additional

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

Now lets provide data to properties of the "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 object argument expects an object, whose properties must match the column names in the Person data table.

data_transactions_save_one_object_9_additional

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 saves a single object to the data table called Person:

data_transactions_save_one_object_2

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_transactions_save_one_object_10_additional

Furthermore, the operation has saved a new object to the  data table:

data_transactions_save_one_object_3