Service

  1. Create service

    When creating a service, first enter nest generate service, abbreviated nest g s. Enter the name after that.

    service is used to process logic and can be reused after being extracted. In nest, each service is a provider.


  2. Provider

    The main idea of Provider is that it can inject dependencies. This means that various relationships can be created between objects, and the logic to wire object instances together can all be handled by the Nest runtime system, rather than trying to create and manage this type of dependency injection yourself.

    Provider in nest is a class with a decorator named @Injectable().


  3. Provider dependency

    If we want to inject the provider, we can simply use the constructor.

    In the Contoller file, write constructor(){} in the Controller class.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import { CoffeesService } from './coffees.service';

    @Controller('coffees')
    export class CoffeesController {
    constructor(private readonly coffeesService: CoffeesService){}

    @Post()
    @HttpCode(HttpStatus.GONE) //Decorator position, return 410GONE.
    create(@Body('name') body){
    return body;
    }
    }

    private: TypeScript usage that allows us to declare and initialize CoffeesService members at once in the same place. And it’s only accessible within the class itself, hence private.

    readonly: Ensures that the referenced service is not modified and content is only accessed from it.

    coffeesService : Named parameter.

    : CoffeesService: In typeScript, use types to manage dependencies. nest will resolve CoffeesService by creating an instance of CoffeesService and returning it to our CoffeesController , or returning an existing instance if there is one. This dependency is resolved and passed to the controller constructor, or assigned to the property specified here.


  4. Service,CRUD

    In service document:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    import { Coffee } from './entities/coffee.entity';   //coffee class

    @Injectable()
    export class CoffeesService {
    private coffees: Coffee[] = [ //Create an array, using multiple instances.
    {
    id: 1,
    name: 'Shipwreck Roast',
    brand: 'Buddy Brew',
    flavors: ['chocolate', 'vanilla']
    }
    ];

    findAll(){return this.coffees};
    findOne(id: string){item => item.id === +id};
    create(createCoffeeDto: any){this.coffees.push(createCoffeeDto)};
    update(id: string, updateCoffeeDto: any){
    const existingCoffee = this.findOne(id);
    if(exsitingCoffee){}
    }
    remove(id: string){
    const coffeeIndex = this.coffees.findIndex(item => item.id === +id);
    if(coffeeIndex >= 0){ this.coffees.splice(coffeeIndex, 1) };
    }
    }

  5. Use dependencies in controller

    In the controller file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    @Controller('coffees')
    export class CoffeesController {
    constructor(private readonly coffeesService: CoffeesService){}

    @Get()
    findAll(@Query() paginationQuery){
    //const {limit, offset} = paginationQuery;
    return this.coffeesService.findAll();
    }

    @Get(':id')
    findOne(@Param('id') id:string){
    return this.coffeesService.findOne(id);
    }

    @Post()
    create(@Body() body){
    return this.coffeesService.create(body);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() body){
    return this.coffeesService.update(id, body);
    }

    @Delete(':id')
    remove(@Param('id') id: string){
    return this.coffeesService.remove(id);
    }
    }
Share