Unable to use CommonJS modules
papswell opened this issue · 3 comments
Hi there,
First, thanks for the tools you made, that helped me a lot :)
I am having trouble to use couchdb-bootstrap
with CommonJS modules though. I would like to be able to write any view, filter or validation function in a module, so that i can unit test it. I am sure this is possible, as you mention that in the README.
My problem is that the generated _design documents are not as i would expect them to be.
It might be related to #49
I have to mention that i am pretty new with CouchDB, so i probably might be missing something obvious...
Here is the structure i am trying to use (only a small relevant part):
├── package.json
├── bin
│ └── bootstrap.js
├── tests
│ └── // Here i will test my CJS modules with Jest
└── src
├── _users
│ ├── alice.json
│ └── bob.json
├── official-db
│ ├── _design
│ │ └── validation
│ │ ├── index.js
│ │ └── validate_doc_update.js
│ ├── _security.json
│ └── adoc.json
├── alice-db
│ └── _security.json
└── bob-db
└── _security.json
I run the bootstrap process with a custom script (bin/bootsrap.js
) which basically only executes your module with my CouchDB server admin credentials, passes the index = true
option, and generate a report of what have been done. (I don't think the problem comes from that script, so i'm not including it in this issue)
Following the README
, here is the content of my official-db/_design/validation
folder :
// index.js
module.exports = {
validate_doc_update: require("./validate_doc_update")
};
// validate_doc_update.js
module.exports = function(newDoc, oldDoc, userCtx, secObj) {
throw { forbidden: "Nobody can update anything here !!" }; // this is only pseudo code to test :)
};
In the official-db
database, i would have expected the validation design document to be like :
// offical-db > _design/validation
{
"_id": "_design/validation",
"validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) {\n throw { forbidden: \"Nobody can update anything here !!\" };\n};"
}
But instead, i end up with that :
// offical-db > _design/validation
{
"_id": "_design/validation",
"index": "module.exports = {\n validate_doc_update: require(\"./validate_doc_update\")\n};",
"validate_doc_update": "module.exports = function(newDoc, oldDoc, userCtx, secObj) {\n throw { forbidden: \"Nobody can update anything here !!\" };\n};"
}
My main concern is that the module.export
is not stripped out, so the database is "broken" with the error Save failed: Expression does not eval to a function
.
If i run the command couchdb-compile src --index
, the problem remains, but if i couchdb-compile src/official-db/_design/validation --index
, everything works :
couchdb-compile src/official-db/_design/validation --index
# outputs
{
"_id": "_design/validation",
"validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) {\n throw { forbidden: \"Nobody can update anything here !!\" };\n};"
}
As far as i understand the code from couchdb-compile
, the option index
is only used for the root folder, and is not recursive. Shouldn't it be ?
I am sorry for this long issue, but i have been stuck on it for the whole day, and i really need some help... I hope i have provided enough details to get some :) Let me know if you need more infos !
Thanks a lot, regards
Yeah, this is a very good issue description, thank you for this!
I indeed bet you ran into #49 - could you, as a quick fix, try downgrading to couchdb-bootstrap@1.14.0
?
Yeah it works ! 👍
Let me know if i can help you in any way to resolve this issue, and thank you for the job you have done on these CouchDB tools, i can now relax for real :)
Great to hear!