Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It will run in the node.js context.</p> <p>You can also use it in node, like this, as a dynamic function:</p> <pre><code>var cradle = require('cradle'); var db = new(cradle.Connection)().database('db_name'); db.get('_design/node%2Fyour_code', function (err, doc) { if (!err){ var your_code = new Function(doc['arguments'].join(','), doc.code); your_code("cool", "also cool"); }else{ console.error('error:', err); } }); </code></pre> <p>make your docs look like this:</p> <pre><code>{ "_id": "_design/node/your_code", "arguments": [ "nameOfArg1", "nameOfArg2" ], "code": "console.log('arg1', nameOfArg1); console.log('arg2', nameOfArg2);" } </code></pre> <p>It's in the same scope as where the new Function is called, so you have access to cradle, or you can require other libs, which will be loaded as if it was an anon function in that scope.</p> <p>Put it in a design doc, then only admin can make changes, out of the box.</p> <p>Here is a nicer, but similar approach:</p> <pre><code>// Format, in db: doc = { "_id": "_design/node", "your_function_name": { "arguments": [ "nameOfArg1", "nameOfArg2" ], "code": "console.log('arg1', nameOfArg1); console.log('arg2', nameOfArg2);" }, "your_other_function_name": { "arguments": [ "name" ], "code": "console.log('hello', name, 'how\\'s it going, bro?');" } }; var cradle = require('cradle'); var db = new(cradle.Connection)().database('db_name'); function run_from_db(name, args){ db.get('_design/node', function (err, doc) { if (!err){ if (doc[name] !== undefined){ var fn = new Function(doc[name]['arguments'].join(','), doc[name].code); fn.apply(fn, args); }else{ console.error("could not find", name, "in _design/node"); } }else{ console.error(err); } }); } run_from_db('your_other_function_name', ['konsumer']); </code></pre> <p>this will output:</p> <pre><code>hello konsumer how's it going, bro? </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload