UNPKG

@thisisagile/easy-mongo

Version:
59 lines (58 loc) 1.85 kB
import { lucene } from "./chunk-HRTULRYM.mjs"; import { MongoGateway } from "./chunk-GFBKWYDV.mjs"; import { stages } from "./chunk-WEJO6T5Q.mjs"; import "./chunk-MZE7UWQC.mjs"; // src/AtlasSearchGateway.ts import { asNumber, entries, text, toPageList, tuple2 } from "@thisisagile/easy"; var { skip, take } = stages.skip; var { replaceWith } = stages.replaceWith; var { searchWithDef, searchMeta, facets } = lucene; var toFilters = (facets2) => entries(facets2).map(([k, fs]) => ({ label: text(k).title.toString(), field: k, values: fs.map((f) => toValue(f)) })); var toValue = (f) => ({ label: f._id ?? "unknown", value: f._id ?? "unknown", count: f.count }); var AtlasSearchGateway = class extends MongoGateway { constructor(collection, searchDef, sortDef = {}, provider = collection.provider) { super(collection, provider); this.searchDef = searchDef; this.sortDef = sortDef; } query(query, additionalStages = []) { return tuple2( this.aggregate( searchWithDef(query, this.searchDef), skip({ skip: query?.skip ?? 0 }), take({ take: query?.take ?? 250 }), ...additionalStages ), this.aggregate( searchMeta(query, this.searchDef), replaceWith({ total: "$count.total", facets: Object.keys(facets(this.searchDef)).reduce((acc, k) => ({ ...acc, [k]: `$facet.${k}.buckets` }), {}) }) ) ).then(([data, meta]) => ({ data, meta: meta.first() })).then( ({ data, meta }) => toPageList(data, { total: asNumber(meta?.total, 0), skip: asNumber(query?.skip, 0), take: asNumber(query?.take, 250), sorts: Object.keys(this.sortDef), filters: toFilters(meta.facets) }) ); } }; export { AtlasSearchGateway, toFilters }; //# sourceMappingURL=AtlasSearchGateway.mjs.map