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

Inserts job in existing transaction by passing pgClient instance #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

SylvainMarty
Copy link

This is my take at the attempt made four years ago with the PR #200.

These changes don't introduce any breaking change with the existing behavior.

Fixes #199

Description

The idea is to be able to pass a pg.Client instance to be used by the executeSql() of the database wrapper instead of using the connection pool.
For this, I created a new property executeOptions in the ConnectionOptions to allow us to add more options later if we need it.
This new executeOptions property is optional and can totally be used by custom implementation of the Db wrapper since it was the only workaround available since then.

@timgit
Copy link
Owner

timgit commented Oct 19, 2024

This already exists via the db option in most functions.

@SylvainMarty
Copy link
Author

@timgit From PGBoss documentation, it wasn't clear for me how to create a job in an existing db transaction.

The documentation about the db wrapper says this:

Instead of using pg-boss's default adapter, you can use your own, as long as it implements the following interface (the same as the pg module).

IMO, this documentation is not clear enough for someone that doesn't know the inner working of PGBoss. Why would I use this option if not to replace the whole database driver used by PGBoss by some other driver?

This is why I still think the option I added in this PR makes it a lot easier to use transactions with PGBoss for codebases that are also using the pg package, and without having to know the inner working of the library.

@timgit
Copy link
Owner

timgit commented Oct 25, 2024

Individual functions also support a db prop, which would allow you to use an existing transaction. I agree that this is not documented very clearly.

@SylvainMarty
Copy link
Author

@timgit It would be awesome to have some examples how to use the db prop for transactions (and maybe for other use cases I don't know about?).
If you have some idea about how you would like the doc to be, I'd love to help improving it. 👍

@timgit
Copy link
Owner

timgit commented Oct 29, 2024

I would probably provide an example from the pg package. They have a docs page for using transactions by reserving a client from the connection pool here: https://node-postgres.com/features/transactions.

It would be as simple as building a wrapper around client.query, renamed to executeSql.

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

Successfully merging this pull request may close these issues.

publishing in a transaction
2 participants