egg-mysql2

基于mysql2的egg插件


Keywords
egg, mysql2, plugin
License
MIT
Install
npm install egg-mysql2@1.0.2

Documentation

egg-mysql2

NPM version David deps Known Vulnerabilities npm download

Install

$ npm i egg-mysql2 --save

Usage

enable plugin

// {app_root}/config/plugin.js
exports.mysql = {
  enable: true,
  package: 'egg-mysql2',
};

Configuration

  • Simple database instance
exports.mysql = {
  // database configuration
  client: {
    // host
    host: 'mysql.com',
    // port
    port: '3306',
    // username
    user: 'test_user',
    // password
    password: 'test_password',
    // database
    database: 'test',    
  },
  // load into app, default is open
  app: true,
  // load into agent, default is close
  agent: false,
};

Usage:

app.mysql.query(sql, values); // you can access to simple database instance by using app.mysql.
  • Multiple database instance
exports.mysql = {
  clients: {
    // clientId, access the client instance by app.mysql.get('clientId')
    db1: {
      // host
      host: 'mysql.com',
      // port
      port: '3306',
      // username
      user: 'test_user',
      // password
      password: 'test_password',
      // database
      database: 'test',
    },
    // ...
  },
  // default configuration for all databases
  default: {

  },

  // load into app, default is open
  app: true,
  // load into agent, default is close
  agent: false,
};

Usage:

const client1 = app.mysql.get('db1');
client1.query(sql, values);

const client2 = app.mysql.get('db2');
client2.query(sql, values);

License

MIT