Skip to content

createDecisionLoader()

type createDecisionLoader = (
schemas: SchemaConfig[],
sources: (DecisionSource | string)[],
moduleResolver: (moduleName: string) => Promise<string>,
) => () => Promise<StaticDecisionStore>;

Return value

() => Promise< StaticDecisionStore>

A loader function that resolves with a populated decision store.

This loader caches the promise on the first call. Any subsequent calls do not result in any schemas or input files being reloaded, yielding the same result as the first one.

Loader implementation

Given a list of paths and schemas configs:

  • Loads schemas from paths and creates a schema map.
  • Validates the schema map and creates a validator.
  • Loads all decisions files and merges all data.
  • Validates decision inputs and returns a StaticDecisionStore.

Params

schemas: SchemaConfig[]

A list of schema configurations to load validation schemas from.

sources: Array<DecisionSource | string>

A list of data sources to load decision data from.

moduleResolver: (moduleName: string) => Promise<string>

A function to resolve the physical location of node modules containing the schemas.

Usage

import { DECISION_SCHEMAS } from '@noodlestan/designer-schemas';
const DATA_PATH = path.resolve('./data/decisions');
const loader = createDecisionLoader(
[DECISION_SCHEMAS],
[DATA_PATH],
async moduleName => `./node_modules/${moduleName}`,
);
const store = await loader();