Loading TOC...

opdsl.import

opdsl.import(
   source as String
) as ModifyPlan

Summary

This function takes the Query DSL representation of an Optic query as a string or text and returns the built query, which can then be executed with the prototype.result method.

Parameters
source The string or text node with the Query DSL representation of an Optic query.

Usage Notes

The Query DSL builds the query with the same functions used in SJS. Namespaces are all predefined including the op namespace for the Optic functions. The DSL consists of any number of constant assignments followed by a single call that builds the final query.

The Query DSL supports the following JavaScript syntax:

Parenthetical groupingConstantsString templates
Function calls Functions in the predefined namespaces and methods of objects are available (as described later).
Literal Including objects, arrays, and string, number, boolean, and null values. Strings can be delimited with double, single, or (for multiline strings) grave quotes. The undefined (void 0) literal value is not supported; use op.isDefined() instead.
Arithmetic, comparison, and boolean operators The == and != equality operators are supported but not the === or !=== identity operators. The tertiary boolean operator (testExpression ? consequentExpression : alternateExpression) is supported. The + operator is supported only for arithmetic; use fn.concat() for string concatenation.
For instance, arithmetic expressions can be grouped to indicate precedence.
Dot notation Including accessing functions in the predefined namespaces and chaining method calls.
const statements can assign names to literals or return values from builder functions for reuse or clarity.
Multiline grave-quoted strings can evaluate constants.

The Query DSL supports the following vocabulary:

cts, fn, geo, json, map, math, op, rdf, sem, spell, sql, xdmp, and xs namespaces The side-effect free functions (such as op.fn.abs()) supported by the Optic Query Builder in SJS are supported for both literal arguments and query expressions in the Query DSL; see Expression Functions For Processing Column Values. The capitalization of function names is exactly the same as in SJS. The op.result(), op.explain(), and op.export() functions are not supported by the Query DSL because the Query DSL builds the query; processing of the built query occurs subsequently (as described below).
cts.query and sem.store constructors The Query DSL can construct constraining queries for the where() operator.

The Query DSL supports the following shortcuts that aren't available in the Optic Query Builder in SJS. In particular, the Query DSL doesn't have to use different functions or operators for expressions evaluated on literals at build time and on rows at query time:

Function calls The op namespace may be omitted from namespaced expression functions (such as op.fn.abs()) even where the expression must evaluate on rows at query time.
Operators The JavaScript operator (such as > may be used instead of the named Optic functional operator (such as op.gt()) even where the expression must evaluate on rows at query time instead of build time.
$c prefix The $c prefix may be used instead of the op.col(), op.viewCol(), or op.schemaCol() function to identify a column, using a period as a separator (as in $c.firstName, $c.customer.firstName, or $c.inventory.customer.firstName)

The import() function throws an error if the Query DSL contains:

To use the query returned by the import() function, call methods on the built query including:

Example

import opdsl from '/MarkLogic/optic/optic-dsl-js.mjs';

opdsl.import(`

const lastYear = fn.yearFromDate(fn.currentDate()) - 1
op.fromView(null, 'expenses')
  .where(op.and(
      op.ge(op.col('date'), xs.date(fn.concat(lastYear, '-01-01'))),
      op.le(op.col('date'), xs.date(fn.concat(lastYear, '-12-31')))
      ))
  .select([op.col('date'), op.col('amount'), op.col('purpose')])
  .orderBy(op.col('date'))

`).result();
    

Example

import opdsl from '/MarkLogic/optic/optic-dsl-js.mjs';

// the sample query as the previous query but with shortcuts
opdsl.import(`

const lastYear = fn.yearFromDate(fn.currentDate()) - 1
op.fromView(null, 'expenses')
  .where(
      $c.date >= xs.date(fn.concat(lastYear, '-01-01')) &&
      $c.date <= xs.date(fn.concat(lastYear, '-12-31'))
      )
  .select([$c.date, $c.amount, $c.purpose])
  .orderBy($c.date)

`).result();
    

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.