2

I am using the MarkLogic Node Client API, and would like to DatabaseClient.eval() some code on every database on the cluster. But as far as I can tell (and unlike xdmp.eval() or /v1/eval) it is not possible to set the content database against which to eval the code.

Did I miss anything?

Extra points for setting the modules database :-)

Florent Georges
  • 2,190
  • 1
  • 15
  • 24

1 Answers1

2

You should be able to set the database as part of constructing the client.

https://docs.marklogic.com/jsdoc/marklogic.html#.createDatabaseClient

In all cases, since you are using the xdmp.eval() endpoint, you already have some sharp tools available for the switching of any context (user, database, modules-database) by use of xdmp.invokeFunction() inside of your eval.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Hi @david-ennis-17llamas! Yes, I was afraid that's the response I would get :-) As I have one query to eval on several databases, I hoped I didn't have to instantiate a new client every time. And as I have a repository of queries, I would prefer to leave that responsibility to the part of the code that actually executes it. If there is no built-in way to achieve this, then I think I will go with passing the query as a string param, and use `xdmp.eval()` on it... – Florent Georges Nov 04 '22 at 13:10
  • Technically, you could probably implement your own context switching of content and modules database with declarative XML rewriter. Use one client and do runtime context switching on the request handler. I could see this being possible if you could find a way to set an extra request header or query Param on the requests and monitor for those in a rewriter match rule. I've done things like that to make ML work as multi-tenant with different modules root and different content dB in the past, but I do not know the Node.js client library and where I would inject the Param on that end – David Ennis -CleverLlamas.com Nov 05 '22 at 14:03
  • The use of eval has a side effect of always needing to be compiled including any included modules. Monitor the request log for slow compile times and avoid a try/catch in an eval. Experience has seen it add a strange amount of overhead. Using eval with same functions over and over can achieve performance gains if you create an xdmp:function and save it in a server or session field. The statically compiled version can be re used. – David Ennis -CleverLlamas.com Nov 05 '22 at 14:07