ChemPal Documentation - v1.6.0
    Preparing search index...

    Function getIdbStorageBreakdown

    • Measure every IndexedDB object store: the number of records it holds and the byte size of its contents when serialized to JSON. Used by the settings panel to show cache/price-history sizes; the summed totalBytes can be paired with navigator.storage.estimate() to scale each store's JSON size up to its true on-disk footprint (indexes, keys, structured-clone overhead). Returns all-zero counts on failure.

      Returns Promise<IdbStorageBreakdown>

      A per-store breakdown of record counts and JSON byte sizes, plus the total.

      const { byStore, totalBytes } = await getIdbStorageBreakdown();
      byStore["price_history"].count; // => number of price series
      totalBytes; // => summed JSON bytes across all stores
      export async function getIdbStorageBreakdown(): Promise<IdbStorageBreakdown> {
      const byStore: Record<IdbStore, { count: number; bytes: number }> = {
      [IDB_STORE.SEARCH_RESULTS]: { count: 0, bytes: 0 },
      [IDB_STORE.SEARCH_HISTORY]: { count: 0, bytes: 0 },
      [IDB_STORE.SUPPLIER_QUERY_CACHE]: { count: 0, bytes: 0 },
      [IDB_STORE.SUPPLIER_PRODUCT_DATA_CACHE]: { count: 0, bytes: 0 },
      [IDB_STORE.SUPPLIER_STATS]: { count: 0, bytes: 0 },
      [IDB_STORE.EXCLUDED_PRODUCTS]: { count: 0, bytes: 0 },
      [IDB_STORE.PRICE_HISTORY]: { count: 0, bytes: 0 },
      [IDB_STORE.APP_META]: { count: 0, bytes: 0 },
      [IDB_STORE.EXPORTS]: { count: 0, bytes: 0 },
      };
      try {
      const db = await getDB();
      for (const store of Object.values(IDB_STORE)) {
      const entries = await db.getAll(store);
      // Export records hold Blobs, which JSON.stringify flattens to `{}`; sum the
      // tracked `sizeBytes` instead so the breakdown reflects real on-disk size.
      const bytes =
      store === IDB_STORE.EXPORTS
      ? entries.reduce(
      (sum, entry) =>
      sum +
      (typeof entry === 'object' &&
      entry !== null &&
      'sizeBytes' in entry &&
      typeof entry.sizeBytes === 'number'
      ? entry.sizeBytes
      : 0),
      0,
      )
      : new Blob([JSON.stringify(entries)]).size;
      byStore[store] = { count: entries.length, bytes };
      }
      } catch (error) {
      logger.error('Failed to compute IndexedDB storage breakdown', { error });
      }
      const totalBytes = Object.values(byStore).reduce((sum, entry) => sum + entry.bytes, 0);
      return { byStore, totalBytes };
      }