A
A
alaskafx2022-02-21 14:03:27
Node.js
alaskafx, 2022-02-21 14:03:27

Is this correct and how will it be correct?

Right now I have 3 controllers, 3 entities, 3 services and 3 modules.
The situation now is that in one of the services I need to refer to the other one, respectively, but nest said something like: you forgot to connect the entity as well, because it is not in the second one.
In the final form, it all looks like services and entities are connected one by one in modules, where almost all services are already connected in the final module, due to some reuse.

Here is the example itself:

PurchaseModule.ts

import { Module } from '@nestjs/common';
import { PurchaseController } from './PurchaseController';
import { PurchaseService } from './PurchaseService';
import { TypeOrmModule } from '@nestjs/typeorm';
import { paymentsEntity } from './purchasedEntity';
import { userEntity } from '../userAuthorization/userEntity';
import { userService } from '../userAuthorization/user.service';
import { ConfigModule } from '@nestjs/config';
import { ForgotEntity } from '../userAuthorization/forgotEntity';


@Module({
  imports: [
    ConfigModule.forRoot(),
    TypeOrmModule.forFeature([paymentsEntity, userEntity, ForgotEntity]),
  ],
  exports: [TypeOrmModule],
  controllers:[PurchaseController],
  providers: [PurchaseService, userService]
})
export class PurchaseModule{}


userModule.ts
import {Module} from '@nestjs/common';
import { userController } from './user.controller';
import { userService } from './user.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { userEntity } from './userEntity';
import { ForgotEntity } from './forgotEntity';

@Module({
  imports: [
    TypeOrmModule.forFeature([userEntity, ForgotEntity]),
  ],
  exports: [TypeOrmModule],
  controllers: [userController],
  providers: [userService]
})

export class userModule{}


Here you can clearly see how I drag the ForgotEntity entity from the module to the module, which is used only in the userService.
If I remove it from the PurchaseModule, then Nest will tell me that this entity is used in the userService, which means it should be here too!

Is this a normal approach or how to do it differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2022-02-21
@iDmitriyWinX

If you want to use providers from one module in another, use an arrayexports: []

@Module({
  imports: [
    TypeOrmModule.forFeature([UserEntity, ForgotEntity]),
  ],
  controllers: [UserController],
  providers: [UserService],
  exports: [UserService] // Экспортируем зависимости данного модуля.
})
export class UserModule{}

@Module({
  imports: [
    TypeOrmModule.forFeature([PaymentsEntity]),
    UserModule // Импортируем его сдесь. Теперь в пределах данного модуля нам доступен UserService.
  ],
  controllers: [PurchaseController],
  providers: [PurchaseService]
})
export class PurchaseModule {}

Well, do not forget about the nest's style guide: all modules, services, controllers, entities, in general - we name classes with a capital letter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question