pouchdb-validation
Advanced tools
Comparing version 1.1.6 to 1.2.0
203
index.js
@@ -21,28 +21,9 @@ /* | ||
var couchdb_objects = require("couchdb-objects"); | ||
var nodify = require("promise-nodify"); | ||
var wrappers = require("pouchdb-wrappers"); | ||
var createBulkDocsWrapper = require("pouchdb-bulkdocs-wrapper"); | ||
var PouchPluginError = require("pouchdb-plugin-error"); | ||
var uuid = require("node-uuid"); | ||
var uuid = require("random-uuid-v4"); | ||
var Promise = require("pouchdb-promise"); | ||
var PouchPluginError = require("pouchdb-plugin-error"); | ||
var dbs = []; | ||
var methodNames = ["put", "post", "remove", "bulkDocs", "putAttachment", "removeAttachment"]; | ||
var methodsByDbsIdx = []; | ||
function methods(db) { | ||
var index = dbs.indexOf(db); | ||
if (index === -1) { | ||
return methodsFromDb(db); | ||
} | ||
return methodsByDbsIdx[index]; | ||
} | ||
function methodsFromDb(db) { | ||
var meths = {}; | ||
methodNames.forEach(function (name) { | ||
meths[name] = db[name].bind(db); | ||
}); | ||
return meths; | ||
} | ||
function oldDoc(db, id) { | ||
@@ -101,7 +82,6 @@ return db.get(id, {revs: true}).catch(function () { | ||
return Promise.all([completeOptionsPromise, oldDocPromise]).then(function (args) { | ||
var completeOptions = args[0]; | ||
var oldDoc = args[1]; | ||
return validate(validationFuncs, newDoc, oldDoc, completeOptions); | ||
}); | ||
return Promise.all([completeOptionsPromise, oldDocPromise]) | ||
.then(Function.prototype.apply.bind(function (completeOptions, oldDoc) { | ||
return validate(validationFuncs, newDoc, oldDoc, completeOptions); | ||
}, null)); | ||
}); | ||
@@ -156,122 +136,66 @@ } | ||
function processArgs(db, callback, options) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} | ||
return { | ||
db: db, | ||
callback: callback, | ||
options: options | ||
}; | ||
} | ||
var wrapperApi = {}; | ||
exports.validatingPut = function (doc, options, callback) { | ||
var args = processArgs(this, callback, options); | ||
var promise = doValidation(args.db, doc, args.options).then(function () { | ||
return methods(args.db).put(doc, args.options); | ||
}); | ||
nodify(promise, args.callback); | ||
return promise; | ||
wrapperApi.put = function (orig, args) { | ||
return doValidation(args.db, args.doc, args.options).then(orig); | ||
}; | ||
exports.validatingPost = function (doc, options, callback) { | ||
var args = processArgs(this, callback, options); | ||
doc._id = doc._id || uuid.v4(); | ||
var promise = doValidation(args.db, doc, args.options).then(function () { | ||
return methods(args.db).post(doc, args.options); | ||
}); | ||
nodify(promise, args.callback); | ||
return promise; | ||
wrapperApi.post = function (orig, args) { | ||
args.doc._id = args.doc._id || uuid(); | ||
return doValidation(args.db, args.doc, args.options).then(orig); | ||
}; | ||
exports.validatingRemove = function (doc, options, callback) { | ||
var args = processArgs(this, callback, options); | ||
doc._deleted = true; | ||
var promise = doValidation(args.db, doc, args.options).then(function () { | ||
return methods(args.db).remove(doc, args.options); | ||
}); | ||
nodify(promise, args.callback); | ||
return promise; | ||
wrapperApi.remove = function (orig, args) { | ||
args.doc._deleted = true; | ||
return doValidation(args.db, args.doc, args.options).then(orig); | ||
}; | ||
exports.validatingBulkDocs = function (bulkDocs, options, callback) { | ||
//the ``all_or_nothing`` attribute on ``bulkDocs`` is unsupported. | ||
//Also, the result array might not be in the same order as | ||
//``bulkDocs.docs`` | ||
var args = processArgs(this, callback, options); | ||
wrapperApi.bulkDocs = createBulkDocsWrapper(function (doc, args) { | ||
doc._id = doc._id || uuid(); | ||
return doValidation(args.db, doc, args.options); | ||
}); | ||
var done = []; | ||
var notYetDone = []; | ||
wrapperApi.putAttachment = function (orig, args) { | ||
return args.db.get(args.docId, {rev: args.rev, revs: true}) | ||
.catch(function (err) { | ||
return {_id: args.docId}; | ||
}) | ||
.then(function (doc) { | ||
//validate the doc + attachment | ||
doc._attachments = doc._attachments || {}; | ||
doc._attachments[args.attachmentId] = { | ||
content_type: args.type, | ||
data: args.doc | ||
}; | ||
if (!Array.isArray(bulkDocs)) { | ||
bulkDocs = bulkDocs.docs; | ||
} | ||
var validations = bulkDocs.map(function (doc) { | ||
doc._id = doc._id || uuid.v4(); | ||
var validationPromise = doValidation(args.db, doc, args.options); | ||
return validationPromise.then(function (resp) { | ||
notYetDone.push(doc); | ||
}).catch(function (err) { | ||
err.id = doc._id; | ||
done.push(err); | ||
}); | ||
}); | ||
var allValidationsPromise = Promise.all(validations).then(function () { | ||
return methods(args.db).bulkDocs(notYetDone, args.options); | ||
}).then(function (insertedDocs) { | ||
return done.concat(insertedDocs); | ||
}); | ||
nodify(allValidationsPromise, args.callback); | ||
return allValidationsPromise; | ||
return doValidation(args.db, doc, args.options); | ||
}) | ||
.then(orig); | ||
}; | ||
var vpa = function (docId, attachmentId, rev, attachment, type, options, callback) { | ||
var args = processArgs(this, callback, options); | ||
wrapperApi.removeAttachment = function (orig, args) { | ||
return args.db.get(args.docId, {rev: args.rev, revs: true}) | ||
.then(function (doc) { | ||
//validate the doc without attachment | ||
delete doc._attachments[args.attachmentId]; | ||
//get the doc | ||
var promise = args.db.get(docId, {rev: rev, revs: true}).catch(function (err) { | ||
return {_id: docId}; | ||
}).then(function (doc) { | ||
//validate the doc + attachment | ||
doc._attachments = doc._attachments || {}; | ||
doc._attachments[attachmentId] = { | ||
content_type: type, | ||
data: attachment, | ||
}; | ||
return doValidation(args.db, doc, args.options); | ||
}).then(function () { | ||
//save the attachment | ||
return methods(args.db).putAttachment(docId, attachmentId, rev, attachment, type); | ||
}); | ||
nodify(promise, args.callback); | ||
return promise; | ||
return doValidation(args.db, doc, args.options); | ||
}) | ||
.then(orig); | ||
}; | ||
exports.validatingPutAttachment = vpa; | ||
var vra = function (docId, attachmentId, rev, options, callback) { | ||
var args = processArgs(this, callback, options); | ||
//get the doc | ||
var promise = args.db.get(docId, {rev: rev, revs: true}).then(function (doc) { | ||
//validate the doc without attachment | ||
delete doc._attachments[attachmentId]; | ||
Object.keys(wrapperApi).forEach(function (name) { | ||
var exportName = "validating" + name[0].toUpperCase() + name.substr(1); | ||
var orig = function () { | ||
return this[name].apply(this, arguments); | ||
}; | ||
exports[exportName] = wrappers.createWrapperMethod(name, orig, wrapperApi[name]); | ||
}); | ||
return doValidation(args.db, doc, args.options); | ||
}).then(function () { | ||
//remove the attachment | ||
return methods(args.db).removeAttachment(docId, attachmentId, rev); | ||
}); | ||
nodify(promise, args.callback); | ||
return promise; | ||
}; | ||
exports.validatingRemoveAttachment = vra; | ||
exports.installValidationMethods = function () { | ||
var db = this; | ||
if (dbs.indexOf(db) !== -1) { | ||
try { | ||
wrappers.installWrapperMethods(db, wrapperApi); | ||
} catch (err) { | ||
throw new PouchPluginError({ | ||
@@ -283,8 +207,2 @@ status: 500, | ||
} | ||
dbs.push(db); | ||
methodsByDbsIdx.push(methodsFromDb(db)); | ||
methodNames.forEach(function (name) { | ||
db[name] = exports["validating" + name[0].toUpperCase() + name.substr(1)].bind(db); | ||
}); | ||
}; | ||
@@ -295,4 +213,5 @@ | ||
var index = dbs.indexOf(db); | ||
if (index === -1) { | ||
try { | ||
wrappers.uninstallWrapperMethods(db, wrapperApi); | ||
} catch (err) { | ||
throw new PouchPluginError({ | ||
@@ -304,10 +223,2 @@ status: 500, | ||
} | ||
var meths = methods(db); | ||
methodNames.forEach(function (name) { | ||
db[name] = meths[name]; | ||
}); | ||
//cleanup | ||
dbs.splice(index, 1); | ||
methodsByDbsIdx.splice(index, 1); | ||
}; |
{ | ||
"name": "pouchdb-validation", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -26,6 +26,7 @@ "description": "A PouchDB plug-in that allows you to re-use your CouchDB validate_doc_update functions on the client side.", | ||
"couchdb-eval": "^1.0.0", | ||
"promise-nodify": "^1.0.0", | ||
"pouchdb-promise": "^0.0.0", | ||
"node-uuid": "^1.4.1", | ||
"pouchdb-plugin-error": "^0.2.0" | ||
"random-uuid-v4": "^0.0.4", | ||
"pouchdb-plugin-error": "^1.0.0", | ||
"pouchdb-wrappers": "^1.0.0", | ||
"pouchdb-bulkdocs-wrapper": "^1.0.0" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
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
8038
7
189
+ Addedpouchdb-wrappers@^1.0.0
+ Addedrandom-uuid-v4@^0.0.4
+ Addedpouchdb-bulkdocs-wrapper@1.0.2(transitive)
+ Addedpouchdb-wrappers@1.3.6(transitive)
- Removednode-uuid@^1.4.1
- Removedpromise-nodify@^1.0.0
- Removednode-uuid@1.4.8(transitive)
- Removedpouchdb-plugin-error@0.2.0(transitive)
Updatedpouchdb-plugin-error@^1.0.0