MySQLの、SQL Serverのデータベース接続およびその他の統合ライブラリ

住所:https://github.com/fmfe/lib-sql

インスト

1
$ NPM @ fmfe / libに-SQLをインストール

または

1
$糸アド@ fmfe / libに-SQL

使用法

コンフィギュレーション情報を渡すの2つの方法があります。

使用すると、configの私たちのプロフィールを管理すること。

dev.jsonファイルがあるあなたが私たちのプロジェクトディレクトリ下のconfigディレクトリがあると、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の":{
"ホスト": "127.0.0.1"、
"ポート":3306、
"データベース": "テスト"、
"ユーザー": "ルート"、
"パスワード": "123456"
}、
"MSSQL" :{
"ユーザ": "SA"、
"パスワード": "123456"、
"サーバ": "127.0.0.1"、
"データベース": "テスト"、
"ポート":1433、
"プール":{
"MIN" :0、
"最大":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の} =( '@ fmfe / LIB-SQL')を必要とします。

CONST mysqlPool = mysql.init()。

constの_getNewSqlParamEntity = mysql._getNewSqlParamEntity。

//単一のSQLステートメントを実行
// mysql.exec(mysqlPool、SQL、paramsは)を、
非同期関数Execの(){
CONST SQL1 = '限界^ 2 SELECT * FROM';
CONST =データのawait(mysqlPool、SQL1をmysql.exec [ 'tbl_user']);
}

// MySQLのトランザクションを実行し、それが追加/削除/変更するSQL文を複数に渡すことができる
// mysql.exectrans(mysqlpool、sqlParamsEntity);
非同期execTrans関数(){
CONST sqlParamsEntity = [];
CONST SQL1 =「INSERT INTOを??(名前、年齢、性別)の値(,,)??? ';
PARAM1 =定数[' tbl_user '' AAA」、20 ,. 1である];
sqlParamsEntity.push(_getNewSqlParamEntity(SQL1、PARAM1))。

constのSQL2 =「INSERT INTO ?? (名前、年齢、性別)の値'(?、?、?);
CONST PARAM2 = [ 'tbl_user'、 'BBB'、22、0]。
sqlParamsEntity.push(_getNewSqlParamEntity(SQL2、PARAM2))。

SQL3 =定数'更新?? SET ID =年齢= ??';
CONST Param3 = [ 'tbl_user'、10 ,. 1];
sqlParamsEntity.push(_getNewS 大きい列  のMySQL、SQLサーバと統合データベースライブラリに接続など qlParamEntity(SQL3、 Param3));
// ...
CONST =データmysql.execTransの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} =( '@ 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
}
}

非同期関数のexec(){
...
CONST =データのawait mssql.exec(SQL1、設定)
}

非同期関数exectrans(){ ... CONST =データのawait mssql.execTrans(sqlParamsEntity、設定) }




おすすめ

転載: www.cnblogs.com/liuzhongrong/p/11874406.html