Good practice for checking database existence on startup
See original GitHub issueI understand that this library does a few things other than manage the mysql, it can be used as a query builder on it’s own.
I am however surprised that there is no option for knex to check if a connection actually has been made if using it with a mysql database.
I’m using the following:
const instance = knex({
client: 'mysql',
connection: {
database: 'my_db',
host: '127.0.0.1',
port: '3306',
user: 'my_user',
password: 'pass'
},
pool: {
min: 3,
max: 7
},
migrations: {
tableName: 'mig'
},
acquireConnectionTimeout: 5000
});
The only way I can check this is to do a quick “check” query, which feels very dirty:
instance.raw('select 1+1 as result').catch(err => {
console.log(err);
process.exit(1);
});
It crashes and displays why (which is what I want).
I’m wondering if yous recommend a specific way of doing this, the quick query was actually taken from my search over on StackOverflow.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:21
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Database Testing Complete Guide (Why, What, and How to ...
#1) Write Queries yourself: To test the Database accurately, the tester should have very good knowledge of SQL and DML (Data Manipulation ...
Read more >How to check if mysql database exists - Stack Overflow
It's much safer and better practice to use an "=" equal sign to test for existence. The correct and safe way to test...
Read more >How to find out if a database exists in a given system - Ask TOM
How to find out if a database exists in a given system TomSuppose if a server is given to you , and you...
Read more >Best practices for monitoring workloads with Query Store
Best practices for monitoring workloads with Query Store · Use the latest SQL Server Management Studio · Use Query Performance Insight in Azure ......
Read more >Best practices for writing SQL queries - Metabase
The standard warning against premature optimization applies here. Avoid tuning your SQL query until you know your query returns the data you're looking...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Because I found this issue when doing a Google search for techniques to test database connections, I’m going to leave this here.
That looks unnecessary complicated way to do few queries.