Monday, 10 April 2017

Node.js Knex: connection pooling

In mysql2 ( mysql2/promises ) to work with connection pool, I followed next steps:

  1. In mysql.config.js : { ..., connectionLimit : 100 }
  2. In app.js :

    import mysql from "mysql2/promise"; 
    const connectionPool = mysql.createPool( mysqlConfig );
    ...
    connectionPool.getConnection()
            .then( connection => ... )
            .catch( err => ... );

In knex site -> docs -> Pooling :

The client created by the configuration initializes a connection pool, using the generic-pool library. Checkout the generic-pool library for more information.

Example config from site knex( not from generic-pool ):

    var knex = require('knex')({
      client: 'mysql',
      connection: {
        host : '127.0.0.1',
        user : 'your_database_user',
        password : 'your_database_password',
        database : 'myapp_test'
      },
      pool: { min: 0, max: 7 }
    });

In knex site -> docs -> Upgrade :

Upgrading 0.11 -> 0.12 generic-pool is swapped out for Pool2 for pooling management

( knex Latest Release: 0.12.9 )

I do not quite understand what they offer: Using "third-party"( for knex ) library, in addition, without Promises( it's using "classical" callback func ) and any good tutorials OR, for pulling, nothing need to do, except setting in config pool: { min: .., max: .. } ?

And, with pooling, make requests as without pool? :


    import knex from "knex";
    const knexConnection = knex( { ..., pool: { min: ..., max: ... }} );
    ...
    knexConnection.select().table( "..." )
            .then( ... )
            .catch( ... );



via AlEX

No comments:

Post a Comment