如何在 xquery 中访问环境变量?

How do I access environment variables in xquery?

我在 xquery 中的程序有一些基于函数运行环境的变量 运行。比如dev指向"devserver",test指向"testserver",prod指向"server",等等

如何在我的 application.xml 文件中设置它以及如何在我的 .xqy 函数中引用它们?

"workaround" solution 1

use "switch" to determine host:

switch (xdmp:host-name(xdmp:host()))
    case <dev environment as string> return "devserver"
    case <test environment as string> return "testserver"
    .
    .
    .
    default return fn:error(xs:QName("ERROR"), "Unknown host: " || xdmp:host-name(xdmp:host()))

"workaround" solution 2

create an xml file in your project for each host, update your application.xml to place the xml file in a directory depending on the environment name, then refer to the document now built on installation.

application.xml:

<db dbName="!mydata-database">
   <dir name="/config/" permissionsMode="set">
       <uriPrivilege roles="*_uberuser"/>
       <permissions roles="*_uberuser" access="riu"/>
       <load env="DEV" root="/config/DEV/" merge="true" include="*.xml"/>
       <load env="TEST" root="/config/TEST/" merge="true" include="*.xml"/>

documents located in project directory /config//environment.xml

<environment>
   <services>
       <damApi>https://stage.mydam.org</damApi>
       <dimeApi>https://stage.mydime.org/api/Services</dimeApi>
   </services>
</environment>

usage when I need to get the value

fn:doc("/config/environment.xml")/environment/services/damApi/fn:string()

neither solution seems the best to me.

不知道 MarkLogic 是否支持它,但 XQuery 3.1 具有函数 available-environment-variables()environment-variable()

您可以考虑使用 https://github.com/marklogic-community/commons/tree/master/properties

上的微型“属性”库

我们很久很久以前就为 MarkMail.org 编写了它,我们相信我们不想将配置放入数据库文档中,因为配置应该与数据分开。数据得到备份,在别处恢复,新位置可能与旧位置不同。

因此,我们做了一些修改,将配置放入静态命名空间上下文(每个组和应用服务器都有)。配置的前缀是 属性 名称。配置的值为属性值(包括类型信息)。这是 MarkMail 部署的屏幕截图,显示它是一个生产服务器,可以通过电子邮件发送错误信息,提供什么静态文件版本,以及输出什么域作为其基础。

这种方法允许您以管理方式配置属性(通过 Red GUI 或 REST),并且它们与数据分开。它们对执行上下文静态可用,无需额外成本。您可以在组级别或应用程序服务器级别或两者配置它们。该库是一个方便的包装器,用于提取键入的值。

也许现在有更好的方法,例如 XQuery 3.1 函数,但这个方法已经运行了 10 多年。

如果您使用 ml-gradle 来部署您的项目,it can do substitutions in your code。这意味着您可以使用如下代码设置 XQuery 库:

declare variable $ENV = "%%environmentName%%";

然后您可以在任何需要的地方导入该库。

还没有在我们的项目中使用 gradle,我设法弄清楚如何使用 maven 配置文件 find/replace 我需要的值基于它部署到的环境。我只需将要包含的插件、要更新的文件以及要替换的内容添加到正确的配置文件中。

pom.xml:

<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <version>1.5.2</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>**/modules/runTasks.xqy</include>
      <include>**/imports/resetKey.xqy</include>
    </includes>
    <replacements>
      <replacement>
        <token>https://stage.mydime.org/api/Services</token>
        <value>https://www.mydime.org/api/Services</value>
      </replacement>
    </replacements>
  </configuration>
</plugin>