UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

101 lines (100 loc) • 3.4 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const errorMessage = require("../error-message.cjs"); let devModeConfig = { enabled: true, collectionSizeThreshold: 1e3, slowQueryThresholdMs: 10, onSuggestion: null }; const queryPatterns = /* @__PURE__ */ new Map(); function configureIndexDevMode(config) { devModeConfig = { ...devModeConfig, ...config }; } function getIndexDevModeConfig() { return devModeConfig; } function isDevModeEnabled() { return errorMessage.devBuild() && process.env.NODE_ENV !== `production` && devModeConfig.enabled; } function emitIndexSuggestion(details) { if (!(errorMessage.devBuild() && process.env.NODE_ENV !== `production` && devModeConfig.enabled)) return; const field = details.fieldPath.join(`.`); const { type, collectionId, fieldPath, ...stats } = details; const suggestion = { type, collectionId, fieldPath, message: type === `slow-query` ? `Queries on "${field}" are slow (avg ${(stats.queryTimeMs ?? 0).toFixed(1)}ms). Consider adding an index.` : `Collection has ${stats.collectionSize} items. Queries on "${field}" may benefit from an index.`, ...stats }; if (devModeConfig.onSuggestion) { try { devModeConfig.onSuggestion(suggestion); } catch { } } else { console.warn( `[TanStack DB] Index suggestion for "${suggestion.collectionId}": ${suggestion.message} Field: ${suggestion.fieldPath.join(`.`)} Add index: collection.createIndex((row) => row.${suggestion.fieldPath.join(`.`)})` ); } } function trackQuery(collectionId, fieldPath, executionTimeMs) { if (!(errorMessage.devBuild() && process.env.NODE_ENV !== `production` && devModeConfig.enabled)) return; const key = `${collectionId}:${fieldPath.join(`.`)}`; const existing = queryPatterns.get(key); if (existing) { existing.queryCount++; existing.totalTimeMs += executionTimeMs; existing.avgTimeMs = existing.totalTimeMs / existing.queryCount; } else { queryPatterns.set(key, { fieldPath, queryCount: 1, totalTimeMs: executionTimeMs, avgTimeMs: executionTimeMs }); } const pattern = queryPatterns.get(key); if (pattern.avgTimeMs > devModeConfig.slowQueryThresholdMs) { emitIndexSuggestion({ type: `slow-query`, collectionId, fieldPath, queryTimeMs: pattern.avgTimeMs, queryCount: pattern.queryCount }); } } function checkCollectionSizeForIndex(collectionId, collectionSize, fieldPath) { if (!(errorMessage.devBuild() && process.env.NODE_ENV !== `production` && devModeConfig.enabled)) return; if (collectionSize > devModeConfig.collectionSizeThreshold) { emitIndexSuggestion({ type: `collection-size`, collectionId, fieldPath, collectionSize }); } } function clearQueryPatterns() { queryPatterns.clear(); } function getQueryPatterns() { return new Map(queryPatterns); } exports.checkCollectionSizeForIndex = checkCollectionSizeForIndex; exports.clearQueryPatterns = clearQueryPatterns; exports.configureIndexDevMode = configureIndexDevMode; exports.emitIndexSuggestion = emitIndexSuggestion; exports.getIndexDevModeConfig = getIndexDevModeConfig; exports.getQueryPatterns = getQueryPatterns; exports.isDevModeEnabled = isDevModeEnabled; exports.trackQuery = trackQuery; //# sourceMappingURL=index-registry.cjs.map