@applifting-io/nestjs-dataloader

中文文档
1.1.6发布于 2025/09/22MITTypeScriptCommonJS近 30 天下载 1779

A NestJS decorator for dataloader

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. NestDataLoader Creation
  4. Providing the NestDataLoader
  5. Using the NestDataLoader
  6. Contributing

NestJS Dataloader

Node.js CI Coverage Status

NestJS dataloader simplifies adding graphql/dataloader to your NestJS project. DataLoader aims to solve the common N+1 loading problem.

This is a fork of krislefeber/nestjs-dataloader with the following changes:

  • Replace generateDataLoader method in interface with getBatchFunction, reducing the need to import anything from the dataloader package and simplifying the code
  • Added sortDataLoaderResultsByKey convenience method, since some batching methods you can use (like SELECT * IN (...)) do not guarantee the order of the returned results to match the order of provided keys, which is essential for dataloader to work properly
  • Updated all dependencies to latest versions

Installation

npm i @applifting-io/nestjs-dataloader

Usage

NestDataLoader Creation

We start by implementing the NestDataLoader interface. This tells DataLoader how to load our objects.

import { Injectable } from '@nestjs/common';
import { NestDataLoader, DataLoaderBatchFunction } from '@applifting-io/nestjs-dataloader';
...

@Injectable()
export class AccountLoader implements NestDataLoader<string, Account> {
  constructor(private readonly accountService: AccountService) { }

  getBatchFunction(): DataLoaderBatchFunction<string, Account> {
    return (keys: string[]) => {
      const results = await this.accountService.findByIds(keys);
      return sortDataLoaderResultsByKey(keys, results, "id");
    };
  }
}

The first generic of the interface is the type of ID the datastore uses. The second generic is the type of object that will be returned. In the above instance, we want DataLoader to return instances of the Account class.

Providing the NestDataLoader

For each NestDataLoader we create, we need to provide it to our module.

import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { DataLoaderInterceptor } from 'nestjs-dataloader'
...

@Module({
  providers: [
    AccountResolver,
    AccountLoader,
    {
      provide: APP_INTERCEPTOR,
      useClass: DataLoaderInterceptor,
    },
  ],

})
export class ResolversModule { }

Using the NestDataLoader

Now that we have a dataloader and our module is aware of it, we need to pass it as a parameter to an endpoint in our graphQL resolver.

import * as DataLoader from 'dataloader';
import { Loader } from 'nestjs-dataloader';
...

@Resolver(Account)
export class AccountResolver {

    @Query(() => [Account])
    public getAccounts(
        @Args({ name: 'ids', type: () => [String] }) ids: string[],
        @Loader(AccountLoader) accountLoader: DataLoader<Account['id'], Account>): Promise<Account[]> {
        return accountLoader.loadMany(ids);
    }
}

The important thing to note is that the parameter of the @Loader decorator is the entity/class of the NestDataLoader we want to be injected to the method. The DataLoader library will handle bulk retrieval and caching of our requests. Note that the caching is stored on a per-request basis.

Contributing

Pull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.

版本与发布记录

@applifting-io/nestjs-dataloader 共发布 7 个版本,首次发布于 2023/11/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.6latest2025/09/221.1 年前
1.1.52023/11/073 年前
1.1.32023/11/073 年前
1.1.22023/11/073 年前
1.1.12023/11/073 年前
1.1.02023/11/073 年前

关于 @applifting-io/nestjs-dataloader 的常见问题

@applifting-io/nestjs-dataloader 是什么?

A NestJS decorator for dataloader

如何安装 @applifting-io/nestjs-dataloader?

在项目目录运行 npm install @applifting-io/nestjs-dataloader,或使用 pnpm add / yarn add / bun add。

@applifting-io/nestjs-dataloader 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@applifting-io/nestjs-dataloader 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@applifting-io/nestjs-dataloader 可以商用吗?

@applifting-io/nestjs-dataloader 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@applifting-io/nestjs-dataloader 的下载量有多少?

根据 npm 官方下载统计,@applifting-io/nestjs-dataloader 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,779 次,日均约 59 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@applifting-io/nestjs-dataloader 还在维护吗?

最新版本 1.1.6 发布于 2025/09/22(1.1 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/11/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。