Closed ycg520520 closed 6 years ago
import mssql from 'mssql' // 引入mssql连接SQLSever数据库类库 import conf from '../config' // 引入配置文件 class MssqlConnect { constructor() { this.connections = {} // 连接库对象 this.request = null } /** * 使用数据库 * @param {String} database * return 返回相关操作方法 */ query(sql, inputs) { return this.request.then(pool => { let request = pool.request() if(inputs) { for(let i in inputs) { if(inputs.hasOwnProperty(i)) { request = request.input(i, inputs[i]) } } } return request.query(sql) }) } /** * [use 要使用的数组] * @param {[String]} database [要选择的数据库库名称] * @return {[Object]} [将对象返回] */ use(database) { // defaltDatabase, server1.database, server2.databae database || 'defaltDatabase' // 判断是否存在连接库对象 const connection = this.connections[database] if(connection) { this.request = connection } else { // 默认选择的数据库 let db = conf.db.default, initDatabase = database // 如果存在以.的字符则是选择不同服务的数据库 if(database.indexOf('.') !== -1) { const tmpArr = database.split('.') db = conf.db[tmpArr[0]] database = tmpArr[1] } const config = Object.assign({}, db, (initDatabase ? { database: database } : false)) const pool = mssql.connect(config) this.connections[initDatabase] = pool this.request = pool return this } } }
This isn't detailed enough to do anything with.
my code:
About every 3 minutes,CPU processing will appear the following picture