-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmango-finder-async.plugin.ts
231 lines (219 loc) · 7.4 KB
/
mango-finder-async.plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import AbstractMangoFinder from '@/abstracts/mango-finder.abstract'
import logger from '@/config/logger'
import type {
AggregationStages,
IMangoFinderAsync,
MangoCacheFinder,
QueryCriteriaOptions
} from '@/interfaces'
import type {
AggregationPipelineResult as PipelineResult,
DocumentPartial as PartialDoc,
DocumentSortingRules,
DUID,
MangoParsedUrlQuery,
MangoSearchParams,
ProjectStage,
UID
} from '@/types'
import type {
ObjectPlain,
ObjectUnknown,
OneOrMany
} from '@flex-development/tutils'
import type { Debugger } from 'debug'
/**
* @file Plugin - MangoFinderAsync
* @module plugins/MangoFinderAsync
*/
/**
* Asynchronous plugin for [`mingo`][1] and [`qs-to-mongo`][2].
*
* Run aggregation pipelines and execute searches against in-memory object
* collections. Query documents using a URL query, or search for them using a
* query criteria and options object.
*
* [1]: https://github.com./kofrasa/mingo
* [2]: https://github.com./fox1t/qs-to-mongo
*
* @template D - Document (collection object)
* @template U - Name of document uid field
* @template P - Search parameters (query criteria and options)
* @template Q - Parsed URL query object
*
* @class
* @extends AbstractMangoFinder
* @implements {IMangoFinderAsync<D, U, P, Q>}
*/
export default class MangoFinderAsync<
D extends ObjectPlain = ObjectUnknown,
U extends string = DUID,
P extends MangoSearchParams<D> = MangoSearchParams<D>,
Q extends MangoParsedUrlQuery<D> = MangoParsedUrlQuery<D>
>
extends AbstractMangoFinder<D, U, P, Q>
implements IMangoFinderAsync<D, U, P, Q> {
/**
* @readonly
* @instance
* @property {Debugger} logger - Internal logger
*/
readonly logger: Debugger = logger.extend('plugin').extend('async')
/**
* Runs an aggregation pipeline for `this.cache.collection`.
*
* If the cache is empty, a warning will be logged to the console instructing
* developers to call `setCache`.
*
* @async
* @param {OneOrMany<AggregationStages<D>>} [pipeline] - Aggregation stage(s)
* @return {Promise<PipelineResult<D>>} Promise containing pipeline results
*/
async aggregate(
pipeline?: OneOrMany<AggregationStages<D>>
): Promise<PipelineResult<D>> {
return super.aggregate(pipeline)
}
/**
* Executes a search against `this.cache.collection`.
*
* If the cache is empty, a warning will be logged to the console instructing
* developers to call `setCache`.
*
* @async
* @param {P} [params] - Search parameters
* @param {QueryCriteriaOptions<D>} [params.options] - Search options
* @param {ProjectStage<D>} [params.options.$project] - Fields to include
* @param {number} [params.options.limit] - Limit number of results
* @param {number} [params.options.skip] - Skips the first n documents
* @param {DocumentSortingRules<D>} [params.options.sort] - Sorting rules
* @return {Promise<PartialDoc<D, U>[]>} Promise containing search results
*/
async find(params?: P): Promise<PartialDoc<D, U>[]> {
return super.find(params)
}
/**
* Finds multiple documents by id.
*
* @async
* @param {UID[]} [uids] - Array of unique identifiers
* @param {P} [params] - Search parameters
* @param {QueryCriteriaOptions<D>} [params.options] - Search options
* @param {ProjectStage<D>} [params.options.$project] - Fields to include
* @param {number} [params.options.limit] - Limit number of results
* @param {number} [params.options.skip] - Skips the first n documents
* @param {DocumentSortingRules<D>} [params.options.sort] - Sorting rules
* @return {Promise<PartialDoc<D, U>[]>} Promise containing specified docs
*/
async findByIds(uids?: UID[], params?: P): Promise<PartialDoc<D, U>[]> {
return super.findByIds(uids, params)
}
/**
* Finds a document by unique identifier.
*
* Returns `null` if the document isn't found.
*
* @async
* @param {UID} uid - Unique identifier for document
* @param {P} [params] - Search parameters
* @param {QueryCriteriaOptions<D>} [params.options] - Search options
* @param {ProjectStage<D>} [params.options.$project] - Fields to include
* @param {number} [params.options.limit] - Limit number of results
* @param {number} [params.options.skip] - Skips the first n documents
* @param {DocumentSortingRules<D>} [params.options.sort] - Sorting rules
* @return {Promise<PartialDoc<D, U> | null>} Promise containing doc or null
*/
async findOne(uid: UID, params?: P): Promise<PartialDoc<D, U> | null> {
return super.findOne(uid, params)
}
/**
* Finds a document by unique identifier.
*
* Throws an error if the document isn't found.
*
* @async
* @param {UID} uid - Unique identifier for document
* @param {P} [params] - Search parameters
* @param {QueryCriteriaOptions<D>} [params.options] - Search options
* @param {ProjectStage<D>} [params.options.$project] - Fields to include
* @param {number} [params.options.limit] - Limit number of results
* @param {number} [params.options.skip] - Skips the first n documents
* @param {DocumentSortingRules<D>} [params.options.sort] - Sorting rules
* @return {Promise<PartialDoc<D, U>>} Promise containing document
* @throws {Exception}
*/
async findOneOrFail(uid: UID, params?: P): Promise<PartialDoc<D, U>> {
return super.findOneOrFail(uid, params)
}
/**
* Queries `this.cache.collection`.
*
* If the cache is empty, a warning will be logged to the console instructing
* developers to call `setCache`.
*
* @async
* @param {Q | string} [query] - Document query object or string
* @return {Promise<PartialDoc<D, U>[]>} Promise containing search results
*/
async query(query?: Q | string): Promise<PartialDoc<D, U>[]> {
return super.query(query)
}
/**
* Queries multiple documents by unique identifier.
*
* @async
* @param {UID[]} [uids] - Array of unique identifiers
* @param {Q | string} [query] - Document query object or string
* @return {Promise<PartialDoc<D, U>[]>} Promise containing specified docs
*/
async queryByIds(
uids?: UID[],
query?: Q | string
): Promise<PartialDoc<D, U>[]> {
return super.queryByIds(uids, query)
}
/**
* Queries a document by unique identifier.
*
* Returns `null` if the document isn't found.
*
* @async
* @param {UID} uid - Unique identifier for document
* @param {Q | string} [query] - Document query object or string
* @return {Promise<PartialDoc<D, U> | null>} Promise containing doc or null
*/
async queryOne(
uid: UID,
query?: Q | string
): Promise<PartialDoc<D, U> | null> {
return super.queryOne(uid, query)
}
/**
* Queries a document by id.
*
* Throws an error if the document isn't found.
*
* @async
* @param {UID} uid - Unique identifier for document
* @param {Q | string} [query] - Document query object or string
* @return {Promise<PartialDoc<D, U>>} Promise containing document
*/
async queryOneOrFail(
uid: UID,
query?: Q | string
): Promise<PartialDoc<D, U>> {
return super.queryOneOrFail(uid, query)
}
/**
* Overwrites the data collection cache.
*
* If `collection` isn't defined, the cache will be cleared.
*
* @async
* @param {D[]} [collection] - Documents to overwrite cache
* @return {Promise<MangoCacheFinder<D>>} Promise containing copy of new cache
*/
async setCache(collection?: D[]): Promise<MangoCacheFinder<D>> {
return super.setCache(collection)
}
}