The createStore(dbName, storeName) function creates a custom store configuration. The resulting object is a function that accepts a transaction mode ("readonly" or "readwrite") and a callback. The callback provides access to the IndexedDB object store.
Note that because of how IndexedDB handles schema migrations, you cannot use createStore to target different stores within the same database name. Each createStore call with a unique dbName is valid, but multiple calls with the same dbName but different storeName will not work as expected.
import { promisifyRequest } from 'idb-keyval';
function createStore(dbName, storeName) {
const request = indexedDB.open(dbName);
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
const dbp = promisifyRequest(request);
return (txMode, callback) =>
dbp.then((db) =>
callback(db.transaction(storeName, txMode).objectStore(storeName)),
);
}