UNPKG

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