@thisisagile/easy-mongo 17.25.1 → 17.25.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +587 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -5
- package/dist/AtlasSearchGateway.mjs +0 -59
- package/dist/AtlasSearchGateway.mjs.map +0 -1
- package/dist/Collection.mjs +0 -57
- package/dist/Collection.mjs.map +0 -1
- package/dist/Lucene.mjs +0 -8
- package/dist/Lucene.mjs.map +0 -1
- package/dist/MongoGateway.mjs +0 -9
- package/dist/MongoGateway.mjs.map +0 -1
- package/dist/MongoProvider.mjs +0 -8
- package/dist/MongoProvider.mjs.map +0 -1
- package/dist/Stages.mjs +0 -18
- package/dist/Stages.mjs.map +0 -1
- package/dist/Utils.mjs +0 -7
- package/dist/Utils.mjs.map +0 -1
- package/dist/chunk-GFBKWYDV.mjs +0 -65
- package/dist/chunk-GFBKWYDV.mjs.map +0 -1
- package/dist/chunk-J3OXGWPD.mjs +0 -129
- package/dist/chunk-J3OXGWPD.mjs.map +0 -1
- package/dist/chunk-MZE7UWQC.mjs +0 -13
- package/dist/chunk-MZE7UWQC.mjs.map +0 -1
- package/dist/chunk-P372VCR6.mjs +0 -157
- package/dist/chunk-P372VCR6.mjs.map +0 -1
- package/dist/chunk-WEJO6T5Q.mjs +0 -155
- package/dist/chunk-WEJO6T5Q.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,588 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// src/Stages.ts
|
|
2
|
+
import {
|
|
3
|
+
asNumber,
|
|
4
|
+
asString,
|
|
5
|
+
ifDefined,
|
|
6
|
+
ifNotEmpty,
|
|
7
|
+
isDefined,
|
|
8
|
+
isPresent,
|
|
9
|
+
isPrimitive,
|
|
10
|
+
isString,
|
|
11
|
+
meta as meta2,
|
|
12
|
+
ofGet,
|
|
13
|
+
on,
|
|
14
|
+
toArray,
|
|
15
|
+
use
|
|
16
|
+
} from "@thisisagile/easy";
|
|
17
|
+
|
|
18
|
+
// src/Utils.ts
|
|
19
|
+
import { choose, DateTime, isArray, isDate, isDateTime, isIsoDateString, isObject, meta } from "@thisisagile/easy";
|
|
20
|
+
var toMongoType = (input) => choose(input).type(isIsoDateString, (i) => new DateTime(i).toDate()).type(isDateTime, (i) => i.toDate()).type(isArray, (a) => a.map((i) => toMongoType(i))).type(isDate, (d) => d).type(
|
|
21
|
+
isObject,
|
|
22
|
+
(o) => Object.fromEntries(
|
|
23
|
+
meta(o).entries().map(([k, i]) => [k, toMongoType(i)])
|
|
24
|
+
)
|
|
25
|
+
).else(input);
|
|
26
|
+
|
|
27
|
+
// src/Stages.ts
|
|
28
|
+
var asc = 1;
|
|
29
|
+
var desc = -1;
|
|
30
|
+
var FilterBuilder = class {
|
|
31
|
+
constructor(filters) {
|
|
32
|
+
this.filters = filters;
|
|
33
|
+
}
|
|
34
|
+
from = (q = {}) => stages.match.match(
|
|
35
|
+
meta2(q).entries().reduce((acc, [key, value]) => ({ ...acc, ...ifDefined(this.filters[key], (f) => f(value)) }), {})
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
var SortBuilder = class {
|
|
39
|
+
constructor(sorts) {
|
|
40
|
+
this.sorts = sorts;
|
|
41
|
+
}
|
|
42
|
+
get keys() {
|
|
43
|
+
return Object.keys(this.sorts);
|
|
44
|
+
}
|
|
45
|
+
from = (s = {}, alt) => stages.sort.sort(this.sorts[s?.s ?? ""] ?? this.sorts[alt ?? ""]);
|
|
46
|
+
};
|
|
47
|
+
var IncludeBuilder = class {
|
|
48
|
+
constructor(includes) {
|
|
49
|
+
this.includes = includes;
|
|
50
|
+
}
|
|
51
|
+
get keys() {
|
|
52
|
+
return Object.keys(this.includes);
|
|
53
|
+
}
|
|
54
|
+
from = (i = {}, alt) => stages.project.include(...this.includes[i?.i ?? ""] ?? this.includes[alt ?? ""] ?? []);
|
|
55
|
+
};
|
|
56
|
+
var escapeRegex = (s) => s.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
|
57
|
+
var stages = {
|
|
58
|
+
root: "$$ROOT",
|
|
59
|
+
current: "$$CURRENT",
|
|
60
|
+
id: "_id",
|
|
61
|
+
decode: {
|
|
62
|
+
object: (f) => use(Object.entries(f)[0], ([k, v]) => ofGet(v, k)),
|
|
63
|
+
fields: (f) => Object.entries(f).reduce((res, [k, v]) => on(res, (r) => ifDefined(ofGet(v, k), (nv) => r[k] = nv)), {}),
|
|
64
|
+
fieldsArrays: (f) => Object.entries(f).reduce((res, [k, v]) => on(res, (r) => r[k] = use(toArray(v), (vs) => vs.map((v2) => ofGet(v2, k)))), {}),
|
|
65
|
+
id: (f) => isString(f) ? `$${asString(f)}` : isPrimitive(f) ? f : Object.entries(f).map(([k, v]) => ofGet(v, k))[0]
|
|
66
|
+
},
|
|
67
|
+
match: {
|
|
68
|
+
match: (f) => ({ $match: stages.decode.fields(f) }),
|
|
69
|
+
filter: (filters) => new FilterBuilder(filters),
|
|
70
|
+
or: (...filters) => ({ $or: toArray(filters).map((f) => stages.decode.object(f)) }),
|
|
71
|
+
gt: (value) => ({ $gt: value }),
|
|
72
|
+
gte: (value) => ({ $gte: value }),
|
|
73
|
+
lt: (value) => ({ $lt: value }),
|
|
74
|
+
lte: (value) => ({ $lte: value }),
|
|
75
|
+
isIn: (value, separator = ",") => ({ $in: isString(value) ? value.split(separator) : value }),
|
|
76
|
+
notIn: (value, separator = ",") => ({ $nin: isString(value) ? value.split(separator) : value }),
|
|
77
|
+
after: (date) => stages.match.gte(toMongoType(date)),
|
|
78
|
+
before: (date) => stages.match.lt(toMongoType(date)),
|
|
79
|
+
anywhere: (q) => ({ $regex: escapeRegex(q), $options: "i" }),
|
|
80
|
+
money: (currency, value) => (key) => ({
|
|
81
|
+
[`${key}.currency`]: currency.id,
|
|
82
|
+
...stages.decode.fields({ [`${key}.value`]: value })
|
|
83
|
+
})
|
|
84
|
+
},
|
|
85
|
+
sort: {
|
|
86
|
+
sort: ($sort) => isPresent($sort) ? { $sort } : void 0,
|
|
87
|
+
sorter: (sorts) => new SortBuilder(sorts),
|
|
88
|
+
asc: (key) => stages.sort.sort({ [key]: asc }),
|
|
89
|
+
desc: (key) => stages.sort.sort({ [key]: desc })
|
|
90
|
+
},
|
|
91
|
+
group: {
|
|
92
|
+
group: (fields) => ({
|
|
93
|
+
by: (by) => ({ $group: Object.assign({ _id: stages.decode.id(by) }, stages.decode.fields(fields)) })
|
|
94
|
+
}),
|
|
95
|
+
date: (format = "%Y-%m-%d") => (key) => ({ $dateToString: { date: `$${key}`, format } }),
|
|
96
|
+
count: () => ({ $count: {} }),
|
|
97
|
+
sum: (from) => isDefined(from) ? { $sum: `$${from}` } : { $sum: 1 },
|
|
98
|
+
avg: (from) => ({ $avg: `$${from}` }),
|
|
99
|
+
multiply: (...multiply) => ({ $multiply: multiply.map((m) => `$${m}`) }),
|
|
100
|
+
first: (from) => ({ $first: `$${from}` }),
|
|
101
|
+
last: (from) => ({ $last: `$${from}` }),
|
|
102
|
+
min: (from) => ({ $min: `$${from}` }),
|
|
103
|
+
max: (from) => ({ $max: `$${from}` }),
|
|
104
|
+
addToSet: (from) => ({ $addToSet: `$${from}` }),
|
|
105
|
+
push: (from = "$ROOT") => ({ $push: `$${from}` }),
|
|
106
|
+
size: (from) => ({ $size: `$${from}` })
|
|
107
|
+
},
|
|
108
|
+
search: {
|
|
109
|
+
search: (f) => ifDefined(stages.decode.id(f), ($search) => ({ $search })),
|
|
110
|
+
auto: (value) => (key) => ifDefined(value, (v) => ({ autocomplete: { path: key, query: [v] } })),
|
|
111
|
+
fuzzy: (value, maxEdits = 1) => (key) => ifDefined(value, (v) => ({
|
|
112
|
+
text: {
|
|
113
|
+
query: v,
|
|
114
|
+
path: key === "wildcard" ? { wildcard: "*" } : key,
|
|
115
|
+
fuzzy: { maxEdits }
|
|
116
|
+
}
|
|
117
|
+
}))
|
|
118
|
+
},
|
|
119
|
+
set: {
|
|
120
|
+
set: (f) => ({ $set: stages.decode.fields(f) }),
|
|
121
|
+
score: () => ({ $meta: "searchScore" })
|
|
122
|
+
},
|
|
123
|
+
skip: {
|
|
124
|
+
skip: (o = {}) => ifDefined(o.skip, { $skip: asNumber(o.skip) }),
|
|
125
|
+
take: (o = {}) => ifDefined(o.take, { $limit: asNumber(o.take) })
|
|
126
|
+
},
|
|
127
|
+
project: {
|
|
128
|
+
include: (...includes) => ifNotEmpty(includes, (es) => ({ $project: es.reduce((a, b) => ({ ...a, ...isString(b) ? { [b]: 1 } : b }), {}) })),
|
|
129
|
+
exclude: (...excludes) => ifNotEmpty(excludes, (es) => ({ $project: es.reduce((a, b) => ({ ...a, ...isString(b) ? { [b]: 0 } : b }), {}) })),
|
|
130
|
+
includes: (includes) => new IncludeBuilder(includes),
|
|
131
|
+
project: (project) => ifDefined(project, ($project) => ({ $project })),
|
|
132
|
+
date: (key, format) => ({ $toDate: `$${key}`, ...ifDefined(format, { format }) }),
|
|
133
|
+
duration: (from, to) => ({ $divide: [{ $subtract: [stages.project.date(from), stages.project.date(to)] }, 1e3] })
|
|
134
|
+
},
|
|
135
|
+
replaceWith: {
|
|
136
|
+
replaceWith: (f) => ifDefined(f, { $replaceWith: f }),
|
|
137
|
+
merge: (...objects) => ifNotEmpty(objects, (os) => ({ $mergeObjects: os })),
|
|
138
|
+
rootAnd: (...objects) => stages.replaceWith.merge(stages.root, ...objects),
|
|
139
|
+
currentAnd: (...objects) => stages.replaceWith.merge(stages.current, ...objects),
|
|
140
|
+
reroot: (prop) => ({ $replaceRoot: { newRoot: `$${prop}` } }),
|
|
141
|
+
concat: (...props) => ifNotEmpty(props, (ps) => ({ $concatArrays: ps.map((p) => `$${p}`) }))
|
|
142
|
+
},
|
|
143
|
+
facet: {
|
|
144
|
+
facet: (f) => ({ $facet: stages.decode.fieldsArrays(f) }),
|
|
145
|
+
unwind: (from) => (f) => ({ $unwind: `$${from ?? f}` }),
|
|
146
|
+
count: (from) => (f) => ({ $sortByCount: `$${from ?? f}` }),
|
|
147
|
+
data: () => []
|
|
148
|
+
},
|
|
149
|
+
unwind: {
|
|
150
|
+
unwind: (prop) => ({ $unwind: `$${prop}` })
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// src/MongoGateway.ts
|
|
155
|
+
import {
|
|
156
|
+
asJson,
|
|
157
|
+
ifDefined as ifDefined2,
|
|
158
|
+
isDefined as isDefined2,
|
|
159
|
+
isPresent as isPresent2,
|
|
160
|
+
toArray as toArray2
|
|
161
|
+
} from "@thisisagile/easy";
|
|
162
|
+
var MongoGateway = class {
|
|
163
|
+
constructor(collection, provider = collection.provider) {
|
|
164
|
+
this.collection = collection;
|
|
165
|
+
this.provider = provider;
|
|
166
|
+
}
|
|
167
|
+
all(options) {
|
|
168
|
+
return this.provider.all(options).then((js) => js.map((j) => this.collection.in(j)));
|
|
169
|
+
}
|
|
170
|
+
byId(id) {
|
|
171
|
+
return this.provider.byId(id).then((j) => ifDefined2(j, this.collection.in(j)));
|
|
172
|
+
}
|
|
173
|
+
by(key, value, options) {
|
|
174
|
+
return this.provider.by(key, value, options).then((js) => js.map((j) => this.collection.in(j)));
|
|
175
|
+
}
|
|
176
|
+
byIds(...ids) {
|
|
177
|
+
return this.find(this.collection.id.isIn(...ids));
|
|
178
|
+
}
|
|
179
|
+
find(q, options) {
|
|
180
|
+
return this.provider.find(asJson(q), options).then((js) => js.map((j) => this.collection.in(j)));
|
|
181
|
+
}
|
|
182
|
+
search(q, options) {
|
|
183
|
+
return this.find(this.collection.google(q), options);
|
|
184
|
+
}
|
|
185
|
+
filter(options) {
|
|
186
|
+
return this.all(options);
|
|
187
|
+
}
|
|
188
|
+
exists(id) {
|
|
189
|
+
return this.provider.byId(id).then((i) => isDefined2(i));
|
|
190
|
+
}
|
|
191
|
+
aggregate(...filters) {
|
|
192
|
+
return this.provider.aggregate(toArray2(...filters).filter(isPresent2));
|
|
193
|
+
}
|
|
194
|
+
count(...filters) {
|
|
195
|
+
return this.aggregate(...filters, { $count: "total" }).then((d) => d.first()?.total ?? 0);
|
|
196
|
+
}
|
|
197
|
+
match(f) {
|
|
198
|
+
return this.aggregate(stages.match.match(f));
|
|
199
|
+
}
|
|
200
|
+
add(item) {
|
|
201
|
+
return this.provider.add(this.collection.out(item)).then((j) => this.collection.in(j));
|
|
202
|
+
}
|
|
203
|
+
update(item) {
|
|
204
|
+
return this.provider.update(this.collection.out(item)).then((j) => this.collection.in(j));
|
|
205
|
+
}
|
|
206
|
+
remove(id) {
|
|
207
|
+
return this.provider.remove(id);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// src/Lucene.ts
|
|
212
|
+
import {
|
|
213
|
+
entries,
|
|
214
|
+
ifDefined as ifDefined3,
|
|
215
|
+
ifNotEmpty as ifNotEmpty2,
|
|
216
|
+
ifTrue,
|
|
217
|
+
isDefined as isDefined3,
|
|
218
|
+
isEmptyObject,
|
|
219
|
+
isFunction,
|
|
220
|
+
on as on2,
|
|
221
|
+
toArray as toArray3,
|
|
222
|
+
toList
|
|
223
|
+
} from "@thisisagile/easy";
|
|
224
|
+
var should = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.should);
|
|
225
|
+
var must = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.must);
|
|
226
|
+
var mustNot = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.mustNot);
|
|
227
|
+
var filter = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.filter);
|
|
228
|
+
var lucene = {
|
|
229
|
+
clause: (c) => entries(c).reduce((res, [k, v]) => res.add(isFunction(v) ? v(k) : v), toList()),
|
|
230
|
+
clauses: (cs) => toArray3(cs).flatMap((c) => lucene.clause(c)),
|
|
231
|
+
compound: (query, def, wildcard = true) => ifNotEmpty2(
|
|
232
|
+
entries({
|
|
233
|
+
should: should(query, def),
|
|
234
|
+
filter: filter(query, def),
|
|
235
|
+
mustNot: mustNot(query, def),
|
|
236
|
+
must: must(query, def)
|
|
237
|
+
}).filter(([_, v]) => v.length > 0),
|
|
238
|
+
(e) => e.reduce((res, [k, v]) => on2(res, (r) => r[k] = lucene.clauses(v)), should(query, def).length > 0 ? { minimumShouldMatch: 1 } : {}),
|
|
239
|
+
() => ifTrue(wildcard, () => ({
|
|
240
|
+
should: lucene.clauses([{ wildcard: lucene.wildcard() }]),
|
|
241
|
+
minimumShouldMatch: 0
|
|
242
|
+
}))
|
|
243
|
+
),
|
|
244
|
+
search: (c, index) => ({
|
|
245
|
+
$search: {
|
|
246
|
+
...ifDefined3(index, { index }),
|
|
247
|
+
compound: entries(c).reduce((res, [k, v]) => on2(res, (r) => r[k] = lucene.clauses(v)), {})
|
|
248
|
+
}
|
|
249
|
+
}),
|
|
250
|
+
searchWithDef: (query, options, count = "total", index) => {
|
|
251
|
+
const sort = entries(query).mapDefined(([k, v]) => options[k]?.(v, query)?.sort).first();
|
|
252
|
+
return {
|
|
253
|
+
$search: {
|
|
254
|
+
...ifDefined3(index, { index }),
|
|
255
|
+
compound: lucene.compound(query, options),
|
|
256
|
+
...ifDefined3(sort, { sort }),
|
|
257
|
+
count: { type: count }
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
searchMeta: (query, def, count = "total", index) => ({
|
|
262
|
+
$searchMeta: {
|
|
263
|
+
...ifDefined3(index, { index }),
|
|
264
|
+
...ifTrue(
|
|
265
|
+
!isEmptyObject(lucene.facets(def)),
|
|
266
|
+
{
|
|
267
|
+
facet: {
|
|
268
|
+
operator: {
|
|
269
|
+
compound: lucene.compound(query, def)
|
|
270
|
+
},
|
|
271
|
+
facets: lucene.facets(def)
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
{ compound: lucene.compound(query, def) }
|
|
275
|
+
),
|
|
276
|
+
count: { type: count }
|
|
277
|
+
}
|
|
278
|
+
}),
|
|
279
|
+
exists: () => (path) => ({ exists: { path } }),
|
|
280
|
+
text: (value, fuzzy) => (path) => ifDefined3(value, (v) => ({
|
|
281
|
+
text: {
|
|
282
|
+
path: path === "wildcard" ? { wildcard: "*" } : path,
|
|
283
|
+
query: v,
|
|
284
|
+
...ifDefined3(fuzzy, { fuzzy })
|
|
285
|
+
}
|
|
286
|
+
})),
|
|
287
|
+
wildcard: (value, allowAnalyzedField = true) => (path) => ({
|
|
288
|
+
wildcard: {
|
|
289
|
+
path: path === "wildcard" ? { wildcard: "*" } : path,
|
|
290
|
+
query: ifDefined3(value, value, "*"),
|
|
291
|
+
allowAnalyzedField
|
|
292
|
+
}
|
|
293
|
+
}),
|
|
294
|
+
lt: (value) => (path) => ifDefined3(value, (lt) => ({ range: { path, lt } })),
|
|
295
|
+
lte: (value) => (path) => ifDefined3(value, (lte) => ({ range: { path, lte } })),
|
|
296
|
+
gt: (value) => (path) => ifDefined3(value, (gt) => ({ range: { path, gt } })),
|
|
297
|
+
gte: (value) => (path) => ifDefined3(value, (gte) => ({ range: { path, gte } })),
|
|
298
|
+
after: (date) => lucene.gte(toMongoType(date)),
|
|
299
|
+
before: (date) => lucene.lt(toMongoType(date)),
|
|
300
|
+
between: (after, before, includeLimit) => (path) => {
|
|
301
|
+
const upperLimit = includeLimit ? { lte: toMongoType(before) } : { lt: toMongoType(before) };
|
|
302
|
+
return {
|
|
303
|
+
range: {
|
|
304
|
+
path,
|
|
305
|
+
gte: toMongoType(after),
|
|
306
|
+
...upperLimit
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
facets: (def) => entries(def).filter(([k, v]) => isDefined3(v(k)?.facet)).map(([k, v]) => ({ [k]: v(k)?.facet })).reduce((acc, v) => ({ ...acc, ...v }), {}),
|
|
311
|
+
facet: {
|
|
312
|
+
string: (path, numBuckets = 1e3) => ({
|
|
313
|
+
type: "string",
|
|
314
|
+
path,
|
|
315
|
+
numBuckets
|
|
316
|
+
}),
|
|
317
|
+
number: (path, boundaries, alt) => ({
|
|
318
|
+
type: "number",
|
|
319
|
+
path,
|
|
320
|
+
boundaries,
|
|
321
|
+
...ifDefined3(alt, (a) => ({ default: a }))
|
|
322
|
+
}),
|
|
323
|
+
date: (path, boundaries, alt) => ({
|
|
324
|
+
type: "date",
|
|
325
|
+
path,
|
|
326
|
+
boundaries: boundaries.mapDefined((b) => b.toDate()),
|
|
327
|
+
...ifDefined3(alt, (a) => ({ default: a }))
|
|
328
|
+
})
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
// src/AtlasSearchGateway.ts
|
|
333
|
+
import { asNumber as asNumber2, entries as entries2, text, toPageList, tuple2 } from "@thisisagile/easy";
|
|
334
|
+
var { skip, take } = stages.skip;
|
|
335
|
+
var { replaceWith } = stages.replaceWith;
|
|
336
|
+
var { searchWithDef, searchMeta, facets } = lucene;
|
|
337
|
+
var toFilters = (facets2) => entries2(facets2).map(([k, fs]) => ({
|
|
338
|
+
label: text(k).title.toString(),
|
|
339
|
+
field: k,
|
|
340
|
+
values: fs.map((f) => toValue(f))
|
|
341
|
+
}));
|
|
342
|
+
var toValue = (f) => ({ label: f._id ?? "unknown", value: f._id ?? "unknown", count: f.count });
|
|
343
|
+
var AtlasSearchGateway = class extends MongoGateway {
|
|
344
|
+
constructor(collection, searchDef, sortDef = {}, provider = collection.provider) {
|
|
345
|
+
super(collection, provider);
|
|
346
|
+
this.searchDef = searchDef;
|
|
347
|
+
this.sortDef = sortDef;
|
|
348
|
+
}
|
|
349
|
+
query(query, additionalStages = []) {
|
|
350
|
+
return tuple2(
|
|
351
|
+
this.aggregate(
|
|
352
|
+
searchWithDef(query, this.searchDef),
|
|
353
|
+
skip({ skip: query?.skip ?? 0 }),
|
|
354
|
+
take({ take: query?.take ?? 250 }),
|
|
355
|
+
...additionalStages
|
|
356
|
+
),
|
|
357
|
+
this.aggregate(
|
|
358
|
+
searchMeta(query, this.searchDef),
|
|
359
|
+
replaceWith({
|
|
360
|
+
total: "$count.total",
|
|
361
|
+
facets: Object.keys(facets(this.searchDef)).reduce((acc, k) => ({ ...acc, [k]: `$facet.${k}.buckets` }), {})
|
|
362
|
+
})
|
|
363
|
+
)
|
|
364
|
+
).then(([data, meta3]) => ({ data, meta: meta3.first() })).then(
|
|
365
|
+
({ data, meta: meta3 }) => toPageList(data, {
|
|
366
|
+
total: asNumber2(meta3?.total, 0),
|
|
367
|
+
skip: asNumber2(query?.skip, 0),
|
|
368
|
+
take: asNumber2(query?.take, 250),
|
|
369
|
+
sorts: Object.keys(this.sortDef),
|
|
370
|
+
filters: toFilters(meta3.facets)
|
|
371
|
+
})
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// src/Collection.ts
|
|
377
|
+
import {
|
|
378
|
+
asString as asString3,
|
|
379
|
+
Database as Database2,
|
|
380
|
+
Field as Field2,
|
|
381
|
+
LogicalCondition as LogicalCondition3,
|
|
382
|
+
Mapper,
|
|
383
|
+
mappings,
|
|
384
|
+
ofGet as ofGet2,
|
|
385
|
+
toCondition,
|
|
386
|
+
toUuid
|
|
387
|
+
} from "@thisisagile/easy";
|
|
388
|
+
|
|
389
|
+
// src/MongoProvider.ts
|
|
390
|
+
import {
|
|
391
|
+
asJson as asJson2,
|
|
392
|
+
asNumber as asNumber3,
|
|
393
|
+
asString as asString2,
|
|
394
|
+
choose as choose2,
|
|
395
|
+
entries as entries3,
|
|
396
|
+
Exception,
|
|
397
|
+
ifTrue as ifTrue2,
|
|
398
|
+
isArray as isArray2,
|
|
399
|
+
isDefined as isDefined4,
|
|
400
|
+
isField,
|
|
401
|
+
isSortCondition,
|
|
402
|
+
json,
|
|
403
|
+
toArray as toArray4,
|
|
404
|
+
toPageList as toPageList2,
|
|
405
|
+
tuple2 as tuple22,
|
|
406
|
+
tuple3,
|
|
407
|
+
use as use2,
|
|
408
|
+
when
|
|
409
|
+
} from "@thisisagile/easy";
|
|
410
|
+
import {
|
|
411
|
+
MongoClient
|
|
412
|
+
} from "mongodb";
|
|
413
|
+
var omitId = (j) => json.delete(j, "_id");
|
|
414
|
+
function omitOptions(obj) {
|
|
415
|
+
const { maxTimeMS, ...rest } = obj ?? {};
|
|
416
|
+
return rest;
|
|
417
|
+
}
|
|
418
|
+
var MongoProvider = class _MongoProvider {
|
|
419
|
+
constructor(coll) {
|
|
420
|
+
this.coll = coll;
|
|
421
|
+
}
|
|
422
|
+
static clients = {};
|
|
423
|
+
static destroyAll() {
|
|
424
|
+
return Promise.all(entries3(_MongoProvider.clients).map(([u, c]) => c.then((c2) => c2.close()).then(() => delete _MongoProvider.clients[u]))).then(
|
|
425
|
+
() => void 0
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
static connect(u, db) {
|
|
429
|
+
return MongoClient.connect(u, {
|
|
430
|
+
auth: {
|
|
431
|
+
username: asString2(db.options?.user),
|
|
432
|
+
password: asString2(db.options?.password)
|
|
433
|
+
},
|
|
434
|
+
...db.options?.maxPoolSize && { maxPoolSize: db.options?.maxPoolSize },
|
|
435
|
+
...db.options?.minPoolSize && { minPoolSize: db.options?.minPoolSize },
|
|
436
|
+
...db.options?.maxIdleTimeMS && { maxIdleTimeMS: db.options?.maxIdleTimeMS },
|
|
437
|
+
...db.options?.socketTimeoutMS && { socketTimeoutMS: db.options?.socketTimeoutMS }
|
|
438
|
+
}).then((c) => {
|
|
439
|
+
c.on("error", () => delete _MongoProvider.clients[u]);
|
|
440
|
+
c.on("close", () => delete _MongoProvider.clients[u]);
|
|
441
|
+
return c;
|
|
442
|
+
}).catch((err) => {
|
|
443
|
+
delete _MongoProvider.clients[u];
|
|
444
|
+
return Promise.reject(err);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
cluster() {
|
|
448
|
+
return use2(
|
|
449
|
+
this.coll.db,
|
|
450
|
+
(db) => when(db.options?.cluster).not.isDefined.reject(Exception.IsNotValid.because("Missing cluster in database options.")).then((c) => _MongoProvider.clients[c] ?? (_MongoProvider.clients[c] = _MongoProvider.connect(c, db)))
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
collection() {
|
|
454
|
+
return this.cluster().then((c) => c.db(this.coll.db.name)).then((db) => db.collection(asString2(this.coll)));
|
|
455
|
+
}
|
|
456
|
+
toMongoJson(query) {
|
|
457
|
+
return toMongoType(asJson2(query));
|
|
458
|
+
}
|
|
459
|
+
withTimeout(options) {
|
|
460
|
+
return { ...options, maxTimeMS: options?.maxTimeMS ?? this.coll.db?.options?.queryTimeoutMS ?? 3e4 };
|
|
461
|
+
}
|
|
462
|
+
find(query, options) {
|
|
463
|
+
return tuple3(this.collection(), this.toMongoJson(query), this.toFindOptions(options)).then(
|
|
464
|
+
([c, q, o]) => tuple22(
|
|
465
|
+
c.find(q, o),
|
|
466
|
+
ifTrue2(o.total, () => c.countDocuments(q, { maxTimeMS: this.withTimeout(options).maxTimeMS }))
|
|
467
|
+
)
|
|
468
|
+
).then(([res, total]) => this.toArray(res, { ...omitOptions(options), total }));
|
|
469
|
+
}
|
|
470
|
+
all(options) {
|
|
471
|
+
return this.find({}, options);
|
|
472
|
+
}
|
|
473
|
+
byId(id, options) {
|
|
474
|
+
return this.collection().then((c) => c.findOne(this.toMongoJson({ id }), this.toFindOptions(options)));
|
|
475
|
+
}
|
|
476
|
+
by(key, value, options) {
|
|
477
|
+
return this.find({ [key]: value }, options);
|
|
478
|
+
}
|
|
479
|
+
group(qs, options) {
|
|
480
|
+
return this.aggregate(qs, options);
|
|
481
|
+
}
|
|
482
|
+
aggregate(qs, options) {
|
|
483
|
+
return this.collection().then(
|
|
484
|
+
(c) => c.aggregate(
|
|
485
|
+
qs.map((q) => this.toMongoJson(q)),
|
|
486
|
+
this.withTimeout(options)
|
|
487
|
+
)
|
|
488
|
+
).then((res) => this.toArray(res));
|
|
489
|
+
}
|
|
490
|
+
add(item) {
|
|
491
|
+
return this.collection().then((c) => c.insertOne(omitId(item))).then(() => omitId(item));
|
|
492
|
+
}
|
|
493
|
+
update(item) {
|
|
494
|
+
return this.collection().then((c) => c.updateOne(this.toMongoJson({ id: item.id }), { $set: omitId(item) })).then(() => this.byId(item.id));
|
|
495
|
+
}
|
|
496
|
+
remove(id) {
|
|
497
|
+
return this.collection().then((c) => c.deleteOne(this.toMongoJson({ id }))).then((d) => d.acknowledged);
|
|
498
|
+
}
|
|
499
|
+
count(query, options) {
|
|
500
|
+
return this.collection().then((c) => c.countDocuments(this.toMongoJson(query ?? {}), this.withTimeout(options)));
|
|
501
|
+
}
|
|
502
|
+
createIndex(indexes, options) {
|
|
503
|
+
return this.collection().then((c) => c.createIndex(this.toIndexSpecification(indexes), this.toCreateIndexesOptions(options)));
|
|
504
|
+
}
|
|
505
|
+
createPartialIndex(indexes, filter2, options) {
|
|
506
|
+
return this.createIndex(indexes, { ...options, filter: filter2 });
|
|
507
|
+
}
|
|
508
|
+
createTextIndex(indexes, options) {
|
|
509
|
+
const ii = toArray4(indexes).reduce((i, f) => ({ ...i, [asString2(f)]: "text" }), {});
|
|
510
|
+
return this.createIndex(ii, { unique: false, ...options });
|
|
511
|
+
}
|
|
512
|
+
toFindOptions(options) {
|
|
513
|
+
return {
|
|
514
|
+
limit: asNumber3(options?.take ?? 250),
|
|
515
|
+
...options?.skip && { skip: asNumber3(options?.skip) },
|
|
516
|
+
...options?.sorts && { sort: options?.sorts } || options?.sort && { sort: this.coll.sort(...options?.sort ?? []) },
|
|
517
|
+
total: isDefined4(options?.skip) || isDefined4(options?.take),
|
|
518
|
+
projection: options?.projection ?? { _id: 0 },
|
|
519
|
+
maxTimeMS: this.withTimeout(options).maxTimeMS
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
toIndexSpecification(index) {
|
|
523
|
+
return choose2(index).type(isField, (f) => f.property).type(isSortCondition, (s) => s.toJSON()).type(isArray2, (aa) => aa.map((a) => this.toIndexSpecification(a))).else((i) => i);
|
|
524
|
+
}
|
|
525
|
+
toCreateIndexesOptions(options) {
|
|
526
|
+
return {
|
|
527
|
+
unique: options?.unique ?? true,
|
|
528
|
+
...options?.languageOverride && { language_override: options.languageOverride },
|
|
529
|
+
...options?.languageDefault && { default_language: options.languageDefault },
|
|
530
|
+
...options?.filter && { partialFilterExpression: toMongoType(asJson2(options.filter)) }
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
toArray(cursor, options) {
|
|
534
|
+
return cursor.toArray().then((r) => toPageList2(r, options));
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
// src/Collection.ts
|
|
539
|
+
var Collection = class extends Mapper {
|
|
540
|
+
map = {
|
|
541
|
+
...mappings,
|
|
542
|
+
field: (name, options) => new Field2(name, options)
|
|
543
|
+
};
|
|
544
|
+
id = this.map.field("id", { dflt: toUuid });
|
|
545
|
+
constructor(options = { startFrom: "source" }) {
|
|
546
|
+
super(options);
|
|
547
|
+
}
|
|
548
|
+
get db() {
|
|
549
|
+
return Database2.Default;
|
|
550
|
+
}
|
|
551
|
+
get provider() {
|
|
552
|
+
return new MongoProvider(this);
|
|
553
|
+
}
|
|
554
|
+
where = (...conditions) => new LogicalCondition3(
|
|
555
|
+
"and",
|
|
556
|
+
conditions.map((c) => ofGet2(c, this))
|
|
557
|
+
).toJSON();
|
|
558
|
+
match = (condition) => ({ $match: ofGet2(condition, this).toJSON() });
|
|
559
|
+
group = (...conditions) => new LogicalCondition3(
|
|
560
|
+
"group",
|
|
561
|
+
conditions.map((c) => ofGet2(c, this))
|
|
562
|
+
).toJSON();
|
|
563
|
+
google = (value) => toCondition("$text", "search", value);
|
|
564
|
+
search = (key) => this.map.field(asString3(key));
|
|
565
|
+
sort = (...conditions) => conditions.reduce((cs, c) => {
|
|
566
|
+
cs[c.key] = c.value;
|
|
567
|
+
return cs;
|
|
568
|
+
}, {});
|
|
569
|
+
out(to = {}) {
|
|
570
|
+
return toMongoType(super.out(to));
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
export {
|
|
574
|
+
AtlasSearchGateway,
|
|
575
|
+
Collection,
|
|
576
|
+
FilterBuilder,
|
|
577
|
+
IncludeBuilder,
|
|
578
|
+
MongoGateway,
|
|
579
|
+
MongoProvider,
|
|
580
|
+
SortBuilder,
|
|
581
|
+
asc,
|
|
582
|
+
desc,
|
|
583
|
+
lucene,
|
|
584
|
+
stages,
|
|
585
|
+
toFilters,
|
|
586
|
+
toMongoType
|
|
587
|
+
};
|
|
8
588
|
//# sourceMappingURL=index.mjs.map
|