1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How do I write generic get record by index function using idb package?

Discussão em 'Angular' iniciado por Monica.J, Maio 30, 2025.

  1. Monica.J

    Monica.J Guest

    My main goal is to avoid hardcoded values so I store string literals via const object as const.

    import { Injectable } from '@angular/core';
    import { openDB, DBSchema, IDBPDatabase } from 'idb';

    const ObjectStoreName = {
    SummaryData: 'SummaryData',
    TestData: 'TestData',
    } as const;

    const ObjectStoreIndex = {
    ByTimestamp: 'ByTimestamp',
    BySizeAndName: 'BySizeAndName',
    } as const;

    interface SummaryFileCache {
    key: string;
    blob: Blob;
    timestamp: number;
    mimeType: string;
    }

    interface TestFileCache {
    key: number;
    timestamp: number;
    name: string;
    size: number;
    correct: boolean;
    }

    // database schema
    interface PersistentCacheSchema extends DBSchema {
    [ObjectStoreTable.SummaryData]: {
    key: string;
    value: SummaryFileCache;
    indexes: {
    [ObjectStoreIndex.ByTimestamp]: number;
    };
    };
    [ObjectStoreTable.TestData]: {
    key: number,
    value: TestFileCache;
    indexes: {
    [ObjectStoreIndex.BySizeAndName]: [number, string];
    };
    }
    }


    Now I want to write a generic function that will get records using an index in a store, I try this:

    @Injectable({ providedIn: 'root' })
    export class PersistentCacheService {
    private readonly _cache: Promise<IDBPDatabase<PersistentCacheSchema>>;
    private readonly _cacheName: string = 'myCache';
    private readonly _cacheSchemaVersion: number = 1;

    constructor() {
    this._cache = this._init();
    }

    private async _init(): Promise<IDBPDatabase<PersistentCacheSchema>> {
    // init code
    }

    async getByIndex<
    Store extends keyof PersistentCacheSchema,
    Index extends keyof PersistentCacheSchema[Store]['indexes']
    >(
    storeName: Store,
    indexName: Index,
    indexValue: PersistentCacheSchema[Store]['indexes'][Index]
    ): Promise<PersistentCacheSchema[Store]['value'][]> {
    const cache = await this._cache;
    const tx = cache.transaction(storeName, 'readonly');
    const store = tx.objectStore(storeName);
    const index = store.index(indexName);
    const results = await index.getAll(indexValue);
    await tx.done;
    return results;
    }
    }


    I want to be able to use the generic function and get correct auto type inference:

    // TypeScript should be able to inference the type of data as SummaryFileCache[]
    const data = await this.persistentCacheService.getByIndex(
    ObjectStoreName.SummaryData,
    ObjectStoreIndex.ByTimestamp,
    1748593517098
    )

    // TypeScript should be able to inference the type of test as TestFileCache[]
    const test = await this.persistentCacheService.getByIndex(
    ObjectStoreName.TestData,
    ObjectStoreIndex.BySizeAndName,
    [1748593581660, 'test1']
    )


    But I get these errors:

    [​IMG]

    How to I get rid of those errors? My ultimate goals are avoid passing hardcoded string literal around and retain auto type inference.

    Continue reading...

Compartilhe esta Página