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.

Confused: connect or not to connect?

See original GitHub issue

Hello,

In the project that I’m working on, instead of using Sequalize I decided to do old school, since the project is small. This means I’m spending lots of time in this repo 👻 😋. And I’m confused how to talk with the DB.

On the main README.md I read:

//call done() to release the client back to the pool

So my brain assumes that not only I have to do connect() at each request, but I also have to release the connection. Sounds fine to me.

But then I go hear https://github.com/brianc/node-postgres/wiki/Query, and suddenly no connection(), and no release to be seen in the top part of the document.

In my code I made queries using both options and they work, but since I don’t see the difference I don’t know which one to chose 😃

How should I talk with the DB? 😎 To many options, and not enough explanation on the differences between all this approaches.

What I’m looking for:

  • to write as little code as possible
  • to have code that can handle 10,000 request a minute for example.
  • to reuse the open connection as much as possible.

Which aproceh will give the best results?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:26 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Globikcommented, Oct 20, 2016

Take attention. From pg-pool module index.js (pool.query instance):

Pool.prototype.query = function (text, values, cb) {
  if (typeof values === 'function') {
    cb = values
    values = undefined
  }

  return new this.Promise(function (resolve, reject) {
    this.connect(function (err, client, done) {
      if (err) {
        if (cb) {
          cb(err)
        }
        return reject(err)
      }
      client.query(text, values, function (err, res) {
        done(err)
        err ? reject(err) : resolve(res)
        if (cb) {
          cb(err, res)
        }
      })
    })
  }.bind(this))
}
0reactions
Globikcommented, Feb 7, 2019

@johncmunson no, I mean this: app.use(function(req, res, next){ res.locals.your_database_instance = pool; next(); }); And then in your router call it for queries. let db = res.locals.your_database_instance; try{ db.query(blablabla…) }catch(error){}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting With People - What It Is and Isn't, And Why You ...
It's not connection, it's showmanship. Your 'connection' is based around a shared dislike of other people, or on talking about others.
Read more >
How to Pronounce Confused, Concerned and Connected ...
How to Pronounce Confused, Concerned and Connected (words starting with CON). Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
Read more >
How to Fix the ERR_CONNECTION_REFUSED Error in Chrome
When you visit a website using Google Chrome and encounter this message, it means that your attempt to connect was refused.
Read more >
Devices Connecting to the Wrong Network? How to Change ...
If your laptop or phone is ignoring your preferred network, and sticking to a weak one, here's how to adjust the connection priority...
Read more >
Confused about Gateway Connection- Connect Directly option ...
Confused about Gateway Connection- Connect Directly option not there. ‎09-06-2017 06:39 AM. Power BI Pro user. I have two workspaces.
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