小编Fel*_*rim的帖子

NestJS - 如何使用不同的配置多次注册动态模块提供程序?

我有一个 knex 模块,其实现如下:

import { DynamicModule, Module } from '@nestjs/common';
import { Knex, knex } from 'knex';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';

export const KNEX_MODULE = 'KNEX_MODULE';

@Module({})
export class KnexModule {
  static register(options: Knex.Config): DynamicModule {
    return {
      module: KnexModule,
      providers: [
        {
          inject: [WINSTON_MODULE_PROVIDER],
          provide: KNEX_MODULE,
          useFactory: (logger: Logger) => {
            logger.info('Creating new knex instance', {
              context: KnexModule.name,
              tags: ['instance', 'knex', 'create'],
            });
            return knex(options);
          },
        },
      ],
      exports: [KNEX_MODULE],
    }; …
Run Code Online (Sandbox Code Playgroud)

node.js typescript nestjs

4
推荐指数
1
解决办法
4946
查看次数

标签 统计

nestjs ×1

node.js ×1

typescript ×1