A simple JavaScript synchronisation module for Node.JS. Used to keep two sets of data which have a unique key in sync whilst keeping data writes to a minimum.
npm install data-sync
var dataSync = require('data-sync')()
dataSync.sync(
  dataSync.formatArray('id', originalData)
  dataSync.formatArray('id', newData),
  'id',
  isDifferent,
  create,
  update,
  del,
  function(error) {
    console.log('Data Sync Complete')
  }
)
For a complete example please refer to the included unit test.
The data-sync module contains two functions. These are:
formatArray(uniqueId, dataArray)
sync(originalDataHash, newDataHash, uniqueId, comparatorFunction, createFunction, updateFunction, deleteFunction, finalCallback)
formatArray is used to convert an array of data into the correct format required to perform the data sync. It takes two parameters. These are as follows:
- unqiueId: The name of the key which is to be used as a unique ID for each object.
- dataArray: The array of data.
e.g input
formatArray(
  'id',
  [
    {id: 1, name: 'Fred'},
    {id: 2, name: 'Jim'},
    {id: 3, name: 'Tom'}
  ]
)
e.g output
{
  1: {id: 1, name: 'Fred'},
  2: {id: 2, name: 'Jim'},
  3: {id: 3, name: 'Tom'}
}
sync is used to perform the actual data syncronisation. It takes 8 parameters. These are as follows:
- originalDataHash: Original data in the format produced by calling formatArray().
- newDataHash: New data in the format produced by calling formatArray().
- uniqueId: Name of the key which is to be used as a unique ID for each object.
- comparatorFunction: Used to determine if two objects should be considered different and therefore call update(). Should return true if there is a difference. Parameters are: oldObject, newObject.
- createFunction Function called when an object is present within the new data which is not in the original. Parameters are newObject, callback.
- updateFunction Function called when an object has changed within the new data. Parameters are uniqueIdValue, oldObject, newObject, callback.
- deleteFunction Function called when an object is present within the original data, but not the new data. Parameters are uniqueIdValue, oldObject, callback.
- finalCallback Function called when data syncronisation is complete.
e.g
Given the following:
var originalData = [
      {id: 1, name: 'Fred'},
      {id: 2, name: 'Jim'},
      {id: 3, name: 'Tom'}
    ]
  , newData = [
      {id: 1, name: 'Fred'},
      {id: 3, name: 'Barney'},
      {id: 4, name: 'Dave'}
    ];
You would expect the output from calling sync() to be:
originalData = [
  {id: 1, name: 'Fred'},
  {id: 3, name: 'Barney'},
  {id: 4, name: 'Dave'}
];
Adam Duncan follow me on twitter
Licenced under the New BSD License
