@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
71 lines (70 loc) • 2.47 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const intersection = require("./index.cjs24.js");
function getMergedIndexInfo(indexProviders, selector) {
return indexProviders.reduce((memo, indexProvider) => {
const info = indexProvider.query(selector);
if (!info.matched)
return memo;
const optimizedSelector = Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
return {
matched: true,
positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
optimizedSelector
};
}, {
matched: false,
positions: [],
optimizedSelector: { ...selector }
});
}
function getIndexInfo(indexProviders, selector) {
if (selector == null || Object.keys(selector).length <= 0) {
return { matched: false, positions: [], optimizedSelector: selector };
}
const { $and, $or, ...rest } = selector;
const flatInfo = getMergedIndexInfo(indexProviders, rest);
let { matched, positions } = flatInfo;
const newSelector = flatInfo.optimizedSelector;
if (Array.isArray($and)) {
const $andNew = [];
for (const sel of $and) {
const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
if (selMatched) {
positions = matched ? intersection(positions, selPositions) : selPositions;
matched = true;
if (Object.keys(optimizedSelector).length > 0) {
$andNew.push(optimizedSelector);
}
} else {
$andNew.push(sel);
}
}
if ($andNew.length > 0)
newSelector.$and = $andNew;
}
if (Array.isArray($or)) {
const $orNew = [];
for (const sel of $or) {
const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
if (selMatched) {
positions = [.../* @__PURE__ */ new Set([...positions, ...selPositions])];
matched = true;
if (Object.keys(optimizedSelector).length > 0) {
$orNew.push(optimizedSelector);
}
} else {
$orNew.push(sel);
}
}
if ($orNew.length > 0)
newSelector.$or = $orNew;
}
return {
matched,
positions: positions || [],
optimizedSelector: newSelector
};
}
exports.default = getIndexInfo;
exports.getMergedIndexInfo = getMergedIndexInfo;