Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quasicomputational/73d408f8f9034976901de8a4836a7e88 to your computer and use it in GitHub Desktop.
Save quasicomputational/73d408f8f9034976901de8a4836a7e88 to your computer and use it in GitHub Desktop.
import indexedDB from "fake-indexeddb";
import IDBKeyRange from 'fake-indexeddb/lib/FDBKeyRange';
import IDBIndex from 'fake-indexeddb/lib/FDBIndex';
import IDBCursor from 'fake-indexeddb/lib/FDBCursor';
import IDBDatabase from 'fake-indexeddb/lib/FDBDatabase';
import IDBTransaction from 'fake-indexeddb/lib/FDBTransaction';
import IDBObjectStore from 'fake-indexeddb/lib/FDBObjectStore';
import IDBRequest from 'fake-indexeddb/lib/FDBRequest';
global.indexedDB = indexedDB;
global.IDBKeyRange = IDBKeyRange;
global.IDBIndex = IDBIndex;
global.IDBTransaction = IDBTransaction;
global.IDBDatabase = IDBDatabase;
global.IDBCursor = IDBCursor;
global.IDBObjectStore = IDBObjectStore;
global.IDBRequest = IDBRequest;
import { openDB } from "./node_modules/idb/build/esm/index.js";
export const STORE = "store";
export const dbPromise = () => openDB("db", 1, {
upgrade: (db, oldVersion) => {
switch (oldVersion) {
case 0:
db.createObjectStore(STORE, { autoIncrement: true });
break;
}
},
});
export const checkForGarbage = async () => {
const db = await dbPromise();
try {
const tx = db.transaction([STORE], "readwrite");
const store = tx.objectStore(STORE);
const id = await store.get(0);
console.log("Done!");
} finally {
db.close();
}
};
for (let i = 0; i < 3; i++) {
setTimeout(checkForGarbage, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment