Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure the Knex connection in nodejs

I am using NodeJS, Express, and MySQL for my project and want to use Bookshelf ORM with it.

Bookshelf uses Knex for querying, modeling, and suggests to setup the DB connection through Knex(http://bookshelfjs.org/#installation).

I am having trouble in establishing a successful DB connection with Knex. I want to start the server only if DB connection is successful, but it seems like it doesn't offer anything after establishing the connection to do so(no promise or property).

This is the code I have been using.

import _knex from "knex"; // npm install knex --save
import _bookshelf from "bookshelf"; // npm install bookshelf --save

let knex = _knex({
    client: "mysql",
    connection: {
        host: "127.0.0.1",
        database: process.env.DB,
        user: process.env.DB_USERNAME,
        password: process.env.DB_PASSWORD
    },
    debug: true
});

let bookshelf = _bookshelf(knex);

module.exports.knex = knex;
module.exports.bookshelf = bookshelf;

More reference: There is another ORM named Sequelize and it provides sequelize.authenticate() which return Promise and could be used as (http://docs.sequelizejs.com/en/latest/api/sequelize/#authenticate-promise)

sequelize.authenticate()
    .then( () => {
        console.log("Db successfully connected");
        app.listen(port, () => console.log(`App started at: ${port}`) );
    })
    .catch( err => console.log('Unable to connect to the database') );

The only solution I can think of is to perform a raw query like USE {DB_NAME} and use its output to decide whether to start the server or not. Is this solution good enough?

like image 540
Raghav Garg Avatar asked Feb 01 '17 15:02

Raghav Garg


People also ask

How does KNEX connect to database?

The database version can be added in knex configuration, when you use the PostgreSQL adapter to connect a non-standard database. const knex = require('knex')({ client: 'pg', version: '7.2', connection: { host : '127.0.

What is KNEX in node js?

js (pronounced /kəˈnɛks/) is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use.

Is KNEX an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

How do I know my KNEX version?

Use npx to solve the error "knex: command not found", e.g. npx knex --version and make sure to install the package locally by opening your terminal in the root directory of your project and running npm install knex . The fastest way to solve the error is to use the npx command. Copied!


1 Answers

This was discussed earlier this week in knex issue tracker. https://github.com/tgriesser/knex/issues/1886

Making query is a good way to check that connection can be made to database. If no queries are made, pool doesn't necessary create any initial connections (depends on pool settings).

You may also also wire up pool's afterCreate callback to notify you when ever there is new connection made to database (https://github.com/knex/documentation/pull/17/files).

like image 88
Mikael Lepistö Avatar answered Sep 19 '22 15:09

Mikael Lepistö