Helper functions
See original GitHub issueHey, I use these two helper functions in pretty much every project I use node-postgres in. I was just wondering if you’d be open to a PR to add these to the project proper?
export async function withClient(pool: Pool, fn: (client: PoolClient) => Promise<void>) {
const client = await pool.connect();
try {
await fn(client);
} finally {
client.release();
}
}
export async function withTransaction(pool: Pool, fn: (client: PoolClient) => Promise<void>) {
await withClient(async client => {
try {
await client.query('BEGIN');
await fn(client);
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
}
});
}
The basic idea here is I can do:
await withTransaction(pool, async client => {
client.query(...);
});
and then there’s no chance of me messing up and forgetting the ROLLBACK or forgetting to release the client from the pool; it all gets handled for me. If you’re interested, I’ll do up a PR and write some docs.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Role of "helper functions"? - Stack Overflow
A main function that prompts the user for pertinent data (applicant name, math SAT score, reading SAT score, writing SAT score, and class...
Read more >What are Helper Functions, How to Use a Helper ... - YouTube
In this JavaScript lesson from Codecademy's JavaScript course we look at What are Helper Functions, How to Use a Helper Function in ...
Read more >Creating Helper Functions - The Click Reader
A helper function is a function that performs part of the computation of another function following the DRY (Don't repeat yourself) concept.
Read more >Helper Functions and How They Work - Brown CS
Helper functions are the next step up in reflecting structure in code. We use helpers to name key tasks within our problem. Accounting...
Read more >Helper Functions - Components - Ember Guides
Helper functions are JavaScript functions that you can call from your template. Ember's template syntax limits what you can express to keep the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with you - most of those traps are due to me designing a…maybe not the best…api a long time ago and trying hard to limit backwards incompatible breaks. query queuing, emitting
row
events, manually releasing clients, being really opinionated with timezones and type coercion, etc…are all things I could have done a better job of a long time ago. Now it’s a careful dance of weighing the cost of forcing thousands of folks to change their apps versus maintaining things which aren’t very nice or intuitive. I’ve focused this year on improving performance (we’re up something like 30-40% from 7.x branch) and will likely start the work of deprecating and slowly sweeping away some of those bad decisions in 2021. I planned on more of that in 2020 but you know 2020 wasn’t the very best year for focus/productivity while the world has been on fire. ❤️ 😬I will say if you feel the pain I feel it 10x.
I’m down to merge something like this if it’s sufficiently tested! I think ideally you could create another project in the monorepo like
pg-fns
or something & add it there. Then you could write it with sweet, sweet typescript. Currently converting the corepg
module to typescript is something I’ve fiddled with locally a few times but isn’t quite in scope yet, and would 100% be a semver major just because typescript is gonna fiddle w/ the internals of things enough to likely break a bunch of integrations and stuff (things like datadog and sentry sometimes poke deeply into the module’s code). That would also keep anyone who wants the main package “clean” to have it their way, and I could long term own the maintenance and documentation ofpg-fns
(with help from the community of course!) This would hopefully allow on-boarding into the ecosystem to be easier, reduce having to write the slightly finickywithTransaction
function yourself in every project, might be a cool place for new contributors to contribute to, etc.The name of the module can be whatever you want - I just picked
pg-fns
because it’s available on npm. Apparently a lot ofpg-*
modules are taken already 🙃