Richard Wood
Richard Wood

Reputation: 5

Need help on CouchDB rewrites for view with multiple arguments

The original question is below the double bar. Here is an update and I am hoping for more help.

Hi,

I am finally actively back working on this. I had this project out-prioritized repeatedly over the past year. I apologize for not even commenting on your response, it really is giving me hope.

I have a cleaner approach and have made some progress I believe. I now have around 1900 documents in a database. Each document has a "type" key. I've made 30 design documents, one per "type", that each have a view called "all" that returns all rows of the document of that type.

So, now I can execute from a browser a GET like this:

dasvm01.com:5984/registryservice/_design/airplaneidtypes/_view/all

My immediate goal is to shorten this to:

dasvm01.com:5984/registryservice/airplaneidtypes

or

dasvm01.com:5984/registryservice/airplaneidtypes/all

To this end, I added a rewrites function to the airplaneidtypes design doc:

{
  "_id": "_design/airplaneidtypes",
  "_rev": "11-c28b41a718017cbcd65f82f4acc611cb",
  "views": {
    "all": {
      "map": "function (doc) 
           {  if(doc.ddoc === 'airplaneidtypes') 
               {    emit(doc._rev,doc);  }
           }"
    }
  },
  "language": "javascript",
  "rewrites": [
    {
      "from": "/airplaneidtypes",
      "to": "registryservice/_design/airplaneidtypes/_rewrite",
      "method": "GET"
    }
  ]
}

Now I think that I need to update the CouchDB daemon:vhosts setting: I took a crack at it, but I really had no level of confidence and it doesn't seem to work/ In Fauxton, I have:

daemons     
auth_cache {couch_auth_cache, start_link, []}
... 
vhosts {dasvm01.com:5984, /registryservice/_design/airplaneidtypes/_rewrite, []} 

Not sure if this is: - close, - way off, - not the correct place, - just needs quotes...

What can you tell me? I don't understand the notation in the default value of Fauxton:

vhosts

    Virtual hosts manager. Provides dynamic add of vhosts without restart, wildcards support and dynamic routing via pattern matching
        [daemons]
            vhosts={couch_httpd_vhost, start_link, []}

================================================================

I am struggling to get (any) rewrites to work. I've looked at a lot of examples both here and on other sites. I've tried to emulate the solutions but with no success. Some level of confusion comes into play because I am passing two arguments to my view. But based on the examples that I see here [10.5.11. /db/_design/design-doc/_rewrite/path1 that shouldn't be a problem.

I have this view:

 {"dataProvider": {
  "map": "function(doc) {
    if(doc.dataProvider && doc.status) {
      emit([doc.dataProvider, doc.status], doc); 
      }
    }"
  }
}

When I hit this URL, the view works fine:

"//ddpspc28:8080/data_providers/_design/basic/_view/dataProvider?key=["TBC-ACT","unstable"]"

I would like to rewrite this so that the URL looks like this:

"//ddpspc28:8080/data_providers/dataProvider?key=["TBC-ACT","unstable"]"

which currently returns:{"error":"not_found","reason":"missing"}

In an effort to do this, I created a field named "rewrites" in my design document "_design/basic" for my database "data_providers". In the value of rewrites I have this:

"[ 
  {  "from":"/dataProvider",
     "to": "_view/dataProvider"  
  }
 ]”

I have also tried this:

“[
  {  "from":"/dataProvider",
     "to": "_design/basic/_view/dataProvider"
  }
]”

I have also tried this:

“[
  {  "from":"dataProvider",
     "to": "/_design/basic/_view/dataProvider"
  }
]”

I suspect that I may also need a "query" component to the rewrite, but I would be happy at this point to just get the rewrite to take "dataProvider" and anything that follows and replace the dataProvider with the fully qualified path.

Upvotes: 0

Views: 234

Answers (1)

Nuno Cruces
Nuno Cruces

Reputation: 1743

A rewrite that's defined in the _design/basic document, is accessed at: //ddpspc28:8080/data_providers/_design/basic/_rewrite.

So, both your first and last try should work, but they'll be accessed at: //ddpspc28:8080/data_providers/_design/basic/_rewrite/dataProvider

Rewrites won't be very useful (to reduce URL sizes) unless you combine them with virtual hosts. Combined with virtual hosts can be a nice way to centralize an API.

I typically add a design document with no views (called api) that rewrites to the other design documents' views, etc.

Upvotes: 0

Related Questions