1

I'm trying to utilize Knex with the Loopback framework. Currently, Loopback does not provide a good way to create advanced queries.

I'm using Knex's query builder, however by default Knex will initialize its own connection pool ON TOP of Loopback's. Instead, I want to use the connection pool already created by Loopback.

I've tried to use Knex's .connection() method to set the connection to be the one from loopback, however when I monitor the processes on my MySql server I notice that each time I make a call that uses Knex a new connection is created. Over time this is causing my server to run out of connections to the database.

I'm doing something like this:

var knex = require('knex')({
  client: 'mysql',
  connection: {
    host: mysql.host,
    port: mysql.port,
    user: mysql.username,
    password: mysql.password,
    database: mysql.database,
    debug: false
  }
});

app.datasources.mysqldb.client.getConnection(function(err, connection){
    knex.connection(connection).
    //continue with the query building
}

My question is, how do I utilize Loopback's existing connection with Knex so that Knex doesn't burn through all the available connections in my database? I've also tried using knex's "pool" configuration but it doesnt seem to do anything...

John
  • 93
  • 1
  • 11
  • I [answered something similar for the mongo connector](http://stackoverflow.com/questions/28346169/how-do-i-get-the-mongodb-connection-from-inside-loopback-io/28349665#28349665), but basically it came down to accessing the `ModelName.dataSource.connector` object. But that answer is specific to mongo, obviously. This isn't a (well) documented thing as it is mostly for internal use. – Jordan Kasper Feb 18 '15 at 19:35
  • Thanks @jakerella ! This is currently what we're doing however ideally i'd like to still use Knex's interface and just supply a mysql connection to use – John Feb 26 '15 at 16:48
  • Hmm... yeah, that's all I have. :) Good luck! – Jordan Kasper Feb 26 '15 at 17:05

0 Answers0