Keeping Common and On-Demand realms in sync


(Chev) #1

Following App Architecture Guidelines, I’d like to keep some of the common data in sync within my on-demand realm.

As a simple example:

  • Cars are kept in sync for all clients and are stored in /commonRealm
  • Trips only sync for the relevant user and are stored in /~/driverRealm

Below is a minimal schema for the app

const CarSchema = {
    name: 'Car',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        make: {type: 'string'},
        model: {type: 'string'}
    }
};
const TripSchema = {
    name: 'Trip',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        name: {type: 'string'},
        car: {type: 'Car'}
    }
};

commonRealm includes the CarSchema only.
driverReam includes the CarSchema and TripSchema. Cars are never updated by the driver and purpose is really for lookup.

I cannot find an example of how to keep Cars in sync between the realms so that any updates in commonRealm are propagated to the driver’s driverRealm.

Listening to the commonRealm Cars object with a subscribe() doesn’t help as you cannot get access to data that has been deleted etc.

From what I can deduce from the docs, the architecture for this should actually be something like:

const CarSchema = {
    name: 'Car',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        make: {type: 'string'},
        model: {type: 'string'}
    }
};
const TripSchema = {
    name: 'Trip',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        name: {type: 'string'},
        carId: {type: 'string'} // foreign key
    }
};

commonRealm would include the CarSchema and driverReam would only include the TripSchema with carId as a type string for the Car.id - not a type of Car. It would be up to the client to run a map/loop to manually join to Car from the commonRealm like:

        let tripsWithCar = [];
        trips.forEach(trip => {
            trip.car = commonRealm.objects('Car').filtered(`id == "${trip.carId}"`)[0];
            tripsWithCar.push(trip);
        });

Would like to get some feedback from developers with more experience if this is the correct approach.