Skip to content

jackhenhao/egg-mssql

Repository files navigation

egg-mssql

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

Description

This plugin based on mssql.

recommend to use egg-sequelize.

Install

$ npm i egg-mssql --save

Usage

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

Configuration

// {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.

Example

// {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;

Unit testing

// https://eggjs.org/zh-cn/core/unittest.html
npm run test

Questions & Suggestions

Please open an issue here.

License

MIT