lib in views when using couchdb query server

196 Views Asked by At

I've built a custom query server for a custom query language.

I've tried to create a design document with views map/reduce.

In those map/reduce functions (most importantly in the map function) I want to be able to reference library code.

I can see there is a lib node that can be added to the design document.

I am expecting the custom query server to receive add_lib message from CouchDB, but I have never seen this yet.

http://docs.couchdb.org/en/stable/query-server/protocol.html#add-lib

add_fun messages fail for code that depends on the library in the query server because the add_lib message has not yet been received in advance.

"The Query Server should parse, compile, and evaluate the function it receives to make it callable later."

http://docs.couchdb.org/en/stable/query-server/protocol.html#add-fun

Since add_fun fails, this means that I am unable to save the design document.

I have viewed the following pages but I still no success:

How do I add the moment.js library to Cloudant NoSQL Design Doc on Bluemix

How do I DRY up my CouchDB views?

https://www.oreilly.com/library/view/couchdb-the-definitive/9780596158156/ch05.html

I'm doing something wrong or made a mistake it seems, your help is greatly appreciated.

1

There are 1 best solutions below

0
On

The following website has the correct way to put a lib into a view.

https://caolan.org/posts/commonjs_modules_in_couchdb.html

Here's a new example for a fictitious language which is based on the working one.

{
  "_id": "_design/example",
  "language": "customlang",
  "views": {
    "lib": {
      "math": "pi = 3.14"
    },
    "map_everything_to_pi": {
      "map": "imports math; pi"
    }
  }
}

My problem arose because I could not save the design document when I had code in the map function that depended on the library.

By adjusting the map function to not depend on the library, I have now been able to save the document and upon querying the view, I witnessed the add_lib message get sent to the query server.

In CouchhDB 2.2.0 add_lib is sent when the view is queried, but not immediately on saving the design document where add_fun is called. In my view add_lib should be called before for each lib before CouchDB sends the add_fun messages.

This fact makes it impossible for add_fun to evaluate / precompile the code since the library is not yet known. add_lib must succeed in order for the design document to save, so it is impossible to save the design document.

I probably need to raise a bug tracker issue.