Better parameter handling for tasks + transactions
Something to consider, for the tasks + transactions support.
pg-promise has a number of methods for tasks and transactions, and this library wraps some of them. But the way it does so is not consistent with how
pg-promise does it. Each task and transaction method uses a reversed-optional approach, to make the callback function the last parameter, which allows for easier-to-read code. Plus the way typing is supported there is also quite complex. For example, those methods support
text as option
tag passed in directly.
The logic of correct handling of all parameters supported by tasks and transaction is quite complex. And this is why it was moved into an utility method taskArgs, so if you want to create a custom task / transaction method, you should reuse the exact parameter handling, consistent with the existing methods.
i.e. methods such as
withTransaction should make use of method taskArgs, to support all parameters in the way that's consistent with
pg-promise existing methods for tasks and transactions -
Method taskArgs has been there for a while, but I didn't see before how you went about implementing those custom methods. And as a result, they do not support the full syntax supported by
pg-promise. If you refactor your code into using taskArgs, then everything
pg-promise supports for tasks and transactions will become supported automatically, and not just what's supported presently, but also what may become supported in the future, as you would correctly wrap all the parameters and pass them on.
And as a result, you won't need to document the complex parameters for those methods, you will be able to just forward it to
pg-promise documentation of the method used underneath.
The approach is quite simple to use...
At the beginning of your custom method you do this:
const args = pgp.utils.taskArgs(arguments); // prepare all arguments
Then you do all the processing as needed by your method, and when it is time to forward to the corresponding method, like
task, for example, you do:
return obj.task.apply(this, args);
So you won't care for the complex parameter syntax supported by the method
task here, while guaranteeing that it is passed on correctly always.