UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

90 lines (89 loc) 2.6 kB
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 devModeConfig.enabled && process.env.NODE_ENV !== `production`; } function emitIndexSuggestion(suggestion) { if (!isDevModeEnabled()) return; 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 (!isDevModeEnabled()) 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, message: `Queries on "${fieldPath.join(`.`)}" are slow (avg ${pattern.avgTimeMs.toFixed(1)}ms). Consider adding an index.`, queryTimeMs: pattern.avgTimeMs, queryCount: pattern.queryCount }); } } function checkCollectionSizeForIndex(collectionId, collectionSize, fieldPath) { if (!isDevModeEnabled()) return; if (collectionSize > devModeConfig.collectionSizeThreshold) { emitIndexSuggestion({ type: `collection-size`, collectionId, fieldPath, message: `Collection has ${collectionSize} items. Queries on "${fieldPath.join(`.`)}" may benefit from an index.`, collectionSize }); } } function clearQueryPatterns() { queryPatterns.clear(); } function getQueryPatterns() { return new Map(queryPatterns); } export { checkCollectionSizeForIndex, clearQueryPatterns, configureIndexDevMode, emitIndexSuggestion, getIndexDevModeConfig, getQueryPatterns, isDevModeEnabled, trackQuery }; //# sourceMappingURL=index-registry.js.map