user1635697
user1635697

Reputation: 11

call stored procedure in loopback4

I want to call a stored procedure with parameters which is in MYSQL using loopback4. Do I need to add new controller and how to handle return type for that do I need to create a specific Model?

Upvotes: 0

Views: 199

Answers (2)

tkalatz
tkalatz

Reputation: 36

//In loopback 4: //The repository method

  async calcExtreme(lot: String): Promise<Object[]> {
    const result = await this.execute(
      "call packetcount(?,@r1,@r2)", [lot]).then(
        (value) => {
          return [value];
        },
        (error) => {
          console.log(error);
          return [];
        }
      );
    return result;
  }

//The controller method

@get('/products/cext/{lot}')
  @response(200, {
    description: 'Products Extreme Fresh',
  })
  async cext(
    @param.path.string('lot') lot: String,
  ): Promise<Object[]> {
    return this.productsRepository.calcExtreme(lot);
  }

Upvotes: 0

Luis Videla
Luis Videla

Reputation: 11

You can call DataSource.execute() directly from a controller

@get('/sptest', {
  responses: {
    '200': {
      description: 'test for executing sp',
    },
  },
})
async sptest(): Promise<any> {
  return this.anyRepository.dataSource.execute('EXEC DBO.SP_SOMETHING param1, param2');
}

Upvotes: 1

Related Questions