UNPKG

3.62 kBTypeScriptView Raw
1import './Globals';
2import { IModel, IModelType } from "./ModelTypes";
3import { Repo } from "./Repo";
4import { ICoordinator, ICoordinatorOptions } from "./Types";
5import { IModelKey, IKeyValue, TKeyValue } from "./decorations/ModelDecorations";
6/**
7 * Model persistence events
8 */
9export declare enum PluginEventType {
10 RepoInit = 1,
11 ModelRegister = 2,
12}
13/**
14 * Indexer options
15 */
16export interface IIndexOptions {
17 fields: string[];
18}
19/**
20 * Different indexing actions
21 */
22export declare enum IndexAction {
23 Add = 0,
24 Update = 1,
25 Remove = 2,
26}
27/**
28 * Responsible for indexing given models
29 */
30export interface IIndexerPlugin extends IRepoSupportPlugin {
31 /**
32 * Called in persistence chain after put/save
33 * before return.
34 *
35 * Note: indexing can be done asynchronously based on your
36 * requirements, but we suggest whenever possible to do this sync
37 *
38 * Obviously if you have a high write throughput solution
39 * THIS IS A BAD IDEA - do indexing async or offline
40 *
41 * @param modelType
42 * @param options
43 * @param modelType
44 * @param repo
45 */
46 index<M extends IModel>(type: IndexAction, options: IIndexOptions, modelType: IModelType, repo: Repo<M>, ...models: IModel[]): Promise<boolean>;
47}
48/**
49 * Maps search results to keys for a given repo
50 */
51export declare type ISearchResultToKeyMapper<R> = (repo: Repo<any>, resultType: {
52 new (): R;
53}, result: R) => IModelKey;
54/**
55 * Super simply default key mapper for search results
56 * field names in, key out, must all be top level in result object
57 *
58 * @param fields
59 * @returns {function(Repo<any>, {new(): R}, R): IModelKey}
60 * @constructor
61 */
62export declare function DefaultKeyMapper<R extends any>(...fields: any[]): ISearchResultToKeyMapper<R>;
63/**
64 * Custom search options for search(s)
65 */
66export interface ISearchOptions<R extends any> {
67 resultType: {
68 new (): R;
69 };
70 resultKeyMapper: ISearchResultToKeyMapper<R>;
71}
72/**
73 * Custom external search provider
74 */
75export interface ISearchProvider extends IRepoSupportPlugin {
76 search<R extends any>(modelType: IModelType, opts: ISearchOptions<R>, ...args: any[]): Promise<R[]>;
77}
78/**
79 * A plugin that supports specific models
80 */
81export interface IModelSupportPlugin extends IPlugin {
82 supportedModels: any[];
83}
84/**
85 * A plugin that provides repo capabilities
86 */
87export interface IRepoSupportPlugin extends IModelSupportPlugin {
88 initRepo<T extends Repo<M>, M extends IModel>(repo: T): T;
89}
90/**
91 * Store interface that must be fulfilled for
92 * a valid store to work
93 */
94export interface IStorePlugin extends IRepoSupportPlugin {
95 syncModels(): Promise<ICoordinator>;
96}
97/**
98 * Plugin that provides finders
99 */
100export interface IFinderPlugin extends IModelSupportPlugin {
101 decorateFinder(repo: Repo<any>, finderKey: string): any;
102}
103export interface IRepoPlugin<M extends IModel> extends IModelSupportPlugin {
104 key?(...args: any[]): IKeyValue;
105 get(key: TKeyValue): Promise<M>;
106 bulkGet(...keys: TKeyValue[]): Promise<M[]>;
107 save(o: M): Promise<M>;
108 bulkSave(...o: M[]): Promise<M[]>;
109 remove(key: TKeyValue): Promise<any>;
110 bulkRemove(...key: TKeyValue[]): Promise<any[]>;
111 count(): Promise<number>;
112}
113export declare enum PluginType {
114 Indexer = 1,
115 Store = 2,
116 Repo = 4,
117 Finder = 8,
118}
119export interface IPlugin {
120 type: PluginType;
121 handle(eventType: PluginEventType, ...args: any[]): boolean | any;
122 init(coordinator: ICoordinator, opts: ICoordinatorOptions): Promise<ICoordinator>;
123 start(): Promise<ICoordinator>;
124 stop(): Promise<ICoordinator>;
125}