From 5db8c0908133ec6e8540728336ca3c1edf46f81c Mon Sep 17 00:00:00 2001 From: ghazi Date: Wed, 24 Sep 2025 19:29:12 +0100 Subject: [PATCH] Add support for the direction option in getAll and getAllKeys add overloads of getAll and getAllKeys to enable calling the methods by passing an options object containing query, count and (the new) direction. Also, update the tests to include an example of calling the methods by passing an options object. --- src/entry.ts | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/main.ts | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) diff --git a/src/entry.ts b/src/entry.ts index 2538d6a..3771390 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -246,6 +246,40 @@ type CursorKey< ? IndexKey : StoreKey; +/** + * The new options interface for `IDBObjectStore.getAll`, `IDBObjectStore.getAllKeys` and + * `IDBObjectStore.getAllRecords` supported in chrome/edge 141 or newer. + * + * @template DBTypes DB schema type, or unknown if the DB isn't typed. + * @template StoreName Names of the object stores to get the types of. + */ +export interface IDBPStoreGetAllOptions< + DBTypes extends DBSchema | unknown, + StoreName extends StoreNames, +> { + query?: StoreKey | IDBKeyRange | null; + count?: number; + direction?: 'next' | 'prev'; +} + +/** + * The new options interface for `IDBIndex.getAll`, `IDBIndex.getAllKeys` and + * `IDBIndex.getAllRecords` supported in chrome/edge 141 or newer. + * + * @template DBTypes DB schema type, or unknown if the DB isn't typed. + * @template StoreName Names of the object stores to get the types of. + * @template IndexName Names of the indexes to get the types of. + */ +export interface IDBPIndexGetAllOptions< + DBTypes extends DBSchema | unknown, + StoreName extends StoreNames, + IndexName extends IndexNames, +> { + query?: IndexKey | IDBKeyRange | null; + count?: number; + direction?: IDBCursorDirection; +} + type IDBPDatabaseExtends = Omit< IDBDatabase, 'createObjectStore' | 'deleteObjectStore' | 'transaction' | 'objectStoreNames' @@ -437,6 +471,20 @@ export interface IDBPDatabase indexName: IndexName, query: IndexKey | IDBKeyRange, ): Promise | undefined>; + /** + * Retrieves all values in a store that match the query. + * Supported in chrome/edge 141 or newer. + * + * This is a shortcut that creates a transaction for this single action. If you need to do more + * than one action, create a transaction instead. + * + * @param storeName Name of the store. + * @param options + */ + getAll>( + storeName: Name, + options?: IDBPStoreGetAllOptions, + ): Promise[]>; /** * Retrieves all values in a store that match the query. * @@ -452,6 +500,25 @@ export interface IDBPDatabase query?: StoreKey | IDBKeyRange | null, count?: number, ): Promise[]>; + /** + * Retrieves all values in an index that match the query. + * Supported in chrome/edge 141 or newer. + * + * This is a shortcut that creates a transaction for this single action. If you need to do more + * than one action, create a transaction instead. + * + * @param storeName Name of the store. + * @param indexName Name of the index within the store. + * @param options + */ + getAllFromIndex< + Name extends StoreNames, + IndexName extends IndexNames, + >( + storeName: Name, + indexName: IndexName, + options?: IDBPIndexGetAllOptions, + ): Promise[]>; /** * Retrieves all values in an index that match the query. * @@ -472,6 +539,20 @@ export interface IDBPDatabase query?: IndexKey | IDBKeyRange | null, count?: number, ): Promise[]>; + /** + * Retrieves the keys of records in a store matching the query. + * Supported in chrome/edge 141 or newer. + * + * This is a shortcut that creates a transaction for this single action. If you need to do more + * than one action, create a transaction instead. + * + * @param storeName Name of the store. + * @param options + */ + getAllKeys>( + storeName: Name, + options?: IDBPStoreGetAllOptions, + ): Promise[]>; /** * Retrieves the keys of records in a store matching the query. * @@ -487,6 +568,25 @@ export interface IDBPDatabase query?: StoreKey | IDBKeyRange | null, count?: number, ): Promise[]>; + /** + * Retrieves the keys of records in an index matching the query. + * Supported in chrome/edge 141 or newer. + * + * This is a shortcut that creates a transaction for this single action. If you need to do more + * than one action, create a transaction instead. + * + * @param storeName Name of the store. + * @param indexName Name of the index within the store. + * @param options + */ + getAllKeysFromIndex< + Name extends StoreNames, + IndexName extends IndexNames, + >( + storeName: Name, + indexName: IndexName, + options?: IDBPIndexGetAllOptions, + ): Promise[]>; /** * Retrieves the keys of records in an index matching the query. * @@ -685,6 +785,15 @@ export interface IDBPObjectStore< get( query: StoreKey | IDBKeyRange, ): Promise | undefined>; + /** + * Retrieves all values that match the query. + * Supported in chrome/edge 141 or newer. + * + * @param options + */ + getAll( + options?: IDBPStoreGetAllOptions, + ): Promise[]>; /** * Retrieves all values that match the query. * @@ -695,6 +804,15 @@ export interface IDBPObjectStore< query?: StoreKey | IDBKeyRange | null, count?: number, ): Promise[]>; + /** + * Retrieves the keys of records matching the query. + * Supported in chrome/edge 141 or newer. + * + * @param options + */ + getAllKeys( + options?: IDBPStoreGetAllOptions, + ): Promise[]>; /** * Retrieves the keys of records matching the query. * @@ -835,6 +953,15 @@ export interface IDBPIndex< get( query: IndexKey | IDBKeyRange, ): Promise | undefined>; + /** + * Retrieves all values that match the query. + * Supported in chrome/edge 141 or newer. + * + * @param options + */ + getAll( + options?: IDBPIndexGetAllOptions, + ): Promise[]>; /** * Retrieves all values that match the query. * @@ -845,6 +972,15 @@ export interface IDBPIndex< query?: IndexKey | IDBKeyRange | null, count?: number, ): Promise[]>; + /** + * Retrieves the keys of records matching the query. + * Supported in chrome/edge 141 or newer. + * + * @param options + */ + getAllKeys( + options?: IDBPIndexGetAllOptions, + ): Promise[]>; /** * Retrieves the keys of records matching the query. * diff --git a/test/main.ts b/test/main.ts index 36edeca..53b1a55 100644 --- a/test/main.ts +++ b/test/main.ts @@ -265,6 +265,15 @@ suite('IDBPDatabase', () => { typeAssert>(true); assert.deepStrictEqual(val2, [456, 123, 789], 'Correct values from store'); + + if ('getAllRecords' in schemaDB.transaction('key-val-store').store) { + const val3 = await schemaDB.getAll('key-val-store', { + direction: 'prev', + count: 2, + }); + typeAssert>(true); + assert.deepStrictEqual(val3, [789, 123], 'Correct values from store'); + } }); test('getAllFromIndex', async function () { @@ -334,6 +343,22 @@ suite('IDBPDatabase', () => { ], 'Correct values from store', ); + + if ('getAllRecords' in schemaDB.transaction('object-store').store) { + const val3 = await schemaDB.getAllFromIndex('object-store', 'date', { + direction: 'prev', + count: 2, + }); + typeAssert>(true); + assert.deepStrictEqual( + val3, + [ + { id: 1, title: 'Article 1', date: new Date('2019-01-04') }, + { id: 2, title: 'Article 2', date: new Date('2019-01-03') }, + ], + 'Correct values from store', + ); + } }); test('getAllKeys', async function () { @@ -369,6 +394,19 @@ suite('IDBPDatabase', () => { ['bar', 'foo', 'hello'], 'Correct values from store', ); + + if ('getAllRecords' in schemaDB.transaction('key-val-store').store) { + const val3 = await schemaDB.getAllKeys('key-val-store', { + direction: 'prev', + count: 2, + }); + typeAssert>(true); + assert.deepStrictEqual( + val3, + ['hello', 'foo'], + 'Correct values from store', + ); + } }); test('getAllKeysFromIndex', async function () { @@ -388,6 +426,15 @@ suite('IDBPDatabase', () => { typeAssert>(true); assert.deepStrictEqual(val2, [1, 2, 3, 4], 'Correct values from store'); + + if ('getAllRecords' in schemaDB.transaction('object-store').store) { + const val3 = await schemaDB.getAllKeysFromIndex('object-store', 'date', { + direction: 'prev', + count: 2, + }); + typeAssert>(true); + assert.deepStrictEqual(val3, [1, 2], 'Correct values from store'); + } }); test('count', async () => { @@ -1224,6 +1271,13 @@ suite('IDBPObjectStore', () => { typeAssert>(true); assert.deepStrictEqual(val2, [456, 123, 789], 'Correct values from store'); + + const store3 = schemaDB.transaction('key-val-store').store; + if ('getAllRecords' in store3) { + const val3 = await store3.getAll({ direction: 'prev', count: 2 }); + typeAssert>(true); + assert.deepStrictEqual(val3, [789, 123], 'Correct values from store'); + } }); test('getAllKeys', async function () { @@ -1268,6 +1322,17 @@ suite('IDBPObjectStore', () => { ['bar', 'foo', 'hello'], 'Correct values from store', ); + + const store3 = schemaDB.transaction('key-val-store').store; + if ('getAllRecords' in store3) { + const val3 = await store3.getAllKeys({ direction: 'prev', count: 2 }); + typeAssert>(true); + assert.deepStrictEqual( + val3, + ['hello', 'foo'], + 'Correct values from store', + ); + } }); test('getKey', async function () { @@ -1765,6 +1830,23 @@ suite('IDBPIndex', () => { 'Correct values from store', ); } + + { + const store3 = schemaDB.transaction('object-store').store; + if ('getAllRecords' in store3) { + const index = store3.index('date'); + const val3 = await index.getAll({ direction: 'prev', count: 2 }); + typeAssert>(true); + assert.deepStrictEqual( + val3, + [ + { id: 1, title: 'Article 1', date: new Date('2019-01-04') }, + { id: 2, title: 'Article 2', date: new Date('2019-01-03') }, + ], + 'Correct values from store', + ); + } + } }); test('getAllKeys', async function () { @@ -1805,6 +1887,16 @@ suite('IDBPIndex', () => { assert.deepStrictEqual(val, [1, 2, 3, 4], 'Correct values from store'); } + + { + const store3 = schemaDB.transaction('object-store').store; + if ('getAllRecords' in store3) { + const index = store3.index('date'); + const val3 = await index.getAllKeys({ direction: 'prev', count: 2 }); + typeAssert>(true); + assert.deepStrictEqual(val3, [1, 2], 'Correct values from store'); + } + } }); test('getKey', async () => {