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

dataloader-hook

v0.0.1

Published

Modified version of [Facebook's dataloader](https://github.com/graphql/dataloader) with each dataloader scoped to it's async context automatically using Node's [AsyncLocalStorage feature](https://nodejs.org/api/async_context.html#class-asynclocalstorage).

Downloads

3

Readme

dataloader-hook

Modified version of Facebook's dataloader with each dataloader scoped to it's async context automatically using Node's AsyncLocalStorage feature. This allowed each dataloader to be used within a single request without having to create and then pass down the loaders via a resolvers context value or attach to a req object.

Why?

  • Reduces amount of code - no need to create all your dataloaders at the start of a new request, and no need to have to pass the dataloaders down via arguments, handy especially if you have lots of deep call stacks per request.

  • Allows functions or modules in your code to be more decoupled, by not needing users of the modules to have to know about the dataloaders, and require them to initiate them in the request middleware and pass it around to different modules/functions via arguments.

How it works

Ctx-dataloader will create a new dataloader and an async context when the .load/loadMany or any other method has been called, and the dataloader will then propagate throughout the callbacks and promise chains on the subsequent calls of that request/async context until finished.

Example

import { DataLoaderHook } from 'dataloader-hook'

// No need to load run this for each request! Each dataloader is automatically scoped to each request. Just directly import this loader anywhere in your code, and if its in the request/async context batching and caching will work correctly.
const userLoader = new DataLoaderHook((ids) => getUsers(ids))

async function getUserBestFriend(user) {
  // 2. Looks up if there is a async context, because there is, the same loader will be used again, meaning there was only 1 db call for the entire request

  const bestFriend = await userLoader.load(user.bestFriendId)
  return bestFriend
}

async function getUserAndTheirBestFriend(userId) {
  // 1. New request scoped dataloader is created on .load call
  const user = await userLoader.load(postId)

  const bestFriend = await getUserBestFriend(user)
  return { ...user, bestFriend }
}

const app = express()

app.get('/user', async (req, res) => {
  const user = await getUserAndTheirBestFriend(req.query.id)
  return res.json(user)
})

app.listen(3000)