Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: BATCH API in node-postgres #3828

Open
1 task done
jakeleventhal opened this issue Dec 23, 2024 · 0 comments
Open
1 task done

[FEATURE]: BATCH API in node-postgres #3828

jakeleventhal opened this issue Dec 23, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jakeleventhal
Copy link

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Reopening #2291

Saw this was closed, but wondering why. Assume the following code:

await db.transaction(async (tx) => {
  await tx.update(...);
  await tx.update(...);
});

This will perform 2 updates inside a transaction. If one fails, the transaction fails. We can speed this up by putting each update inside a Promise.all, but we are still making 3 trips to our database:

  1. start transaction
  2. update rows
  3. end transaction

It would be much more efficient to do something like

db.batch([
  db.update(...),
  db.update(...)
]);

Under the hood this could produce a single SQL statement that does the following:

BEGIN;
UPDATE table1 SET column_name1 = 'new_value' WHERE condition1;
UPDATE table2 SET column_name2 = 'new_value' WHERE condition2;
COMMIT;

Now I only have one chunk of SQL being sent to the database rather than 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant