pouchdb / pouchdb-express-router Goto Github PK
View Code? Open in Web Editor NEWAn Express submodule with a CouchDB style REST interface to PouchDB.
An Express submodule with a CouchDB style REST interface to PouchDB.
Hi,
I faced a problem, when trying to integrate this lib with Express 5, and I thought this issue could be of some interest for you guys.
Although Express 5 is still alpha, you might want to consider it, as this is definitely an upcoming problem...
Here are my investigations, and 2 proposed workarounds / solutions.
Starting from Express 5, req.query
(corresponding to the URL query string) is now read-only (a getter, technically): see this link for more info.
Regarding pouchdb-express-router, the impact is that, basically, the following Express middleware in index.js
(line 15) now becomes useless:
app.use(function (req, res, next) {
for (var prop in req.query) {
try {
req.query[prop] = JSON.parse(req.query[prop]);
} catch (e) {}
}
next();
});
=> req.query
won't get modified anymore, whatever you do here...
The issue is linked to the fact that pouchdb-express-router passes the URL query parameters directly to PouchDB functions (e.g. db.get(...)
), and those won't get transformed anymore.
It still works in most of the requests, when params are strings, but it fails when an array is passed, like on document, with the open_revs
param.
Starting from Express 5, the only way to modify req.query is from the query parser, called before any of the routers (this link for more info).
Instead of using the standard query parser, we can define our own, still based on qs
, just to add an additional JSON parse step:
const qs = require('qs')
app.set('query parser', function (str) {
const query = qs.parse(str) // call the standard parser
Object.keys(query).map(key => {
try {
query[key] = JSON.parse(query[key])
} catch (e) {} // silently ignore
})
return query
})
Important note: this is linked to Express app, not router, which makes it out of this library's scope, on a strict point of view.
I don't find this approach neither efficient nor elegant, but it works, and could be an acceptable workaround for some people I guess.
Pros:
README.md
file, to indicate people how to integrate this router.Cons:
The Express-way of storing user-defined data is to use res.locals
.
So it's quite simple: we could modify the middleware, to store JSON parsed values in res.locals.query
instead of req.query
(or res.locals.pouchdb.query
, or anything you'd prefer).
And then modify all the route functions, to get data from res.locals.query
instead of req.query
.
The middleware could be as simple as:
app.use(function (req, res, next) {
if (!res.locals.query) {
res.locals.query = {}
}
for (var prop in req.query) {
try {
res.locals.query[prop] = JSON.parse(req.query[prop])
} catch (e) {
res.locals.query[prop] = res.query[prop] // copy value with no parsing
}
}
next()
})
Pros:
Cons:
I hope you will find this issue interesting, and maybe it will bring some help to people struggling with Express 5 the way I did.
As you may have anticipated, I would certainly go with solution 2, but I'll let you decide which one you prefer, of course.
If needed, and depending on your decision, I can take some time to make a proper PR if you'd like.
Many thanks for your work on this great library, I think it perfectly matches an important need: a lightweight and agnostic Express router for PouchDB, we need it!.
Not a full-stack app or server, no fancy UI... just a router.
Let's keep simple things... simple! :)
Hope it will help,
Keep up the good work guys!
Cheers,
Pascal.
Suggested in response to pouchdb/pouchdb#3391. Temp view support is going away in CouchDB 2.0 so consider removing it as a minimum requirement.
I think it's very confusing having three projects that do similar things?
Maybe it's just a documentation issue.
1) test.attachments.js-http issue 2803 should throw 412:
AssertionError: expected 500 to equal 412
+ expected - actual
+412
-500
at /home/travis/build/pouchdb/pouchdb/tests/integration/test.attachments.js:102:27
at tryCatch1 (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/util.js:63:19)
at Promise$_callHandler [as _callHandler] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/promise.js:695:13)
at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/promise.js:711:18)
at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/promise.js:868:14)
at Promise$_settlePromises [as _settlePromises] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/promise.js:1006:14)
at Promise$_rejectPromises [as _rejectPromises] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/promise.js:999:10)
at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/async.js:74:12)
at Async$consumeFunctionBuffer (/home/travis/build/pouchdb/pouchdb/node_modules/bluebird/js/main/async.js:37:14)
at process._tickCallback (node.js:448:13)
See also pouchdb/pouchdb#3889
An express-pouchdb issue that will crop up in pouchdb-express-router as well:
Includes a fix.
ive tried implementing with express-pouchdb but seems to fail in replication.
but this one.
awesome works! man @daleharvey.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.