user2741831
user2741831

Reputation: 2398

Is it possible to get the result of 2 queries in a single roundtrip with pg-promise?

I am working on a serverless application (AWS), which means the machine running my code and the one running my database may be physically separated. So I have to make sure that individual API request don't generate too many roundtrips to the server.

So I was wondering, is it possible to execute multiple queries in a single round trip to the server and receive the response?

Something like this:

db.result("SELECT * FROM employees WHERE office=$1;DELETE FROM offices WHERE id=$1 RETURNING *",[office]);

So that both the employees and the office are returned seperately? If I run it the way it is now, only the office is returned.

Upvotes: 0

Views: 279

Answers (1)

vitaly-t
vitaly-t

Reputation: 25840

is it possible to execute multiple queries in a single round trip to the server and receive the response?

Yes, the library has method multi for that:

const [employees, offices] = await db.multi(`SELECT * FROM employees WHERE office = $1; 
                          DELETE FROM offices WHERE id = $1 RETURNING *`, [office]);

Upvotes: 2

Related Questions