question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Good practice for checking database existence on startup

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

48reactions
hdoncommented, Aug 17, 2017

Because I found this issue when doing a Google search for techniques to test database connections, I’m going to leave this here.

const Promise = require('bluebird');
const knex = require('knex');
const fs = require('fs');
const _ = require('lodash');
module.exports = _.transform(require('./database.json'), (o,v,k) => {o[k]=knex(v)});
if (require.main === module) {
  Promise.map(
    Object.keys(module.exports)
  , dbName => module.exports[dbName].raw(`select 'I have ${dbName}' as status`)
    .then(result => {
      console.log(result[0][0].status);
      module.exports[dbName].destroy();
      return null;
    })
    .catch(err => {
      console.error(dbName, err);
      return {status: 'I do NOT have '+dbName}
    })
  ).then(ignore => console.log('database connection test complete'))
}
33reactions
elhigucommented, Aug 17, 2017

That looks unnecessary complicated way to do few queries.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found