Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The data sync functionality is handled by Cloudant Sync. Cloudant Sync is a  replication-protocol-compatible datastore for devices that don't want or need to run a full CouchDB instance.  Cloudant replication functionality is used to synchronise data between the local datastore and a remote database, either a CouchDB instance or a Cloudant database. Many datastores can replicate with the same remote database, meaning that cross-device synchronisation is achieved by setting up replications from each device to the remote database.





Setup and Configuration

Dependencies

...

PropertyLocationNotes
datastore_manager_directoryCloudantDataHandler.dataThis is the directory where cloudant will create the client db file. Set to 'data' by default
datastore_nameCloudantDataHandler.opensrp_clients_eventsThe name of the cloudant database in the client app. Set to 'opensrp_clients_events'
dbURL
port- AllConstants.CloudantSync.COUCHDB_PORTThis is a combination of the server IP address (by default using the IP specified in the settings screen when logging in), server couchdb port number (set to 5984 by default) and the server couch database name
databaseName
AllConstants.CloudantSync.COUCH_DATABASE_NAME
The server side couch database name to be synced with the client app
COUCH_DATABASE_USER
AllConstants.CloudantSync
Server couchdb  username
COUCH_DATABASE_PASS
AllConstants.CloudantSync
Server Couchdb password
syncFiltersassets/sync_filters.json
Code Block
languagejs
linenumberstrue
{
  "_id": "_design/cloudantFilter",
  "filters": {
    "locationId": "function(doc, req){ if ( doc.locationId != req.query.locationId ){ return false; } return true; }",
    "providerId": "function(doc, req){ if(doc.type != \"Event\"){ return true; } if ( doc.providerId == req.query.providerId ){ return true; } return false; }",
    "team": "function(doc, req){ if(doc.type != \"Event\"){ return true; } if(req.query.team){ if(req.query.team.split(',').indexOf(doc.providerId) >= 0){ return true; } } return false; }"
  }
}

Sync filters are special couchdb design document functions that allow data querying from the database returning only specific documents that pass filter rules. Filter rules are composed of fields in the documents being selected whereby the values for those fields are compared against the remotely passed filter values from cloudant and available in the 'req' object. The above sample shows what would be required in the server couchdb for cloudant to sync data by location, provider or team. The sync filters are defined in the client app and automatically loaded through CloudantSyncHandler class replicationFilterSettings() method and added to the server couchdb in getReplicationFiler() method. Every time a static instance of CloudantSyncHandler class is initialized, replicationFilterSettings() method is called to compare the contents of the local sync filters JSON file with the server version to see if there are changes and if so update the server with the new sync filters configs. Once the filters are set, Cloudant sync is then setup to do a filtered pull from the server database by instantiating the PullFilter class and passing the server side filter document and the filter params as key/value pairs (PullFilter("filterDoc/filterFunctionName", parameters)). NB As shown in the example above team filter expects a comma separated list of team members to compare against the providerId value in the EC documents. More on cloudant filtered replication here

...