mysql,sql server等數據庫鏈接集成庫

地址: https://github.com/fmfe/lib-sql

Installation

$ npm install @fmfe/lib-sql

ornode

$ yarn add @fmfe/lib-sql

Usage

有兩種傳入配置信息的方式:mysql

使用config來管理咱們的配置文件.

假設咱們的項目目錄下有一個config目錄,config目錄裏有一個dev.json文件.git

config/dev.json
{
    "mysql": {
        "host": "127.0.0.1",
        "port": 3306,
        "database": "test",
        "user": "root",
        "password": "123456"
    },
    "mssql": {
        "user": "sa",
        "password": "123456",
        "server": "127.0.0.1",
        "database": "test",
        "port": 1433,
        "pool": {
            "min": 0,
            "max": 10,
            "idleTimeoutMillis": 3000
        }
    }
}
mysql.js
const { mysql } = require('@fmfe/lib-sql');

const mysqlPool = mysql.init();

const _getNewSqlParamEntity = mysql._getNewSqlParamEntity;

// 執行單條sql語句  
// mysql.exec(mysqlPool, sql, params);
async function exec() {
    const sql1 = 'select * from ?? limit 2';
    const data = await mysql.exec(mysqlPool, sql1, ['tbl_user']);
}

// 執行mysql事務,能夠傳入多條增/刪/改sql語句 
// mysql.exectrans(mysqlpool, sqlParamsEntity);
async function execTrans() {
    const sqlParamsEntity = [];
    const sql1 = 'insert into ?? (name, age, sex) values (?, ?, ?)';
    const param1 = ['tbl_user', 'aaa', 20, 1];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql1, param1));

    const sql2 = 'insert into ?? (name, age, sex) values (?, ?, ?)';
    const param2 = ['tbl_user', 'bbb', 22, 0];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql2, param2));

    const sql3 = 'update ?? set age = ? where id = ?';
    const param3 = ['tbl_user', 10, 1];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql3, param3));
    // ....
    const data = await mysql.execTrans(mysqlPool, sqlParamsEntity);  
}
mssql.js
const { mssql } = require('@fmfe/lib-sql');
const _getNewSqlParamEntity = mssql._getNewSqlParamEntity;

// 執行單條語句  
// mssql.exec(sql)
async function exec() {
    const sql1 = 'select Top 3 name, age, sex from tbl_user order by age desc';
    const data = await mssql.exec(sql1);  
}

// 執行sql server 事務, 最好執行增/刪/改語句,這裏只是用select演示使用方法
// mssql.exectrans(sqlParamsEntity); 
async function exectrans() {
  
    const sqlParamsEntity = [];
    const sql1 = 'select * from tbl_user where id = 1';
    sqlParamsEntity.push(_getNewSqlParamEntity(sql1));

    const sql2 = 'select * from tbl_user where id = 2';
    sqlParamsEntity.push(_getNewSqlParamEntity(sql2));
    // ...
    const data = await mssql.execTrans(sqlParamsEntity);  
}

因爲使用config管理配置文件, 運行項目時經過使用命令: NODE_ENV=dev node ..., @fmfe/lib-sql便可自動獲取到數據庫相關配置.github

經過傳入配置文件來調用庫

咱們引用上邊的代碼示例,只需作一點改動:sql

mysql.js

只需在初始化時傳入mysql數據庫配置就好.數據庫

const { mysql } = require('@fmfe/lib-sql');

const mysqlPool = mysql.init({
    host: '127.0.0.1',
    port: 3306,
    database: 'test',
    user: 'root',
    password: '123456'
});

......
mssql.js

在每次調用方法時傳入配置npm

const { mssql } = require('@fmfe/lib-sql');
const config = {
    user: 'sa',
    password: '123456',
    server: '127.0.0.1',
    database: 'test',
    port: 1433,
    pool: {
        min: 0,
        max: 10,
        idleTimeoutMillis: 3000
    }  
}

async function exec() {
    ......
    const data = await mssql.exec(sql1, config);  
}

async function exectrans() {
  
    ......
    const data = await mssql.execTrans(sqlParamsEntity, config);  
}
相關文章
相關標籤/搜索