Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,40 @@ type CursorKey<
? IndexKey<DBTypes, StoreName, IndexName>
: StoreKey<DBTypes, StoreName>;

/**
* 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<DBTypes>,
> {
query?: StoreKey<DBTypes, StoreName> | 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<DBTypes>,
IndexName extends IndexNames<DBTypes, StoreName>,
> {
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null;
count?: number;
direction?: IDBCursorDirection;
}

type IDBPDatabaseExtends = Omit<
IDBDatabase,
'createObjectStore' | 'deleteObjectStore' | 'transaction' | 'objectStoreNames'
Expand Down Expand Up @@ -437,6 +471,20 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
indexName: IndexName,
query: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange,
): Promise<StoreValue<DBTypes, Name> | 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<Name extends StoreNames<DBTypes>>(
storeName: Name,
options?: IDBPStoreGetAllOptions<DBTypes, Name>,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
* Retrieves all values in a store that match the query.
*
Expand All @@ -452,6 +500,25 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
query?: StoreKey<DBTypes, Name> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
* 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<DBTypes>,
IndexName extends IndexNames<DBTypes, Name>,
>(
storeName: Name,
indexName: IndexName,
options?: IDBPIndexGetAllOptions<DBTypes, Name, IndexName>,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
* Retrieves all values in an index that match the query.
*
Expand All @@ -472,6 +539,20 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
* 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<Name extends StoreNames<DBTypes>>(
storeName: Name,
options?: IDBPStoreGetAllOptions<DBTypes, Name>,
): Promise<StoreKey<DBTypes, Name>[]>;
/**
* Retrieves the keys of records in a store matching the query.
*
Expand All @@ -487,6 +568,25 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
query?: StoreKey<DBTypes, Name> | IDBKeyRange | null,
count?: number,
): Promise<StoreKey<DBTypes, Name>[]>;
/**
* 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<DBTypes>,
IndexName extends IndexNames<DBTypes, Name>,
>(
storeName: Name,
indexName: IndexName,
options?: IDBPIndexGetAllOptions<DBTypes, Name, IndexName>,
): Promise<StoreKey<DBTypes, Name>[]>;
/**
* Retrieves the keys of records in an index matching the query.
*
Expand Down Expand Up @@ -685,6 +785,15 @@ export interface IDBPObjectStore<
get(
query: StoreKey<DBTypes, StoreName> | IDBKeyRange,
): Promise<StoreValue<DBTypes, StoreName> | undefined>;
/**
* Retrieves all values that match the query.
* Supported in chrome/edge 141 or newer.
*
* @param options
*/
getAll(
options?: IDBPStoreGetAllOptions<DBTypes, StoreName>,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
* Retrieves all values that match the query.
*
Expand All @@ -695,6 +804,15 @@ export interface IDBPObjectStore<
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
* Retrieves the keys of records matching the query.
* Supported in chrome/edge 141 or newer.
*
* @param options
*/
getAllKeys(
options?: IDBPStoreGetAllOptions<DBTypes, StoreName>,
): Promise<StoreKey<DBTypes, StoreName>[]>;
/**
* Retrieves the keys of records matching the query.
*
Expand Down Expand Up @@ -835,6 +953,15 @@ export interface IDBPIndex<
get(
query: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
): Promise<StoreValue<DBTypes, StoreName> | undefined>;
/**
* Retrieves all values that match the query.
* Supported in chrome/edge 141 or newer.
*
* @param options
*/
getAll(
options?: IDBPIndexGetAllOptions<DBTypes, StoreName, IndexName>,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
* Retrieves all values that match the query.
*
Expand All @@ -845,6 +972,15 @@ export interface IDBPIndex<
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
* Retrieves the keys of records matching the query.
* Supported in chrome/edge 141 or newer.
*
* @param options
*/
getAllKeys(
options?: IDBPIndexGetAllOptions<DBTypes, StoreName, IndexName>,
): Promise<StoreKey<DBTypes, StoreName>[]>;
/**
* Retrieves the keys of records matching the query.
*
Expand Down
92 changes: 92 additions & 0 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ suite('IDBPDatabase', () => {
typeAssert<IsExact<typeof val2, any[]>>(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<IsExact<typeof val3, number[]>>(true);
assert.deepStrictEqual(val3, [789, 123], 'Correct values from store');
}
});

test('getAllFromIndex', async function () {
Expand Down Expand Up @@ -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<IsExact<typeof val3, ObjectStoreValue[]>>(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 () {
Expand Down Expand Up @@ -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<IsExact<typeof val3, string[]>>(true);
assert.deepStrictEqual(
val3,
['hello', 'foo'],
'Correct values from store',
);
}
});

test('getAllKeysFromIndex', async function () {
Expand All @@ -388,6 +426,15 @@ suite('IDBPDatabase', () => {
typeAssert<IsExact<typeof val2, IDBValidKey[]>>(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<IsExact<typeof val3, number[]>>(true);
assert.deepStrictEqual(val3, [1, 2], 'Correct values from store');
}
});

test('count', async () => {
Expand Down Expand Up @@ -1224,6 +1271,13 @@ suite('IDBPObjectStore', () => {
typeAssert<IsExact<typeof val2, any[]>>(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<IsExact<typeof val3, number[]>>(true);
assert.deepStrictEqual(val3, [789, 123], 'Correct values from store');
}
});

test('getAllKeys', async function () {
Expand Down Expand Up @@ -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<IsExact<typeof val3, string[]>>(true);
assert.deepStrictEqual(
val3,
['hello', 'foo'],
'Correct values from store',
);
}
});

test('getKey', async function () {
Expand Down Expand Up @@ -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<IsExact<typeof val3, ObjectStoreValue[]>>(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 () {
Expand Down Expand Up @@ -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<IsExact<typeof val3, number[]>>(true);
assert.deepStrictEqual(val3, [1, 2], 'Correct values from store');
}
}
});

test('getKey', async () => {
Expand Down