This plugin based on mssql.
recommend to use egg-sequelize.
$ npm i egg-mssql --save
// {app_root}/config/plugin.js
exports.mssql = {
enable: true,
package: 'egg-mssql',
};
// {app_root}/config/config.default.js
exports.mssql = {
// Single Database
// client: {
// server: 'host',
// port: 'port',
// user: 'user',
// password: 'password',
// database: 'database',
// },
// Multi Databases
// clients: {
// db1: {
// server: 'host',
// port: 'port',
// user: 'user',
// password: 'password',
// database: 'database',
// },
// db2: {
// server: 'host',
// user: 'user',
// password: 'password',
// database: 'database',
// },
// },
};
see npm/mssql tedious for more detail.
// {app_root}/service/user.js
'use strict';
const mssql = require('mssql');
const Service = require('egg').Service;
class UserService extends Service {
async get() {
// use db1
const request = new mssql.Request((await this.app.mssql.get('db1')));
const rows = await request.query('SELECT name FROM sysobjects where xtype = \'U\';');
// use db2
const request1 = new mssql.Request((await this.app.mssql.get('db2')));
const rows1 = await request1.query('SELECT name FROM sysobjects where xtype = \'U\';');
console.log(rows, rows1);
return '6';
}
}
module.exports = UserService;
// https://eggjs.org/zh-cn/core/unittest.html
npm run test
Please open an issue here.