mysql, sql server database connectivity and other integrated library

Address: https://github.com/fmfe/lib-sql

Installation

1
$ npm install @fmfe/lib-sql

or

1
$ yarn add @fmfe/lib-sql

Usage

There are two ways of passing configuration information:

Use config to manage our profile.

Suppose you have a config directory under our project directory, config directory there is a dev.json file.

config/dev.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

{
"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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { mysql } = require('@fmfe/lib-sql');

const mysqlPool = mysql.init();

const _getNewSqlParamEntity = mysql._getNewSqlParamEntity;

// execute a single sql statement
// mysql.exec (mysqlPool, sql, the params);
the async function Exec () {
const SQL1 = 'SELECT * from limit ?? 2';
const = Data mysql.exec the await (mysqlPool, SQL1, [ 'the tbl_user']);
}

// Perform mysql transaction, it can be passed in a plurality of add / delete / change the sql statement
// mysql.exectrans (mysqlpool, sqlParamsEntity);
the async execTrans function () {
const sqlParamsEntity = [];
const SQL1 = 'INSERT INTO ?? ( name, Age, Sex) values (,,)??? ';
const the param1 = [' the tbl_user ',' AAA ', 20 is,. 1];
sqlParamsEntity.push (_getNewSqlParamEntity (SQL1, the param1));

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

SQL3 = const 'Update ?? SET ID = WHERE Age =??';
const Param3 = [ 'the tbl_user', 10,. 1];
sqlParamsEntity.push (_getNewS large column  mysql, sql server and the like connected to the integrated database library qlParamEntity (sql3, Param3));
// ....
const = Data mysql.execTrans the await (mysqlPool, sqlParamsEntity);
}

mssql.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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即可自动获取到数据库相关配置.

通过传入配置文件来调用库

我们引用上边的代码示例,只需做一点改动:

mysql.js

只需在初始化时传入mysql数据库配置就好.

1
2
3
4
5
6
7
8
9
10
11
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

在每次调用方法时传入配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
}

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11874406.html