I have a confusion regarding the implementation of a global module in NestJS, and the import of a shared service
I’m following this guide: NestJS- Global Modules
and this is what I have so far:
shared.service:
import { Injectable } from '@nestjs/common';
@Injectable()
export class SharedService {
async catchError<T>(promise: Promise<T>): Promise<[undefined, T] | [Error]> {
return promise
.then((data): [undefined, T] => {
return [undefined, data]}
)
.catch((error): [Error] => {
console.error('Error:', error);
return [error];
});
}
}
shared.module:
import { Module, Global } from '@nestjs/common';
import { SharedService } from './shared.service';
@Global()
@Module({
providers: [SharedService],
exports: [SharedService],
})
export class SharedModule {}
the app.module:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { AppService } from './app.service';
import { PersonsModule } from './persons/persons.module';
import { SharedModule } from './shared/shared.module';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
uri: configService.get<string>('MONGODB_URI'),
}),
inject: [ConfigService],
}),
PersonsModule,
SharedModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
and this is my implementation on the persons.service:
import { Inject, Injectable, InternalServerErrorException } from '@nestjs/common';
import { CreatePersonDto } from './dto/create-person.dto';
import { UpdatePersonDto } from './dto/update-person.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Person } from './persons.schema';
import { SharedService } from 'src/shared/shared.service';
@Injectable()
export class PersonsService {
constructor(
private readonly sharedService: SharedService,
@InjectModel('Person') private readonly personModel: Model<Person>,
) {}
create(createPersonDto: CreatePersonDto) {
return 'This action adds a new person';
}
async findAll(): Promise<Person[]> {
const [error, data] = await this.sharedService.catchError(
this.personModel.find().exec()
);
if (error || !data) {
throw new InternalServerErrorException('Error fetching persons');
}
return data;
}
async findOne(guid: string) {
const [error, data] = await this.sharedService.catchError(
this.personModel.findOne({guid}).exec()
);
if (error || !data) {
throw new InternalServerErrorException('Error fetching person');
}
return data;
}
update(id: number, updatePersonDto: UpdatePersonDto) {
return `This action updates a #${id} person`;
}
remove(id: number) {
return `This action removes a #${id} person`;
}
}
And this is where the confusion comes in, if I’m making a global module…
why do I need to make this import on the persons.service above ?
import { SharedService } from 'src/shared/shared.service';
and… if I have to make that import, what’s the point of a global module ?, I mean… if I have to make that import anyways, what’s the point of the global module ?