Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Call Stored Procedure in NestJs+TypeOrm (Backend)

Tags:

nestjs

I have stored procedure in oracle database, and I want to call it in NestJs. How do I to call stored procedure in NestJs?

This is my stored procedure

PROCEDURE pipeline_critical (
        i_main_company IN NUMBER,
        o_value OUT NUMBER
   ) AS
   BEGIN
    SELECT COUNT(A.PIPELINE_ID) AS IGNORED_PIPELINE 
    INTO o_value
    FROM T_PIPELINE A
    LEFT JOIN T_PIPELINE_PRODUCT P ON P.PIPELINE_ID = A.PIPELINE_ID
    WHERE 
        1 = ( CASE WHEN
                i_main_company <> 3
                AND A.MAIN_COMPANY_ID = i_main_company
                AND TO_CHAR(A.EST_DELIVERY,'YYYY') >= TO_CHAR(SYSDATE,'YYYY') - 1
                AND A.PIPELINE_STATUS_ID IN (1,2,3)
                AND TO_CHAR(ADD_MONTHS(A.UPDATE_DATE,3),'YYYYMM') < TO_CHAR(SYSDATE,'YYYYMM')
                AND P.PAID_DATE IS NULL THEN 1
            WHEN
                i_main_company = 3
                AND TO_CHAR(A.EST_DELIVERY,'YYYY') >= TO_CHAR(SYSDATE,'YYYY') - 1
                AND A.PIPELINE_STATUS_ID IN (1,2,3)
                AND TO_CHAR(ADD_MONTHS(A.UPDATE_DATE,3),'YYYYMM') < TO_CHAR(SYSDATE,'YYYYMM')
                AND P.PAID_DATE IS NULL THEN 1
            END
        );
   END pipeline_critical;
like image 331
Riki Nurhidayat Avatar asked Dec 17 '22 19:12

Riki Nurhidayat


1 Answers

I am migrating Javascript/sequealize Backend, and I am testing my needs. This is a simple example that's work for me:

This is stored procedure

CREATE PROCEDURE sp_prueba
@email varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
    select * from tbusua where emai_usua=@email
END

In NESTJS - Controller

import { Controller, Get, Param } from '@nestjs/common';
import { UsuariosService } from './usuarios.service';
import { async } from 'rxjs/internal/scheduler/async';
import { Usuario } from './usuarios.entity';


@Controller('Usuarios')
export class UsuariosController {
    constructor(private readonly UsuariosService: UsuariosService) { }

    @Get('/:email')
    async find(@Param('email') email): Promise <Usuario[]>{
        return await this.UsuariosService.find(email);
    }
}

And in the NESTJS - TypeORM Backend in the service

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Usuario } from './usuarios.entity';
import { Repository, Db, EntityManager } from 'typeorm';

@Injectable()
export class UsuariosService {
    constructor(
        @InjectRepository(Usuario)
        private readonly usuariosRepository: Repository<Usuario>) { }

    async find(email: string): Promise<Usuario[]>{
        return await this.usuariosRepository.query("sp_prueba @email='"+email +"'");
    }
}

this work for me

like image 62
Fernando Lacoumette Avatar answered Dec 31 '22 08:12

Fernando Lacoumette