You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Hank Knight <hk...@gmail.com> on 2014/01/19 18:35:29 UTC

CouchDB: JavaScript functions inside show functions

How can I add a JavaScript function to a show function?
'XYZ: '+ (111*2)

I would like to reference a custom function like this:
'XYZ: '+ zooph(111)

This example does NOT work:

"myshowfunction": "function(doc, req){ return{'headers':
{'Content-Type' : 'text/plain'}, 'body': function zooph(x) {return
x*2;} + 'XYZ: '+ zooph(111)}}"

Re: CouchDB: JavaScript functions inside show functions

Posted by Andy Wenk <an...@nms.de>.
On 19 January 2014 22:01, Hank Knight <hk...@gmail.com> wrote:

> Great!  Is there a way for a JavaScript function to exist in one place
> but to be available to ALL design documents?


afaik no ... it would not make sense imho because show and list functions
are always assigned to one design doc ....

Cheers

Andy
-- 
Andy Wenk
Hamburg - Germany
RockIt!

http://www.couchdb-buch.de
http://www.pg-praxisbuch.de

GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588

https://people.apache.org/keys/committer/andywenk.asc

Re: CouchDB: JavaScript functions inside show functions

Posted by Hank Knight <hk...@gmail.com>.
Great!  Is there a way for a JavaScript function to exist in one place
but to be available to ALL design documents?


On Sun, Jan 19, 2014 at 2:56 PM, Andy Wenk <an...@nms.de> wrote:
> On 19 January 2014 18:35, Hank Knight <hk...@gmail.com> wrote:
>
>> How can I add a JavaScript function to a show function?
>> 'XYZ: '+ (111*2)
>>
>> I would like to reference a custom function like this:
>> 'XYZ: '+ zooph(111)
>>
>> This example does NOT work:
>>
>> "myshowfunction": "function(doc, req){ return{'headers':
>> {'Content-Type' : 'text/plain'}, 'body': function zooph(x) {return
>> x*2;} + 'XYZ: '+ zooph(111)}}"
>>
>
> you can add a commonJS module to a design document. It is available within
> the whole design document and you can reference it from your show function.
> here is an example:
> {
>   "_id": "_design/somename",
>   "_rev": "275-a3f8495cc032a09fb1dfdbaa26da0703",
>   "views": {
>     "lib": {
>       "multi2": "function(x) { return x * 2; }",
>       "multi4": "function(x) { return x * 4; }"
>       }
>     }
>   }
> }
>
> You would then require the module in your show function with
>
> require('lib/multi2') or require('lib/multi4').
>
> You could also namespace this in the code above with
>
>  "lib": {
>     "modulenamesapce": {
>         "multi2": "function(x) { return x * 2; }",
>         "multi4": "function(x) { return x * 4; }"
>       }
>     }
>  }
>
> and require it respectively.
>
> Please refer also to
> http://docs.couchdb.org/en/latest/query-server/javascript.html?highlight=commonjs#commonjs-modules
>
> Cheers
>
> Andy
>
> --
> Andy Wenk
> Hamburg - Germany
> RockIt!
>
> http://www.couchdb-buch.de
> http://www.pg-praxisbuch.de
>
> GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
>
> https://people.apache.org/keys/committer/andywenk.asc

Re: CouchDB: JavaScript functions inside show functions

Posted by Andy Wenk <an...@nms.de>.
On 19 January 2014 18:35, Hank Knight <hk...@gmail.com> wrote:

> How can I add a JavaScript function to a show function?
> 'XYZ: '+ (111*2)
>
> I would like to reference a custom function like this:
> 'XYZ: '+ zooph(111)
>
> This example does NOT work:
>
> "myshowfunction": "function(doc, req){ return{'headers':
> {'Content-Type' : 'text/plain'}, 'body': function zooph(x) {return
> x*2;} + 'XYZ: '+ zooph(111)}}"
>

you can add a commonJS module to a design document. It is available within
the whole design document and you can reference it from your show function.
here is an example:
{
  "_id": "_design/somename",
  "_rev": "275-a3f8495cc032a09fb1dfdbaa26da0703",
  "views": {
    "lib": {
      "multi2": "function(x) { return x * 2; }",
      "multi4": "function(x) { return x * 4; }"
      }
    }
  }
}

You would then require the module in your show function with

require('lib/multi2') or require('lib/multi4').

You could also namespace this in the code above with

 "lib": {
    "modulenamesapce": {
        "multi2": "function(x) { return x * 2; }",
        "multi4": "function(x) { return x * 4; }"
      }
    }
 }

and require it respectively.

Please refer also to
http://docs.couchdb.org/en/latest/query-server/javascript.html?highlight=commonjs#commonjs-modules

Cheers

Andy

-- 
Andy Wenk
Hamburg - Germany
RockIt!

http://www.couchdb-buch.de
http://www.pg-praxisbuch.de

GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588

https://people.apache.org/keys/committer/andywenk.asc