如何在Nestjs中使用请求范围提供程序动态更改数据库连接? [英] How to change a Database connection dynamically with Request Scope Providers in Nestjs?

查看:176
本文介绍了如何在Nestjs中使用请求范围提供程序动态更改数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Nestjs 6.x,Mongoose,Mongo 等进行项目... 关于后端,在我的用例中,我必须根据来自某些请求的某些条件/参数来更改我的数据库之一的连接强>.

Working on a project with Nestjs 6.x, Mongoose, Mongo, etc... Regarding to the Back End, in my use case, I must change the connection of one of my databases depending of some conditions/parameters coming from some requests.

基本上,我这个

mongoose.createConnection('mongodb://127.0.0.1/whatever-a', { useNewUrlParser: true })

例如,我想更改为

mongoose.createConnection('mongodb://127.0.0.1/whatever-b', { useNewUrlParser: true })

因此,我在Nestjs中拥有第一个提供程序

Therefore, I have in Nestjs the first provider

export const databaseProviders = [
  {
    provide: 'DbConnectionToken',
    useFactory: async (): Promise<typeof mongoose> =>
    await mongoose.createConnection('mongodb://127.0.0.1/whatever', { useNewUrlParser: true })
  }

我研究了一段时间,发现在 Nestjs 6.x 版本中,提供程序请求允许我动态修改每个请求注入一些提供商.

I was researching for a while and I found out that in release Nestjs 6.x there are provider requests allowing me to modify dynamically Per-request the injection of some providers.

无论如何,如果能成功实现更改,我也不知道如何实现

Anyway, I don't know how to achieve my change neither if it is going to be working in case I'd achieve that

任何人都可以帮助或指导我吗? 预先非常感谢.

Can anyone help or guide me? Many thanks in advance.

推荐答案

您可以使用Nest的内置Mongoose软件包执行以下操作:

You can do the following using Nest's built-in Mongoose package:

/*************************
* mognoose.service.ts
*************************/
import { Inject, Injectable, Scope } from '@nestjs/common';
import { MongooseOptionsFactory, MongooseModuleOptions } from '@nestjs/mongoose';
import { REQUEST } from '@nestjs/core';
import { Request } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class MongooseConfigService implements MongooseOptionsFactory {
    constructor(
        @Inject(REQUEST) private readonly request: Request,) {
    }

    createMongooseOptions(): MongooseModuleOptions {
        return {
            uri: request.params.uri, // Change this to whatever you want; you have full access to the request object.
        };
    }
}

/*************************
* mongoose.module.ts
*************************/
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MongooseConfigService } from 'mognoose.service';

@Module({
    imports: [
        MongooseModule.forRootAsync({
            useClass: MongooseConfigService,
        }),
    ]
})
export class DbModule {}

然后,您可以将所需的任何内容附加到请求中,并根据请求更改数据库;因此使用Scope.REQUEST.您可以在其文档上阅读有关注入范围的更多信息.

Then, you can attach whatever you want to the request and change the database per request; hence the use of the Scope.REQUEST. You can read more about Injection Scopes on their docs.

如果您遇到PassportJS(或任何其他程序包)问题或请求为空,则似乎是与PassportJS(或其他程序包)不支持请求范围有关的错误;您可能会了解有关有关GitHub的与PassportJS有关的问题的更多信息.

If you run into issues with PassportJS (or any other package) or the request is empty, it seems to be an error that relates to PassportJS (or the other package) not supporting request scopes; you may read more about the issue on GitHub regarding PassportJS.

这篇关于如何在Nestjs中使用请求范围提供程序动态更改数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆