Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.db.put({ _id: this.docId }).then((result) => {
this.doc._rev = result.rev
if(this.onLoad) this.onLoad(null)
})
} else {
console.log(error)
}
})
// Set up replication subscriptions
let options = { live: true }
PouchDB.replicate(this.db, this.remote, options)
.on("error", console.log)
PouchDB.replicate(this.remote, this.db, options)
.on("change", (info) => {
console.log("Remote change")
this.db.get(this.doc._id).then((doc) => {
this.doc = doc
if(this.onChange) this.onChange(doc.state)
})
})
.on("error", console.log)
}
var PouchDB = require('pouchdb');
// Expose PouchDB on the window object to use the
// PouchDB Chrome debugger extension http://bit.ly/1L6dArH
window.PouchDB = PouchDB;
var db = new PouchDB('timely-news');
PouchDB.plugin(require('pouchdb-find'));
PouchDB.replicate('timely-news', 'http://localhost:4984/db', {
live: true
});
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js', {scope: './'});
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true})
.then(function (pushSubscription) {
console.log('The reg ID is :: ', pushSubscription.subscriptionId);
db.createIndex({index: {fields: ['type']}})
.then(function() {
db.find({
selector: {type: 'profile'}
}).then(function (res) {
console.log(res);
if(this.onLoad) this.onLoad(this.doc.state)
}).catch((error) => {
if(error.status === 404) {
this.db.put({ _id: this.docId }).then((result) => {
this.doc._rev = result.rev
if(this.onLoad) this.onLoad(null)
})
} else {
console.log(error)
}
})
// Set up replication subscriptions
let options = { live: true }
PouchDB.replicate(this.db, this.remote, options)
.on("error", console.log)
PouchDB.replicate(this.remote, this.db, options)
.on("change", (info) => {
console.log("Remote change")
this.db.get(this.doc._id).then((doc) => {
this.doc = doc
if(this.onChange) this.onChange(doc.state)
})
})
.on("error", console.log)
}
return new Promise((resolve, reject) => {
PouchDB
.replicate(originalDB, changedDB,
{live: false, retry: false})
.on('complete', info => resolve({output: info, message: 'complete'}))
.on('error', err => reject(Error({output: err, message: 'error'})))
.on('denied', err => reject(Error({output: err, message: 'denied'})))
})
.then(response => {
function firstTimePull() {
PouchDB.replicate(remote, db, {
complete: function(){
db.get(id, function(err){
if(err) {
if(err.status === 404) return initializeRecord();
return console.error(err)
}
emitter.emit('db-ready')
})
}
})
}
function setupPulling(){
PouchDB.replicate(remote, db, {
live: true,
onChange: function() {
emitter.emit('db-ready')
}
})
}
db.get(id, function(err, doc){
var data = JSON.parse(decrypt(doc.data, sercret))
processData(data)
doc.data = encrypt(JSON.stringify(data), sercret)
db.put(doc, callback)
PouchDB.replicate(db, remote, function(err){
if(err) console.error("failed to replicate changes to server", err)
})
})
}