1 | import {
|
2 | TypeStoreFinderKey,
|
3 | TypeStoreRepoKey,
|
4 | TypeStoreFindersKey
|
5 | } from "./Constants"
|
6 |
|
7 | import {
|
8 | IModel,
|
9 | IFinderOptions,
|
10 | IndexAction,
|
11 | IRepoOptions,
|
12 | IIndexOptions,
|
13 | ISearchProvider,
|
14 | IPlugin,
|
15 | IModelMapper,
|
16 | ISearchOptions,
|
17 | IRepoPlugin,
|
18 | IFinderPlugin,
|
19 | IIndexerPlugin,
|
20 | ModelPersistenceEventType,
|
21 | PluginType,
|
22 | IPredicate
|
23 | } from "./Types"
|
24 |
|
25 | import {Coordinator} from './Coordinator'
|
26 | import {NotImplemented} from "./Errors"
|
27 | import * as Log from './log'
|
28 |
|
29 | import {
|
30 | isFunction,
|
31 | isFinderPlugin,
|
32 | PluginFilter,
|
33 | PromiseMap
|
34 | } from "./Util"
|
35 |
|
36 | import {ModelMapper,getDefaultMapper} from "./ModelMapper"
|
37 | import {IModelType} from "./ModelTypes"
|
38 | import {IModelOptions, IModelKey, IKeyValue, TKeyValue, IModelAttributeOptions} from "./decorations/ModelDecorations";
|
39 | import {getMetadata} from "./MetadataManager";
|
40 |
|
41 |
|
42 | const log = Log.create(__filename)
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | export class Repo<M extends IModel> {
|
55 |
|
56 | modelOpts:IModelOptions
|
57 | repoOpts:IRepoOptions
|
58 | modelType:IModelType
|
59 | mapper
|
60 | coordinator:Coordinator
|
61 | protected plugins = Array<IPlugin>()
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | constructor(public repoClazz:any,public modelClazz:{new ():M;}) {
|
71 | }
|
72 |
|
73 | protected getRepoPlugins() {
|
74 | return PluginFilter<IRepoPlugin<M>>(this.plugins,PluginType.Repo)
|
75 |
|
76 |
|
77 | }
|
78 |
|
79 | protected getFinderPlugins():IFinderPlugin[] {
|
80 | return PluginFilter<IFinderPlugin>(this.plugins,PluginType.Finder)
|
81 | }
|
82 |
|
83 | attr(name:string):IModelAttributeOptions {
|
84 | return this.modelType.options.attrs.find(attr => attr.name === name)
|
85 | }
|
86 |
|
87 | init(coordinator) {
|
88 | this.coordinator = coordinator
|
89 | this.modelType = coordinator.getModel(this.modelClazz)
|
90 | this.modelOpts = this.modelType.options
|
91 | this.repoOpts = Reflect.getMetadata(TypeStoreRepoKey,this.repoClazz) || {}
|
92 |
|
93 | }
|
94 |
|
95 | start() {
|
96 |
|
97 | this.mapper = this.getMapper(this.modelClazz)
|
98 |
|
99 |
|
100 | this.decorateFinders()
|
101 | }
|
102 |
|
103 |
|
104 | getMapper<M extends IModel>(clazz:{new():M;}):IModelMapper<M> {
|
105 | return getDefaultMapper(clazz)
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | attach(plugin:IPlugin):this {
|
118 | if (this.plugins.includes(plugin)) {
|
119 | log.warn(`Trying to register repo plugin a second time`)
|
120 | } else {
|
121 | this.plugins.push(plugin)
|
122 | }
|
123 |
|
124 | return this
|
125 | }
|
126 |
|
127 | getFinderOptions(finderKey:string):IFinderOptions {
|
128 | return getMetadata(
|
129 | TypeStoreFinderKey,
|
130 | this,
|
131 | finderKey
|
132 | ) as IFinderOptions
|
133 | }
|
134 |
|
135 | getPlugins = (predicate:IPredicate) => this.plugins.filter(predicate)
|
136 |
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | decorateFinder(finderKey) {
|
145 | let finder
|
146 |
|
147 |
|
148 | for (let plugin of this.getPlugins(isFinderPlugin)) {
|
149 | if (!isFunction((plugin as any).decorateFinder))
|
150 | continue
|
151 |
|
152 | const finderPlugin = plugin as IFinderPlugin
|
153 |
|
154 |
|
155 |
|
156 | if (finder = finderPlugin.decorateFinder(this,finderKey)) {
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | break
|
178 | }
|
179 | }
|
180 |
|
181 | if (!finder && this.getFinderOptions(finderKey).optional !== true)
|
182 | NotImplemented(`No plugin supports this finder ${finderKey}`)
|
183 |
|
184 | this.setFinder(finderKey,finder)
|
185 | }
|
186 |
|
187 | |
188 |
|
189 |
|
190 | decorateFinders() {
|
191 | (Reflect.getMetadata(TypeStoreFindersKey,this) || [])
|
192 | .forEach(finderKey => this.decorateFinder(finderKey))
|
193 |
|
194 | }
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | makeGenericFinder(
|
207 | finderKey:string,
|
208 | searchProvider:ISearchProvider,
|
209 | searchOpts:ISearchOptions<any>
|
210 | ) {
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 | const opts:IFinderOptions = this.getFinderOptions(finderKey)
|
217 |
|
218 | return async (...args) => {
|
219 | let results = await searchProvider.search(
|
220 | this.modelType,
|
221 | searchOpts,
|
222 | args
|
223 | )
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | const keys:IModelKey[] = results.map((result:any) => {
|
229 | return searchOpts.resultKeyMapper(
|
230 | this,
|
231 | searchOpts.resultType,
|
232 | result
|
233 | )
|
234 | })
|
235 |
|
236 | return keys.map(async (key) => await this.get(key))
|
237 |
|
238 | }
|
239 | }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | protected setFinder(finderKey:string,finderFn:(...args) => any) {
|
248 | this[finderKey] = finderFn
|
249 | }
|
250 |
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | triggerPersistenceEvent(type:ModelPersistenceEventType,...models:any[]) {
|
259 | if (models.length < 1)
|
260 | return
|
261 |
|
262 | const {onPersistenceEvent} = this.modelType.options
|
263 | onPersistenceEvent && onPersistenceEvent(type,...models)
|
264 | }
|
265 |
|
266 | supportPersistenceEvents() {
|
267 | const {onPersistenceEvent} = this.modelType.options
|
268 | return typeof onPersistenceEvent !== 'undefined' && onPersistenceEvent !== null
|
269 | }
|
270 |
|
271 | |
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | async index(type:IndexAction,...models:IModel[]):Promise<boolean> {
|
279 | const indexPlugins = PluginFilter<IIndexerPlugin>(this.plugins,PluginType.Indexer)
|
280 |
|
281 | const doIndex = (indexConfig:IIndexOptions):Promise<any>[] => {
|
282 | return indexPlugins.map(plugin => plugin.index(
|
283 | type,
|
284 | indexConfig,
|
285 | this.modelType,
|
286 | this,
|
287 | ...models
|
288 | ))
|
289 | }
|
290 |
|
291 |
|
292 | if (this.repoOpts && this.repoOpts.indexes)
|
293 | await Promise.all(this.repoOpts.indexes.reduce((promises,indexConfig) => {
|
294 | return promises.concat(doIndex(indexConfig))
|
295 | },[]))
|
296 |
|
297 | return Promise.resolve(true)
|
298 | }
|
299 |
|
300 | indexPromise(action:IndexAction) {
|
301 | return async (models:M[]) => {
|
302 | const indexPromise = this.index(action,...models.filter((model) => !!model))
|
303 |
|
304 | await Promise.resolve(indexPromise)
|
305 | return models
|
306 | }
|
307 | }
|
308 |
|
309 | |
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | key(...args):IKeyValue {
|
316 | for (let plugin of this.getRepoPlugins()) {
|
317 | const key = plugin.key(...args)
|
318 | if (key)
|
319 | return key
|
320 | }
|
321 |
|
322 | return NotImplemented('key')
|
323 | }
|
324 |
|
325 | |
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | async get(key:TKeyValue):Promise<M> {
|
332 |
|
333 | let results = this.getRepoPlugins().map(async (plugin) => await plugin.get(key))
|
334 | for (let result of results) {
|
335 | if (result)
|
336 | return result
|
337 | }
|
338 |
|
339 | return null
|
340 | }
|
341 |
|
342 |
|
343 |
|
344 | |
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | async save(o:M):Promise<M> {
|
351 | let results = await PromiseMap(this.getRepoPlugins(), plugin => plugin.save(o))
|
352 | await this.indexPromise(IndexAction.Add)(results)
|
353 | for (let result of results) {
|
354 | if (result)
|
355 | return result
|
356 | }
|
357 |
|
358 | return null
|
359 |
|
360 | }
|
361 |
|
362 |
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | async remove(key:TKeyValue):Promise<any> {
|
370 | let model = await this.get(key)
|
371 | if (!model) {
|
372 | log.warn(`No model found to remove with key`,key)
|
373 | return null
|
374 | }
|
375 |
|
376 | await PromiseMap(this.getRepoPlugins(), plugin => plugin.remove(key))
|
377 | return this.indexPromise(IndexAction.Remove)([model])
|
378 |
|
379 | }
|
380 |
|
381 |
|
382 | |
383 |
|
384 |
|
385 |
|
386 |
|
387 | async count():Promise<number> {
|
388 | let results = await Promise.all(this.getRepoPlugins().map(async (plugin) => await plugin.count()))
|
389 | return results.reduce((prev,current) => prev + current)
|
390 |
|
391 | }
|
392 |
|
393 | async bulkGet(...keys:TKeyValue[]):Promise<M[]> {
|
394 | let results = await PromiseMap(
|
395 | this.getRepoPlugins(), plugin => plugin.bulkGet(...keys)
|
396 | )
|
397 |
|
398 | return results.reduce((allResults,result) => {
|
399 | return allResults.concat(result)
|
400 | },[])
|
401 |
|
402 | }
|
403 |
|
404 | async bulkSave(...models:M[]):Promise<M[]> {
|
405 | let results = await PromiseMap(
|
406 | this.getRepoPlugins(), plugin => plugin.bulkSave(...models)
|
407 | )
|
408 |
|
409 | results = results.reduce((allResults,result) => {
|
410 | return allResults.concat(result)
|
411 | },[])
|
412 |
|
413 | return this.indexPromise(IndexAction.Add)(results)
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | }
|
423 |
|
424 | async bulkRemove(...keys:TKeyValue[]):Promise<any[]> {
|
425 | const models = await this.bulkGet(...keys)
|
426 | if (models.length != keys.length)
|
427 | throw new Error('Not all keys exist')
|
428 |
|
429 | await PromiseMap(
|
430 | this.getRepoPlugins(), plugin => plugin.bulkRemove(...keys)
|
431 | )
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | return this.indexPromise(IndexAction.Remove)(models)
|
438 |
|
439 |
|
440 | }
|
441 | }
|
442 |
|