jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.31k stars 356 forks source link

incorrect multientry index typings #153

Closed akhilpai closed 4 years ago

akhilpai commented 4 years ago

Hello, it seems multiEntry index typings are incorrect.

For example, given a schema: entries: { key: string value: { tags: string[] } indexes }

created as: const store = db.createObjectStore("entries") store.createIndex("tags", "tags", { multiEntry: true })

Retrieval by await db.getFromIndex("entries", "tags", ["some tag"]) returns an empty array

while await db.getFromIndex("entries", "tags", "some tag") fails with a cast error.

and await db.getFromIndex("entries", "tags", ("some tag" as unknown) as string[]) returns desired data.

jakearchibald commented 4 years ago

Can you give me a full reduced code sample?

akhilpai commented 4 years ago

Sure, try removing the cast at: https://github.com/akhilpai/idb-153/blob/master/src/example.ts

I did not expect the error: Argument of type '"some tag"' is not assignable to parameter of type 'string[] | IDBKeyRange | undefined'.

jakearchibald commented 4 years ago

Just to keep things in the thread:

import { DBSchema, openDB } from "idb";

interface Schema extends DBSchema {
    entries: {
        key: string;
        value: { id: string; tags: string[] };
        indexes: { tags: string[] };
    };
}

(async () => {
    let db = await openDB<Schema>("entryDB", 1, {
        async upgrade(db) {
            const entryStore = db.createObjectStore("entries", { keyPath: "id" });
            entryStore.createIndex("tags", "tags", {
                multiEntry: true
            });
        }
    });

    await db.add("entries", { id: "some id", tags: ["some tag"] });

    let result = await db.getAllFromIndex(
        "entries",
        "tags",
        ("some tag" as unknown) as string[]
    );

    console.log(result);
})();
jakearchibald commented 4 years ago

Shouldn't your index type string rather than string[]?

interface Schema extends DBSchema {
    entries: {
        key: string;
        value: { id: string; tags: string[] };
        indexes: { tags: string }; // <- this line is changed
    };
}
akhilpai commented 4 years ago

Yes, you are right. Thank you for investigating and sorry for the noise!

jakearchibald commented 4 years ago

No worries! Glad folks are finding the typings useful.