Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheriting pg.Pool will overwrite any custom methods. #1505

Open
ghost opened this issue Nov 15, 2017 · 1 comment
Open

Inheriting pg.Pool will overwrite any custom methods. #1505

ghost opened this issue Nov 15, 2017 · 1 comment
Assignees

Comments

@ghost
Copy link

ghost commented Nov 15, 2017

If you were to create a class that inherits pg.Pool like so:

const pg = require('pg');

class Db extends pg.Pool {
  insert(args) {
    return this.query('...');
  }
}

const a = new Db();

console.log(a.insert); // undefined

The method insert() would be undefined on all instances of this custom class. If you were to do the exact same thing with pg.Client, it works as expected.

@charmander
Copy link
Collaborator

pg doesn’t quite export the Pool type. Since Pool is an ES6 class, though, there’s no nice way to let BoundPool inherit from it instead while maintaining backwards compatibility (allowing pg.Pool() to be called without new).

You can access pg-pool directly instead (making sure to add a dependency on it in your package.json):

const {Client} = require('pg');
const Pool = require('pg-pool');

class Db extends Pool {
  constructor(options) {
    super({Client, ...options});
  }
  

@brianc, any interest in deprecating calling BoundPool without new?

@brianc brianc self-assigned this Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants