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

Way to test db connection #545

Closed
tgriesser opened this issue Oct 31, 2014 · 10 comments
Closed

Way to test db connection #545

tgriesser opened this issue Oct 31, 2014 · 10 comments

Comments

@tgriesser
Copy link
Member

Would be useful...

@rbvea
Copy link

rbvea commented Nov 30, 2014

+1

1 similar comment
@Voffkaa
Copy link

Voffkaa commented Dec 10, 2014

+1

@bendrucker
Copy link
Member

For people finding this issue, this is easy enough to do manually now:

#407 (comment)

@kibertoad
Copy link
Collaborator

There is also a library that provides this functionality (with preset test queries for different dbs) out of the box: https://www.npmjs.com/package/knex-utils

@elhigu
Copy link
Member

elhigu commented May 31, 2018

I think this can be closed or the use case needs to be specified more clearly.

Currently one can easily test the connection by just making some dummy query to the DB. That guarantees, that connection is available at that moment, but of course connection may be broken in next moment after the check.

@elhigu elhigu closed this as completed May 31, 2018
@KaKi87
Copy link

KaKi87 commented Nov 29, 2019

Hello,

This issue should be reopened and a connection method should be implemented.

Thanks

@elhigu
Copy link
Member

elhigu commented Nov 29, 2019

@KaKi87 nope. This should not be reopened and I have no idea what you mean by connection method 🤷‍♂ Please open a feature request if there is a new feature that you would like knex to have.

@yumindeckard
Copy link

The connection status should be logged in the console. Query some dummy data from database is a workaround, not the solution.

@KaKi87
Copy link

KaKi87 commented Mar 10, 2020

@elhigu A connection method can be a method called db.connect() for example.

@elhigu
Copy link
Member

elhigu commented Mar 10, 2020

@yumindeckard you can add event handlers to tarn to log that kind of stuff. There are events telling if connection acquire fails for example and many other. Also if you need to log connection status on knex side you can add knex.on('query-success', ...) and knex.on('query-error', ...)event handler to print if query was success or if it failed for some reason and you can even choose to print error only if it was connection error (btw. it is also ambiguous if for example bad credentials as connection error or invalid SQL db user rights for some table / db or is it strictly the case where network or database is down).

@KaKi87 Making query is the only way to check if you are able to access your DB and having db.connect() would just wrap that dummy query to pretty much useless wrapper method which might give you false sense of security that there is stable connection... Next time you call db.connect() 10ms after it was fine the connection might be already broken.

If you need that kind of method knex nowadays offers API to add extensions so you can write separate knex-plugin for that method or you can just use knex-utils library.

EDIT:
tl;dr Knex already offers multiple ways to implement this functionality which can be bent to any use-cases we have thought... so it is useless to add one more opinionated wrapper for those to have separate API for just a single use-case, where is not even perfectly clear what it should check exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants