UNPKG

@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

69 lines (68 loc) 2.07 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const createIndexProvider = require("./index.cjs5.js"); const get = require("./index.cjs25.js"); const getMatchingKeys = require("./index.cjs26.js"); const serializeValue = require("./index.cjs16.js"); function createExternalIndex(field, index) { return createIndexProvider({ query(selector) { const keys = 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] }; }, 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;