Check if Item Exists in IndexedDB

Checks if a specific item exists in IndexedDB.

Contributed by @itsbrunodev

javascript
function isItemInIndexedDB(databaseName, storeName, id) {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open(databaseName);
 
    request.onsuccess = (event) => {
      const db = event.target.result;
      const transaction = db.transaction(storeName, "readonly");
      const store = transaction.objectStore(storeName);
 
      const getRequest = store.get(id);
 
      getRequest.onsuccess = () => resolve(!!getRequest.result);
      getRequest.onerror = () => reject(getRequest.error);
    };
 
    request.onerror = () => reject(request.error);
  });
}
javascript
isItemInIndexedDB("myDatabase", "users", 1)
  .then(console.log) // true or false
  .catch(console.error);
GitHubEdit on GitHub