@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
75 lines (74 loc) • 2.43 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const createIndexProvider = require("./index.cjs6.js");
const get = require("./index.cjs24.js");
const getMatchingKeys = require("./index.cjs28.js");
const serializeValue = require("./index.cjs20.js");
function createExternalIndex(field, index) {
return createIndexProvider({
query(selector) {
if (!Object.hasOwnProperty.call(selector, field)) {
return { matched: false };
}
const fieldSelector = selector[field];
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
if (keys.include == null && keys.exclude == null)
return { matched: false };
let includedPositions = [];
if (keys.include == null) {
for (const set of index.values()) {
for (const pos of set) {
includedPositions.push(pos);
}
}
} else {
for (const key of keys.include) {
const posSet = index.get(key);
if (posSet) {
for (const pos of posSet) {
includedPositions.push(pos);
}
}
}
}
if (keys.exclude != null) {
const excludeSet = /* @__PURE__ */ new Set();
for (const key of keys.exclude) {
const posSet = index.get(key);
if (posSet) {
for (const pos of posSet) {
excludeSet.add(pos);
}
}
}
includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
}
return {
matched: true,
positions: includedPositions,
fields: [field],
keepSelector: filteresForNull
};
},
rebuild() {
}
});
}
function createIndex(field) {
const index = /* @__PURE__ */ new Map();
return {
...createExternalIndex(field, index),
rebuild(items) {
index.clear();
items.forEach((item, i) => {
const value = serializeValue(get(item, field));
const current = index.get(value) || /* @__PURE__ */ new Set();
current.add(i);
index.set(value, current);
});
}
};
}
exports.createExternalIndex = createExternalIndex;
exports.default = createIndex;