Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use CommonJS modules #54

Open
papswell opened this issue Jun 6, 2019 · 3 comments
Open

Unable to use CommonJS modules #54

papswell opened this issue Jun 6, 2019 · 3 comments

Comments

@papswell
Copy link

papswell commented Jun 6, 2019

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

@jo
Copy link
Owner

jo commented Jun 6, 2019

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 [email protected]?

@papswell
Copy link
Author

papswell commented Jun 6, 2019

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 :)

@jo
Copy link
Owner

jo commented Jun 6, 2019

Great to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants