Skip to content

Commit

Permalink
Merge pull request #1 from redgeoff/initial
Browse files Browse the repository at this point in the history
feat(initial)
  • Loading branch information
redgeoff authored Jul 28, 2017
2 parents fbaf062 + 1e7f401 commit b83e16b
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 56 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# replicate-couchdb-cluster

[![Circle CI](https://circleci.com/gh/redgeoff/replicate-couchdb-cluster.svg?style=svg&circle-token=29186a9bacd110b627323d86119076539d8b144e)](https://circleci.com/gh/redgeoff/replicate-couchdb-cluster)

A fault-tolerant way to replicate an entire CouchDB cluster


Expand Down
37 changes: 28 additions & 9 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
# Doesn't actually work!
# node:
# version: 7.10.0
# version: 8.1.4

# Uncomment to test with CouchDB 2
machine:
services:
- docker

dependencies:
pre:
- nvm install 7.10.0
- nvm use 7.10.0 && npm install -g npm
- nvm install 8.1.4
- nvm use 8.1.4 && npm install -g npm

# Install CouchDB 2
- docker run -d --name couchdb --restart always -p 5984:5984 -e COUCHDB_USER='admin' -e COUCHDB_PASSWORD='admin' redgeoff/couchdb
# Wait for DB to be ready
- sleep 15
# Create system DBs
- curl -X PUT http://admin:[email protected]:5984/_users
- curl -X PUT http://admin:[email protected]:5984/_replicator
- curl -X PUT http://admin:[email protected]:5984/_global_changes

# Enable CORS
- nvm use 8.1.4 && npm install -g add-cors-to-couchdb
- nvm use 8.1.4 && add-cors-to-couchdb http://localhost:5984 -u admin -p admin

override:
- nvm use 7.10.0 && npm install
- nvm use 8.1.4 && npm install

test:
pre:
- nvm use 7.10.0 && npm run assert-beautified
- nvm use 7.10.0 && npm run jshint
- nvm use 8.1.4 && npm run assert-beautified
- nvm use 8.1.4 && npm run jshint

override:
- nvm use 7.10.0 && npm run node-full-test
- nvm use 7.10.0 && npm run browser-test-phantomjs
- nvm use 7.10.0 && npm run browser-coverage-full-test
# Test on CouchDB 2
- nvm use 8.1.4 && npm run node-full-test
- nvm use 8.1.4 && npm run browser-test-phantomjs
- nvm use 8.1.4 && npm run browser-coverage-full-test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
]
},
"dependencies": {
"sporks": "0.0.1"
"couch-slouch": "0.0.3",
"sporks": "0.0.1",
"squadron": "0.0.3"
}
}
69 changes: 69 additions & 0 deletions scripts/cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

var Slouch = require('couch-slouch'),
squadron = require('squadron');

// params
// source
// target
// concurrency
// skip
var Cluster = function (params) {
this._params = params;

this._sourceSlouch = new Slouch(params.source);
this._targetSlouch = new Slouch(params.target);

if (this._params.concurrency === 1) {
// Don't use a throttler
this._throttler = undefined;
} else {
// Create a throttler with the specified or default concurrency
var concurrency = this._params.concurrency ? this._params.concurrency : null;
this._throttler = new squadron.Throttler(concurrency);
}
};

Cluster.prototype.replicate = function () {
var self = this;
return self._sourceSlouch.db.all().each(function (db) {
if (!self._params.skip || self._params.skip.indexOf(db) === -1) {
return self._replicateDB(db, db);
}
}, self._throttler);
};

Cluster.prototype._createDBIfMissing = function (db) {
var self = this;
return self._targetSlouch.db.exists(db).then(function (exists) {
if (!exists) {
return self._targetSlouch.db.create(db);
}
});
};

Cluster.prototype._replicateSecurity = function (sourceDB, targetDB) {
var self = this;
return self._sourceSlouch.security.get(sourceDB).then(function (security) {
return self._targetSlouch.security.set(targetDB, security);
});
};

Cluster.prototype._replicateRawDB = function (sourceDB, targetDB) {
return this._sourceSlouch.db.replicate({
source: this._params.source + '/' + sourceDB,
target: this._params.target + '/' + targetDB
});
};

Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
var self = this;
return self._createDBIfMissing(targetDB).then(function () {
// Replicate security first so that security is put in place before data is copied over
return self._replicateSecurity(sourceDB, targetDB);
}).then(function () {
return self._replicateRawDB(sourceDB, targetDB);
});
};

module.exports = Cluster;
13 changes: 0 additions & 13 deletions scripts/foo.js

This file was deleted.

3 changes: 3 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./cluster');
14 changes: 0 additions & 14 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,4 @@ var chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();

var Foo = require('../scripts/foo');

require('./node-and-browser');

describe('browser', function () {

it('should test in only the browser', function () {
// TODO: insert tests that can only be tested in the browser
var foo = new Foo();
return foo.bar().then(function (thing) {
thing.should.eql('yar');
});
});

});
Loading

0 comments on commit b83e16b

Please sign in to comment.