question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hey, 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:open
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
brianccommented, Dec 11, 2020

I also think that pg could be offering slightly nicer helpers for these things by default, to avoid people running into common traps of forgetting to release clients, rollback transactions, etc.

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.

1reaction
brianccommented, Dec 11, 2020

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 core pg 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 of pg-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 finicky withTransaction 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 of pg-* modules are taken already 🙃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found