Skip to content

Commit

Permalink
Allow null queries (fixes jakearchibald#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Jan 6, 2021
1 parent 5fddd5f commit 3437ff9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
38 changes: 20 additions & 18 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
*/
count<Name extends StoreNames<DBTypes>>(
storeName: Name,
key?: StoreKey<DBTypes, Name> | IDBKeyRange,
key?: StoreKey<DBTypes, Name> | IDBKeyRange | null,
): Promise<number>;
/**
* Retrieves the number of records matching the given query in an index.
Expand All @@ -331,7 +331,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
>(
storeName: Name,
indexName: IndexName,
key?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange,
key?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange | null,
): Promise<number>;
/**
* Deletes records in a store matching the given query.
Expand Down Expand Up @@ -393,7 +393,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
*/
getAll<Name extends StoreNames<DBTypes>>(
storeName: Name,
query?: StoreKey<DBTypes, Name> | IDBKeyRange,
query?: StoreKey<DBTypes, Name> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
Expand All @@ -413,7 +413,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
>(
storeName: Name,
indexName: IndexName,
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, Name>[]>;
/**
Expand All @@ -428,7 +428,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
*/
getAllKeys<Name extends StoreNames<DBTypes>>(
storeName: Name,
query?: StoreKey<DBTypes, Name> | IDBKeyRange,
query?: StoreKey<DBTypes, Name> | IDBKeyRange | null,
count?: number,
): Promise<StoreKey<DBTypes, Name>[]>;
/**
Expand All @@ -448,7 +448,7 @@ export interface IDBPDatabase<DBTypes extends DBSchema | unknown = unknown>
>(
storeName: Name,
indexName: IndexName,
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, Name, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreKey<DBTypes, Name>[]>;
/**
Expand Down Expand Up @@ -596,7 +596,9 @@ export interface IDBPObjectStore<
/**
* Retrieves the number of records matching the given query.
*/
count(key?: StoreKey<DBTypes, StoreName> | IDBKeyRange): Promise<number>;
count(
key?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
): Promise<number>;
/**
* Creates a new index in store.
*
Expand Down Expand Up @@ -630,7 +632,7 @@ export interface IDBPObjectStore<
* @param count Maximum number of values to return.
*/
getAll(
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange,
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
Expand All @@ -640,7 +642,7 @@ export interface IDBPObjectStore<
* @param count Maximum number of keys to return.
*/
getAllKeys(
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange,
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
count?: number,
): Promise<StoreKey<DBTypes, StoreName>[]>;
/**
Expand All @@ -666,7 +668,7 @@ export interface IDBPObjectStore<
* @param direction
*/
openCursor(
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange,
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): Promise<IDBPCursorWithValue<
DBTypes,
Expand All @@ -684,7 +686,7 @@ export interface IDBPObjectStore<
* @param direction
*/
openKeyCursor(
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange,
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): Promise<IDBPCursor<DBTypes, TxStores, StoreName, unknown, Mode> | null>;
/**
Expand Down Expand Up @@ -717,7 +719,7 @@ export interface IDBPObjectStore<
* @param direction
*/
iterate(
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange,
query?: StoreKey<DBTypes, StoreName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): AsyncIterableIterator<
IDBPCursorWithValueIteratorValue<
Expand Down Expand Up @@ -761,7 +763,7 @@ export interface IDBPIndex<
* Retrieves the number of records matching the given query.
*/
count(
key?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
key?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
): Promise<number>;
/**
* Retrieves the value of the first record matching the query.
Expand All @@ -778,7 +780,7 @@ export interface IDBPIndex<
* @param count Maximum number of values to return.
*/
getAll(
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreValue<DBTypes, StoreName>[]>;
/**
Expand All @@ -788,7 +790,7 @@ export interface IDBPIndex<
* @param count Maximum number of keys to return.
*/
getAllKeys(
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
count?: number,
): Promise<StoreKey<DBTypes, StoreName>[]>;
/**
Expand All @@ -808,7 +810,7 @@ export interface IDBPIndex<
* @param direction
*/
openCursor(
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): Promise<IDBPCursorWithValue<
DBTypes,
Expand All @@ -826,7 +828,7 @@ export interface IDBPIndex<
* @param direction
*/
openKeyCursor(
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): Promise<IDBPCursor<DBTypes, TxStores, StoreName, IndexName, Mode> | null>;
/**
Expand All @@ -850,7 +852,7 @@ export interface IDBPIndex<
* @param direction
*/
iterate(
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange,
query?: IndexKey<DBTypes, StoreName, IndexName> | IDBKeyRange | null,
direction?: IDBCursorDirection,
): AsyncIterableIterator<
IDBPCursorWithValueIteratorValue<
Expand Down
8 changes: 4 additions & 4 deletions test/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ suite('Async iterators', () => {
typeAssert<
IsExact<
Parameters<typeof store.iterate>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -114,7 +114,7 @@ suite('Async iterators', () => {
typeAssert<
IsExact<
Parameters<typeof store.iterate>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -279,7 +279,7 @@ suite('Async iterators', () => {
typeAssert<
IsExact<
Parameters<typeof index.iterate>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -294,7 +294,7 @@ suite('Async iterators', () => {
typeAssert<
IsExact<
Parameters<typeof index.iterate>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down
50 changes: 25 additions & 25 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store1.count>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1044,7 +1044,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store2.count>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1070,12 +1070,12 @@ suite('IDBPObjectStore', () => {
db = (await openDB(dbName, getNextVersion(), {
upgrade(db, oldVersion, newVersion, tx) {
const store = db.createObjectStore('object-store');
typeAssert<
IsExact<Parameters<typeof store.createIndex>[0], string>
>(true);
typeAssert<IsExact<Parameters<typeof store.createIndex>[0], string>>(
true,
);
},
})) as IDBPDatabase;
});
})) as IDBPDatabase;
});

test('delete', async () => {
const schemaDB = await openDBWithData();
Expand Down Expand Up @@ -1143,7 +1143,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store1.getAll>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1158,7 +1158,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store2.getAll>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1179,7 +1179,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store1.getAllKeys>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1198,7 +1198,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store2.getAllKeys>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1267,7 +1267,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store1.openCursor>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1292,7 +1292,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store2.openCursor>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1323,7 +1323,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store1.openKeyCursor>[0],
string | IDBKeyRange | undefined
string | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1346,7 +1346,7 @@ suite('IDBPObjectStore', () => {
typeAssert<
IsExact<
Parameters<typeof store2.openKeyCursor>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1546,7 +1546,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index1.count>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1561,7 +1561,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index2.count>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1628,7 +1628,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.getAll>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1670,7 +1670,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.getAll>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1718,7 +1718,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.getAllKeys>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1735,7 +1735,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.getAllKeys>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1792,7 +1792,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.openCursor>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1819,7 +1819,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.openCursor>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down Expand Up @@ -1851,7 +1851,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.openKeyCursor>[0],
Date | IDBKeyRange | undefined
Date | IDBKeyRange | undefined | null
>
>(true);

Expand All @@ -1878,7 +1878,7 @@ suite('IDBPIndex', () => {
typeAssert<
IsExact<
Parameters<typeof index.openKeyCursor>[0],
IDBValidKey | IDBKeyRange | undefined
IDBValidKey | IDBKeyRange | undefined | null
>
>(true);

Expand Down

0 comments on commit 3437ff9

Please sign in to comment.