npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

idb-request

v3.2.1

Published

Transform IndexedDB request-like object to a Promise

Downloads

6

Readme

idb-request

Transform IndexedDB request-like object to a Promise.

Promise is a nice way to deal with primitives of IndexedDB. IDBRequest has onsuccess and onerror callbacks, which perfectly map to Promise's resolve and reject. The same applies to oncomplete and onerror of IDBTransaction.

If you're going to reuse transactions with Promise syntax, you can't be sure that it will work in all browsers. You need to rely on the default callback syntax or use idb-batch. This issue is well explained in "Tasks, microtasks, queues and schedules" article.

Internally idb-request fixes IndexedDBShim#204 issue and allows safely iterate over index cursors.

Installation

npm install --save idb-request

Example

Using ES2016 async/await syntax.

import { open } from 'idb-factory'
import { request, requestTransaction, requestCursor } from 'idb-request'
import map from 'lodash.map'

async () => {  
  const db = await open('mydb', 1, upgradeCallback)
  const tr = db.transaction(['books'], 'readwrite')
  const books = tr.objectStore('books')

  await Promise.all([
    request(books.put({ id: 1, title: 'Quarry Memories', author: 'Fred' })),
    request(books.put({ id: 2, title: 'Water Buffaloes', author: 'Fred' })),
    request(books.put({ id: 3, title: 'Bedrock Nights', author: 'Barney' })),
    requestTransaction(tr),
  ])

  const req = books.index('byAuthor').openCursor(null, 'nextunique') // works everywhere
  const result = []
  await requestCursor(req, (cursor) => {
    result.push(cursor.value)
    cursor.continue()
  })

  console.assert(map(result, 'author') === ['Barney', 'Fred'])
}()

function upgradeCallback(e) {
  const books = e.target.result.createObjectStore('books', { keyPath: 'id' })
  books.createIndex('byTitle', 'title', { unique: true })
  books.createIndex('byAuthor', 'author')
}

API

Each function returns a Promise.

request(req, [tr])

Listen to request's onsuccess event.

import { request } from 'idb-request'

const books = db.transaction(['books'], 'readonly').objectStore('books')
request(books.count()).then((count) => {})

Pass the transaction as a second argument to await completion and return the result of the request.

import { request } from 'idb-request'

const tr = db.transaction(['books'], 'readwrite')
const books = tr.objectStore('books')
const req = books.put({ title: 'Store 1' })

request(req, tr).then((requestResult) => {})

mapCursor(req, iterator)

Map values over cursor. Iterator has 3 arguments:

  • cursor object,
  • result array, which is returned on resolve. Default value is [].
  • stop resolve promise and exit earlier
import { mapCursor } from 'idb-request'

const limit = 10
const result = await mapCursor(books.openCursor(), (cursor, memo, stop) => {
  memo.push(cursor.value)
  if (memo.length >= limit) stop()
  else cursor.continue()
})

requestCursor(req, iterator)

Iterate through object store or index using cursor.

The same example as mapCursor above:

import { requestCursor } from 'idb-request'

const limit = 10
const result = []
const req = books.openCursor()

await requestCursor(req, (cursor, stop) => {
  result.push(cursor.value)
  if (memo.length >= limit) stop()
  else cursor.continue()
})

// use result array

requestTransaction(tr)

Listen to transaction's oncomplete event.

import { request, requestTransaction } from 'idb-request'

const tr = db.transaction(['books'], 'readwrite')
const books = tr.objectStore('books')

await Promise.all([
  request(books.put({ id: 1, title: 'Quarry Memories' })),
  request(books.put({ id: 2, title: 'Water Buffaloes' })),
  request(books.put({ id: 3, title: 'Bedrock Nights' })),
  requestTransaction(tr),
])

License

MIT