-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
DataloaderModule.forFeature()
💪
- Loading branch information
1 parent
d74f04c
commit 21c6985
Showing
9 changed files
with
129 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,33 @@ | ||
import { DynamicModule, type FactoryProvider, Module } from '@nestjs/common' | ||
import { APP_INTERCEPTOR } from '@nestjs/core' | ||
import { DataloaderInterceptor } from './Dataloader.interceptor.js' | ||
import { OPTIONS_TOKEN } from './internal.js' | ||
import { type DataloaderOptions } from './types.js' | ||
import { type DynamicModule, Module } from '@nestjs/common' | ||
import { type DataloaderModuleOptions, type Factory, type DataloaderOptions } from './types.js' | ||
import { DataloaderCoreModule } from './DataloaderCore.module.js' | ||
|
||
@Module({ | ||
providers: [ | ||
{ provide: APP_INTERCEPTOR, useClass: DataloaderInterceptor }, | ||
], | ||
}) | ||
@Module({}) | ||
class DataloaderModule { | ||
static forRoot(options?: DataloaderOptions): DynamicModule { | ||
return { | ||
module: DataloaderModule, | ||
providers: [{ | ||
provide: OPTIONS_TOKEN, | ||
useValue: options, | ||
}], | ||
imports: [DataloaderCoreModule.forRoot(options)], | ||
} | ||
} | ||
|
||
static forRootAsync(options: DataloaderModuleOptions): DynamicModule { | ||
return { | ||
module: DataloaderModule, | ||
imports: options.imports ?? [], | ||
providers: [{ | ||
provide: OPTIONS_TOKEN, | ||
inject: options.inject ?? [], | ||
useFactory: options.useFactory, | ||
}], | ||
imports: [DataloaderCoreModule.forRootAsync(options)], | ||
} | ||
} | ||
} | ||
|
||
/** Dataloader module options for async configuration */ | ||
type DataloaderModuleOptions = Omit<FactoryProvider<DataloaderOptions>, 'provide'> & Pick<DynamicModule, 'imports'> | ||
static forFeature(loaders: Factory[]): DynamicModule { | ||
return { | ||
module: DataloaderModule, | ||
providers: loaders, | ||
exports: loaders, | ||
} | ||
} | ||
} | ||
|
||
|
||
export { | ||
DataloaderModule, | ||
DataloaderModuleOptions, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { type Provider, type DynamicModule, Module } from '@nestjs/common' | ||
import { APP_INTERCEPTOR } from '@nestjs/core' | ||
import { DataloaderInterceptor } from './Dataloader.interceptor.js' | ||
import { OPTIONS_TOKEN } from './internal.js' | ||
import { type DataloaderModuleOptions, type DataloaderOptions } from './types.js' | ||
|
||
/** @private */ | ||
const interceptor: Provider = { provide: APP_INTERCEPTOR, useClass: DataloaderInterceptor } | ||
|
||
/** @private */ | ||
@Module({}) | ||
class DataloaderCoreModule { | ||
static forRoot(options?: DataloaderOptions): DynamicModule { | ||
return { | ||
module: DataloaderCoreModule, | ||
providers: [ | ||
interceptor, | ||
{ | ||
provide: OPTIONS_TOKEN, | ||
useValue: options, | ||
}, | ||
], | ||
} | ||
} | ||
|
||
static forRootAsync(options: DataloaderModuleOptions): DynamicModule { | ||
return { | ||
module: DataloaderCoreModule, | ||
imports: options.imports ?? [], | ||
providers: [ | ||
interceptor, | ||
{ | ||
provide: OPTIONS_TOKEN, | ||
inject: options.inject ?? [], | ||
useFactory: options.useFactory, | ||
}, | ||
], | ||
} | ||
} | ||
} | ||
|
||
export { | ||
DataloaderCoreModule, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { LifetimeKeyFn, DataloaderOptions } from './types.js' | ||
export { LifetimeKeyFn, DataloaderOptions, DataloaderModuleOptions } from './types.js' | ||
export { DataloaderException } from './DataloaderException.js' | ||
export { Loader, createLoaderDecorator } from './Loader.decorator.js' | ||
export { DataloaderFactory, LoaderFrom, Aggregated } from './Dataloader.factory.js' | ||
export { DataloaderModule, DataloaderModuleOptions } from './Dataloader.module.js' | ||
export { DataloaderModule } from './Dataloader.module.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters