@thisisagile/easy-mongo
Version:
Add support for MongoDB
130 lines (128 loc) • 4.26 kB
JavaScript
import {
toMongoType
} from "./chunk-MZE7UWQC.mjs";
// src/Lucene.ts
import {
entries,
ifDefined,
ifNotEmpty,
ifTrue,
isDefined,
isEmptyObject,
isFunction,
on,
toArray,
toList
} from "@thisisagile/easy";
var should = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.should);
var must = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.must);
var mustNot = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.mustNot);
var filter = (query, def) => entries(query).mapDefined(([k, v]) => def[k]?.(v, query)?.filter);
var lucene = {
clause: (c) => entries(c).reduce((res, [k, v]) => res.add(isFunction(v) ? v(k) : v), toList()),
clauses: (cs) => toArray(cs).flatMap((c) => lucene.clause(c)),
compound: (query, def, wildcard = true) => ifNotEmpty(
entries({
should: should(query, def),
filter: filter(query, def),
mustNot: mustNot(query, def),
must: must(query, def)
}).filter(([_, v]) => v.length > 0),
(e) => e.reduce((res, [k, v]) => on(res, (r) => r[k] = lucene.clauses(v)), should(query, def).length > 0 ? { minimumShouldMatch: 1 } : {}),
() => ifTrue(wildcard, () => ({
should: lucene.clauses([{ r: { exists: { path: "id" } } }]),
minimumShouldMatch: 0
}))
),
search: (c, index) => ({
$search: {
...ifDefined(index, { index }),
compound: entries(c).reduce((res, [k, v]) => on(res, (r) => r[k] = lucene.clauses(v)), {})
}
}),
searchWithDef: (query, options, count = "total", index) => {
const sort = entries(query).mapDefined(([k, v]) => options[k]?.(v, query)?.sort).first();
return {
$search: {
...ifDefined(index, { index }),
compound: lucene.compound(query, options),
...ifDefined(sort, { sort }),
count: { type: count }
}
};
},
searchMeta: (query, def, count = "total", index) => ({
$searchMeta: {
...ifDefined(index, { index }),
...ifTrue(
!isEmptyObject(lucene.facets(def)),
{
facet: {
operator: {
compound: lucene.compound(query, def)
},
facets: lucene.facets(def)
}
},
{ compound: lucene.compound(query, def) }
),
count: { type: count }
}
}),
exists: () => (path) => ({ exists: { path } }),
equals: (value) => (path) => ({ equals: { path, value: value === 1 } }),
text: (value, fuzzy) => (path) => ifDefined(value, (v) => ({
text: {
path: path === "wildcard" ? { wildcard: "*" } : path,
query: v,
...ifDefined(fuzzy, { fuzzy })
}
})),
wildcard: (value, allowAnalyzedField = true) => (path) => ({
wildcard: {
path: path === "wildcard" ? { wildcard: "*" } : path,
query: ifDefined(value, value, "*"),
allowAnalyzedField
}
}),
lt: (value) => (path) => ifDefined(value, (lt) => ({ range: { path, lt } })),
lte: (value) => (path) => ifDefined(value, (lte) => ({ range: { path, lte } })),
gt: (value) => (path) => ifDefined(value, (gt) => ({ range: { path, gt } })),
gte: (value) => (path) => ifDefined(value, (gte) => ({ range: { path, gte } })),
after: (date) => lucene.gte(toMongoType(date)),
before: (date) => lucene.lt(toMongoType(date)),
between: (after, before, includeLimit) => (path) => {
const upperLimit = includeLimit ? { lte: toMongoType(before) } : { lt: toMongoType(before) };
return {
range: {
path,
gte: toMongoType(after),
...upperLimit
}
};
},
facets: (def) => entries(def).filter(([k, v]) => isDefined(v(k)?.facet)).map(([k, v]) => ({ [k]: v(k)?.facet })).reduce((acc, v) => ({ ...acc, ...v }), {}),
facet: {
string: (path, numBuckets = 1e3) => ({
type: "string",
path,
numBuckets
}),
number: (path, boundaries, alt) => ({
type: "number",
path,
boundaries,
...ifDefined(alt, (a) => ({ default: a }))
}),
date: (path, boundaries, alt) => ({
type: "date",
path,
boundaries: boundaries.mapDefined((b) => b.toDate()),
...ifDefined(alt, (a) => ({ default: a }))
})
}
};
export {
lucene
};
//# sourceMappingURL=chunk-HRTULRYM.mjs.map