Gathering detailed insights and metrics for nestjs-dataloader
Gathering detailed insights and metrics for nestjs-dataloader
Gathering detailed insights and metrics for nestjs-dataloader
Gathering detailed insights and metrics for nestjs-dataloader
npm install nestjs-dataloader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
148 Stars
88 Commits
45 Forks
7 Watching
3 Branches
11 Contributors
Updated on 23 May 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-29.4%
353
Compared to previous day
Last week
-15.3%
2,059
Compared to previous week
Last month
15.2%
9,908
Compared to previous month
Last year
-4%
120,641
Compared to previous year
NestJS dataloader simplifies adding graphql/dataloader to your NestJS project. DataLoader aims to solve the common N+1 loading problem.
Install with yarn
1yarn add nestjs-dataloader
Install with npm
1npm install --save nestjs-dataloader
We start by implementing the NestDataLoader
interface. This tells DataLoader
how to load our objects.
1import * as DataLoader from 'dataloader'; 2import { Injectable } from '@nestjs/common'; 3import { NestDataLoader } from 'nestjs-dataloader'; 4... 5 6@Injectable() 7export class AccountLoader implements NestDataLoader<string, Account> { 8 constructor(private readonly accountService: AccountService) { } 9 10 generateDataLoader(): DataLoader<string, Account> { 11 return new DataLoader<string, Account>(keys => this.accountService.findByIds(keys)); 12 } 13}
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.
For each NestDataLoader we create, we need to provide it to our module.
1import { Module } from '@nestjs/common'; 2import { APP_INTERCEPTOR } from '@nestjs/core'; 3import {DataLoaderInterceptor} from 'nestjs-dataloader' 4... 5 6@Module({ 7 providers: [ 8 AccountResolver, 9 AccountLoader, 10 { 11 provide: APP_INTERCEPTOR, 12 useClass: DataLoaderInterceptor, 13 }, 14 ], 15 16}) 17export class ResolversModule { }
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.
1import * as DataLoader from 'dataloader'; 2import { Loader } from 'nestjs-dataloader'; 3... 4 5@Resolver(Account) 6export class AccountResolver { 7 8 @Query(() => [Account]) 9 public getAccounts( 10 @Args({ name: 'ids', type: () => [String] }) ids: string[], 11 @Loader(AccountLoader) accountLoader: DataLoader<Account['id'], Account>): Promise<Account[]> { 12 return accountLoader.loadMany(ids); 13 } 14}
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.
Pull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 4/7 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More