【问题标题】:NestJs - mongoose - Dynamic collection namingNestJs - mongoose - 动态集合命名
【发布时间】:2020-11-11 00:10:03
【问题描述】:

我想使用基于当前年份的动态集合名称。

例如:从“products”到“products2020”。

使用 NESTJS,我必须导入带有指定集合名称的“module.forFeature”。

import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: 'Products',
        schema: ProductsSchema
      }
    ])
  ],
  controllers: [ProductsController],
  providers: [ProductsService]
})

服务注入也是如此:

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'

import { Model } from 'mongoose'

@Injectable()
export class ProductsService {
  constructor(
    @InjectModel('Products')
    private readonly productsModel: Model<Products>
  ) {}
}

最后,这是我的架构:

import { Schema } from 'mongoose'

export const ProductsSchema = new Schema(
  {
    _id: { Type: String, required: true },
    code: String
  },
  {
    collection: 'Products'
  }
)

有没有办法实现动态命名?

非常感谢!

【问题讨论】:

  • 为什么不能在Products 集合中的文档上保留year 属性?
  • @ethane 因为需要使用动态命名来处理它。

标签: javascript node.js mongodb mongoose nestjs


【解决方案1】:

我一直在寻找解决此类问题的方法,但我碰壁了,没有明确的方法。

下面的(最小)代码实例化服务,每个服务都绑定到特定模型,具体取决于country parameter。即ServiceX绑定到数据库X的模型,ServiceY绑定到数据库Y中的相同模型

但这就是我设法做到的。您绝对可以根据自己的需要进行变通

首先是模型/接口。不同服务之间常用

export interface User extends Document {
    readonly username: string;
    readonly password: string;
}

export const UserSchema = new mongoose.Schema(
    {
        _id: mongoose.ObjectId,
        username: String,
        password: String
    },
    { collection: 'accounts', autoCreate: true }
);

不同数据库/集合中的每个模型的服务定义确实相同

@Injectable()
export class XUserService implements OnModuleInit{
    constructor(
        private userModel: Model<User>,
    ) {
    }

    ////////////////////////////////////////////////////////////////////////////
    async onModuleInit(): Promise<any> {
        console.log(`inside service dbname=: ${this.userModel.db.name} > ${this.userModel.collection.collectionName}` );
        // await new this.userModel({_id: mongoose.Types.ObjectId(), username: 'test', password: 'test', flag: this.c}).save()
    }

    async insert(){
        console.log(`inside service dbname=: ${this.userModel.db.name} > ${this.userModel.collection.collectionName}` );
        await new this.userModel({
            _id: mongoose.Types.ObjectId(),
            username: this.userModel.db.name,
            password: '0000'
        }).save();
    }

    async findOne(): Promise<User>{
        console.log(`inside service in : ${this.userModel.db.name} > ${this.userModel.collection.collectionName}` );
        return this.userModel.findOne()
    }
}

对于模块,我做了一个DynamicModule

  • 导入 DBConnections
  • 为每个需求创建一个模型,(就我而言,每个数据库中都有一个模型)
  • 创建每个Model 并将其绑定到Service,因此服务的实例化将是正确的
@Module({

})
export class XUserModule{
    static register( /*use can pass parameter here*/): DynamicModule{
        return{
            module: XUserModule,
            imports: [
                DatabaseModule
            ],
            controllers: [
                XUserController
            ],
            providers: [
                // Create Models here, #1 and #2 in two different database
                {
                    provide: 'dz-'+'UserModel',
                    useFactory: (connection: Connection)=> {
                        return connection.model('User', UserSchema )
                    },
                    inject: [ dbname.shooffood('dz')+'Connection' ]
                },{
                    provide: 'ca-'+'UserModel',
                    useFactory: (connection: Connection)=> {
                        return connection.model('User', UserSchema )
                    },
                    inject: [ dbname.shooffood('ca')+'Connection' ]
                },

   
                // Create Providers/Services for each Model and Inject the Model to the Service by `TokenString`
                {
                    provide: 'dz' + XUserService.name,
                    useFactory: (m: any)=> {
                        console.log(m);
                        return new XUserService(m);
                    },
                    inject: [ 'dz-'+'UserModel' ]
                },{
                    provide: 'ca' + XUserService.name,
                    useFactory: (m: any)=> {
                        console.log(m);
                        return new XUserService(m);
                    },
                    inject: [ 'ca-'+'UserModel' ]
                }
            ],

            // Export your service with the same `provide` name for later usage.
            exports: [
                'dz' + XUserService.name,
                'ca' + XUserService.name
            ]
        }
    }
}

仅供参考,数据库模块看起来像 常量dbname 是连接名称,uri 是连接字符串。

const databaseProviders = [
    {
        provide: dbname.admin+'Connection',
        useFactory: (): Promise<typeof mongoose> => mongoose.createConnection(uri.admin),
    },{
        provide: dbname.system+'Connection',
        useFactory: (): Promise<typeof mongoose> => mongoose.createConnection(uri.system),
    },{
        provide: dbname.shooffood('dz')+'Connection',
        useFactory: (): Promise<typeof mongoose> => mongoose.createConnection(uri.dzfood),
    },{
        provide: dbname.shooffood('ca')+'Connection',
        useFactory: (): Promise<typeof mongoose> => mongoose.createConnection(uri.cafood),
    }
];

@Module({
    providers: [
        ...databaseProviders
    ],
    exports: [
        dbname.admin+'Connection',
        dbname.system+'Connection',
        dbname.shooffood('dz')+'Connection',
        dbname.shooffood('ca')+'Connection'
    ]
})
export class DatabaseModule {}

对于Controller,只有一个通过请求参数:country处理每个服务。但首先我必须列出所有可能包含在应用程序中的模型和服务。

@Controller(':country')
export class XUserController {
    private byCountryServices = new Map();
    constructor(
        // Inject all required services by `tokenString`
        @Inject('dz' + XUserService.name) private dzUserService: XUserService,
        @Inject('ca' + XUserService.name) private caUserService: XUserService,
    ) {
        // Add to `<key, value>` Map for easy by param access
        this.byCountryServices.set('dz', this.dzUserService );
        this.byCountryServices.set('ca', this.caUserService );
    }

    @Get('post')
    async post(
        @Param('country') c: string
    ): Promise<string>{
        await this.byCountryServices.get(c).insert()
        return 'inserted in ' + c;
    }

    @Get('get')
    async get(
        @Param('country') c: string
    ): Promise<string>{
        console.log('param: ' + c)
        return await this.byCountryServices.get(c).findOne()
    }
}

最后你在 AppModule 中导入模块 XUserModule.register()

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 2013-07-22
    • 2014-09-21
    • 2019-08-22
    • 2013-02-24
    • 2020-07-22
    • 1970-01-01
    • 2019-04-11
    • 2011-11-21
    相关资源
    最近更新 更多