Nestjs sequelize依赖项注入不起作用

时间:2020-04-01 16:39:27

标签: sequelize.js nestjs

我正在使用带有Nestjs的续集ORM。我创建了一个新的模型“ contact”,我想在其中注入服务。当我这样做时,控制台中出现错误。我想我知道为什么不能,但我无法解决。为什么不可能这样做?

谢谢!

contact.entity.ts

@ObjectType()
@Table
export class Contact extends Model<Contact> {

  constructor(private utilsService: UtilsServices) {
    super();
  }

  @Field(type => Int, { nullable: true })
  @PrimaryKey
  @AutoIncrement
  @Column
  id?: number;

  @Field({ nullable: true })
  @Column
  get firstName(): string {
    return this.utilsService.decrypt(this.getDataValue('firstName'));
  }

  set firstName(value) {
    const encryptValue = this.utilsService.encrypt(value);
    this.setDataValue('firstName', encryptValue);
  }

  @Field({ nullable: true })
  @Column
  get lastName(): string {
    return this.utilsService.decrypt(this.getDataValue('lastName'));
  }

contacts.module.ts

import { Module } from '@nestjs/common';
import { UtilsModule } from '../utils/utils.module';
import { DatabaseModule } from '../database/database.module';
import { WinstonModule } from 'nest-winston';
import { contactsProviders } from './providers/contacts.providers';

@Module({
  imports: [
    UtilsModule,
    DatabaseModule,
    WinstonModule
  ],
  providers: [
    ...contactsProviders
  ]
})
export class ContactsModule {}

utils.service.ts

import { Inject, Injectable } from '@nestjs/common';
import { Logger } from 'winston';
import { ConfigService } from '@nestjs/config';
import * as CryptoJS from 'crypto-js';

@Injectable()
export class UtilsServices {

  constructor(@Inject('winston') private readonly logger: Logger,
              private readonly configService: ConfigService) {
  }

  /**
   * Encrypt data with a secret key store in env.
   * @param {string} data - A string to encrypt
   * @returns {Promise<string>} Encrypted data
   */
  encrypt(data): string {

    if (data === null || data === undefined || data === '') {
      return data;
    }
    const secretKey = this.configService.get('encrypt_secret');
    return CryptoJS.AES.encrypt(data, secretKey).toString();
  }

  /**
   * Decrypt data with a secret key store in env
   * @param {string} data - A string to encrypt
   * @returns {Promise<string>} Decrypted data
   */
  decrypt(data): string {
    if (data === null || data === undefined || data === '') {
      return data;
    }
    const secretKey = this.configService.get('encrypt_secret');
    const bytes = CryptoJS.AES.decrypt(data, secretKey);
    return bytes.toString(CryptoJS.enc.Utf8);
  }
}

我的错误消息:


src/database/database.providers.ts:41:9 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type 'typeof Contact' is not assignable to type 'string | ModelCtor<Model<any, any>>'.
      Type 'typeof Contact' is not assignable to type 'ModelCtor<Model<any, any>>'.
        Type 'typeof Contact' is not assignable to type 'new () => Model<any, any>'.

41         Contact,
           ~~~~~~~

  node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.d.ts:16:5
    16     addModels(arg: Array<ModelCtor | string>): any;
           ~~~~~~~~~
    The last overload is declared here.


0 个答案:

没有答案