NestJs/Mongoose 中的自动递增序列 [英] Auto Increment Sequence in NestJs/Mongoose

查看:82
本文介绍了NestJs/Mongoose 中的自动递增序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个 NodeJs 项目迁移到 NestJs,该项目使用 MongoDB 作为后端数据库,使用 Mongoose 作为 ODM.我正在使用 mongoose-sequence 插件来处理自动增量序列,但是我遇到了需要NestJs 下的库.

I'm migrating a NodeJs project to NestJs, this project uses MongoDB as back-end database and Mongoose as ODM. I was using the mongoose-sequence plugin to handle autoincrement sequences, however I'm facing troubles requiring the library under NestJs.

mongoose-sequence 文档解释了如何使用 CommonJS 语法导入库,如下所示:

The mongoose-sequence documentation explains how to import the library using CommonJS syntax as follows:

const mongoose = require('mongoose')
const AutoIncrementFactory = require('mongoose-sequence');

const connection = await mongoose.createConnection('mongodb://...');

const AutoIncrement = AutoIncrementFactory(connection);

使用 ES6 导入语法将类似于:

Using ES6 import syntax it would be something like:

import * as mongoose from 'mongoose';
import * as AutoIncrementFactory from 'mongoose-sequence';

const connection = ...;

const AutoIncrement = AutoIncrementFactory(connection);

但是由于 NestJs 使用了依赖注入,所以访问原生连接并不是那么直接.根据 使用 Mongoose 集成 MongoDB 的文档 访问本机 Mongoose Connection 对象可以使用 @InjectConnection() 装饰器完成,如下所示:

However since NestJs uses Dependency Injection, accessing the native connection is not so direct. According to the documentation to integrate MongoDB using Mongoose accessing the native Mongoose Connection object can be done using the @InjectConnection() decorator as follows:

@Injectable()
export class CatsService {
  constructor(@InjectConnection() private connection: Connection) {}
}

但由于 TypeScript 装饰器 只能附加到类声明,方法、访问器、属性或参数 我不知道如何注入连接,需要插件并在我的 Schema 类上初始化它.

But since TypeScript decorators can only be attached to a class declaration, method, accessor, property, or parameter I don't see how to inject the connection, require the plugin and initialize it on my Schema classes.

推荐答案

可以使用 MongooseModuleforFeatureAsync() 方法为给定架构注册插件以及工厂提供程序(即 useFactory).

It's possible to register a plugin for a given schema using the forFeatureAsync() method of the MongooseModule along with a factory provider (i.e., useFactory).

遵循官方文档中的示例:

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: Cat.name,
        useFactory: () => {
          const schema = CatsSchema;
          schema.plugin(require('mongoose-autopopulate'));
          return schema;
        },
      },
    ]),
  ],
})
export class AppModule {}

但是对于 mongoose-sequence 插件,有必要将本地 Mongoose 连接对象传递给插件初始化.这可以通过使用 getConnectionToken 方法将连接注入到工厂提供程序中来实现:

However with the mongoose-sequence plugin it's necessary to pass the native Mongoose connection object to the plugin initialization. This can be achieved by injecting the connection into the factory provider with the getConnectionToken method:

import {getConnectionToken, MongooseModule} from '@nestjs/mongoose';
import * as AutoIncrementFactory from 'mongoose-sequence';

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: Cat.name,
        useFactory: async (connection: Connection) => {
          const schema = CatsSchema;
          const AutoIncrement = AutoIncrementFactory(connection);
          schema.plugin(AutoIncrement, {inc_field: 'id'});
          return schema;
        },
        inject: [getConnectionToken('YOUR_CONNECTION_NAME')],
      },
    ]),
  ],
})
export class AppModule {}

这篇关于NestJs/Mongoose 中的自动递增序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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