Okira
Okira

Reputation: 52

Execute Multiple Raw SQL Statements in Loopback 4 Using execute()

Loopback 4 uses its own Juggler ORM. But I can't seem to find the required parameter to pass to be able to execute multiple SQL statements using the "execute" method.

await this.myRepository.execute(`SELECT * FROM FIRST_TABLE WHERE TABLE_ID = 1; SELECT * FROM SECOND_TABLE WHERE TABLE_ID = 1;`);

This should be possible using any other ORM by setting a "multi-statement" option to true.

Upvotes: 0

Views: 411

Answers (2)

Gagan
Gagan

Reputation: 163

I think, if you are trying to execute a "SELECT" statement you would want to catch/return the result output. I am not sure how the multiple results will get handled, so, I think you will need to execute single statement like below:

const Result = await this.myRepository.execute(
  `SELECT * FROM FIRST_TABLE WHERE TABLE_ID = 1`
);

However, the way to pass parameters in execute method is by using second parameter as an array:

await this.myRepository.execute(
  `SELECT * FROM FIRST_TABLE WHERE TABLE_ID = ?; 
   SELECT * FROM SECOND_TABLE WHERE TABLE_ID = ?;`,
  [FirstTableId,SecondTableId]
);

Upvotes: 0

Samarpan
Samarpan

Reputation: 943

I think only way to achieve this is by doing promise all.

Try this

await Promise.all([
    this.myRepository.execute(`SELECT * FROM FIRST_TABLE WHERE TABLE_ID = 1`),
    this.myRepository.execute(`SELECT * FROM SECOND_TABLE WHERE TABLE_ID = 1`)
]);

Upvotes: 1

Related Questions