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) 1.89 kB
import createIndexProvider from "./index5.mjs"; import get from "./index25.mjs"; import getMatchingKeys from "./index26.mjs"; import serializeValue from "./index16.mjs"; 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); }); } }; } export { createExternalIndex, createIndex as default };