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

@double-spent/nestjs-dataloader

v0.1.0

Published

A dataloader implementation for NestJS including request-scoped cache and more

Downloads

1

Readme

About

DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

This library simplifies the integration of DataLoader within NestJS applications by providing a friendly wrapper around the original implementation. It also introduces a request-scoped DataLoader, enabling efficient data fetching while ensuring the isolation of requests, especially useful in GraphQL scenarios and services that doesn't support request-scope like NestJS CQRS.

// before:

import DataLoader from 'dataloader';

const userLoader = new DataLoader(async (keys) => {
  // load users ..
});

// after:

import { DataLoader } from '@double-spent/nestjs-dataloader';

@Injectable()
class UserLoader extends DataLoader<User['id'], User> {
  async batchLoad(ids): User[] {
    // load users ..
  }
}

Installation

npm install @double-spent/nestjs-dataloader

Usage

Default DataLoader

The default DataLoader base class offers all the functionality the original implementation does by accepting the same options in the constructor. To use it in a NestJS module, extend the base class, decorate it with @Injectable(), and add it to the module providers and exports:

// 💾 UserLoader.ts

import { DataLoader } from '@double-spent/nestjs-dataloader';

@Injectable()
export class UserLoader extends DataLoader<User['id'], User> {
  async batchLoad(ids): User[] {
    // load users ..
  }
}

// 💾 UserController.ts

@Controller()
export class UserController() {
  constructor(userLoader: UserLoader) {}

  @Get()
  async user(@Param('id') id: User['id']) {
    return this.userLoader.load(id);
  }
}

// 💾 UserModule.ts

@Module({
  providers: [UserLoader],
  controllers: [UserController],
})
export class UserModule {}

Request-scoped DataLoader

The default DataLoader uses a memoized cache by default that is shared across all requests. On the other hand, the request-scoped DataLoader leverages the AsyncLocalStorage class to initialize a cache that expires at the end of the request. This makes it safe to use in NestJS services that only supports global scope but must handle request-scoped (or user-scoped) data, like CQRS.

In order for this loader to work, you must import the RequestScopedDataLoaderModule into your application which initializes the cache storage. An error is thrown otherwise as there's no sense in using this DataLoader if no request-scoped storage is available.

// 💾 UserLoader.ts

import { RequestScopedDataLoader } from '@double-spent/nestjs-dataloader';

@Injectable()
export class UserLoader extends RequestScopedDataLoader<User['id'], User> {
  async batchLoad(ids): User[] {
    // load users ..
  }
}

// 💾 UserController.ts

@Controller()
export class UserController() {
  constructor(userLoader: UserLoader) {}

  @Get()
  async user(@Param('id') id: User['id']) {
    return this.userLoader.load(id);
  }
}

// 💾 UserModule.ts

@Module({
  providers: [UserLoader],
  controllers: [UserController],
  imports: [RequestScopedDataLoaderModule],
})
export class UserModule {}

Troubleshooting

I'm getting a runtime error when extending the RequestScopedDataLoader class.

Make sure you're importing the RequestScopedDataLoaderModule into your application.

License

MIT License © Andrea SonnY