Giter VIP home page Giter VIP logo

multi-master-merge's Introduction

multi-master-merge

A document database with multi master replication and merge support based on leveldb, fwdb and scuttleup

npm install multi-master-merge

build status

Usage

var mmm = require('multi-master-merge')
var level = require('level')

var mdb = mmm(level('data.db'))

mdb.put('hello', {hello:'world'}, function(err, doc) {
  console.log('Inserted:', doc)
  mdb.get('hello', function(err, docs) {
    console.log('"hello" contains the following:', docs)
  })
})

When you do mdb.get(key, cb) you will always get an array of documents back. The reason for this is to support multi master replication which means that we might have multiple values for a given key.

Replication

To replicate your database simply open a sync stream and pipe it to another database

// a and b are two database instances
var s1 = a.sync() // open a sync stream for db a
var s2 = b.sync() // open a sync stream for db b

s1.pipe(s2).pipe(s1) // pipe them together to start replicating

Updates will now be replicated between the two instances. If two databases inserts a document on the same key both of them will be present if you do a mdb.get(key)

a.put('hello', {hello:'a'})
b.put('hello', {hello:'b'})

setTimeout(function() {
  a.get('hello', function(err, docs) {
    console.log(docs) // will print [{hello:'a'}, {hello:'b'}]
  })
}, 1000) // wait a bit for the inserts to replicate

Merging

To combine multiple documents into a single one use mdb.merge(key, docs, newDoc) If we consider the above replication scenario we have two documents for the key hello

a.get('hello', function(err, docs) {
  console.log(docs) // will print [{hello:'a'}, {hello:'b'}]
})

To merge them into a single document do

a.get('hello', function(err, docs) {
  a.merge('hello', docs, {hello:'a + b'})
})

Merges will replicate as well

// wait a bit for a to replicate to b
b.get('hello', function(err, docs) {
  console.log(docs) // will print [{hello:'a + b'}]
})

API

var mdb = mmm(db, [options])

Create a new database. db is a levelup instance. Options can include

{
 id: peerId, // defaults to a cuid
 preupdate: function(logData, cb) {},  // set a preupdate hook
 postupdate: function(logData, cb) {} // set a postupdate hook
}

mdb.put(key, document, [cb])

Insert a new document. Callback is called with cb(err, doc) where doc is the inserted document.

mdb.get(key, cb)

Get documents stored on key. Callback is called with cb(err, documents).

mdb.createReadStream([options])

Get a stream of all {key:key, value:documents} pair in the database. You can pass in gt,gte,lt,lte options similar to levelup.

mdb.createValueStream([options])

Similar to createReadStream but only returns values

mdb.createKeyStream([options])

Similar to createReadSTream but only returns keys

mdb.merge(key, docs, newDoc, [cb])

Merge multiple documents into a new document. Callback is called with cb(err, doc) where doc is the inserted merged document.

var stream = mdb.sync([options])

Returns a replication stream that can be piped to another mdb instance to replicate between them. Per defaults changes are replicated both ways. If you only want to push changes to another instance set {mode: 'push'} and if you only want to get changes do {mode: 'pull'}

mdb.fwdb

The used fwdb instance

mdb.log

The used scuttleup instance

License

MIT

multi-master-merge's People

Contributors

mafintosh avatar

Watchers

 avatar  avatar

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.