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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mavenoid/dataloader

v3.1.0

Published

Utilities to batch and cache data loading in GraphQL like APIs

Readme

@mavenoid/dataloader

Utilities for caching, batching and deduping requests in GraphQL. Inspired by dataloader but extended using WeakMap to create a more convenient API for GraphQL

Rolling Versions

Installation

yarn add @mavenoid/dataloader

Usage

import {
  getDataLoadersForContextType,
  RequestsBatch,
} from '@mavenoid/dataloader';

const {batch, dedupe} = getDataLoadersForContextType<ResolverContextType>();

const getUserDeduped = dedupe(
  async (userID: number, _resolverContext: ResolverContextType) => {
    return await getUserUncached(userID);
  },
);

// You can load a user within a GraphQL resolver using:
//
//   getUserDeduped(userID, resolverContext)
//
// You can call this as many times as you like, and `getUserUncached` will only be called
// once per request.
//
// if you modify a user within a request, you can delete it from the cache using:
//
//   getUserDeduped.cache(resolverContext).delete(userID)
//
// or
//
//   getUserDeduped.cache(resolverContext).clear()

const getUserBatched = batch(
  async (userIDs: number[], _resolverContext: ResolverContextType) => {
    // load all the requested userIDs in one go, and then return a map from
    // the requested IDs to the results
    const users = await db_users.find({id: anyOf(userIDs)}).all();
    return new Map(users.map((user) => [user.id, user]));
  },
  {maxBatchSize: 100},
);

// You can load a user within a GraphQL resolver using:
//
//   getUserBatched(userID, resolverContext)
//
// You can call this as many times as you like, and the database will only be queried
// once per request. The requests will be batched so that up to 100 users are loaded
// at a time.
//
// if you modify a user within a request, you can delete it from the cache using:
//
//   getUserBatched.cache(resolverContext).delete(userID)
//
// or
//
//   getUserBatched.cache(resolverContext).clear()

Handling undefined

The batch methods return undefined for any requested records that are not included in the results. If this is not what you want, you can add a fallback using:

import {
  handleUndefinedResult,
  errorOnUndefinedResult,
} from '@mavenoid/dataloader';

const getUserOrNull = handleUndefinedResult(getUserBatched, () => null);
const getUserOrError = errorOnUndefinedResult(
  getUserBatched,
  (id) => `Could not find user: ${id}`,
);

Handling requests for lists of dependent records

The groupRecordsByKey can be used to group a list of records by a foreign key. It will preserve the order of the records within each group.

const getUserMessagesBatched = handleUndefinedResult(
  batch(
    async (userIDs: number[], _resolverContext: ResolverContextType) => {
      const messages = await db_messages
        .find({sender_id: anyOf(userIDs)})
        .all();
      return groupRecordsByKey(messages, (m) => m.sender_id);
    },
    {maxBatchSize: 100},
  ),
  () => [],
);

Handling requests with composite keys

If you need to query using more than one key, you can use an object. JavaScript compares objects by reference, rather than by value though. This means you will need to transform the key into a string or number wherever it is used to look up a value in a Map/Cache. We provide a few helpers to make that easier.

const getTranslatedMessage = batch(
  async (
    requests: {messageID: number; language: string}[],
    _resolverContext: ResolverContextType,
  ) => {
    const messages = await db_message_translations.query(
      sql`
        SELECT * FROM message_translations
        WHERE ${sql.join(
          requests.map(
            (r) => sql`(message_id=${r.messageID} AND lang=${r.language})`,
          ),
          ' OR ',
        )}
      `,
    );
    return transformMapKey(
      extractRecordsByKey(messages, (m) => `${m.message_id}/${m.lang}`),
    )(
      (req: {messageID: number; language: string}) =>
        `${req.messageID}/${req.language}`,
    );
  },
  {
    maxBatchSize: 100,
    getCacheKey: (req) => `${req.messageID}/${req.language}`,
  },
);