NestJS: Using repository between modules

24 Views Asked by At

I am new to NestJS and developing an application using NestJS with Sequelize. I want to share the repository which belongs to a particular entity across different modules. I have users and instituteAcademics modules let's say and for a very simple use case when I want to create an institute I want to validate the user details. So I need a reference of the users repository in the instituteAcademics module, so I can query in the relevant handler.

Below is the code for different files:

instituteAcademics.module.ts

const repositories: Provider[] = [InstitutesRepository];
const controllers = [InstitutesController, StudentsController];
const entities = [Institutes, InstituteLevels, Students, StudentEnrollments];
const handlers: Provider[] = [
  GetInstitutesHandler,
  GetStudentDetailsHandler,
  GetStudentsListHandler,
  CreateStudentHandler,
  CreateInstituteHandler,
];

@Module({
  imports: [UsersModule, CqrsModule, SequelizeModule.forFeature([...entities])],
  controllers,
  providers: [...repositories, ...handlers],
  exports: [],
})
export class InstituteAcademicsModule {}

users.module.ts

const repositories: Provider[] = [UsersRepository];
const entities = [Users, UserAddresses, UserContacts];
const controllers = [UsersController];
const handlers: Provider[] = [GetUsersHandler, CreateUserHandler];

@Module({
  imports: [CqrsModule, SequelizeModule.forFeature([...entities])],
  controllers,
  providers: [...handlers, ...repositories],
  exports: [SequelizeModule],
})
export class UsersModule {}

Here if you observe I have already exported the SequelizeModule, as per my understanding from link

users.repository.ts

@Injectable()
export class UsersRepository {
  constructor(
    @InjectModel(Users)
    private userModel: typeof Users,
  ) {}

  async createUsers(users: UsersCreationAttributes[]): Promise<Users[]> {
    return this.userModel.bulkCreate(users);
  }

  async createUser(user: UsersCreationAttributes): Promise<Users> {
    return this.userModel.create(user);
  }

  async deleteUser(user: UsersCreationAttributes): Promise<any> {
    return this.userModel.destroy({ where: { _id: user._id } });
  }

  async findOne(firstName: string): Promise<Users | null> {
    return this.userModel.findOne({
      where: {
        first_name: firstName,
      },
    });
  }
}

I want to use the userRepository in the below createInstitute.handler.ts:

@Injectable()
@CommandHandler(CreateInstituteCommand)
@JoiSchemaValidationHandler(CreateInstituteCommand, createStudentCommandSchema)
export class CreateInstituteHandler
  implements ICommandHandler<CreateInstituteCommand>
{
  constructor(
    private userRepository: UsersRepository,
    private institutesRepository: InstitutesRepository,
  ) {}

  async execute(command: CreateInstituteCommand) {
    const userEntity = CreateInstituteMapper.toUserEntity(command.request);
    console.log(`CreateStudentHandler`, userEntity);
  }
}

I am getting the below error:

enter image description here

Appreciate any help. Thanks!

0

There are 0 best solutions below