JSON Model-to-Model Projection (JM2MP) Documentation

JSON Model-to-Model Projection (JM2MP) Documentation

Module

jm2mp/evaluator

This module implements the evaluation process of the projection language JM2MP: given a resolved and normalized projection document, it evaluates its root template over a source document.

The evaluation process is asynchronous because the QueryAdapter's contract is async.

Two API levels:

  • The evaluate function is part of the low level API. If it is invoked without first passing through validateModule, errors that validation would have detected (like non-existent references or out-of-scope aliases) will manifest as EvaluationError at runtime.

  • Use project as the high level API for the full workflow with: module resolution, projection validation and projection evaluation.

Adapter error handling:

Isolation between evaluations:

  • Each call to evaluate creates its own cache of compiled expressions (independent by $syntax). There is no shared state between concurrent evaluations; two evaluate calls running in parallel do not interfere with each other. This allows for safe use in concurrent applications (such as web servers) without the need for an additional synchronization mechanism.

Source

Namespaces

JM2MP_PROJECTIONS

Members

staticconstant

DEFAULT_MAX_DEPTHinteger

It sets the default value to 1000 for the maximum logical nesting depth of expressions evaluated by JM2MP.JS.

This serves as a safeguard against infinite recursion and extremely nested logical expressions, whether created inadvertently or maliciously.

It is not affected by the JavaScript stack, since evaluation is performed asynchronously (async/await).

Type

  • integer

Source

Methods

static

evaluate(module, document, options) → {Promise.<*>}

Evalúa un módulo resuelto sobre un documento de origen.

Parameters

  • module object

    A projection module previously resolved and normalized.

  • document *

    The source document.

  • options object

    Mandatory options for evaluation.

    Properties

    • registry AdapterRegistry

      The mandatory AdapterRegistry used to evaluate each query language expression.

    • maxDepth number <optional>
      1000

      See DEFAULT_MAX_DEPTH.

Returns

  • Promise.<*>

    The (promised) resultant document of apply the root template from module.

Source

inner

compareOrdered(a, b, opName) → {integer}

It compares two ordinal values (sortables): number/number or string/string.

Parameters

  • a *

    The left operand.

  • b *

    The right operand.

  • opName *

    The name of the operation/operator.

Returns

  • integer

    (-1)|(+1)|(0) when (a<b)|(a>b)|(a===b)

Throws

Whenever types of a/b are neither number/number nor string/string.

Type
EvaluationError

Source

inner

deepen(env) → {object}

It returns an execution environment with its depth value increased by one (1).

Parameters

  • env *

    The JM2MP's execution environment.

Returns

  • object

    Same as env but with env.depth increased by 1.

Source

inner

deepEqual(a, b) → {boolean}

It test the recursive structural equality over JSON values.

The rules are:

  • For scalar (primitive) types it applies equal according to ===.
  • Arrays are equal if they have the same length and corresponding elements are equal.
  • Objects are equal if they have the same set of keys and corresponding values are equal.
  • Different types means that they never are equal (type casting neither implicit nor explicit is implemented in JM2MP 1.0).

Parameters

  • a *

    The left operand.

  • b *

    The right operand.

Returns

  • boolean

    (a==b)

Source

asyncinner

evalLiteralObject(obj, env) → {*}

It projects (evaluates) a literal object (JSON value) obj over the execution environment env and returns its resultant value.

For literal objects: each property's value is projected and each property's name (key) can be escaped (using '$' to start literally by '$').

Parameters

  • obj *

    The literal object (JSON value) to project.

  • env *

    The execution environment

Returns

  • *

    It returns the resultant value of project every object's property.

Source

asyncinner

evalOperation(op, env) → {*}

It invokes the specified operation handler from the specified operation's name.

Parameters

  • op function

    The name of the operation (template command, predicate or operator) to evaluate.

  • env *

    The execution environment where the operation will be evaluated.

Returns

  • *

    The resultant value obtained.

Source

asyncinner

evalProjection(proj, env) → {*}

It evaluates the projection proj over the execution environment env and returns its resultant value.

It is async to support asynchronous QueryAdapters.

Parameters

  • proj *

    The projection to evaluatue.

  • env *

    The execution environment where proj is evaluated.

Returns

  • *

    The result of the proj over env.

Source

inner

expectType(value, expectedType, opName, argName)

Asegura que value sea del tipo esperado o lanza EvaluationError.

Parameters

  • value *

    The JSON value to test its type.

  • expectedType *

    The expected type's name.

  • opName *

    The name of the operation in which the value is evaluated.

  • argName *

    The argument's name.

Source

inner

typeName(v) → {string}

It returns the name of the JSON data type associated to v, one of:

  • null
  • boolean
  • number
  • string
  • array
  • object

Parameters

  • v *

    The value to be tested.

Returns

  • string

    A text string with the corresponding JSON data type (not exactly like JavaScript).

Source