Blog

How to Create Custom Mapping Between Data Tables and Client Classes

by on September 1, 2019

In another post, we describe how data tables in Backendless map to the client-side classes whose instances contain persisted data objects. However, there are scenarios when the default mapping is undesirable. In that case, Backendless client libraries provide an API to override the mapping.

For example, consider the following data table (Restaurant):

Suppose that for some reason the client-side class for the objects in the Restaurant table must be called PlaceToEat. The mapping between the Restaurant table and the PlaceToEat class can be established using the following API:

    Backendless.Data.mapTableToClass( "Restaurant", PlaceToEat.class );

    Backendless.Data.mapTableToClass("Restaurant", PlaceToEat::class.java)

    [[Backendless.shared.data of:[PlaceToEat class]] mapToTableWithTableName:@"Restaurant"];

    Backendless.shared.data.of(PlaceToEat.self).mapToTable(tableName: "Restaurant")

    Backendless.Data.mapTableToClass('Restaurant', PlaceToEat)
    Backendless.Data.mapTableToClass('Location', Location)
    

    Backendless.data.mapTableToClass("Restaurant", PlaceToEat);

    Where the PlaceToEat class has the following structure (class’s fields and properties must match the table columns):

      import com.backendless.BackendlessUser;
      import com.backendless.BackendlessUser;
      public class PlaceToEat
      {
        private String name;
        private String objectId;
        private String cuisine;
        private java.util.List locations;
        private BackendlessUser owner;
        // skipping getters and setters for brevity
      }

      import com.backendless.BackendlessUser
      
      class PlaceToEat {
         var name: String? = null
         var objectId: String? = null
         var cuisine: String? = null
         var locations: List<Location>? = null
         var owner: BackendlessUser? = nul
      }
      

      @interface PlaceToEat : NSObject
      
      @property (strong, nonatomic) NSString *objectId;
      @property (strong, nonatomic) NSString *name;
      @property (strong, nonatomic) NSString *cuisine;
      @property (strong, nonatomic) BackendlessUser *owner;
      
      @end

      @objcMembers class PlaceToEat: NSObject {
          var objectId: String?
          var name: String?
          var cuisine: String?
          var owner: BackendlessUser?    
      }

      class PlaceToEat {
        constructor(props = {}) {
          let { name, ownerId, objectId, cuisine, locations } = props
      
          this.name = name
          this.objectId = objectId
          this.cuisine = cuisine
          this.locations = locations
          this.ownerId = ownerId
        }
      
        // skipping getters and setters for brevity
      }
      
      class Location {
        constructor(props = {}) {
          const { streetAddress, country, objectId, updated, created, ownerId, city, phoneNumber, menu } = props
      
          this.objectId = objectId
          this.streetAddress = streetAddress
          this.country = country
          this.updated = updated
          this.created = created
          this.ownerId = ownerId
          this.city = city
          this.phoneNumber = phoneNumber
          this.menu = menu
        }
      
        // skipping getters and setters for brevity
      }
      

      import 'package:backendless_sdk/backendless_sdk.dart';
      
      @reflector
      class PlaceToEat {
       String name;
       String objectId;
       String cuisine;
       List locations;
       BackendlessUser owner;
      }
      

      It is recommended that mapTableToClass method is called right after the initApp method, that is before any other API which fetches data from the server.

      Leave a Reply