Skip to content

Update Parent and Child

The example below demonstrates how the Deep Save API can be used to update both the parent and the child objects in the database. The sample retrieves a Product and the related Manufacturer objects then it changes property values in both objects and saves the changes with a single Deep Save API call. The objects before the code runs are:

The Product object:

product-with-manuf-before-deepsave

The Manufacturer object:

manuf-before-deepsave

const queryBuilder = Backendless.DataQueryBuilder.create()

// load the related manufacturer object
queryBuilder.addRelated('manufacturer')

// get the specific product
queryBuilder.setWhereClause('name = \'iPhone X\'')

// use get( 0 ) to get the first object from the response
const iPhone = (await Backendless.Data.of(Product).find(queryBuilder))[0]

// update values in both parent (Order) and child (Manufacturer) objects
iPhone.price = 1300
iPhone.manufacturer.industry = 'Computer Hardware, Software and Consumer Electronics'

// save both objects back in the database
Backendless.Data.of('Product').deepSave(iPhone)

// load the related manufacturer object
queryBuilder.addRelated('manufacturer')

// get the specific product
queryBuilder.setWhereClause("name = 'iPhone X'")

// use get( 0 ) to get the first object from the response
const iPhone = (await Backendless.Data.of(Product).find(queryBuilder))[0]

// update values in both parent (Order) and child (Manufacturer) objects
iPhone.price = 1300
iPhone.manufacturer.industry = "Computer Hardware, Software and Consumer Electronics"

// save both objects back in the database
Backendless.Data.of( Product ).deepSave( iPhone )
Product class:
class Product {
 constructor(data) {
   data = data || {}

   this.objectId = data.objectId
   this.name = data.name
   this.price = data.price
   this.manufacturer = data.manufacturer
 }
}
OrderItem class:
class Manufacturer {
 constructor(data) {
   data = data || {}

   this.objectId = data.objectId
   this.name = data.name
   this.industry = data.industry
 }
}

After the sample code runs, the objects in the database will appear as shown below:

The Product object:

product-after-deepsave

The Manufacturer object:

manuf-after-deepsave