Skip to content

Related Objects

Establishing object relationship

The process of establishing a relationship between multiple objects in Backendless 3.x is by saving the parent object which references its child(ren) in the same object hierarchy/tree. Version 5 changes the mechanism by introducing a dedicated operation which establishes a relationship between the objects. The example below stores two objects in Backendless Data service: Contact and Address and then sets up a relationship between them:

Version 3.x

var contactStorage = Backendless.Data.of( "Contact" );

var joeThePlumber = {
    name: "Joe",
    age: 27,
    phone: "1-972-5551212",
    title: "Plumber",
    ___class: "Contact"
};

var address = {
     street: "123 Main St.",
     city: "Denver",
     state: "Colorado",
     ___class: "Address"
}

contactStorage.save( joeThePlumber, new Backendless.Async(
  function( savedContact ) {
    contactStorage.save( address, new Backendless.Async(
      function( savedAddress ) {
        savedContact.address = [ savedAddress ];
        contactStorage.save( savedContact, new Backendless.Async(
          function( contactWithRelation ) {
            console.log( "contact now has related address" );
          }
        ));
      }
    ));
  }
));

Version 5.x

var contactStorage = Backendless.Data.of( "Contact" );
var addressStorage = Backendless.Data.of( "Address" );

var joeThePlumber = {
    name: "Joe",
    age: 27,
    phone: "1-972-5551212",
    title: "Plumber",
    ___class:"Contact"
};

var address = {
    street: "123 Main St.",
    city: "Denver",
    state: "Colorado",
    ___class: "Address"
}

Promise.all([
    contactStore.save(joeThePlumber),
    addressStore.save(address)
])
.then(function(results) {
   var savedContact = results[ 0 ];
   var savedAddress = results[ 1 ];

   // for 1:1 relations, use the ":1" notation instead of ":n"   
   return contactStore.setRelation( savedContact, 'address:Address:n', [ savedAddress ] );
})
.then( function(){
  console.log('the relation has been set')
}

Deleting/breaking object relationship

Similar to establishing a relationship between objects, Backendless 3.x relied on the update operation which sends the parent object to the server with an updated list of child objects. Any children removed from the collection on the client-side, are removed on the server as a result of the update call. Version 5 introduces a dedicated method to remove a relationship between objects. The example below removes a relationship between a Contact object and the first related Address object:

Version 3.x

var contactStorage = Backendless.Data.of( "Contact" );

// get the parent object
contactStorage.findById( "XXXX-XXXX-XXXX-XXXXX", new Backendless.Async( 
  function( contact ) {

    // load related objects for the "address" column
    contactStorage.loadRelations( contact, ["address"], new Backendless.Async( 
      function( updatedContact ) {
        // remove first element from the collection
        updatedContact.address = updatedContact.address.splice( 0, 1 )

        // save contact back
        contactStorage.save( updatedContact, new Backendless.Async(
          function( result ) {
            console.log( "contact has been updated, relation deleted");
          }));
      }));     
   }));

Version 5.x

var contactStorage = Backendless.Data.of( "Contact" );

// get the parent object
contactStorage.findById("XXXX-XXXX-XXXX-XXXXX")
  .then(function(contact) {

    // load related objects for the "address" column
    var loadRelationsQueryBuilder = Backendless.LoadRelationsQueryBuilder.create();
    loadRelationsQueryBuilder.setRelationName("address");

    contactStorage.loadRelations(contact, loadRelationsQueryBuilder)
      .then(function(addresses) {

        // break relation between contact and address
        contactStorage.deleteRelation(contact, "address", [addresses[0]]).then(function() {
          console.log("relation has been deleted");
        });
      });
  });

Additional resources: