-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmango-finder.plugin.ts
201 lines (190 loc) · 6.84 KB
/
mango-finder.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
import AbstractMangoFinder from '@/abstracts/mango-finder.abstract'
import type {
AggregationStages,
IMangoFinder,
MangoCacheFinder,
QueryCriteriaOptions
} from '@/interfaces'
import type {
AggregationPipelineResult,
DocumentPartial,
DocumentSortingRules,
DUID,
MangoParsedUrlQuery,
MangoSearchParams,
ProjectStage,
UID
} from '@/types'
import type {
ObjectPlain,
ObjectUnknown,
OneOrMany
} from '@flex-development/tutils'
/**
* @file Plugin - MangoFinder
* @module plugins/MangoFinder
*/
/**
* Synchronous 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 {IMangoFinder<D, U, P, Q>}
*/
export default class MangoFinder<
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 IMangoFinder<D, U, P, Q> {
/**
* 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`.
*
* @param {OneOrMany<AggregationStages<D>>} [pipeline] - Aggregation stage(s)
* @return {AggregationPipelineResult<D>} Pipeline results
*/
aggregate(
pipeline?: OneOrMany<AggregationStages<D>>
): AggregationPipelineResult<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`.
*
* @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 {DocumentPartial<D, U>[]} Search results
*/
find(params?: P): DocumentPartial<D, U>[] {
return super.find(params) as DocumentPartial<D, U>[]
}
/**
* Finds multiple documents by id.
*
* @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 {DocumentPartial<D, U>[]} Documents
*/
findByIds(uids?: UID[], params?: P): DocumentPartial<D, U>[] {
return super.findByIds(uids, params) as DocumentPartial<D, U>[]
}
/**
* Finds a document by unique identifier.
*
* Returns `null` if the document isn't found.
*
* @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 {DocumentPartial<D, U> | null} Document or null
*/
findOne(uid: UID, params?: P): DocumentPartial<D, U> | null {
return super.findOne(uid, params) as DocumentPartial<D, U> | null
}
/**
* Finds a document by unique identifier.
*
* Throws an error if the document isn't found.
*
* @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 {DocumentPartial<D, U>} Document
*/
findOneOrFail(uid: UID, params?: P): DocumentPartial<D, U> {
return super.findOneOrFail(uid, params) as DocumentPartial<D, U>
}
/**
* Queries `this.cache.collection`.
*
* If the cache is empty, a warning will be logged to the console instructing
* developers to call `setCache`.
*
* @param {Q | string} [query] - Document query object or string
* @return {DocumentPartial<D, U>[]} Search results
*/
query(query?: Q | string): DocumentPartial<D, U>[] {
return super.query(query) as DocumentPartial<D, U>[]
}
/**
* Queries multiple documents by unique identifier.
*
* @param {UID[]} [uids] - Array of unique identifiers
* @param {Q | string} [query] - Document query object or string
* @return {DocumentPartial<D, U>[]} Documents
*/
queryByIds(uids?: UID[], query?: Q | string): DocumentPartial<D, U>[] {
return super.queryByIds(uids, query) as DocumentPartial<D, U>[]
}
/**
* Queries a document by unique identifier.
*
* Returns `null` if the document isn't found.
*
* @param {UID} uid - Unique identifier for document
* @param {Q | string} [query] - Document query object or string
* @return {DocumentPartial<D, U> | null} Document or null
*/
queryOne(uid: UID, query?: Q | string): DocumentPartial<D, U> | null {
return super.queryOne(uid, query) as DocumentPartial<D, U> | null
}
/**
* Queries a document by id.
*
* Throws an error if the document isn't found.
*
* @param {UID} uid - Unique identifier for document
* @param {Q | string} [query] - Document query object or string
* @return {DocumentPartial<D, U>} Document
*/
queryOneOrFail(uid: UID, query?: Q | string): DocumentPartial<D, U> {
return super.queryOneOrFail(uid, query) as DocumentPartial<D, U>
}
/**
* Overwrites the data collection cache.
* If `collection` isn't defined, the cache will be cleared.
*
* @param {D[]} [collection] - Documents to overwrite cache
* @return {MangoCacheFinder<D>} Copy of new cache
*/
setCache(collection?: D[]): MangoCacheFinder<D> {
return super.setCache(collection) as MangoCacheFinder<D>
}
}