$ yarn add pouchdb
import PouchDB from 'pouchdb';
const localDb = new PouchDB('db_name');
const remoteDb = new PouchDB('https://user:password@couchdb.example.com/db_name');
localDb.sync(remoteDb, {
live: true,
retry: true
}).on('change', function (change) {
// yo, something changed!
}).on('paused', function (info) {
// replication was paused, usually because of a lost connection
}).on('active', function (info) {
// replication was resumed
}).on('error', function (err) {
// totally unhandled error (shouldn't happen)
});
localDb
.allDocs({include_docs: true})
.then(response => {
const docs = response.rows.map(row => row.doc);
console.log({docs});
});
localDb
.changes({ since: 'now', live: true })
.on('change', change => {
console.log({ change });
loadDocumentsIntoState();
});
localDb
.put({
_id: '1',
//...
})
.catch(console.error);
localDb.get(id)
.then(doc => {
doc[attribute] = value;
return db.put(doc);
})
.catch(console.error);