mysql2 initializer for app-context
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}
})
};
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).
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.