thichxai
thichxai

Reputation: 1133

Marklogic how to store xquery file into Modules database

I know how to insert document in xml file but not knowing how store xquery file into Marklogic Module database. The reason I want store xquery file to setup schedule task. I have a xquery file below. Thanks in advance.

declare namespace...
let $uris := cts:element-value(...code...),
             cts:and-query((...code...))

let $result := {<stats><top-docs>
                  for $uri in $uris[1 to 10]
                       ...more code here..
               }</top-docs></stats>
let $permissions := (xdmp:permission(..give permissions here.))
let $_ := xdmp:log("Inserting Stats Document")
return xdmp:document-insert("/acitivity/stats.xml",$result, $permissions))   

Upvotes: 2

Views: 760

Answers (3)

Harry Bakken
Harry Bakken

Reputation: 793

There's a handy recipe on the MarkLogic website as well. I use this fairly often. It's well commented and has logic to try it without a commit first to make sure it's doing what you expect:

http://developer.marklogic.com/recipe/load-a-module

Upvotes: 0

DALDEI
DALDEI

Reputation: 3732

In addition to Mads answer:

You must set execute permission on the document for a non-admin to execute it. It is important to remember that when running in marklogic (as xquery or javascript or as the result of one of the client apis) -- there are multiple databases associated with your session -- Data, Modules, Security etc -- all 'data like' operations go to the Data DB, all 'execute' operations goto the 'Modules' DB (or filesystem) etc. since there isn't a "database" parameter to xdmp:document-insert, and the database association is not changeable in a single expression -- you use one of the xdmp:eval / xdmp:invoke functions to run the insert in a sub-context that has the desired settings.

Example: https://docs.marklogic.com/xdmp:invoke-function

Upvotes: 0

Rob S.
Rob S.

Reputation: 3609

There's a number of ways to insert an xqy file into the modules database. Here are just a few:

  1. Best practice, in my opinion, is to use a deployment tool like ml-gradle so that you can easily deploy your module to multiple environments and share your deployment setup with other developers.
  2. You can run a query just like the one you showed in QConsole, but select the Modules database instead of your Content database in the database dropdown
  3. You can use a rest endpoint such as /v1/documents PUT and select the modules database with the database param.

Upvotes: 5

Related Questions