function cdb_sync(pRecordIDL, pTable, pSource, pAllowDeletes, pDetectCollisions)


Updated 12/17/19

Summary

This function will sync records in a specified table between local and cloud.

Inputs

  • pRecordIDL (String) - A line-delimited list of records to be synced
    • If "*" is passed, all records will be synced.
  • pTable (String) - The specified table name or table ID to be synced.
  • pSource (String) - Must be either "cloud" or "local". pSource determines the direction of the sync. If "cloud" is chosen, records will be synced from cloud to local. If "local" is chosen, records will be synced from local to cloud.
  • pAllowDeletes (Boolean) - "True" or "False". A value of "true" means any recordIDs passed that are empty in the source (i.e. the record does not exist in the source) will be deleted in the target. A value of "false" means that no deletions will occur -- recordIDs that don't exist in the source will be ignored in the target.
  • pDetectCollisions (Boolean) - "True" or "False". A value of "true" means that the record versions between each record will be compared. Any time the source has a lower version (i.e. when the target record has been updated after the last time the source and target were in sync), the record will not be overwritten. Instead, the record will be listed in the response as a collision. A value of "false" means that records will not be compared for versions, and that all source records will overwrite all target records.

Output

(Array) - An array with two keys:

  • ["collisions"] -
    • (String) - If there are no collisions, the value is 0.
    • (Array) - If there are collisions:
      • "Collisions" key is an array whose key is the specified tableID mapped to recordIDs that were not synced. An empty value is mapped to these recordIDs.
  • ["sync"] -
    • (String) - If there are no records synced, the value is 0.
    • (Array) - If there are records synced:
      • "Sync" key is an array whose key is the specified tableID mapped to recordIDs and each recordID is mapped to a value, its cdbCloudSyncVersion.

BatchSync output diagram

Additional Requirements

This API call requires internet access.

Examples

Example 1:

# Table: clients
## Example 1: Force our local to match what is on the cloud.

get cdb_sync("*","clients","cloud",true,false)

# Local database will now have all records of cloud database.
# It will delete any records not on the cloud
# It will not check if anything local has been updated more recently.

Example 2:

## Example 2: Make our cloud get anything new from our local
## But do not overwrite anything that is newer.

local tResultA, tTableID

put cdb_tableID("clients") into tTableID

put cdb_sync("*","clients","local",false,true) into tResultA
repeat for each key xKey in tResultA["collisions"][tTableID]
    # Process our collisions here
end repeat

# Cloud database will now have all records of local database.
# It will not remove any records that only exist on cloud.
# It will not overwrite any records that are newer on cloud.

Example 3:

## Example 3: Make our local data sync to what is on cloud
## Overwrite everything locally, but keep any records that
## only exist locally.

get cdb_sync("*","clients","cloud",false,false)

# Anything that exists on the cloud will now exist locally,
# It will not remove any records that only exist locally.
# It will overwrite all records, including newer records.

Example 4:

## Example 4: Sync certain records from local to cloud
## Only overwrite if local is newer

local tList, tResultA, tTableID

put cdb_tableID("clients") into tTableID

put "535f96c2-c08a-447d-9293-68183699a17a" into tList
put lf & "cdb2dabc-cd91-49d3-af02-a69a73c5928e" after tList
put lf & "7c397cb0-91bb-4f0a-9d42-5798af59b902" after tList

put cdb_sync(tList,"clients","cloud",false,true) into tResultA
repeat for each key xKey in tResultA["collisions"][tTableID]
    # Process our collisions here
end repeat

# These three records will be updated on the cloud.
# They will not overwrite records on the cloud that are newer.