Skip to content

Instantly share code, notes, and snippets.

@craftzdog
Last active January 9, 2016 15:03
Show Gist options
  • Save craftzdog/ea55c8a1c835130e8974 to your computer and use it in GitHub Desktop.
Save craftzdog/ea55c8a1c835130e8974 to your computer and use it in GitHub Desktop.
PouchDB sync issue
var PouchDB = require('pouchdb');
var local = new PouchDB('./db');
function createDB () {
var promise = Promise.resolve();
var docs = [];
for (var i=0; i<100; i++) {
docs.push({
_id: 'note/' + i,
data: i
});
}
docs.forEach(function (doc) {
promise = promise.then(function () {
return local.put(doc);
})
.then((result) => {
doc._rev = result.rev;
doc.revision = 2;
return local.put(doc);
});
});
promise.then(function () {
console.log('saved');
});
}
createDB();
var PouchDB = require('pouchdb');
var util = require('util');
PouchDB.plugin(require('pouchdb-find'));
var local = new PouchDB('./db');
function syncDB () {
var remote = new PouchDB('http://localhost:5984/note');
local.sync(remote, {
//live: true,
//retry: true
}).on('complete', function (status) {
local.find({
selector: {
_id: { $gte: 'note/', $lte: 'note/\uffff' }
}
}).then(function (result) {
console.info('notes:', result.docs.length);
});
}).on('error', function (err) {
// boo, something went wrong!
console.error('replication failed:', util.inspect(err, { showHidden: true, depth: null, colors: true }));
});
}
syncDB();
@craftzdog
Copy link
Author

Steps to reproduce:

# Create database
DEBUG="*" node create.js
# Sync to remote CouchDB
DEBUG="*" node sync.js
# Remove local db
rm -r db
# Replicate from remote CouchDB
DEBUG="*" node sync.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment