我一直在使用sequelize-typescript
,tsyringe
在这里进行重构工作,我在这里遇到了一个非常有趣的错误。好吧,通常我的所有存储库容器都正常注册并使用以下设置正确实例化:
我在我的应用程序的根目录和
tsyringe
它之后的容器上导入反映元数据import 'reflect-metadata'; import 'dotenv/config'; import express, { Request, Response, NextFunction } from 'express'; ... import '@shared/infra/sequelize/index'; // default sequelize connection import '@shared/container/index';
容器/索引内部是以下代码,我
sequelize-typescript
用来制作存储库。import { container } from 'tsyringe'; import IUsersRepository from '@modules/users/repositories/IUsersRepositories'; import UsersRepository from '@modules/users/infra/sequelize/repositories/UserRepository'; ... import IModelRepository from '@modules/model/repositories/IModelRepository'; import ModelRepository from '@modules/model/infra/sequelize/repositories/ModelRepository'; container.registerSingleton<IUsersRepository>( 'UsersRepository', UsersRepository, ); ... container.registerSingleton<IModelRepository>( 'ModelRepository', ModelRepository, );
然后我用快递做所有的路由:
import { Router } from 'express'; import bdDecider from '@shared/infra/http/middlewares/bdDecider'; import ensureAuthenticated from '@modules/users/infra/http/middlewares/EnsureAuthenticated'; import UserRouter from '@modules/users/infra/http/routes/user.routes'; ... import ModelRouter from '@modules/model/infra/http/routes/model.routes'; const routes = Router(); routes.use('/sessions', SessionRouter); routes.use(ensureAuthenticated); routes.use(bdDecider); //bdDecider as it says gets the logged on user bdConfig so i can connect to it routes.use('/model', ModelRouter);
所以我们去调用的路线
container.resolve()
import { container } from 'tsyringe'; import { Response, Request } from 'express'; import ShowModelsService from '@modules/invoices/services/ShowModelsService'; export default class ShowModelsController { public async show(req: Request, res: Response): Promise<Response> { const { id } = req.params; const showModelsService = container.resolve( //it calls the model service ShowModelsService, ); const models = await showModelsService.execute( id, ); return res.json(models); } }
这是他调用的服务
import IModelRepository from '@modules/model/repositories/IModelRepository'; import AppError from '@shared/errors/AppError'; import { inject, injectable } from 'tsyringe'; import IModelDTO from '../dtos/IModelDTO'; type IResponse = IModelDTO; @injectable() export default class ShowModelService { constructor( @inject('ModelRepository') private modelRepository: IModelRepository, ) {} public async execute(id: string): Promise<IResponse> { const model = await this.modelRepository.get_models( id, ); if (!model) { throw new AppError('Model not found!', 404); } return model; } }
直到那里一切都很好并且工作正常,但是当我尝试将该模型与另一个使用
sequelize
.Tsyringe
停止能够正确注入依赖项并给出标题错误消息。这是我要关联的模型。import Model from '@modules/model/infra/sequelize/entities/Model'; import { DataTypes } from 'sequelize'; import { Sequelize } from 'sequelize-typescript'; import User from '../entities/User'; export default (sequelize: Sequelize): void => { Model.init( { primary: { allowNull: false, autoIncrement: true, primaryKey: true, type: DataTypes.INTEGER, }, camp1: DataTypes.DECIMAL(20, 2), camp2: DataTypes.DATE, }, { tableName: 'tbl_model', sequelize, }, ); Model.hasMany(User); }
如果我删除那个“
Model.hasMany(User);
”它会恢复正常并且注射发生,我很困惑不知道发生了什么......顺便说一下,这是我的模型库
import IModelRepository from '@modules/model/repositories/IModelRepository'; import AppError from '@shared/errors/AppError'; import { connection } from '@shared/infra/http/middlewares/bdDecider'; import { Repository } from 'sequelize-typescript'; import IModelDTO from '@modules/model/dtos/IModelDTO'; import initModel from '../utils/initInvoice'; import initUser from '../utils/initItem'; import Model from '../entities/Model'; import User from '../entities/User'; export default class ModelRepository implements IModelRepository { private ormRepository: Repository<Model>; constructor() { if (!connection) { throw new AppError( `You don't have a client settled on your user credentials or you don't have a client settled or both`, ); } initModel(connection); // it Model.init() the model with the hasmany association initUser(connection); // it User.init() this.ormRepository = connection.getRepository(Model); } public async get_models( id: string, ): Promise<IModelDTO | null> { const models = await this.ormRepository.findOne({ where: { id }, include: [ { model: User, }, ], }); return models; } }
如果信息太多,我很抱歉,但我试图提供尽可能多的信息,以便有人可以帮助我。但老实说,我对找到解决方案持怀疑态度,可能我不得不将此方法与这个模型并尝试分开做。正如您注意到的那样,我更改了模型的名称,所以我可能拼错了一些东西,但不用担心实际代码上没有拼写错误,因为这个错误只会在我将“
Model.hasMany(User);
”添加到initModel()
函数时发生。希望有人能够帮助我,我会在身边的任何问题都不要害怕问。:)