All Projects → krislefeber → Nestjs Dataloader

krislefeber / Nestjs Dataloader

Licence: mit
Dataloader plugin for NestJS

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Nestjs Dataloader

Ts Proto
An idiomatic protobuf generator for TypeScript
Stars: ✭ 340 (+250.52%)
Mutual labels:  dataloader, nestjs
Nestjs Example
NestJS example with GraphQL, Schema-Stitching, Dataloader, GraphQL Upload, RabbitMQ, Redis, Scalable Websocket and JWT authentication
Stars: ✭ 111 (+14.43%)
Mutual labels:  dataloader, nestjs
Blog
📚 专注Web与算法
Stars: ✭ 1,140 (+1075.26%)
Mutual labels:  nestjs
Abraham
小程序垃圾分类
Stars: ✭ 90 (-7.22%)
Mutual labels:  nestjs
Nestjs Roles
Type safe roles guard and decorator made easy
Stars: ✭ 78 (-19.59%)
Mutual labels:  nestjs
Node Blog
🔥✨ A react blog project base on nodejs, nestjs, mongoose, typescript, react, ant-design,nextjs
Stars: ✭ 69 (-28.87%)
Mutual labels:  nestjs
Nestjs Sentry
Provides an injectable sentry.io client to provide enterprise logging nestjs modules
Stars: ✭ 80 (-17.53%)
Mutual labels:  nestjs
Nestjs Monorepo Starter
A full-stack nestjs starter project. Includes authorization, authentication, MongoDB, Redis queue support, Crons jobs, and end-to-end testing.
Stars: ✭ 58 (-40.21%)
Mutual labels:  nestjs
Dataloaders
Pytorch and TensorFlow data loaders for several audio datasets
Stars: ✭ 97 (+0%)
Mutual labels:  dataloader
Drivine
Best and fastest graph database client (Neo4j & AgensGraph) for Node.js & TypeScript.
Stars: ✭ 74 (-23.71%)
Mutual labels:  nestjs
Vscode Nestjs Snippets
💥 Vscode NestJS Code Snippets
Stars: ✭ 88 (-9.28%)
Mutual labels:  nestjs
Nestjs Components
A list of useful components for NestJS applications
Stars: ✭ 72 (-25.77%)
Mutual labels:  nestjs
Purify
All-in-one tool for managing vulnerability reports from AppSec pipelines
Stars: ✭ 72 (-25.77%)
Mutual labels:  nestjs
Simple Todos
A simple web application powered by Nuxt.js 💚 & Nest Framework 😻
Stars: ✭ 81 (-16.49%)
Mutual labels:  nestjs
Bank
🏦 Full Stack Web Application similar to financial software that is used in banking institutions | React.js and Node.js
Stars: ✭ 1,158 (+1093.81%)
Mutual labels:  nestjs
Video Dataset Loading Pytorch
Generic PyTorch Dataset Implementation for Loading, Preprocessing and Augmenting Video Datasets
Stars: ✭ 92 (-5.15%)
Mutual labels:  dataloader
Nestjs Braintree
A module for braintree reoccurring payments and transactions 💳
Stars: ✭ 62 (-36.08%)
Mutual labels:  nestjs
Type Graphql Dataloader
TypeGraphQL + DataLoader + TypeORM made easy
Stars: ✭ 73 (-24.74%)
Mutual labels:  dataloader
Nestjs Cqrs Starter
NestJS CQRS Microservices Starter Project
Stars: ✭ 80 (-17.53%)
Mutual labels:  nestjs
Newschool Backend
Plataforma de ensino para pessoas carentes
Stars: ✭ 98 (+1.03%)
Mutual labels:  nestjs

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.

Installation

Install with yarn

yarn add nestjs-dataloader

Install with npm

npm install --save nestjs-dataloader

Usage

NestDataLoader Creation

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

import * as DataLoader from 'dataloader';
import { Injectable } from '@nestjs/common';
import { NestDataLoader } from 'nestjs-dataloader';
...

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

  generateDataLoader(): DataLoader<string, Account> {
    return new DataLoader<string, Account>(keys => this.accountService.findByIds(keys));
  }
}

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.name) 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 name of the NestDataLoader class 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].