timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.15k stars 160 forks source link

Inserts job in existing transaction by passing pgClient instance #515

Open SylvainMarty opened 1 month ago

SylvainMarty commented 1 month ago

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 commented 1 month ago

This already exists via the db option in most functions.

SylvainMarty commented 4 weeks ago

@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 commented 3 weeks ago

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 commented 3 weeks ago

@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 commented 3 weeks ago

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.