Skip to content

docs: "project structure" issues #1457

Closed
@dwelle

Description

@dwelle

in https://node-postgres.com/guides/project-structure the getClient API contains numerous errors (man, we really need to opensource the docs to be able to make proper PRs):

image

Fixed below:

module.exports = {
  getClient: (callback) {
    pool.connect((err, client, done) => {
      const query = client.query.bind(client)

      // monkey patch the query method to keep track of the last query executed
      client.query = (...args) => {
        query.lastQuery = args
        query(...args)
      }

      // set a timeout of 5 seconds, after which we will log this client's last query
      const timeout = setTimeout(() => {
        console.error('A client has been checked out for more than 5 seconds!')
        console.error('The last executed query on this client was:', query.lastQuery)
      }, 5000)

      const release = (err) => {
        // call the actual 'done' method, returning this client to the pool
        done(err)

        // clear our timeout
        clearTimeout(timeout)

        // set the query method back to its old un-monkey-patched version
        client.query = query
      }

      callback(err, client, release)
    })
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions