From 0e7c99952fe0b4c435a8b244e310a2cb29a373dc Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Mon, 28 Apr 2014 18:24:26 +0200 Subject: [PATCH 01/23] Converted all the JS to node modules. (Re)added geopouch. --- index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 00000000..1e4292fa --- /dev/null +++ b/index.js @@ -0,0 +1,17 @@ +"use strict"; + +//FIXME. +exports.updatingPut = function (doc, _id, _rev, options, callback) { + if (options.validation) { + db.validatingPut(); + } else { + db.put(); + } +}; +exports.updatingPost = function (doc, options, callback) { + if (options.validation) { + db.validatingPost(); + } else { + db.post(); + } +}; From 858ffb0df0be155fcf3be2985691a6b1d5b1fe7d Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Wed, 7 May 2014 14:34:17 +0200 Subject: [PATCH 02/23] Quite some stuff. --- index.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 1e4292fa..3a579976 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,84 @@ "use strict"; -//FIXME. -exports.updatingPut = function (doc, _id, _rev, options, callback) { - if (options.validation) { - db.validatingPut(); - } else { - db.put(); +//TODO: call http equivalent if http adapter + +var Promise = require("lie"); +var couchdb_objects = require("../couchdb-objects"); +var addCallback = require("../utils/promisewrapper.js"); +var coucheval = require("../utils/coucheval.js"); + +function doUpdating(methodName, db, query, options, callback) { + if (typeof options === "function") { + callback = options; + options = {}; } + + var designDocName = query.split("/")[0]; + var updateName = query.split("/")[1]; + var docId = query.split("/")[2]; + + //build request object + var infoPromise = db.info(); + var pathPromise = infoPromise.then(function (info) { + var path = [info.db_name, "_design", designDocName, "_update", updateName]; + if (docId) { + path.push(docId); + } + return path; + }); + var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db) + + //get the documents involved + var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { + if (!(designDoc.updates || {}).hasOwnProperty(updateName)) { + throw { + status: 404, + error: "not_found", + reason: "missing update function " + updateName + " on design doc _design/" + designDocName + }; + } + return designDoc; + }); + var docPromise = db.get(docId).catch(function () { + //doc might not exist - that's ok and expected. + return null; + }); + + var promise = Promise.all([reqPromise, ddocPromise, docPromise]).then(function (args) { + var req = args[0]; + var designDoc = args[1]; + var doc = args[2]; + + //run update function + var func = coucheval.evaluate(designDoc, {}, designDoc.updates[updateName]); + var result; + try { + result = func(doc, req); + } catch (e) { + throw coucheval.wrapExecutionError(e); + } + //save result if necessary + if (result[0] !== null) { + return doc["method"](result[0]).then(function () { + return result[1]; + }); + } + return result[1]; + }); + addCallback(promise, callback); + return promise; +} + +exports.updatingPut = function (doc, _id, _rev, options, callback) { + var db = this; + var methodName = options.withValidation ? "validatingPut" : "put"; + return doUpdating(methodName, db, query, options, callback); }; -exports.updatingPost = function (doc, options, callback) { - if (options.validation) { - db.validatingPost(); - } else { - db.post(); - } + +//TODO: Shouldn't this method add an 'id' itself if not already there +//and then afterwards just call exports.updatingPut? +exports.updatingPost = function (query, options, callback) { + var db = this; + var methodName = options.withValidation ? "validatingPost" : "post"; + return doUpdating(methodName, db, query, options, callback); }; From ebd9c465eb6b1526b50df56a0d1bb8d7993c93d5 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Wed, 7 May 2014 16:40:50 +0200 Subject: [PATCH 03/23] Updated node modules (PouchDB 2.2!). Made licensing (Apache v2.0) a bit clearer. --- index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3a579976..74f0e06a 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,19 @@ +/* + Copyright 2013-2014, Marten de Vries + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + "use strict"; //TODO: call http equivalent if http adapter @@ -26,7 +42,7 @@ function doUpdating(methodName, db, query, options, callback) { } return path; }); - var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db) + var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db); //get the documents involved var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { @@ -59,7 +75,7 @@ function doUpdating(methodName, db, query, options, callback) { } //save result if necessary if (result[0] !== null) { - return doc["method"](result[0]).then(function () { + return db[methodName](result[0]).then(function () { return result[1]; }); } @@ -69,7 +85,7 @@ function doUpdating(methodName, db, query, options, callback) { return promise; } -exports.updatingPut = function (doc, _id, _rev, options, callback) { +exports.updatingPut = function (query, options, callback) { var db = this; var methodName = options.withValidation ? "validatingPut" : "put"; return doUpdating(methodName, db, query, options, callback); From e563fe2d03a5f8f928d72d3d76e276ecb27eaf6c Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Thu, 8 May 2014 15:49:40 +0200 Subject: [PATCH 04/23] Fixed test regression on Ubuntu 12.04; switched to bluebird on node (and node only). --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 74f0e06a..e990a52f 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ //TODO: call http equivalent if http adapter -var Promise = require("lie"); +var Promise = require("bluebird"); var couchdb_objects = require("../couchdb-objects"); var addCallback = require("../utils/promisewrapper.js"); var coucheval = require("../utils/coucheval.js"); From dd296359ad3095703c2178df2a5028e28896b850 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Thu, 8 May 2014 19:50:18 +0200 Subject: [PATCH 05/23] All JS are now Node modules of their own, that could even be published to the registry if necessary. For now just there's a script that links them locally (and installs all their dependencies). --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index e990a52f..50b937dd 100644 --- a/index.js +++ b/index.js @@ -19,9 +19,9 @@ //TODO: call http equivalent if http adapter var Promise = require("bluebird"); -var couchdb_objects = require("../couchdb-objects"); -var addCallback = require("../utils/promisewrapper.js"); -var coucheval = require("../utils/coucheval.js"); +var couchdb_objects = require("couchdb-objects"); +var nodify = require("promise-nodify"); +var coucheval = require("couchdb-eval"); function doUpdating(methodName, db, query, options, callback) { if (typeof options === "function") { @@ -81,7 +81,7 @@ function doUpdating(methodName, db, query, options, callback) { } return result[1]; }); - addCallback(promise, callback); + nodify(promise, callback); return promise; } From d4caece314eef4fd8a1fb82d9c72412ee3ebc222 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Tue, 20 May 2014 14:01:52 +0200 Subject: [PATCH 06/23] All dev tools are now accessable via setup.py. Reduced code size of the plug-ins (the largest is now 4.1kb min+gzip). Added JS Coverage info. Some new tests. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 50b937dd..200c80c0 100644 --- a/index.js +++ b/index.js @@ -18,12 +18,12 @@ //TODO: call http equivalent if http adapter -var Promise = require("bluebird"); var couchdb_objects = require("couchdb-objects"); var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); function doUpdating(methodName, db, query, options, callback) { + var Promise = db.constructor.utils.Promise; if (typeof options === "function") { callback = options; options = {}; From a224938e205b9108b78fe40ee6da5f86367df11f Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Wed, 28 May 2014 15:28:36 +0200 Subject: [PATCH 07/23] JS coverage is back at 100%. Added viewCleanup() wrapper function. Removed unintentional side effects for some tests, and made them faster. Improved tests + .coveragerc file. Added httpQuery function to the pouchdb-update plug-in too. --- index.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 200c80c0..c29ef180 100644 --- a/index.js +++ b/index.js @@ -16,14 +16,12 @@ "use strict"; -//TODO: call http equivalent if http adapter - var couchdb_objects = require("couchdb-objects"); var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); +var httpQuery = require("couchdb-req-http-query"); function doUpdating(methodName, db, query, options, callback) { - var Promise = db.constructor.utils.Promise; if (typeof options === "function") { callback = options; options = {}; @@ -43,6 +41,21 @@ function doUpdating(methodName, db, query, options, callback) { return path; }); var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db); + return reqPromise.then(function (req) { + var promise; + if (["http", "https"].indexOf(db.type()) === -1) { + promise = offlineQuery(methodName, db, designDocName, updateName, docId, req, options); + } else { + promise = httpQuery(db, req); + } + + nodify(promise, callback); + return promise; + }); +} + +function offlineQuery(methodName, db, designDocName, updateName, docId, req, options) { + var Promise = db.constructor.utils.Promise; //get the documents involved var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { @@ -60,10 +73,9 @@ function doUpdating(methodName, db, query, options, callback) { return null; }); - var promise = Promise.all([reqPromise, ddocPromise, docPromise]).then(function (args) { - var req = args[0]; - var designDoc = args[1]; - var doc = args[2]; + return Promise.all([ddocPromise, docPromise]).then(function (args) { + var designDoc = args[0]; + var doc = args[1]; //run update function var func = coucheval.evaluate(designDoc, {}, designDoc.updates[updateName]); @@ -75,19 +87,18 @@ function doUpdating(methodName, db, query, options, callback) { } //save result if necessary if (result[0] !== null) { - return db[methodName](result[0]).then(function () { + return db[methodName](result[0], options).then(function () { return result[1]; }); } return result[1]; }); - nodify(promise, callback); - return promise; } exports.updatingPut = function (query, options, callback) { var db = this; var methodName = options.withValidation ? "validatingPut" : "put"; + options.method = "PUT"; return doUpdating(methodName, db, query, options, callback); }; @@ -96,5 +107,6 @@ exports.updatingPut = function (query, options, callback) { exports.updatingPost = function (query, options, callback) { var db = this; var methodName = options.withValidation ? "validatingPost" : "post"; + options.method = "POST"; return doUpdating(methodName, db, query, options, callback); }; From 29dd477cd85e60508b8f8bc3f62d4112fd093071 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Wed, 28 May 2014 20:29:23 +0200 Subject: [PATCH 08/23] Update plug-in now included as public method. More consistent errors. A few bug fixes. Code coverage to 100% (python, js); no more fixmes/todos. Pretty much only pouchdb.mapping is now left as experimental. --- index.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index c29ef180..e43dbd07 100644 --- a/index.js +++ b/index.js @@ -20,8 +20,9 @@ var couchdb_objects = require("couchdb-objects"); var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); var httpQuery = require("couchdb-req-http-query"); +var completeRespObj = require("couchdb-resp-completer"); -function doUpdating(methodName, db, query, options, callback) { +function doUpdating(db, query, options, callback) { if (typeof options === "function") { callback = options; options = {}; @@ -42,9 +43,12 @@ function doUpdating(methodName, db, query, options, callback) { }); var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db); return reqPromise.then(function (req) { + //the only option that isn't related to the request object. + delete req.withValidation; + var promise; if (["http", "https"].indexOf(db.type()) === -1) { - promise = offlineQuery(methodName, db, designDocName, updateName, docId, req, options); + promise = offlineQuery(db, designDocName, updateName, docId, req, options); } else { promise = httpQuery(db, req); } @@ -54,7 +58,7 @@ function doUpdating(methodName, db, query, options, callback) { }); } -function offlineQuery(methodName, db, designDocName, updateName, docId, req, options) { +function offlineQuery(db, designDocName, updateName, docId, req, options) { var Promise = db.constructor.utils.Promise; //get the documents involved @@ -62,8 +66,8 @@ function offlineQuery(methodName, db, designDocName, updateName, docId, req, opt if (!(designDoc.updates || {}).hasOwnProperty(updateName)) { throw { status: 404, - error: "not_found", - reason: "missing update function " + updateName + " on design doc _design/" + designDocName + name: "not_found", + message: "missing update function " + updateName + " on design doc _design/" + designDocName }; } return designDoc; @@ -85,28 +89,26 @@ function offlineQuery(methodName, db, designDocName, updateName, docId, req, opt } catch (e) { throw coucheval.wrapExecutionError(e); } + var resp = completeRespObj(result[1]); //save result if necessary - if (result[0] !== null) { - return db[methodName](result[0], options).then(function () { - return result[1]; - }); + if (result[0] === null) { + return resp; } - return result[1]; + var methodName = options.withValidation ? "validatingPut" : "put"; + return db[methodName](result[0], options).then(function () { + return resp; + }); }); } exports.updatingPut = function (query, options, callback) { var db = this; - var methodName = options.withValidation ? "validatingPut" : "put"; options.method = "PUT"; - return doUpdating(methodName, db, query, options, callback); + return doUpdating(db, query, options, callback); }; -//TODO: Shouldn't this method add an 'id' itself if not already there -//and then afterwards just call exports.updatingPut? exports.updatingPost = function (query, options, callback) { var db = this; - var methodName = options.withValidation ? "validatingPost" : "post"; options.method = "POST"; - return doUpdating(methodName, db, query, options, callback); + return doUpdating(db, query, options, callback); }; From 6355916ff6b7f4331af0982a1ac6314d9d6a7133 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Fri, 30 May 2014 19:02:05 +0200 Subject: [PATCH 09/23] Python-PouchDB 0.4 - added a single mapping test & added some info on the node modules. --- README | 7 +++++++ index.js | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 00000000..1d1480ac --- /dev/null +++ b/README @@ -0,0 +1,7 @@ +pouchdb-update +============== + +A PouchDB plug-in that allows you to re-use your CouchDB update +functions on the client side. A browser version is available. + +[Website of this plug-in and a few others](http://python-pouchdb.marten-de-vries.nl/plugins.html) diff --git a/index.js b/index.js index e43dbd07..64965701 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ var couchdb_objects = require("couchdb-objects"); var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); -var httpQuery = require("couchdb-req-http-query"); +var httpQuery = require("pouchdb-req-http-query"); var completeRespObj = require("couchdb-resp-completer"); function doUpdating(db, query, options, callback) { From 9cd5fa3e08bc36833ccfa31e001bcd4eb38111da Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Sat, 31 May 2014 21:43:39 +0200 Subject: [PATCH 10/23] Adds NodeJS tests. Some small fixes. --- index.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 64965701..48647475 100644 --- a/index.js +++ b/index.js @@ -22,11 +22,12 @@ var coucheval = require("couchdb-eval"); var httpQuery = require("pouchdb-req-http-query"); var completeRespObj = require("couchdb-resp-completer"); -function doUpdating(db, query, options, callback) { - if (typeof options === "function") { +function doUpdating(method, db, query, options, callback) { + if (["function", "undefined"].indexOf(typeof options) !== -1) { callback = options; options = {}; } + options.method = method; var designDocName = query.split("/")[0]; var updateName = query.split("/")[1]; @@ -85,30 +86,29 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { var func = coucheval.evaluate(designDoc, {}, designDoc.updates[updateName]); var result; try { - result = func(doc, req); + result = func.call(designDoc, doc, req); } catch (e) { throw coucheval.wrapExecutionError(e); } - var resp = completeRespObj(result[1]); - //save result if necessary + var savePromise; + //save result[0] if necessary if (result[0] === null) { - return resp; + savePromise = Promise.resolve(); + } else { + var methodName = options.withValidation ? "validatingPut" : "put"; + savePromise = db[methodName](result[0], options); } - var methodName = options.withValidation ? "validatingPut" : "put"; - return db[methodName](result[0], options).then(function () { - return resp; + //then return the result + return savePromise.then(function () { + return completeRespObj(result[1]); }); }); } exports.updatingPut = function (query, options, callback) { - var db = this; - options.method = "PUT"; - return doUpdating(db, query, options, callback); + return doUpdating("PUT", this, query, options, callback); }; exports.updatingPost = function (query, options, callback) { - var db = this; - options.method = "POST"; - return doUpdating(db, query, options, callback); + return doUpdating("POST", this, query, options, callback); }; From fb5c4db774bba60dd3d761e2d1493858601d70c4 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Mon, 2 Jun 2014 15:13:30 +0200 Subject: [PATCH 11/23] A start for a pouchdb-rewrite plug-in. Some small other changes along the way. --- index.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 48647475..1cec9190 100644 --- a/index.js +++ b/index.js @@ -22,31 +22,34 @@ var coucheval = require("couchdb-eval"); var httpQuery = require("pouchdb-req-http-query"); var completeRespObj = require("couchdb-resp-completer"); -function doUpdating(method, db, query, options, callback) { +exports.update = function (query, options, callback) { if (["function", "undefined"].indexOf(typeof options) !== -1) { callback = options; options = {}; } - options.method = method; + var db = this; + + //better default than GET. + options.method = options.method || "POST"; var designDocName = query.split("/")[0]; var updateName = query.split("/")[1]; var docId = query.split("/")[2]; //build request object - var infoPromise = db.info(); - var pathPromise = infoPromise.then(function (info) { - var path = [info.db_name, "_design", designDocName, "_update", updateName]; - if (docId) { - path.push(docId); - } - return path; - }); - var reqPromise = couchdb_objects.buildRequestObject(options, pathPromise, infoPromise, db); + var pathEnd = ["_design", designDocName, "_update", updateName]; + if (docId) { + pathEnd.push.apply(pathEnd, docId.split("/")); + } + var reqPromise = couchdb_objects.buildRequestObject(db, pathEnd, options); return reqPromise.then(function (req) { //the only option that isn't related to the request object. delete req.withValidation; + //because we might have set method -> POST, also set a Content-Type + //to prevent a Qt warning in case there isn't one. + req.headers["Content-Type"] = req.headers["Content-Type"] || "application/x-www-form-urlencoded"; + var promise; if (["http", "https"].indexOf(db.type()) === -1) { promise = offlineQuery(db, designDocName, updateName, docId, req, options); @@ -57,7 +60,7 @@ function doUpdating(method, db, query, options, callback) { nodify(promise, callback); return promise; }); -} +}; function offlineQuery(db, designDocName, updateName, docId, req, options) { var Promise = db.constructor.utils.Promise; @@ -104,11 +107,3 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { }); }); } - -exports.updatingPut = function (query, options, callback) { - return doUpdating("PUT", this, query, options, callback); -}; - -exports.updatingPost = function (query, options, callback) { - return doUpdating("POST", this, query, options, callback); -}; From 667fb7ed76b5eabd21399343a9d3c46f84a71783 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Tue, 3 Jun 2014 21:45:43 +0200 Subject: [PATCH 12/23] Ported part of the rewrite test suite over from CouchDB, which found small bugs in the rewrite, list & update plug-ins. Some minor other changes. --- index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1cec9190..37255890 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,8 @@ exports.update = function (query, options, callback) { //because we might have set method -> POST, also set a Content-Type //to prevent a Qt warning in case there isn't one. - req.headers["Content-Type"] = req.headers["Content-Type"] || "application/x-www-form-urlencoded"; + var h = req.headers; + h["Content-Type"] = h["Content-Type"] || "application/x-www-form-urlencoded"; var promise; if (["http", "https"].indexOf(db.type()) === -1) { @@ -96,14 +97,18 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { var savePromise; //save result[0] if necessary if (result[0] === null) { - savePromise = Promise.resolve(); + savePromise = Promise.resolve(200); } else { var methodName = options.withValidation ? "validatingPut" : "put"; - savePromise = db[methodName](result[0], options); + savePromise = db[methodName](result[0], options).then(function () { + return 201; + }); } //then return the result - return savePromise.then(function () { - return completeRespObj(result[1]); + return savePromise.then(function (status) { + var resp = completeRespObj(result[1]); + resp.code = status; + return resp; }); }); } From 80285d9bb079e7797fb3a604b11adce0ed065ec1 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Wed, 4 Jun 2014 20:28:02 +0200 Subject: [PATCH 13/23] Python-PouchDB 0.4.1 - plug-in documentation + JS coverage back at 100% --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 37255890..1d307d4f 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ var coucheval = require("couchdb-eval"); var httpQuery = require("pouchdb-req-http-query"); var completeRespObj = require("couchdb-resp-completer"); -exports.update = function (query, options, callback) { +exports.update = function (updatePath, options, callback) { if (["function", "undefined"].indexOf(typeof options) !== -1) { callback = options; options = {}; @@ -32,9 +32,9 @@ exports.update = function (query, options, callback) { //better default than GET. options.method = options.method || "POST"; - var designDocName = query.split("/")[0]; - var updateName = query.split("/")[1]; - var docId = query.split("/")[2]; + var designDocName = updatePath.split("/")[0]; + var updateName = updatePath.split("/")[1]; + var docId = updatePath.split("/")[2]; //build request object var pathEnd = ["_design", designDocName, "_update", updateName]; From 50b2d0076a9f098ea8dfc283a2ce27d3c1e38964 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Thu, 5 Jun 2014 10:58:22 +0200 Subject: [PATCH 14/23] Gave node modules version numbers and published them on npm, small fix to the nodejs test suite, some documentation changes. --- README | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 README diff --git a/README b/README deleted file mode 100644 index 1d1480ac..00000000 --- a/README +++ /dev/null @@ -1,7 +0,0 @@ -pouchdb-update -============== - -A PouchDB plug-in that allows you to re-use your CouchDB update -functions on the client side. A browser version is available. - -[Website of this plug-in and a few others](http://python-pouchdb.marten-de-vries.nl/plugins.html) From 0efe492184296a07a773b1047fe712b7966c59f1 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Fri, 6 Jun 2014 18:12:39 +0200 Subject: [PATCH 15/23] Some minor JS stuff mostly. --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1d307d4f..fe50bd6e 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,8 @@ "use strict"; +var Promise = require("pouchdb-promise"); + var couchdb_objects = require("couchdb-objects"); var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); @@ -64,8 +66,6 @@ exports.update = function (updatePath, options, callback) { }; function offlineQuery(db, designDocName, updateName, docId, req, options) { - var Promise = db.constructor.utils.Promise; - //get the documents involved var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { if (!(designDoc.updates || {}).hasOwnProperty(updateName)) { From 98f484a22d6b5e8259d2bdc033c29383d2691430 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Sun, 6 Jul 2014 14:28:36 +0200 Subject: [PATCH 16/23] Improved docs + JS --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fe50bd6e..04fde6e7 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ var nodify = require("promise-nodify"); var coucheval = require("couchdb-eval"); var httpQuery = require("pouchdb-req-http-query"); var completeRespObj = require("couchdb-resp-completer"); +var PouchPluginError = require('pouchdb-plugin-error'); exports.update = function (updatePath, options, callback) { if (["function", "undefined"].indexOf(typeof options) !== -1) { @@ -69,11 +70,11 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { //get the documents involved var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { if (!(designDoc.updates || {}).hasOwnProperty(updateName)) { - throw { + throw new PouchPluginError({ status: 404, name: "not_found", message: "missing update function " + updateName + " on design doc _design/" + designDocName - }; + }); } return designDoc; }); From 3d2fb9116c13d99c04ff0a6a9ec547b7d4812356 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Tue, 12 Aug 2014 21:59:58 +0200 Subject: [PATCH 17/23] New: PouchDB Seamless Auth; PouchDB Security. Lots of fixes. PouchDB 3.0. --- index.js | 66 ++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index 04fde6e7..89812141 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,7 @@ exports.update = function (updatePath, options, callback) { pathEnd.push.apply(pathEnd, docId.split("/")); } var reqPromise = couchdb_objects.buildRequestObject(db, pathEnd, options); - return reqPromise.then(function (req) { + var promise = reqPromise.then(function (req) { //the only option that isn't related to the request object. delete req.withValidation; @@ -54,16 +54,14 @@ exports.update = function (updatePath, options, callback) { var h = req.headers; h["Content-Type"] = h["Content-Type"] || "application/x-www-form-urlencoded"; - var promise; if (["http", "https"].indexOf(db.type()) === -1) { - promise = offlineQuery(db, designDocName, updateName, docId, req, options); + return offlineQuery(db, designDocName, updateName, docId, req, options); } else { - promise = httpQuery(db, req); + return httpQuery(db, req); } - - nodify(promise, callback); - return promise; }); + nodify(promise, callback); + return promise; }; function offlineQuery(db, designDocName, updateName, docId, req, options) { @@ -83,33 +81,31 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { return null; }); - return Promise.all([ddocPromise, docPromise]).then(function (args) { - var designDoc = args[0]; - var doc = args[1]; - - //run update function - var func = coucheval.evaluate(designDoc, {}, designDoc.updates[updateName]); - var result; - try { - result = func.call(designDoc, doc, req); - } catch (e) { - throw coucheval.wrapExecutionError(e); - } - var savePromise; - //save result[0] if necessary - if (result[0] === null) { - savePromise = Promise.resolve(200); - } else { - var methodName = options.withValidation ? "validatingPut" : "put"; - savePromise = db[methodName](result[0], options).then(function () { - return 201; + return Promise.all([ddocPromise, docPromise]) + .then(Function.prototype.apply.bind(function (designDoc, doc) { + //run update function + var func = coucheval.evaluate(designDoc, {}, designDoc.updates[updateName]); + var result; + try { + result = func.call(designDoc, doc, req); + } catch (e) { + throw coucheval.wrapExecutionError(e); + } + var savePromise; + //save result[0] if necessary + if (result[0] === null) { + savePromise = Promise.resolve(200); + } else { + var methodName = options.withValidation ? "validatingPut" : "put"; + savePromise = db[methodName](result[0], options).then(function () { + return 201; + }); + } + //then return the result + return savePromise.then(function (status) { + var resp = completeRespObj(result[1]); + resp.code = status; + return resp; }); - } - //then return the result - return savePromise.then(function (status) { - var resp = completeRespObj(result[1]); - resp.code = status; - return resp; - }); - }); + }, null)); } From ce57b9df8cf086b34b8846ebe6d3295248271e75 Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Tue, 13 Jan 2015 11:19:46 +0100 Subject: [PATCH 18/23] Show/list/update fixes against couchdb-harness --- index.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 89812141..26ad5bd8 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ exports.update = function (updatePath, options, callback) { var designDocName = updatePath.split("/")[0]; var updateName = updatePath.split("/")[1]; - var docId = updatePath.split("/")[2]; + var docId = updatePath.split("/").slice(2).join("/"); //build request object var pathEnd = ["_design", designDocName, "_update", updateName]; @@ -65,6 +65,14 @@ exports.update = function (updatePath, options, callback) { }; function offlineQuery(db, designDocName, updateName, docId, req, options) { + if (req.method === "GET") { + return Promise.reject(new PouchPluginError({ + status: 500, // should be 405, but for CouchDB compatibility... + name: "method_not_allowed", + message: "Update functions do not allow GET" + })); + } + //get the documents involved var ddocPromise = db.get("_design/" + designDocName).then(function (designDoc) { if (!(designDoc.updates || {}).hasOwnProperty(updateName)) { @@ -91,21 +99,26 @@ function offlineQuery(db, designDocName, updateName, docId, req, options) { } catch (e) { throw coucheval.wrapExecutionError(e); } - var savePromise; + var code = (result[1] || {}).code; + var couchResp = completeRespObj(result[1]); + function setCode(proposedCode) { + couchResp.code = code || proposedCode; + } //save result[0] if necessary + var savePromise = Promise.resolve(); if (result[0] === null) { - savePromise = Promise.resolve(200); + setCode(200); } else { var methodName = options.withValidation ? "validatingPut" : "put"; - savePromise = db[methodName](result[0], options).then(function () { - return 201; + savePromise = db[methodName](result[0]).then(function (resp) { + couchResp.headers['X-Couch-Id'] = resp.id; + couchResp.headers['X-Couch-Update-NewRev'] = resp.rev; + setCode(201); }); } //then return the result - return savePromise.then(function (status) { - var resp = completeRespObj(result[1]); - resp.code = status; - return resp; + return savePromise.then(function () { + return couchResp; }); }, null)); } From a4993bd8a9e32e5a464929b723afb9b2b7c9133b Mon Sep 17 00:00:00 2001 From: Marten de Vries Date: Mon, 21 Dec 2015 19:53:37 +0100 Subject: [PATCH 19/23] (pouchdb/express-pouchdb#232) - Modernize pouchdb-update --- test/features.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++ test/http.js | 17 ++++++++++ test/signatures.js | 15 +++++++++ test/utils.js | 23 +++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 test/features.js create mode 100644 test/http.js create mode 100644 test/signatures.js create mode 100644 test/utils.js diff --git a/test/features.js b/test/features.js new file mode 100644 index 00000000..6ec6d8b4 --- /dev/null +++ b/test/features.js @@ -0,0 +1,84 @@ +import {setup, setupWithDoc, teardown, updateDocument, should, shouldThrowError} from './utils'; + +let db; + +describe('Sync update tests', () => { + beforeEach(async () => { + db = (await setupWithDoc()).db; + await db.put(updateDocument); + }); + afterEach(teardown); + + it('args', async () => { + const resp = await db.update('test/args/mytest', {query: {'a': 3}}) + const [doc, req] = JSON.parse(resp.body); + doc.test.should.be.ok; + req.id.should.equal('mytest'); + req.raw_path.should.equal('/test/_design/test/_update/args/mytest?a=3'); + }); + + it('args without doc', async () => { + const resp = await db.update('test/args', {withValidation: true}); + + const [doc, req] = JSON.parse(resp.body); + should.equal(doc, null); + req.should.not.have.property('withValidation'); + }); + + it('unexisting function', async () => { + const err = await shouldThrowError(async () => { + await db.update('test/unexisting/mytest'); + }); + err.toString().should.be.ok; + err.name.should.equal('not_found'); + err.message.should.equal('missing update function unexisting on design doc _design/test') + }); + + it('saving', async () => { + const resp = await db.update('test/save-adding-date', {body: JSON.stringify({ + _id: 'test', + name: 'Today' + })}); + resp.body.should.equal('Hello World!'); + + const doc = await db.get('test'); + doc.updated.should.be.ok; + doc.name.should.equal('Today'); + }); +}); + +describe('Async update tests', () => { + beforeEach(done => { + db = setup(); + db.put(updateDocument, done); + }); + afterEach(teardown); + + it('exception', done => { + db.update('test/exception', err => { + err.status.should.equal(500); + err.name.should.equal('ReferenceError'); + err.message.should.contain('abc'); + + done(); + }); + }); +}); + +describe('Async update with empty design doc', () => { + beforeEach(done => { + db = setup(); + db.put({_id: '_design/test'}, done); + }); + afterEach(teardown); + + it('basic', done => { + db.update('test/unexisting', err => { + err.status.should.equal(404); + err.name.should.equal('not_found'); + err.message.should.equal('missing update function unexisting on design doc _design/test'); + + done(); + }); + }); +}); diff --git a/test/http.js b/test/http.js new file mode 100644 index 00000000..2204be6a --- /dev/null +++ b/test/http.js @@ -0,0 +1,17 @@ +import {setupHTTP, teardown, updateDocument, should} from './utils'; + +let db; + +describe('http tests', () => { + beforeEach(async () => { + db = setupHTTP(); + await db.put(updateDocument); + }) + afterEach(teardown); + + it('update', async () => { + const [doc, req] = JSON.parse((await db.update('test/args/my-id')).body); + should.not.exist(doc); + req.id.should.equal('my-id'); + }); +}); diff --git a/test/signatures.js b/test/signatures.js new file mode 100644 index 00000000..e8de55f8 --- /dev/null +++ b/test/signatures.js @@ -0,0 +1,15 @@ +import {setup, teardown} from './utils'; + +describe('signature tests', () => { + let db; + beforeEach(() => { + db = setup(); + }); + afterEach(teardown); + + it('update', () => { + const promise = db.update('test/test/test', () => {}); + promise.then.should.be.ok; + promise.catch.should.be.ok; + }); +}); diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 00000000..9dab0a23 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,23 @@ +import stuff from 'pouchdb-plugin-helper/testutils'; +import Update from '../'; + +stuff.PouchDB.plugin(Update); + +stuff.updateDocument = { + _id: "_design/test", + updates: { + args: `function (doc, req) { + return [null, toJSON([doc, req])]; + }`, + exception: `function (doc, req) { + return abc; + }`, + 'save-adding-date': `function (oldDoc, req) { + var doc = JSON.parse(req.body); + doc.updated = new Date(); + return [doc, "Hello World!"]; + }` + } +} + +module.exports = stuff; From 9c7e499245f2f773d244973aca4063cc06d42258 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 3 Jul 2017 15:33:30 -0700 Subject: [PATCH 20/23] pouchdb-update: post rebase fixes --- .../node_modules/pouchdb-update/README.md | 9 +++++ .../node_modules/pouchdb-update/index.js | 0 .../node_modules/pouchdb-update/package.json | 38 +++++++++++++++++++ {test => tests/pouchdb-update}/features.js | 0 {test => tests/pouchdb-update}/http.js | 0 {test => tests/pouchdb-update}/signatures.js | 0 {test => tests/pouchdb-update}/utils.js | 0 7 files changed, 47 insertions(+) create mode 100644 packages/node_modules/pouchdb-update/README.md rename index.js => packages/node_modules/pouchdb-update/index.js (100%) create mode 100644 packages/node_modules/pouchdb-update/package.json rename {test => tests/pouchdb-update}/features.js (100%) rename {test => tests/pouchdb-update}/http.js (100%) rename {test => tests/pouchdb-update}/signatures.js (100%) rename {test => tests/pouchdb-update}/utils.js (100%) diff --git a/packages/node_modules/pouchdb-update/README.md b/packages/node_modules/pouchdb-update/README.md new file mode 100644 index 00000000..9898a44c --- /dev/null +++ b/packages/node_modules/pouchdb-update/README.md @@ -0,0 +1,9 @@ +pouchdb-update +============== + +A PouchDB plug-in that allows you to re-use your CouchDB update +functions on the client side. A browser version is available. + +See also [pouchdb-update's documentation](http://pythonhosted.org/Python-PouchDB/js-plugins.html#pouchdb-update-plug-in) + +[Website of this plug-in and a few others](http://python-pouchdb.marten-de-vries.nl/plugins.html) diff --git a/index.js b/packages/node_modules/pouchdb-update/index.js similarity index 100% rename from index.js rename to packages/node_modules/pouchdb-update/index.js diff --git a/packages/node_modules/pouchdb-update/package.json b/packages/node_modules/pouchdb-update/package.json new file mode 100644 index 00000000..ed94bb8b --- /dev/null +++ b/packages/node_modules/pouchdb-update/package.json @@ -0,0 +1,38 @@ +{ + "name": "pouchdb-update", + "version": "1.0.8", + "main": "index.js", + "description": "A PouchDB plug-in that allows you to re-use your CouchDB update functions on the client side.", + "repository": { + "type": "git", + "url": "https://github.com/pouchdb/pouchdb-update.git" + }, + "keywords": [ + "pouch", + "pouchdb", + "couch", + "couchdb", + "update", + "design", + "handler" + ], + "license": "Apache-2.0", + "author": "Marten de Vries", + "dependencies": { + "couchdb-objects": "^1.0.0", + "couchdb-eval": "^1.0.0", + "promise-nodify": "^1.0.0", + "pouchdb-req-http-query": "^1.0.3", + "couchdb-resp-completer": "^1.0.0", + "pouchdb-promise": "^0.0.0", + "pouchdb-plugin-error": "^1.0.0" + }, + "devDependencies": { + "pouchdb-plugin-helper": "^3.0.0" + }, + "scripts": { + "helper": "./node_modules/.bin/pouchdb-plugin-helper", + "test": "npm run helper -- test", + "build": "npm run helper -- build Update" + } +} diff --git a/test/features.js b/tests/pouchdb-update/features.js similarity index 100% rename from test/features.js rename to tests/pouchdb-update/features.js diff --git a/test/http.js b/tests/pouchdb-update/http.js similarity index 100% rename from test/http.js rename to tests/pouchdb-update/http.js diff --git a/test/signatures.js b/tests/pouchdb-update/signatures.js similarity index 100% rename from test/signatures.js rename to tests/pouchdb-update/signatures.js diff --git a/test/utils.js b/tests/pouchdb-update/utils.js similarity index 100% rename from test/utils.js rename to tests/pouchdb-update/utils.js From d2deafadfa85a1f9878ad36c04433d7be3ce3c61 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 3 Jul 2017 15:34:50 -0700 Subject: [PATCH 21/23] pouchdb-update: adapt package.json --- .../node_modules/pouchdb-update/package.json | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/packages/node_modules/pouchdb-update/package.json b/packages/node_modules/pouchdb-update/package.json index ed94bb8b..4a2153fc 100644 --- a/packages/node_modules/pouchdb-update/package.json +++ b/packages/node_modules/pouchdb-update/package.json @@ -1,11 +1,10 @@ { "name": "pouchdb-update", - "version": "1.0.8", "main": "index.js", "description": "A PouchDB plug-in that allows you to re-use your CouchDB update functions on the client side.", "repository": { "type": "git", - "url": "https://github.com/pouchdb/pouchdb-update.git" + "url": "https://github.com/pouchdb/pouchdb-server.git" }, "keywords": [ "pouch", @@ -17,22 +16,5 @@ "handler" ], "license": "Apache-2.0", - "author": "Marten de Vries", - "dependencies": { - "couchdb-objects": "^1.0.0", - "couchdb-eval": "^1.0.0", - "promise-nodify": "^1.0.0", - "pouchdb-req-http-query": "^1.0.3", - "couchdb-resp-completer": "^1.0.0", - "pouchdb-promise": "^0.0.0", - "pouchdb-plugin-error": "^1.0.0" - }, - "devDependencies": { - "pouchdb-plugin-helper": "^3.0.0" - }, - "scripts": { - "helper": "./node_modules/.bin/pouchdb-plugin-helper", - "test": "npm run helper -- test", - "build": "npm run helper -- build Update" - } + "author": "Marten de Vries" } From 29ab32e94ab46ccf1aab562f30b0fce877ba38e0 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 3 Jul 2017 15:34:58 -0700 Subject: [PATCH 22/23] pouchdb-update: move dependencies into mono repo --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 61f6eb4c..fae7557a 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "cookie-parser": "^1.4.3", "corser": "~2.0.0", "couchdb-calculate-session-id": "^1.1.0", + "couchdb-eval": "^1.0.0", "couchdb-harness": "*", "couchdb-log-parse": "^0.0.4", "couchdb-objects": "^1.0.0", @@ -58,6 +59,7 @@ "pouchdb-promise": "^6.1.2", "pouchdb-replication": "^6.1.0", "pouchdb-req-http-query": "^1.0.3", + "couchdb-resp-completer": "^1.0.0", "pouchdb-rewrite": "^1.0.7", "pouchdb-show": "^1.0.8", "pouchdb-size": "^1.2.2", From ee02cea2f816badc9dc740023dcc11d229300106 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 3 Jul 2017 15:54:34 -0700 Subject: [PATCH 23/23] pouchdb-update: adapt tests --- tests/pouchdb-update/features.js | 122 ++++++++++++++++++----------- tests/pouchdb-update/http.js | 20 +++-- tests/pouchdb-update/signatures.js | 2 +- tests/pouchdb-update/utils.js | 34 ++++---- 4 files changed, 106 insertions(+), 72 deletions(-) diff --git a/tests/pouchdb-update/features.js b/tests/pouchdb-update/features.js index 6ec6d8b4..69de6b21 100644 --- a/tests/pouchdb-update/features.js +++ b/tests/pouchdb-update/features.js @@ -1,84 +1,114 @@ -import {setup, setupWithDoc, teardown, updateDocument, should, shouldThrowError} from './utils'; - -let db; +const {setup, setupWithDoc, teardown, updateDocument, should, shouldThrowError} = require('./utils'); describe('Sync update tests', () => { - beforeEach(async () => { - db = (await setupWithDoc()).db; - await db.put(updateDocument); + let db; + + beforeEach(() => { + return setupWithDoc() + + .then((result) => { + db = result.db; + return db.put(updateDocument); + }); }); afterEach(teardown); - it('args', async () => { - const resp = await db.update('test/args/mytest', {query: {'a': 3}}) - const [doc, req] = JSON.parse(resp.body); - doc.test.should.be.ok; - req.id.should.equal('mytest'); - req.raw_path.should.equal('/test/_design/test/_update/args/mytest?a=3'); + it('args', () => { + return db.update('test/args/mytest', {query: {'a': 3}}) + + .then((response) => { + const [doc, req] = JSON.parse(response.body); + doc.test.should.be.ok; + req.id.should.equal('mytest'); + req.raw_path.should.equal('/test/_design/test/_update/args/mytest?a=3'); + }); }); - it('args without doc', async () => { - const resp = await db.update('test/args', {withValidation: true}); + it('args without doc', () => { + return db.update('test/args', {withValidation: true}) - const [doc, req] = JSON.parse(resp.body); - should.equal(doc, null); - req.should.not.have.property('withValidation'); + .then((response) => { + const [doc, req] = JSON.parse(response.body); + should.equal(doc, null); + req.should.not.have.property('withValidation'); + }); }); - it('unexisting function', async () => { - const err = await shouldThrowError(async () => { - await db.update('test/unexisting/mytest'); + it('missing function', () => { + shouldThrowError(() => { + return db.update('test/missing/mytest'); + }) + + .then((error) => { + error.toString().should.be.ok; + error.name.should.equal('not_found'); + error.message.should.equal('missing update function missing on design doc _design/test'); }); - err.toString().should.be.ok; - err.name.should.equal('not_found'); - err.message.should.equal('missing update function unexisting on design doc _design/test') }); - it('saving', async () => { - const resp = await db.update('test/save-adding-date', {body: JSON.stringify({ + it('saving', () => { + db.update('test/save-adding-date', {body: JSON.stringify({ _id: 'test', name: 'Today' - })}); - resp.body.should.equal('Hello World!'); + })}) - const doc = await db.get('test'); - doc.updated.should.be.ok; - doc.name.should.equal('Today'); + .then((response) => { + response.body.should.equal('Hello World!'); + + return db.get('test'); + }) + + .then((doc) => { + doc.updated.should.be.ok; + doc.name.should.equal('Today'); + }); }); }); describe('Async update tests', () => { - beforeEach(done => { + let db; + + beforeEach(() => { db = setup(); - db.put(updateDocument, done); + return db.put(updateDocument); }); afterEach(teardown); - it('exception', done => { - db.update('test/exception', err => { - err.status.should.equal(500); - err.name.should.equal('ReferenceError'); - err.message.should.contain('abc'); + it('exception', () => { + return db.update('test/exception') - done(); + .then(() => { + 'db.update("test/exception") should not resolve'.should.equal(''); + }) + + .catch((error) => { + error.status.should.equal(500); + error.name.should.equal('ReferenceError'); + error.message.should.contain('abc'); }); }); }); describe('Async update with empty design doc', () => { - beforeEach(done => { + let db; + + beforeEach(() => { db = setup(); - db.put({_id: '_design/test'}, done); + return db.put({_id: '_design/test'}); }); afterEach(teardown); - it('basic', done => { - db.update('test/unexisting', err => { - err.status.should.equal(404); - err.name.should.equal('not_found'); - err.message.should.equal('missing update function unexisting on design doc _design/test'); + it('basic', () => { + return db.update('test/missing') + + .then(() => { + 'db.update("test/missing") should not resolve'.should.equal(''); + }) - done(); + .catch((error) => { + error.status.should.equal(404); + error.name.should.equal('not_found'); + error.message.should.equal('missing update function missing on design doc _design/test'); }); }); }); diff --git a/tests/pouchdb-update/http.js b/tests/pouchdb-update/http.js index 2204be6a..b411c57a 100644 --- a/tests/pouchdb-update/http.js +++ b/tests/pouchdb-update/http.js @@ -1,17 +1,21 @@ -import {setupHTTP, teardown, updateDocument, should} from './utils'; +const {setupHTTP, teardown, updateDocument, should} = require('./utils'); let db; describe('http tests', () => { - beforeEach(async () => { + beforeEach(() => { db = setupHTTP(); - await db.put(updateDocument); - }) + return db.put(updateDocument); + }); afterEach(teardown); - it('update', async () => { - const [doc, req] = JSON.parse((await db.update('test/args/my-id')).body); - should.not.exist(doc); - req.id.should.equal('my-id'); + it('update', () => { + return db.update('test/args/my-id') + + .then((result) => { + const [doc, req] = JSON.parse(result.body); + should.not.exist(doc); + req.id.should.equal('my-id'); + }); }); }); diff --git a/tests/pouchdb-update/signatures.js b/tests/pouchdb-update/signatures.js index e8de55f8..f1cf25f9 100644 --- a/tests/pouchdb-update/signatures.js +++ b/tests/pouchdb-update/signatures.js @@ -1,4 +1,4 @@ -import {setup, teardown} from './utils'; +const {setup, teardown} = require('./utils'); describe('signature tests', () => { let db; diff --git a/tests/pouchdb-update/utils.js b/tests/pouchdb-update/utils.js index 9dab0a23..446607e4 100644 --- a/tests/pouchdb-update/utils.js +++ b/tests/pouchdb-update/utils.js @@ -1,23 +1,23 @@ -import stuff from 'pouchdb-plugin-helper/testutils'; -import Update from '../'; +const stuff = require('pouchdb-plugin-helper/testutils'); +const Update = require('../../packages/node_modules/pouchdb-update'); stuff.PouchDB.plugin(Update); stuff.updateDocument = { - _id: "_design/test", - updates: { - args: `function (doc, req) { - return [null, toJSON([doc, req])]; - }`, - exception: `function (doc, req) { - return abc; - }`, - 'save-adding-date': `function (oldDoc, req) { - var doc = JSON.parse(req.body); - doc.updated = new Date(); - return [doc, "Hello World!"]; - }` - } -} + _id: "_design/test", + updates: { + args: `function (doc, req) { + return [null, toJSON([doc, req])]; + }`, + exception: `function (doc, req) { + return abc; + }`, + 'save-adding-date': `function (oldDoc, req) { + var doc = JSON.parse(req.body); + doc.updated = new Date(); + return [doc, "Hello World!"]; + }` + } +}; module.exports = stuff;