Code Monkey home page Code Monkey logo

pouchdb-sync-to-anything's Introduction

Sync PouchDB to Anything

This is a plugin that lets you use CouchDBs replication algorithm with checkpointing, resuming, etc, but provide your own function to write the documents. This can be used to sequentially write updates to a REST API,

How to use

var PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-sync-to-anything')

var myDb = new PouchDB('exampleDB')

myDb.syncToAnything(function (docs) {
  // Sync function receives a document batch
  // Should return a promise

  // Example:
  return $.ajax({
    url: 'http://example.com/my-rest-endpoint',
    data: JSON.stringify(docs),
    method: 'POST'
  })
}, {
  sync_id: 'talkingToMyRestAPI',
  batch_size: 10
})

Arguments

Syncer (function)

A function which you use for writing whatever you need to write the documents to. Should return a promise. The function will be passed an array of documents that is minimum 1 document (never called empty) and maximum batch_size documents.

Options (object)

  • sync_id or syncId (string) required used as a checkpoint name. Change this to something else and the replication will start over from 0.
  • batch_size(number) the number of documents passed in to the syncer function.

The CouchDB protocol for not-CouchDB:

This is a partial implementation of the CouchDB replication protocol. It allows you to use PouchDB's tools to save replication checkpoints for syncing with your API. Using those checkpoints, we can start the next replication from the last good checkpoint.

How it works

Every write you make to a PouchDB/CouchDB instance has a sequence number. This is the base for the replication protocol, we use it to read every change that was made sequentially from the database.

  1. Using the sync_id passed in from the user, read the last sync checkpoint (will be 0 if we never synced before with that id).

  2. Read the changes feed from the PouchDB, limiting the number of changes to batch_size.

  3. Call the user provided sync function, passing in the documents from the changes batch. The sync function should return a promise that fails if the write failed.

  4. If the sync promise resolves, write a checkpoint of the last sequence numbers from the written batch

  5. Go back to 1

Good to know/Caveats:

  • Using this works best for one way data-flows only. If you're interested in a 2-way data flow, consider syncing data from the server to a different PouchDB instance, and write changes that are to be sent to the server separately.

  • A document is only sent to the sync function once per sync cycle. If you create a document, hit your sync function, update the document twice, and then hit sync again, it will be passed to the sync function once in the first sync call, and once in the second sync call.

pouchdb-sync-to-anything's People

Contributors

hj29 avatar karlwestin avatar

Forkers

abesamma

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.