app-context-mysql2

1.0.0 • Public • Published

app-context-mysql2

mysql2 initializer for app-context

Usage

This initializer can be auto-installed by using it in your context file.

This initializer will attach the configured connections to APP.mysql.

module.exports = function() {
  this.runlevel('connected')
    // attach a connection to APP.mysql - use the value at APP.config.mysql as the connection string
    .use('mysql2', '$mysql')
 
    // attach a connection to APP.mysql
    .use('mysql2', 'mysql://localhost:3306/foobar')
 
    // attach a connection pool to APP.mysql
    .use('mysql2', {url: 'mysql://localhost:3306/foobar', pool: true})
 
    // create 2 connections and attach them as an object to APP.mysql
    // this will create APP.mysql.users and APP.mysql.data
    .use('mysql2', {
      users: '$mysql.users',
      data: '$mysql.data'
    })
 
    // you can also pass options to each connection
    .use('mysql2', {
      users: {url: 'mysql://localhost:3306/users', pool: true},
      data: {url: 'mysql://localhost:3306/data', connectTimeout: 3000}
    })
};

Connection Configurations

Each connection can be configured with either a connection string (like mysql://localhost:3306/users) or with an object. The object will be passed through to the mysql constructor and can consist of any options from connection options. There is a special url option not listed that this initializer will parse and fill in for you (like the user, password, host, and port).

Connection Pooling

You can make any connection a pool by adding {pool: true} to the configuration. Other options from the configuration will be passed through to the mysql driver. You can see the available options at connection options and pool options.

Package Sidebar

Install

npm i app-context-mysql2

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • mattinsler