Knex: using knexfile.js to specify default connection for knex

Created on 24 Feb 2016  ·  3Comments  ·  Source: knex/knex

Hey,

I would like the same db connections to be used for my migrations and my queries. But I cant find in documentation how to point knex at knexfile.js outside of migration environment.

Is there a way to point knex at knexfile.js instead of specifying the same db connection in both knexfile and require('knex') statement? It feels broken to have to do it in both places.

Probably me just missing something in the docs -.-

Thanks,
-Michal

question

Most helpful comment

// database.js
'use strict';

var knex      = require('knex')(require('./knexfile')[process.env.NODE_ENV]), // Selects the correct DB config object for the current environment
    bookshelf = require('bookshelf')(knex);

module.exports = bookshelf;

something like this?
How to Structure Bookshelf.js Models

All 3 comments

// database.js
'use strict';

var knex      = require('knex')(require('./knexfile')[process.env.NODE_ENV]), // Selects the correct DB config object for the current environment
    bookshelf = require('bookshelf')(knex);

module.exports = bookshelf;

something like this?
How to Structure Bookshelf.js Models

Yeah I can do that but its not without its own problems ... I feel somewhat uneasy hard coding this special filename in my application. Since knexfile has a special meaning to knex I was hoping there is a way to suggest to knex to use its own configuration file. (the clean way to do it would seem to have knex use knexfile if no custom configuration is given.)

well, knex is what's used underneath bookshelf, and that's passing the knexfile to knex, then just injecting knex's settings into bookshelf, not sure how to make it better, seems ok to me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

npow picture npow  ·  3Comments

marianomerlo picture marianomerlo  ·  3Comments

olaferlandsen picture olaferlandsen  ·  3Comments

fsebbah picture fsebbah  ·  3Comments

koskimas picture koskimas  ·  3Comments