question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support polyfilling getAll with one tiny change

See original GitHub issue

Although all modern browsers support getAll, old versions don’t. A while back I wrote indexeddb-getall-shim, and I’m still using it some places because I still have users on those old browsers.

Problem is, idb does not like my polyfill. Specifically it breaks on this line:

  if (value instanceof IDBRequest)

I don’t think it is possible for me to create an actual IDBRequest, so instead I created a fake one. But I can’t make myFakeRequest instanceof IDBRequest return true (well, I can by setting its prototype to IDBRequest, but it seems this results in browsers restricting what I can do with the object, making it useless for my purposes).

If instead your code was changed to:

  if (value instanceof IDBRequest || (value && value.toString() === "[object IDBRequest]"))

then my polyfill would work fine.

Are you interested in adding this ugliness to your library? If so, I’m happy to make a PR.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
erezsobcommented, Mar 9, 2020

Hey there, @jakearchibald and @dumbmatter.

It’s nice to find this discussion here, because I’m actually a user of idb and the lack of support for Edge was a sad realization for us. In order to solve our app crashing on older Edge versions I actually implemented a polyfill based on @dumbmatter 's mentioned library.

Just to contribute to the discussion, an idb pollyfill for the getAll method for unsupported browsers, or at least the tiny change that @dumbmatter suggested in their initial comment, would be very much desirable.

Either way, thanks a lot to both of you for the work on these libraries.

0reactions
jakearchibaldcommented, Jan 6, 2021

Since the number of Edge users is decreasing, I’ve decided not to implement this. However, here’s a polyfill for anyone that needs it:

import type { StoreNames, IDBPDatabase, StoreValue } from "idb";

async function getAll<
  DBTypes,
  StoreName extends StoreNames<DBTypes> = StoreNames<DBTypes>
>(
  db: IDBPDatabase<DBTypes>,
  storeName: StoreName
): Promise<StoreValue<DBTypes, StoreName>[]> {
  if (db.getAll) return db.getAll(storeName);

  const items: StoreValue<DBTypes, StoreName>[] = [];
  let cursor = await db.transaction(storeName).store.openCursor();

  while (cursor) {
    items.push(cursor.value);
    cursor = await cursor.continue();
  }

  return items;
}

Demo: https://codesandbox.io/s/awesome-faraday-9q6bz?file=/src/index.ts

Plain JS version:

async function getAll(db, storeName) {
  if (db.getAll) return db.getAll(storeName);
  
  const items = [];
  let cursor = await db.transaction(storeName).store.openCursor();

  while (cursor) {
    items.push(cursor.value);
    cursor = await cursor.continue();
  }

  return items;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get all properties values of a JavaScript Object (without ...
Using ECMAScript 2015 fat-arrow functions, mapping the object to an array of values becomes a one-liner: const vals = Object.keys(obj).map(key => obj[key]); ...
Read more >
zloirock/core-js: Standard Library - GitHub
Modular standard library for JavaScript. Includes polyfills for ECMAScript up to 2023: promises, symbols, collections, iterators, typed arrays, many other ...
Read more >
Data Loading - Remix
One of the primary features of Remix is simplifying interactions with the server to get data into components. This document will help you ......
Read more >
JavaScript SDK - Split Help Center
All getTreatment calls for a Split now only return the one treatment (and configs, if defined) you have defined in the map. You...
Read more >
Get Query String Parameters with JavaScript - David Walsh Blog
While URLSearchParams is ideal, not all browsers support that API. There's a polyfill available but if you want a tiny function for basic ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found