This module implements the evaluation process of the projection
language JM2MP and exposes it as a public API.
It exports:
-
Hierarchy of errors.
-
Pipeline: resolve, normalizeModule, validateModule, evaluate.
-
Loaders: string, file, and URL.
-
Query adapters: interfaces/contracts, individual factories, and registry's factory.
-
High level functionality: project links all stages.
The
nativeQueryAdapter is always available; the external references (jsonpath,jsonata,jsonquery,jsonpointer, andjmespath) are dynamically loaded on demand.
Source
Methods
createAdapterRegistry(defaultAdaptersToLoadopt, …otherAdaptersToLoad) → {Promise.<AdapterRegistry>}
It creates an adapter registry with the query adapters specified by the caller.
The native query adapter is always included automatically;
it requires no external dependencies.
It combines two ways to add adapters:
-
Adapters predefined by the library: these are enabled using Boolean flags in the
defaultAdaptersToLoadparameter. Their libraries are dynamically imported when enabled, so the cost is zero if they are not used. -
Custom adapters: these are passed as rest arguments after the first parameter; each one can be a pre-constructed QueryAdapter instance object or a factory function (synchronous or asynchronous) that returns an actual QueryAdapter instance.
Parameters
-
defaultAdaptersToLoad
object<optional>
Properties
-
jsonpath
boolean<optional>
falseIt registers the intention to use the
JSONPathquery language (and imports thejsonpath-pluspackage). -
jsonata
boolean<optional>
falseIt registers the intention to use the
JSONataquery language (and imports thejsonatapackage). -
jsonataOptions
object<optional>
It configures the options for
createJsonataAdapter(timeout, ...). -
jsonquery
boolean<optional>
falseIt registers the intention to use the
JSON Queryquery language (and imports the@jsonquerylang/jsonquerypackage). -
jsonpointer
boolean<optional>
falseIt registers the intention to use the
JSON Pointer / RFC 6901query language (and imports thejson-pointerpackage). -
jmespath
boolean<optional>
falseIt registers the intention to use the
JMESPathquery language (and imports thejmespathpackage).
-
jsonpath
-
otherAdaptersToLoad
QueryAdapter|function<repeatable>
It registers the intention to use additional QueryAdapters, allowing both factory functions or already created objects.
Returns
-
Promise.<AdapterRegistry>It returns a new configure AdapterRegistry.
Example
// native only:
const r1 = await createAdapterRegistry();
// Native + JSONata (with timeout):
const r2 = await createAdapterRegistry({
jsonata: true,
jsonataOptions: { timeout: 5000 }
});
// Native + JSONPath + JSON Pointer + JMESPath:
const r3 = await createAdapterRegistry({
jsonpath: true,
jsonpointer: true,
jmespath: true,
});
// Native + another customized adapter:
const myAdapter = await createAnotherAdapter();
const r4 = await createAdapterRegistry({}, myAdapter);
Throws
-
Whenever a package cannot be imported.
Source
project(params) → {Promise.<*>}
The high level convenience function: resolves, validates, and evaluates.
It chains the three stages together in a safe order.
This is the recommended approach for standard usage.
Parameters
-
params
objectProperties
-
rootName
stringThe name for the root projection module.
-
loader
AsyncLoaderFunctionThe loader of projection modules.
-
document
*The source JSON document to project (transform).
-
registry
AdapterRegistry<optional>
Si se omite, se crea uno con sólo el adaptador nativo.
-
options
object<optional>
Properties
-
maxDepth
number<optional>
1000Maximum logical depth (to avoid infinite recursion, for example).
-
maxModules
number<optional>
1000Maximum number of loaded projection modules.
-
maxDepth
-
rootName
Returns
-
Promise.<*>The resultant JSON document obtained.
Source
Type Definitions
AsyncLoaderFunction()
typedef {(name: string) => Promise