0

guys. I'm using a provider(useFactory) to register mongoose-sequence plugin for my User Schema, but it doesn't work because of @type check. This is my module.

import { Module } from '@nestjs/common';
import { MongooseModule, getConnectionToken } from '@nestjs/mongoose';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { User, UserSchema } from './schema/user.schema';
import * as AutoIncrementFactory from 'mongoose-sequence';
import { Connection } from 'mongoose';

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: User.name,
        //schema: UserSchema,
        useFactory: async (connection: Connection) => {
          const schema = UserSchema;
          const AutoIncrement = AutoIncrementFactory(connection);
          schema.plugin(AutoIncrement, { inc_field: 'users' });
          return schema;
        },
        inject: [getConnectionToken()],
      },
    ]),
  ],
  controllers: [UserController],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

When saved, the compiler throws the error:

src/user/user.module.ts:17:54 - error TS2345: Argument of type 'Connection' is not assignable to parameter of type 'Schema<any, Model<any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, { [x: string]: any; }>'.
  Type 'Connection' is missing the following properties from type 'Schema<any, Model<any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, { [x: string]: any; }>': add, alias, childSchemas, clearIndexes, and 24 more.

17           const AutoIncrement = AutoIncrementFactory(connection);
                                                        ~~~~~~~~~~

src/user/user.module.ts:18:25 - error TS2345: Argument of type 'void' is not assignable to parameter of type 'PluginFunction<User, Model<User, any, any, any, any>, any, any, any, any>'.

18           schema.plugin(AutoIncrement, { inc_field: 'users' });

If I uninstall this package @types/mongoose-sequence, it works, so how to fixed it if I want to keep @type check. thanks.

cityvoice
  • 2,409
  • 4
  • 14
  • 25
  • Have you installed the latest *mongoose*? – Mostafa Fakhraei Feb 06 '23 at 08:02
  • @MostafaFakhraei, hi, my mongoose config in package.json is ^6.9.0. – cityvoice Feb 07 '23 at 02:39
  • There is no definite answer to this issue, as other users reported the same problem. Look at [this](https://github.com/ramiel/mongoose-sequence/issues/83) and [this](https://github.com/ramiel/mongoose-sequence/issues/111). My only suggestion is to cast it into `any`, at least for now. – Mostafa Fakhraei Feb 07 '23 at 10:37
  • Thanks for your suggestion, it's a bug caused by @types/mongoose-sequence, maybe it is designed for an old version. I have modified this package and it works for me now. Hope the author can fix this bug soon. – cityvoice Feb 08 '23 at 01:21

0 Answers0