1.0.0 • Published 4 years ago

@eggplugin/mysql v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

egg-mysql

mysql plugin for Egg.js

NOTE: This plugin just for integrate mysql into Egg.js, more documentation please visit https://github.com/fuxingZhang/mysql-promise

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i @eggplugin/mysql --save

Configuration

// {app_root}/config/plugin.js
exports.mysql = {
  enable: true,
  package: '@eggplugin/mysql',
};

see config/config.default.js for more detail.

Simple instance

// {app_root}/config/config.default.js
exports.mysql = {
  client: {
    user: '',
    host: '',
    database: '',
    password: ''
    // port: '3306',
    // ...
  },
  // load into app, default is open
  app: true,
  // load into agent, default is close
  agent: false,
};

Usage:

single query:

;(async () => {
  // you can access to pool instance by using app.mysql
  const pool = app.mysql;
  const { results, fields } = await pool.query('SELECT NOW()');
  console.log({ results, fields });
})().catch(console.error);

check out a client:

;(async () => {
  // you can access to pool instance by using app.mysql
  const pool = app.mysql;
  const client = await pool.getConnection();
  try {
    const res = await client.query('SELECT * FROM users WHERE id = ?', [1]);
    console.log(res.results[0]);
    const { results, fields } = await client.query('SELECT NOW()');
    console.log(results, fields);
  } finally {
    // Make sure to release the client before any error handling,
    // just in case the error handling itself throws an error.
    client.release();
    // Don't use the connection here, it has been returned to the pool.
  }
})().catch(console.error);

more documentation please visit https://github.com/fuxingZhang/mysql-promise

Multiple instance

exports.mysql = {
  // default configuration for all clients
  default: {
    port: 3306,
    connectionLimit: 10,
    // connectTimeout: 10000,
    // dateStrings: true
  },
  clients: {
    // clientId, access the pool instance by app.mysql.get('clientId')
    db1: {
      user: '',
      host: '',
      database: '',
      password: ''
    // ...
    },
    db2: {
      user: '',
      host: '',
      database: '',
      password: ''
      // ...
    },
    // ...
  },
  // load into app, default is open
  app: true,
  // load into agent, default is close
  agent: false,
};

Usage:

;(async () => {
  // you can access the pool instance by app.mysql.get('clientId')
  const pool1 = app.mysql.get('db1'); 
  const pool2 = app.mysql.get('db2'); 
  //
}).catch(console.error);

more documentation please visit https://github.com/fuxingZhang/mysql-promise

Questions & Suggestions

Please open an issue here.

License

MIT