Code Monkey home page Code Monkey logo

connected-dataloader's Introduction

connected-dataloader

This package allows you to create DataLoader instances which share a common cache storage. Loads performed with one DataLoader will automatically prime the cache of other DataLoader instances that are linked to the same storage.

This is useful in cases where a particular piece of data can be located through multiple loaders—for example, a user might be loadable by either their ID or email address.

The connected DataLoader abstraction frees up developers from manually managing cache entries, and decouples DataLoaders from the technical details of each other's cache key function.

Before:

const byId = new DataLoader<string[], User>(async (keys) => {
  const users = await db.users.findMany({
    where: { id: { in: keys } },
  });

  // This is duplicated
  for (const user of users) {
    byEmail.clear(user.email).prime(user.email, user);
  }

  return users;
});

const byEmail = new DataLoader<string[], User>(async (keys) => {
  const users = await db.users.findMany({
    where: { email: { in: keys } },
  });

  // And also means `byEmail` is coupled to cache key semantics of `byId`
  //   (and vice versa!)
  for (const user of users) {
    byId.clear(user.id).prime(user.id, user);
  }

  return users;
});

After:

import {
  ConnectedLoader,
  ConnectedLoaderStorage,
} from 'connected-dataloader';

// Shared storage for connected loaders
const storage = new ConnectedLoaderStorage<string, User>();

// This loader will automatically prime `byEmail`'s cache
const byId = new ConnectedLoader(
  async keys =>
    db.user.findMany({
      where: { id: { in: keys } },
    }),
  // `valueKey` defines mapping from `user` object => cache key
  { storage, valueKey: user => user.id },
);

const byEmail = new ConnectedLoader(
  async keys =>
    db.user.findMany({
      where: { id: { in: keys } },
    }),
  { storage, valueKey: user => user.email },
)

connected-dataloader's People

Contributors

sophiabits avatar

Stargazers

Mario Adisurya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.