@tanstack/db
Version:
A reactive client store for building super fast apps on sync
68 lines (67 loc) • 2.6 kB
JavaScript
import { DEFAULT_COMPARE_OPTIONS } from "../utils.js";
import { indexableOperations } from "../query/builder/functions.js";
import { hasVirtualPropPath } from "../virtual-props.js";
import { findIndexForField } from "../utils/index-optimization.js";
import { codedWarning, devBuild } from "../error-message.js";
import { checkCollectionSizeForIndex, isDevModeEnabled } from "./index-registry.js";
function shouldAutoIndex(collection) {
return collection.config.autoIndex === `eager`;
}
function ensureIndexForField(fieldName, fieldPath, collection, compareOptions, canUse) {
if (hasVirtualPropPath(fieldPath)) return;
const autoIndex = shouldAutoIndex(collection);
if (!autoIndex && !isDevModeEnabled()) return;
const compareOpts = compareOptions ?? {
...DEFAULT_COMPARE_OPTIONS,
...collection.compareOptions
};
if (findIndexForField(collection, fieldPath, compareOpts, canUse)) return;
if (!autoIndex) {
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__ */ devBuild() && process.env.NODE_ENV !== `production` ? `${collection.id ? `[${collection.id}] ` : ``}Failed to create auto-index for field path "${fieldPath.join(`.`)}":` : codedWarning(215, {
id: collection.id,
value: fieldPath.join(`.`)
}), error);
}
}
function ensureIndexForExpression(expression, collection) {
if (!shouldAutoIndex(collection) && !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 (!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;
}
export { ensureIndexForExpression, ensureIndexForField };
//# sourceMappingURL=auto-index.js.map