Skip to content

Saving New Objects

In order to determine if an object needs to be stored as new or the existing one should be updated, Backendless checks if the object in the API call has the objectId value. This principle is used in the save and update API as well as in the Deep Save API. Below are some of the examples demonstrating various scenarios:

Important

When using the Deep Save API to save new objects, make sure the tables are defined in the database. If the table does not have a column defined for a non-relational property in your object, the API will return an error.

Parent/Child Objects with one-to-one Relation

The sample below stores parent (Product) and child (Manufacturer) objects in the corresponding tables and creates a relationship between the objects in the database:

curl \  
-H Content-Type:application/json \  
-X PUT  \  
-d "{    \  
  \"name\":\"iPhone X\", \  
  \"price\":1199,        \  
  \"manufacturer\": {    \  
    \"name\":\"Apple\",  \  
    \"industry\":\"Consumer Electronics\" \  
  } \  
}" \  
-v https://xxxx.backendless.app/api/data/Product/deep-save

Server response:

{
  "price": 1199,
  "created": 1624090691000,
  "name": "iPhone X",
  "___class": "Product",
  "ownerId": null,
  "updated": null,
  "objectId": "748471BF-F2E7-4AF0-A60C-68291B52999F",
  "manufacturer": {
    "created": 1624090692000,
    "name": "Apple",
    "___class": "Manufacturer",
    "industry": "Consumer Electronics",
    "ownerId": null,
    "updated": null,
    "objectId": "8494BFCC-964E-4F7F-95E3-2497C5D34011"
  }
}

Codeless reference:

save-product-manufacturer-codeless

Once the code runs, the following two objects are stored in the Manufacturer and Product tables:

Object in the Manufacturer table:

manufacturer-obj

Object in the Product table:

product-obj

Parent/Child Objects with one-to-many Relation

In the example below the parent object (Order) contains two relations: one is a one-to-one relation to a Customer object and the other  is a collection (а one-to-many relation) of child objects (OrderItem). The Deep Save API stores the parent object, the children and establishes a relationship in the database between the parent and its children:

curl \  
-H Content-Type:application/json \  
-X PUT  \  
-d "{   \  
  \"customer\":{                \  
    \"name\":\"John Smith\",    \  
    \"phoneNumber\":\"5551212\" \  
  },                            \  
  \"orderItems\":[              \  
    {                           \  
      \"name\":\"Apples\",      \  
      \"price\":5.99            \  
    },                          \  
    {                           \  
      \"name\":\"Oranges\",     \  
      \"price\":9.99            \  
    } \  
  ] \  
}" \  
-v https://xxxx.backendless.app/api/data/Order/deep-save

Server response:

{
  "created": 1624093011000,
  "___class": "Order",
  "ownerId": null,
  "orderItems": [
    {
      "created": 1624093012000,
      "price": 9.99,
      "___class": "OrderItem",
      "name": "Oranges",
      "ownerId": null,
      "updated": null,
      "objectId": "81451CE2-5E7E-4200-9DE5-A8BE5E47FA2A"
    },
    {
      "created": 1624093012000,
      "price": 5.99,
      "___class": "OrderItem",
      "name": "Apples",
      "ownerId": null,
      "updated": null,
      "objectId": "B4508F7B-3F6A-4A17-AE0D-B7AE010E64F2"
    }
  ],
  "updated": null,
  "objectId": "479090D2-5E85-48AB-B959-3FA14859A222",
  "customer": {
    "phoneNumber": "5551212",
    "created": 1624093012000,
    "name": "John Smith",
    "___class": "Customer",
    "ownerId": null,
    "updated": null,
    "objectId": "DBCB7BC6-2E29-4A8A-BF0E-FADDC9728AC5"
  }
}

Codeless reference:

save-order-with-customer-orderitems

When the Deep Save operation completes, the database will have the following objects:

Order object/table:

order-obj-deepsave

Customer object/table:

customer-obj-deepsave

OrderItem objects/table:

orderitem-obj-deepsave