-
-
Notifications
You must be signed in to change notification settings - Fork 36
feature: multiple connections #11
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
Conversation
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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 ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
I had tried
name
configurable first time, but I think maybe it makes some user confuesed. sometimespg
is an connection container and sometimes it is an connection. I can not make a choice myself. If you thinkname
configuable better, I could do it. -
using closure because I need refer the connection pool. If I still use
onClose
function, I cloud not get the pool to end.
There was a problem hiding this 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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
LGTM. @delvedor do you want to have another look before releasing? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks!
This PR is for the feature multi-connections support(#10).