go-go-scope provides framework adapters for seamless integration with popular Node.js web frameworks. Each request gets its own scope with automatic cleanup and cancellation support.
NestJS module provides dependency injection integration with request-scoped services.
1
Import the module
import { Module } from '@nestjs/common'import { GoGoScopeModule } from '@go-go-scope/adapter-nestjs'@Module({ imports: [ GoGoScopeModule.forRoot({ name: 'nestjs-api', timeout: 30000, isGlobal: true // Make available in all modules }) ]})export class AppModule {}
2
Inject scope in controllers
import { Controller, Get, Param, Req } from '@nestjs/common'import { Request } from 'express'@Controller('users')export class UsersController { @Get(':id') async getUser(@Param('id') id: string, @Req() req: Request) { const [err, user] = await req.scope.task( () => this.usersService.findOne(id), { retry: 'exponential', timeout: 5000 } ) if (err) { throw new HttpException('User not found', HttpStatus.NOT_FOUND) } return user }}
3
Use in services
import { Injectable } from '@nestjs/common'@Injectable()export class UsersService { async findOne(id: string, scope: Scope) { // Use scope for concurrent operations const [err, results] = await scope.parallel([ () => this.db.query('SELECT * FROM users WHERE id = ?', [id]), () => this.cache.get(`user:${id}`), ]) if (err) throw err const [dbResult, cacheResult] = results.map(r => r[1]) return cacheResult || dbResult }}