UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

69 lines (68 loc) • 2.84 kB
const require_utils = require("../utils.cjs"); const require_functions = require("../query/builder/functions.cjs"); const require_virtual_props = require("../virtual-props.cjs"); const require_index_optimization = require("../utils/index-optimization.cjs"); const require_error_message = require("../error-message.cjs"); const require_index_registry = require("./index-registry.cjs"); function shouldAutoIndex(collection) { return collection.config.autoIndex === `eager`; } function ensureIndexForField(fieldName, fieldPath, collection, compareOptions, canUse) { if (require_virtual_props.hasVirtualPropPath(fieldPath)) return; const autoIndex = shouldAutoIndex(collection); if (!autoIndex && !require_index_registry.isDevModeEnabled()) return; const compareOpts = compareOptions ?? { ...require_utils.DEFAULT_COMPARE_OPTIONS, ...collection.compareOptions }; if (require_index_optimization.findIndexForField(collection, fieldPath, compareOpts, canUse)) return; if (!autoIndex) { require_index_registry.checkCollectionSizeForIndex(collection.id || `unknown`, collection.size, fieldPath); return; } try { collection.createIndex((row) => { let current = row; for (const part of fieldPath) current = current[part]; return current; }, { name: `auto:${fieldPath.join(`.`)}`, options: { compareOptions: compareOpts } }); } catch (error) { console.warn(/* @__PURE__ */ require_error_message.devBuild() && process.env.NODE_ENV !== `production` ? `${collection.id ? `[${collection.id}] ` : ``}Failed to create auto-index for field path "${fieldPath.join(`.`)}":` : require_error_message.codedWarning(215, { id: collection.id, value: fieldPath.join(`.`) }), error); } } function ensureIndexForExpression(expression, collection) { if (!shouldAutoIndex(collection) && !require_index_registry.isDevModeEnabled()) return; const indexableExpressions = extractIndexableExpressions(expression); for (const { fieldName, fieldPath } of indexableExpressions) ensureIndexForField(fieldName, fieldPath, collection); } function extractIndexableExpressions(expression) { const results = []; function extractFromExpression(expr) { if (expr.type !== `func`) return; const func = expr; if (func.name === `and`) { for (const arg of func.args) extractFromExpression(arg); return; } if (!require_functions.indexableOperations.includes(func.name)) return; if (func.args.length < 1 || func.args[0].type !== `ref`) return; const fieldPath = func.args[0].path; if (fieldPath.length === 0) return; const fieldName = fieldPath.join(`_`); results.push({ fieldName, fieldPath }); } extractFromExpression(expression); return results; } exports.ensureIndexForExpression = ensureIndexForExpression; exports.ensureIndexForField = ensureIndexForField; //# sourceMappingURL=auto-index.cjs.map