Skip to content

Conversation

wedgwood
Copy link
Contributor

@wedgwood wedgwood commented Nov 3, 2017

This PR is for the feature multi-connections support(#10).

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Just few nits.
I would make this configurable, if I don't pass a name option, I should be able to use fastify.pg.[api] without the name key.

Somethink like this should work:

const db = {
  connect: pool.connect.bind(pool),
  pool: pool,
  Client: pg.Client,
  query: pool.query.bind(pool)
}
if (name) {
  fastify.pg[name] = db
} else {
  fastify.pg = db
}

What do you think?

index.js Outdated

fastify.addHook('onClose', onClose)
fastify.addHook('onClose', (fastify, done) => pool.end(done))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?
The reason of the separated function is that if something goes wrong its easier understand which function has failed, since it has a name ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I had tried name configurable first time, but I think maybe it makes some user confuesed. sometimes pg is an connection container and sometimes it is an connection. I can not make a choice myself. If you think name configuable better, I could do it.

  2. using closure because I need refer the connection pool. If I still use onClose function, I cloud not get the pool to end.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some nits.

index.js Outdated
fastify.pg[name] = db
} else {
if (fastify.pg) {
console.warn('fastify-postgres has already registered')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should error if it was already register.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

test.js Outdated
connectionString: 'postgres://postgres@localhost/postgres'
})

fastify.ready(err => {
t.error(err)
fastify.pg.connect(onConnect)
fastify.pg.test.connect(onConnect)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not change the tests, and keep using the non-prefixed version. But add a new test to check on the prefixed version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

@mcollina
Copy link
Member

mcollina commented Nov 4, 2017

LGTM. @delvedor do you want to have another look before releasing?

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@delvedor delvedor merged commit 530c555 into fastify:master Nov 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

Successfully merging this pull request may close these issues.

3 participants