pouchdb-checkpointer
Advanced tools
Comparing version 7.3.1 to 8.0.0
@@ -75,42 +75,110 @@ import { explainError } from 'pouchdb-utils'; | ||
function Checkpointer(src, target, id, returnValue, opts) { | ||
this.src = src; | ||
this.target = target; | ||
this.id = id; | ||
this.returnValue = returnValue; | ||
this.opts = opts || {}; | ||
} | ||
class CheckpointerInternal { | ||
constructor(src, target, id, returnValue, opts) { | ||
this.src = src; | ||
this.target = target; | ||
this.id = id; | ||
this.returnValue = returnValue; | ||
this.opts = opts || {}; | ||
} | ||
Checkpointer.prototype.writeCheckpoint = function (checkpoint, session) { | ||
var self = this; | ||
return this.updateTarget(checkpoint, session).then(function () { | ||
return self.updateSource(checkpoint, session); | ||
}); | ||
}; | ||
writeCheckpoint(checkpoint, session) { | ||
var self = this; | ||
return this.updateTarget(checkpoint, session).then(function () { | ||
return self.updateSource(checkpoint, session); | ||
}); | ||
} | ||
Checkpointer.prototype.updateTarget = function (checkpoint, session) { | ||
if (this.opts.writeTargetCheckpoint) { | ||
return updateCheckpoint(this.target, this.id, checkpoint, | ||
session, this.returnValue); | ||
} else { | ||
return Promise.resolve(true); | ||
updateTarget(checkpoint, session) { | ||
if (this.opts.writeTargetCheckpoint) { | ||
return updateCheckpoint(this.target, this.id, checkpoint, | ||
session, this.returnValue); | ||
} else { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
}; | ||
Checkpointer.prototype.updateSource = function (checkpoint, session) { | ||
if (this.opts.writeSourceCheckpoint) { | ||
updateSource(checkpoint, session) { | ||
if (this.opts.writeSourceCheckpoint) { | ||
var self = this; | ||
return updateCheckpoint(this.src, this.id, checkpoint, | ||
session, this.returnValue) | ||
.catch(function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return true; | ||
} | ||
throw err; | ||
}); | ||
} else { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
getCheckpoint() { | ||
var self = this; | ||
return updateCheckpoint(this.src, this.id, checkpoint, | ||
session, this.returnValue) | ||
.catch(function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return true; | ||
if (self.opts && self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) { | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
return sourceDoc.last_seq || LOWEST_SEQ; | ||
}).catch(function (err) { | ||
/* istanbul ignore if */ | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
return self.target.get(self.id).then(function (targetDoc) { | ||
if (self.opts && self.opts.writeTargetCheckpoint && !self.opts.writeSourceCheckpoint) { | ||
return targetDoc.last_seq || LOWEST_SEQ; | ||
} | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
// Since we can't migrate an old version doc to a new one | ||
// (no session id), we just go with the lowest seq in this case | ||
/* istanbul ignore if */ | ||
if (targetDoc.version !== sourceDoc.version) { | ||
return LOWEST_SEQ; | ||
} | ||
var version; | ||
if (targetDoc.version) { | ||
version = targetDoc.version.toString(); | ||
} else { | ||
version = "undefined"; | ||
} | ||
if (version in comparisons) { | ||
return comparisons[version](targetDoc, sourceDoc); | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (err.status === 404 && targetDoc.last_seq) { | ||
return self.src.put({ | ||
_id: self.id, | ||
last_seq: LOWEST_SEQ | ||
}).then(function () { | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return targetDoc.last_seq; | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
throw err; | ||
}); | ||
} else { | ||
return Promise.resolve(true); | ||
}).catch(function (err) { | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
}; | ||
} | ||
@@ -132,67 +200,2 @@ var comparisons = { | ||
Checkpointer.prototype.getCheckpoint = function () { | ||
var self = this; | ||
if (self.opts && self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) { | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
return sourceDoc.last_seq || LOWEST_SEQ; | ||
}).catch(function (err) { | ||
/* istanbul ignore if */ | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
return self.target.get(self.id).then(function (targetDoc) { | ||
if (self.opts && self.opts.writeTargetCheckpoint && !self.opts.writeSourceCheckpoint) { | ||
return targetDoc.last_seq || LOWEST_SEQ; | ||
} | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
// Since we can't migrate an old version doc to a new one | ||
// (no session id), we just go with the lowest seq in this case | ||
/* istanbul ignore if */ | ||
if (targetDoc.version !== sourceDoc.version) { | ||
return LOWEST_SEQ; | ||
} | ||
var version; | ||
if (targetDoc.version) { | ||
version = targetDoc.version.toString(); | ||
} else { | ||
version = "undefined"; | ||
} | ||
if (version in comparisons) { | ||
return comparisons[version](targetDoc, sourceDoc); | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (err.status === 404 && targetDoc.last_seq) { | ||
return self.src.put({ | ||
_id: self.id, | ||
last_seq: LOWEST_SEQ | ||
}).then(function () { | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return targetDoc.last_seq; | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
throw err; | ||
}); | ||
}).catch(function (err) { | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
}; | ||
// This checkpoint comparison is ported from CouchDBs source | ||
@@ -267,2 +270,9 @@ // they come from here: | ||
function Checkpointer(src, target, id, returnValue, opts) { | ||
if (!(this instanceof CheckpointerInternal)) { | ||
return new CheckpointerInternal(src, target, id, returnValue, opts); | ||
} | ||
return Checkpointer; | ||
} | ||
export default Checkpointer; |
202
lib/index.js
@@ -77,42 +77,110 @@ 'use strict'; | ||
function Checkpointer(src, target, id, returnValue, opts) { | ||
this.src = src; | ||
this.target = target; | ||
this.id = id; | ||
this.returnValue = returnValue; | ||
this.opts = opts || {}; | ||
} | ||
class CheckpointerInternal { | ||
constructor(src, target, id, returnValue, opts) { | ||
this.src = src; | ||
this.target = target; | ||
this.id = id; | ||
this.returnValue = returnValue; | ||
this.opts = opts || {}; | ||
} | ||
Checkpointer.prototype.writeCheckpoint = function (checkpoint, session) { | ||
var self = this; | ||
return this.updateTarget(checkpoint, session).then(function () { | ||
return self.updateSource(checkpoint, session); | ||
}); | ||
}; | ||
writeCheckpoint(checkpoint, session) { | ||
var self = this; | ||
return this.updateTarget(checkpoint, session).then(function () { | ||
return self.updateSource(checkpoint, session); | ||
}); | ||
} | ||
Checkpointer.prototype.updateTarget = function (checkpoint, session) { | ||
if (this.opts.writeTargetCheckpoint) { | ||
return updateCheckpoint(this.target, this.id, checkpoint, | ||
session, this.returnValue); | ||
} else { | ||
return Promise.resolve(true); | ||
updateTarget(checkpoint, session) { | ||
if (this.opts.writeTargetCheckpoint) { | ||
return updateCheckpoint(this.target, this.id, checkpoint, | ||
session, this.returnValue); | ||
} else { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
}; | ||
Checkpointer.prototype.updateSource = function (checkpoint, session) { | ||
if (this.opts.writeSourceCheckpoint) { | ||
updateSource(checkpoint, session) { | ||
if (this.opts.writeSourceCheckpoint) { | ||
var self = this; | ||
return updateCheckpoint(this.src, this.id, checkpoint, | ||
session, this.returnValue) | ||
.catch(function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return true; | ||
} | ||
throw err; | ||
}); | ||
} else { | ||
return Promise.resolve(true); | ||
} | ||
} | ||
getCheckpoint() { | ||
var self = this; | ||
return updateCheckpoint(this.src, this.id, checkpoint, | ||
session, this.returnValue) | ||
.catch(function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return true; | ||
if (self.opts && self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) { | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
return sourceDoc.last_seq || LOWEST_SEQ; | ||
}).catch(function (err) { | ||
/* istanbul ignore if */ | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
return self.target.get(self.id).then(function (targetDoc) { | ||
if (self.opts && self.opts.writeTargetCheckpoint && !self.opts.writeSourceCheckpoint) { | ||
return targetDoc.last_seq || LOWEST_SEQ; | ||
} | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
// Since we can't migrate an old version doc to a new one | ||
// (no session id), we just go with the lowest seq in this case | ||
/* istanbul ignore if */ | ||
if (targetDoc.version !== sourceDoc.version) { | ||
return LOWEST_SEQ; | ||
} | ||
var version; | ||
if (targetDoc.version) { | ||
version = targetDoc.version.toString(); | ||
} else { | ||
version = "undefined"; | ||
} | ||
if (version in comparisons) { | ||
return comparisons[version](targetDoc, sourceDoc); | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (err.status === 404 && targetDoc.last_seq) { | ||
return self.src.put({ | ||
_id: self.id, | ||
last_seq: LOWEST_SEQ | ||
}).then(function () { | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return targetDoc.last_seq; | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
throw err; | ||
}); | ||
} else { | ||
return Promise.resolve(true); | ||
}).catch(function (err) { | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
}; | ||
} | ||
@@ -134,67 +202,2 @@ var comparisons = { | ||
Checkpointer.prototype.getCheckpoint = function () { | ||
var self = this; | ||
if (self.opts && self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) { | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
return sourceDoc.last_seq || LOWEST_SEQ; | ||
}).catch(function (err) { | ||
/* istanbul ignore if */ | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
return self.target.get(self.id).then(function (targetDoc) { | ||
if (self.opts && self.opts.writeTargetCheckpoint && !self.opts.writeSourceCheckpoint) { | ||
return targetDoc.last_seq || LOWEST_SEQ; | ||
} | ||
return self.src.get(self.id).then(function (sourceDoc) { | ||
// Since we can't migrate an old version doc to a new one | ||
// (no session id), we just go with the lowest seq in this case | ||
/* istanbul ignore if */ | ||
if (targetDoc.version !== sourceDoc.version) { | ||
return LOWEST_SEQ; | ||
} | ||
var version; | ||
if (targetDoc.version) { | ||
version = targetDoc.version.toString(); | ||
} else { | ||
version = "undefined"; | ||
} | ||
if (version in comparisons) { | ||
return comparisons[version](targetDoc, sourceDoc); | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (err.status === 404 && targetDoc.last_seq) { | ||
return self.src.put({ | ||
_id: self.id, | ||
last_seq: LOWEST_SEQ | ||
}).then(function () { | ||
return LOWEST_SEQ; | ||
}, function (err) { | ||
if (isForbiddenError(err)) { | ||
self.opts.writeSourceCheckpoint = false; | ||
return targetDoc.last_seq; | ||
} | ||
/* istanbul ignore next */ | ||
return LOWEST_SEQ; | ||
}); | ||
} | ||
throw err; | ||
}); | ||
}).catch(function (err) { | ||
if (err.status !== 404) { | ||
throw err; | ||
} | ||
return LOWEST_SEQ; | ||
}); | ||
}; | ||
// This checkpoint comparison is ported from CouchDBs source | ||
@@ -269,2 +272,9 @@ // they come from here: | ||
function Checkpointer(src, target, id, returnValue, opts) { | ||
if (!(this instanceof CheckpointerInternal)) { | ||
return new CheckpointerInternal(src, target, id, returnValue, opts); | ||
} | ||
return Checkpointer; | ||
} | ||
module.exports = Checkpointer; |
{ | ||
"name": "pouchdb-checkpointer", | ||
"version": "7.3.1", | ||
"version": "8.0.0", | ||
"description": "PouchDB tool to write a checkpoint, e.g. during replication.", | ||
@@ -16,4 +16,4 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"pouchdb-collate": "7.3.1", | ||
"pouchdb-utils": "7.3.1" | ||
"pouchdb-collate": "8.0.0", | ||
"pouchdb-utils": "8.0.0" | ||
}, | ||
@@ -20,0 +20,0 @@ "module": "./lib/index.es.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28100
481
+ Addedpouchdb-binary-utils@8.0.0(transitive)
+ Addedpouchdb-collate@8.0.0(transitive)
+ Addedpouchdb-collections@8.0.0(transitive)
+ Addedpouchdb-errors@8.0.0(transitive)
+ Addedpouchdb-md5@8.0.0(transitive)
+ Addedpouchdb-utils@8.0.0(transitive)
- Removedargsarray@0.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedpouchdb-binary-utils@7.3.1(transitive)
- Removedpouchdb-collate@7.3.1(transitive)
- Removedpouchdb-collections@7.3.1(transitive)
- Removedpouchdb-errors@7.3.1(transitive)
- Removedpouchdb-md5@7.3.1(transitive)
- Removedpouchdb-utils@7.3.1(transitive)
Updatedpouchdb-collate@8.0.0
Updatedpouchdb-utils@8.0.0