Skip to content

Commit 2dec430

Browse files
committed
feat(): diving into DI sub-trees
1 parent d314cbc commit 2dec430

File tree

9 files changed

+139
-0
lines changed

9 files changed

+139
-0
lines changed

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SchedulerModule } from './scheduler/scheduler.module'
77
import { CronModule } from './cron/cron.module'
88
import { FibonacciModule } from './fibonacci/fibonacci.module'
99
import { HttpClientModule } from './http-client/http-client.module'
10+
import { TagsModule } from './tags/tags.module';
1011

1112
@Module({
1213
imports: [
@@ -23,6 +24,7 @@ import { HttpClientModule } from './http-client/http-client.module'
2324
// isGlobal is an extra option, that can be used to modify the auto-generated module definition,
2425
// but they are not registered as part of the options object or provider
2526
}),
27+
TagsModule,
2628
// Alternatively, we can use the `forRootAsync` method to register the module with dynamic options
2729
// HttpClientModule.forRootAsync({
2830
// useFactory: () => ({ baseUrl: 'https://nestjs.com' }),

src/tags/dto/create-tag.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class CreateTagDto {}

src/tags/dto/update-tag.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PartialType } from '@nestjs/mapped-types';
2+
import { CreateTagDto } from './create-tag.dto';
3+
4+
export class UpdateTagDto extends PartialType(CreateTagDto) {}

src/tags/entities/tag.entity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class Tag {}

src/tags/tags.controller.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { TagsController } from './tags.controller';
3+
import { TagsService } from './tags.service';
4+
5+
describe('TagsController', () => {
6+
let controller: TagsController;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
controllers: [TagsController],
11+
providers: [TagsService],
12+
}).compile();
13+
14+
controller = module.get<TagsController>(TagsController);
15+
});
16+
17+
it('should be defined', () => {
18+
expect(controller).toBeDefined();
19+
});
20+
});

src/tags/tags.controller.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
2+
import { TagsService } from './tags.service';
3+
import { CreateTagDto } from './dto/create-tag.dto';
4+
import { UpdateTagDto } from './dto/update-tag.dto';
5+
6+
@Controller('tags')
7+
export class TagsController {
8+
constructor(private readonly tagsService: TagsService) {}
9+
10+
@Post()
11+
create(@Body() createTagDto: CreateTagDto) {
12+
return this.tagsService.create(createTagDto);
13+
}
14+
15+
@Get()
16+
findAll() {
17+
return this.tagsService.findAll();
18+
}
19+
20+
@Get(':id')
21+
findOne(@Param('id') id: string) {
22+
return this.tagsService.findOne(+id);
23+
}
24+
25+
@Patch(':id')
26+
update(@Param('id') id: string, @Body() updateTagDto: UpdateTagDto) {
27+
return this.tagsService.update(+id, updateTagDto);
28+
}
29+
30+
@Delete(':id')
31+
remove(@Param('id') id: string) {
32+
return this.tagsService.remove(+id);
33+
}
34+
}

src/tags/tags.module.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Module, OnApplicationBootstrap } from '@nestjs/common'
2+
import { TagsService } from './tags.service'
3+
import { TagsController } from './tags.controller'
4+
import { ContextIdFactory, ModuleRef } from '@nestjs/core'
5+
6+
@Module({
7+
controllers: [TagsController],
8+
providers: [TagsService],
9+
})
10+
export class TagsModule implements OnApplicationBootstrap {
11+
constructor(private readonly moduleRef: ModuleRef) {}
12+
13+
async onApplicationBootstrap() {
14+
const contextId = ContextIdFactory.create()
15+
16+
this.moduleRef.registerRequestByContextId({ hello: 'world' }, contextId)
17+
18+
const tagsService = await this.moduleRef.resolve(TagsService, contextId)
19+
console.log(tagsService)
20+
21+
// const tagsServices = await Promise.all([
22+
// this.moduleRef.resolve(TagsService, contextId),
23+
// this.moduleRef.resolve(TagsService, contextId),
24+
// ])
25+
26+
// console.log(tagsServices[0] === tagsServices[1])
27+
}
28+
}

src/tags/tags.service.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { TagsService } from './tags.service';
3+
4+
describe('TagsService', () => {
5+
let service: TagsService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [TagsService],
10+
}).compile();
11+
12+
service = module.get<TagsService>(TagsService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/tags/tags.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Inject, Injectable, Scope } from '@nestjs/common'
2+
import { CreateTagDto } from './dto/create-tag.dto'
3+
import { UpdateTagDto } from './dto/update-tag.dto'
4+
import { REQUEST } from '@nestjs/core'
5+
6+
@Injectable({ scope: Scope.REQUEST })
7+
export class TagsService {
8+
constructor(@Inject(REQUEST) request: unknown) {
9+
console.log(request)
10+
}
11+
12+
create(createTagDto: CreateTagDto) {
13+
return 'This action adds a new tag'
14+
}
15+
16+
findAll() {
17+
return `This action returns all tags`
18+
}
19+
20+
findOne(id: number) {
21+
return `This action returns a #${id} tag`
22+
}
23+
24+
update(id: number, updateTagDto: UpdateTagDto) {
25+
return `This action updates a #${id} tag`
26+
}
27+
28+
remove(id: number) {
29+
return `This action removes a #${id} tag`
30+
}
31+
}

0 commit comments

Comments
 (0)