npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-sync

v2.0.1

Published

A JavaScript data synchronisation module for Node.JS

Downloads

388

Readme

build status

data-sync.js

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.

Installation

npm install data-sync

Usage

Example

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.

Explanation

The data-sync module contains two functions. These are:

formatArray(uniqueId, dataArray)
sync(originalDataHash, newDataHash, uniqueId, comparatorFunction, createFunction, updateFunction, deleteFunction, finalCallback)

formatArray

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:

  1. unqiueId: The name of the key which is to be used as a unique ID for each object.
  2. 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

sync is used to perform the actual data syncronisation. It takes 8 parameters. These are as follows:

  1. originalDataHash: Original data in the format produced by calling formatArray().
  2. newDataHash: New data in the format produced by calling formatArray().
  3. uniqueId: Name of the key which is to be used as a unique ID for each object.
  4. 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.
  5. createFunction Function called when an object is present within the new data which is not in the original. Parameters are newObject, callback.
  6. updateFunction Function called when an object has changed within the new data. Parameters are uniqueIdValue, oldObject, newObject, callback.
  7. deleteFunction Function called when an object is present within the original data, but not the new data. Parameters are uniqueIdValue, oldObject, callback.
  8. 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'}
];

Credits

Adam Duncan follow me on twitter

Paul Serby

Licence

Licenced under the New BSD License