@getanthill/datastore
Version:
Event-Sourced Datastore
228 lines (227 loc) • 6.8 kB
TypeScript
import type Datastore from '../Datastore';
import Ajv from 'ajv';
import * as jsonpatch from 'fast-json-patch';
import { MultiQuery } from '../utils';
/**
* Type of steps
* - fetch
* - map
* - unset
* - validate
* - each
* - filter
* - op
*
* - parallel
* - request
* - json_patch
*/
export declare const STEP_TYPE_FETCH = "fetch";
export declare const STEP_TYPE_JSON_PATCH = "json_patch";
export declare const STEP_TYPE_MAP = "map";
export declare const STEP_TYPE_UNSET = "unset";
export declare const STEP_TYPE_VALIDATE = "validate";
export declare const STEP_TYPE_EACH = "each";
export declare const STEP_TYPE_IF = "if";
export declare const STEP_TYPE_FILTER = "filter";
export declare const STEP_TYPE_OP = "op";
export declare const STEP_TYPE_PERSIST = "persist";
export declare const STEP_TYPE_FROM = "from";
export declare const DEFAULT_STEP_TYPES: readonly string[];
export interface StepDefinition {
handler: Function;
schema?: object;
name?: string;
description?: string;
}
export interface StepMapItem {
from: string;
to: string;
default: any;
must_hash?: boolean;
json_stringify?: boolean;
relative_date_in_seconds?: number;
}
export interface StepFetch {
type: string;
name?: string;
description?: string;
destination?: string;
datastore?: string;
model: string;
source?: MultiQuery['source'];
query?: object;
headers?: any;
map?: StepMapItem[];
timetravel?: string;
correlation_field?: string;
page?: number;
page_size?: number;
as_entity?: boolean;
must_decrypt?: boolean;
default?: any;
}
export interface StepPersist {
type: string;
name?: string;
description?: string;
destination: string;
datastore?: string;
model: string;
correlation_field?: string;
payload: any;
headers?: any;
map?: StepMapItem[];
imperative_version_next?: string;
}
export interface StepJsonPatch {
type: string;
name?: string;
description?: string;
patch: jsonpatch.Operation[];
}
export interface StepMap {
type: string;
name?: string;
description?: string;
map: StepMapItem[];
}
/**
* @deprecated in favor of JSON Patch
*/
export interface StepUnset {
type: string;
name?: string;
description?: string;
path: string;
}
export interface StepValidate {
type: string;
name?: string;
description?: string;
schema: object;
path?: string;
must_throw?: boolean;
destination?: string;
}
export interface StepEach {
type: string;
name?: string;
description?: string;
path: string;
pipeline: Step[];
destination?: string;
}
export interface StepIf {
type: string;
name?: string;
description?: string;
path: string;
schema: object;
pipeline: Step[];
destination?: string;
repeat_while_true?: boolean;
max_iteration_count?: number;
}
export interface StepFilter {
type: string;
name?: string;
description?: string;
path: string;
schema: object;
destination?: string;
map?: StepMapItem[];
as_entity?: boolean;
default?: any;
}
export interface StepOpArg {
path: string;
default: number;
func: string;
}
export interface StepOp {
type: string;
name?: string;
description?: string;
func: string;
args: StepOpArg[];
path?: string;
destination?: string;
default?: any;
args_as_array?: boolean;
}
export interface StepFrom {
type: string;
name?: string;
description?: string;
path: string;
}
export type Step = StepFetch | StepJsonPatch | StepMap | StepUnset | StepValidate | StepEach | StepIf | StepFilter | StepOp | StepFrom;
export type Aggregation = {
[key: string]: any;
};
export default class Aggregator {
datastores: Map<string, Datastore>;
steps: Map<string, StepDefinition>;
logs: {
level: string;
msg: string;
ts: number;
context?: any;
}[];
static pipelineValidator: Ajv;
static ERROR_INVALID_PIPELINE_DEFINITION: Error;
static ERROR_INVALID_STEP_TYPE: Error;
static ERROR_ENTITY_NOT_FOUND: Error;
static ERROR_DESTINATION_UNDEFINED: Error;
static ERROR_CONFLICT_STEP_TYPE: Error;
static ERROR_CONTRACT_ERROR_STEP_TYPE: Error;
static ERROR_VALIDATE_STEP_FAILED: Error;
static ERROR_ITERATE_STEP_IS_NOT_ARRAY: Error;
static ERROR_OP_STEP_INVALID_VALUE: Error;
static ERRORS: Error[];
config: {
max_retry: number;
};
metrics: {
started_at_in_ms?: number;
ended_at_in_ms?: number;
elapsed_time_in_ms?: number;
iteration?: number;
};
private _pipelineValidator;
constructor(datastores: Map<string, Datastore>, config?: {
max_retry: number;
});
static _customizer(objValue: any, srcValue: any): any[] | undefined;
get pipelineValidator(): Ajv;
log(level: string, msg: string, context?: any): void;
addStepType(stepType: string, stepDefinition: StepDefinition): this;
removeStepType(stepType: string): this;
updateValidator(): void;
mergeData(step: StepFetch | StepFilter, data?: Aggregation, results?: object[], destination?: string | undefined): Aggregation;
static ok(condition: boolean, message: string): void;
static hash(value: any): string;
applyPatch(data: Aggregation, patch: jsonpatch.Operation[]): Aggregation;
applyMap(source?: object, map?: StepMapItem[], data?: Aggregation | null): object;
fetch(step: StepFetch, data?: Aggregation): Promise<object[]>;
getDatastore(datastore: string | undefined): Datastore;
persist(step: StepPersist, data?: Aggregation): Promise<any>;
runStepFetch(step: StepFetch, data?: Aggregation): Promise<Aggregation>;
runStepPersist(step: StepPersist, data?: Aggregation): Promise<Aggregation>;
runStepJsonPatch(step: StepJsonPatch, data?: Aggregation): Promise<Aggregation>;
runStepMap(step: StepMap, data?: Aggregation): Promise<Aggregation>;
runStepUnset(step: StepUnset, data?: Aggregation): Promise<Aggregation>;
runStepValidate(step: StepValidate, data?: Aggregation): Promise<Aggregation>;
runStepEach(step: StepEach, data?: Aggregation): Promise<Aggregation>;
runStepIf(step: StepIf, data?: Aggregation, i?: number): Promise<Aggregation>;
runStepFilter(step: StepFilter, data?: Aggregation): Promise<Aggregation>;
/**
* @alpha
*/
runStepOp(step: StepOp, data?: Aggregation | null): Promise<Aggregation>;
runStepFrom(step: StepFrom, data?: Aggregation): Promise<Aggregation>;
runStep(step: Step, data?: Aggregation): Promise<Aggregation>;
validate(pipeline: Step[]): void;
aggregate(pipeline: Step[], initialData?: Aggregation, iteration?: number): Promise<Aggregation>;
}