Open SylvainMarty opened 1 month ago
This already exists via the db
option in most functions.
@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.
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.
@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. 👍
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
.
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 theexecuteSql()
of the database wrapper instead of using the connection pool. For this, I created a new propertyexecuteOptions
in theConnectionOptions
to allow us to add more options later if we need it. This newexecuteOptions
property is optional and can totally be used by custom implementation of theDb
wrapper since it was the only workaround available since then.