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

@seedcompany/data-loader

v0.5.4

Published

NestJS library wrapper for data loaders

Downloads

314

Readme

Yet another DataLoader for NestJS

As most were, this is inspired by TreeMan360/nestjs-graphql-dataloader

This one offers several differences over that and other forks:

  • It works with any ExecutionContext, not just GraphQL
  • It supports access inside guards not just interceptors & resolvers
  • DataLoader instances are created separately from the strategy logic. Others handle this abstraction via extending a library provided class.
  • Default configuration options can be given when the module is registered. Such as customizing error messages or setting a default max batch size.
  • Like others, this library handles sorting the results to match the order of the keys. How the key is derived from the item is configurable via propertyKey.
  • Errors can be returned from loadMany batch calls, but in a different shape compared to other libraries and raw dataloaders.
    { key: Key, error: Error }
    This allows us to match the error to the appropriate key without enforcing errors to contain them.
  • Type aliases are provided to prevent having to duplicate Item & Key types at call site. Since parameter types have no enforceable correlation to their decorators, it's easy to mess up the item/key types when explicitly providing them. With this library the usage just references the loader strategy which declares the item/key types.
    @Loader(UsersById) users: LoaderOf<UsersById>
  • The @Loader() decorator is durable against referencing a strategy that nodejs hasn't loaded yet. Helpful errors are thrown in this case and we provide an alternative syntax to defer the reference (just like @nestjs/graphql)
    @Loader(() => UsersById)

Usage

Import the module in your app.

DataLoaderModule.register({
  // Any default options for all loaders
})

Create a strategy class that implements DataLoaderStrategy<Item, Key>

@Injectable({ scope: Scope.REQUEST })
class UsersById implements DataLoaderStrategy<User, string> {
  constructor(
    private readonly usersService: UsersService,
  ) {}

  async loadMany(ids: string[]) {
    return this.usersService.findByIds(ids);
  }
}

Be sure to register the strategy as a provider in your module.

Inject the loader in your resolver and use it.

class Resolver {
  @Query()
  user(
    @Args('id') id: string,
    @Loader(UsersById) users: LoaderOf<UsersById>,
  ) {
    return users.load(id);
  }
}

Advanced Usage

It's possible to grab a loader instance outside a resolver, but an ExecutionContext is needed.

@Injectable()
class Thing {
  constructor(private dataLoaderContext: DataLoaderContext) {}

  something(context: ExecutionContext) {
    const users = this.dataLoaderContext.attachToExecutionContext(context).getLoader(UsersById);
    const user = users.load('id');
  }
}