clientlinker-flow-mysql 6.1.4 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@ npm i clientlinker-flow-mysql --save
24
24
  # Usage
25
25
 
26
26
  ```javascript
27
- var clientlinker = require('clientlinker');
27
+ var { clientlinker } = require('clientlinker');
28
28
  var linker = clientlinker({
29
29
  flows: ['mysql'],
30
30
  clients: {
@@ -46,7 +46,7 @@ var linker = clientlinker({
46
46
  }
47
47
  });
48
48
 
49
- linker.loadFlow('mysql', 'clientlinker-flow-mysql', module);
49
+ linker.flow('mysql', require('clientlinker-flow-mysql').mysqlFlow);
50
50
 
51
51
 
52
52
  // use
@@ -62,6 +62,13 @@ linker.run('mysqlCustomClient.subKeys', null, {
62
62
  ```
63
63
 
64
64
 
65
+ # Upgrade
66
+
67
+ ## 7.x
68
+
69
+ * The package entry no longer exports the installer function directly; it is now the named export `mysqlFlow`.
70
+ * Deep imports moved from `flow/*` to `dist/flow/*`; `dist/flow/mysql` exports `mysqlHandler` instead of `.flow`, and `translateValues` instead of `._test.translateValues`.
71
+
65
72
  [npm-image]: https://img.shields.io/npm/v/clientlinker-flow-mysql.svg
66
73
  [downloads-image]: https://img.shields.io/npm/dm/clientlinker-flow-mysql.svg
67
74
  [npm-url]: https://www.npmjs.org/package/clientlinker-flow-mysql
@@ -0,0 +1,2 @@
1
+ import type { Flow } from 'clientlinker';
2
+ export declare function mysqlFlow(flow: Flow): void;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mysqlFlow = mysqlFlow;
4
+ const mysql_1 = require("./mysql");
5
+ function mysqlFlow(flow) {
6
+ flow.run = mysql_1.mysqlHandler;
7
+ flow.methods = mysql_1.methods;
8
+ }
@@ -0,0 +1,4 @@
1
+ import type { Client, ClientRuntime, FlowRuntime } from 'clientlinker';
2
+ export declare function mysqlHandler(runtime: ClientRuntime, callback: FlowRuntime): Promise<unknown>;
3
+ export declare function methods(client: Client): Promise<string[] | undefined>;
4
+ export declare function translateValues(mainValues: any, mainKeys?: string[], subKeys?: Record<string, string[]>): unknown[];
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mysqlHandler = mysqlHandler;
7
+ exports.methods = methods;
8
+ exports.translateValues = translateValues;
9
+ const lodash_1 = require("lodash");
10
+ const fs_1 = require("fs");
11
+ const ini_1 = require("ini");
12
+ const debug_1 = __importDefault(require("debug"));
13
+ const mysql_1 = require("mysql");
14
+ const debug = (0, debug_1.default)('clientlinker-flow-mysql');
15
+ function mysqlHandler(runtime, callback) {
16
+ const client = runtime.client;
17
+ const options = client.options;
18
+ const handlerConfig = options.mysqlhandler && options.mysqlhandler[runtime.method];
19
+ if (!handlerConfig)
20
+ return callback.next();
21
+ return initPool(client).then(function (pool) {
22
+ if (!pool)
23
+ return callback.next();
24
+ return new Promise(function (resolve, reject) {
25
+ pool.getConnection(function (err, connection) {
26
+ if (err)
27
+ return reject(err);
28
+ const values = translateValues(runtime.body, handlerConfig.keys, handlerConfig.subKeys);
29
+ connection.query(handlerConfig.sql, values, function (err, results) {
30
+ if (err)
31
+ reject(err);
32
+ else
33
+ resolve(handlerConfig.onlyFirst
34
+ ? results && results[0]
35
+ : results);
36
+ connection.release();
37
+ });
38
+ });
39
+ });
40
+ });
41
+ }
42
+ function methods(client) {
43
+ return initPool(client).then(function (pool) {
44
+ if (!pool)
45
+ return;
46
+ const options = client.options;
47
+ if (options.mysqlhandler && typeof options.mysqlhandler == 'object') {
48
+ return Object.keys(options.mysqlhandler);
49
+ }
50
+ });
51
+ }
52
+ function initPool(client) {
53
+ if (client.cache.mysqlPool)
54
+ return Promise.resolve(client.cache.mysqlPool);
55
+ const options = client.options;
56
+ let getConfigPromise;
57
+ // 如果有配置文件,优先使用配置文件
58
+ if (options.mysqlConfigFile && options.mysqlConfigKey) {
59
+ getConfigPromise = fs_1.promises
60
+ .readFile(options.mysqlConfigFile, { encoding: 'utf8' })
61
+ .then(function (data) {
62
+ const json = (0, ini_1.parse)(data);
63
+ debug('config json:%o', json);
64
+ const config = json && json[options.mysqlConfigKey];
65
+ if (!config)
66
+ return options.mysql;
67
+ // 格式转化
68
+ // 运维的格式是固定的,非常给力
69
+ return (0, lodash_1.extend)({}, options.mysql, {
70
+ port: config.DBPort,
71
+ host: config.DBIP,
72
+ user: config.DBUser,
73
+ password: config.DBPass,
74
+ database: config.DBName
75
+ });
76
+ })
77
+ .catch(function (err) {
78
+ debug('read config err:%o', err);
79
+ return options.mysql;
80
+ });
81
+ }
82
+ else {
83
+ getConfigPromise = Promise.resolve(options.mysql);
84
+ }
85
+ return getConfigPromise.then(function (config) {
86
+ debug('mysql config:%o', config);
87
+ if (!config ||
88
+ !config.host ||
89
+ !config.user ||
90
+ !config.password ||
91
+ !config.database) {
92
+ return;
93
+ }
94
+ return (client.cache.mysqlPool ||
95
+ (client.cache.mysqlPool = (0, mysql_1.createPool)(config)));
96
+ });
97
+ }
98
+ function translateValues(mainValues, mainKeys, subKeys) {
99
+ const result = [];
100
+ if (!mainValues || !mainKeys)
101
+ return result;
102
+ mainKeys.forEach(function (name) {
103
+ const keys = subKeys && subKeys[name];
104
+ let value = mainValues[name];
105
+ if (keys) {
106
+ const isValArray = Array.isArray(value);
107
+ if (!isValArray)
108
+ value = [value];
109
+ const subVals = [];
110
+ value.forEach(function (params) {
111
+ if (!params)
112
+ return;
113
+ const tmpVal = [];
114
+ subVals.push(tmpVal);
115
+ keys.forEach(function (name) {
116
+ tmpVal.push(params[name]);
117
+ });
118
+ });
119
+ if (isValArray)
120
+ result.push(subVals);
121
+ else
122
+ result.push(subVals[0] || []);
123
+ }
124
+ else {
125
+ result.push(value);
126
+ }
127
+ });
128
+ return result;
129
+ }
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "clientlinker-flow-mysql",
3
- "version": "6.1.4",
3
+ "version": "7.0.1",
4
4
  "description": "Mysql flow for clientlinker",
5
- "main": "flow/index.js",
5
+ "main": "dist/flow/index.js",
6
+ "types": "dist/flow/index.d.ts",
6
7
  "scripts": {
7
- "test": "cross-env DEBUG=clientlinker-flow-mysql* mocha test/test_*"
8
+ "build": "tsc -p tsconfig.json",
9
+ "prepare": "npm run build",
10
+ "pretest": "npm run build",
11
+ "test": "cross-env DEBUG=clientlinker-flow-mysql* jest"
8
12
  },
9
13
  "keywords": [
10
14
  "clientlinker",
@@ -20,9 +24,13 @@
20
24
  "mysql": "^2.18.1"
21
25
  },
22
26
  "devDependencies": {
23
- "cross-env": "^7.0.3",
24
- "expect.js": "^0.3.1",
25
- "mocha": "^8.4.0"
27
+ "@types/debug": "^4.1.13",
28
+ "@types/ini": "^1.3.34",
29
+ "@types/lodash": "^4.17.28",
30
+ "@types/mysql": "^2.15.28",
31
+ "@types/node": "^20.19.43",
32
+ "clientlinker": "^14.0.1",
33
+ "cross-env": "^7.0.3"
26
34
  },
27
35
  "repository": {
28
36
  "type": "git",
@@ -32,5 +40,5 @@
32
40
  "url": "https://github.com/iGdea/node-clientlinker/issues"
33
41
  },
34
42
  "homepage": "https://github.com/iGdea/node-clientlinker/tree/master/packages/clientlinker-flow-mysql",
35
- "gitHead": "bad3b81f70101364868a5b2e7aaa84a1dcdcc85a"
43
+ "gitHead": "83da4b5802bfd2c2cfa6a4a14a1d8e74a6e3d029"
36
44
  }
package/flow/index.js DELETED
@@ -1,6 +0,0 @@
1
- const mysqlFlow = require('./mysql');
2
-
3
- module.exports = function(flow) {
4
- flow.run = mysqlFlow.flow;
5
- flow.methods = mysqlFlow.methods;
6
- };
package/flow/mysql.js DELETED
@@ -1,151 +0,0 @@
1
- const _ = require('lodash');
2
- const fs = require('fs').promises;
3
- const ini = require('ini');
4
- const debug = require('debug')('clientlinker-flow-mysql');
5
- const mysql = require('mysql');
6
-
7
- exports.flow = mysqlFlow;
8
- exports.methods = methods;
9
- exports._test = {};
10
-
11
- function mysqlFlow(runtime, callback) {
12
- const client = runtime.client;
13
- const options = client.options;
14
- // handlerConfig
15
- // [sql] sql 模版,见mysql包
16
- // [keys] sql的values对应runtime.body的转化顺序
17
- // [onlyFirst] 返回结果取第一条数据
18
- const handlerConfig =
19
- options.mysqlhandler && options.mysqlhandler[runtime.method];
20
- if (!handlerConfig) return callback.next();
21
-
22
- return initPool(client).then(function(pool) {
23
- if (!pool) return callback.next();
24
-
25
- return new Promise(function(resolve, reject) {
26
- pool.getConnection(function(err, connection) {
27
- if (err) return reject(err);
28
-
29
- const values = translateValues(
30
- runtime.body,
31
- handlerConfig.keys,
32
- handlerConfig.subKeys
33
- );
34
-
35
- connection.query(handlerConfig.sql, values, function(
36
- err,
37
- results
38
- ) {
39
- if (err) reject(err);
40
- else
41
- resolve(
42
- handlerConfig.onlyFirst
43
- ? results && results[0]
44
- : results
45
- );
46
-
47
- connection.release();
48
- });
49
- });
50
- });
51
- });
52
- }
53
-
54
- function methods(client) {
55
- return initPool(client).then(function(pool) {
56
- if (!pool) return;
57
-
58
- const options = client.options;
59
- if (options.mysqlhandler && typeof options.mysqlhandler == 'object') {
60
- return Object.keys(options.mysqlhandler);
61
- }
62
- });
63
- }
64
-
65
- function initPool(client) {
66
- if (client.cache.mysqlPool) return Promise.resolve(client.cache.mysqlPool);
67
-
68
- const options = client.options;
69
- let getConfigPromise;
70
-
71
- // 如果有配置文件,优先使用配置文件
72
- if (options.mysqlConfigFile && options.mysqlConfigKey) {
73
- getConfigPromise = fs
74
- .readFile(options.mysqlConfigFile, { encoding: 'utf8' })
75
- .then(function(data) {
76
- const json = ini.parse(data);
77
- debug('config json:%o', json);
78
- const config = json && json[options.mysqlConfigKey];
79
-
80
- if (!config) return options.mysql;
81
-
82
- // 格式转化
83
- // 运维的格式是固定的,非常给力
84
- return _.extend({}, options.mysql, {
85
- port: config.DBPort,
86
- host: config.DBIP,
87
- user: config.DBUser,
88
- password: config.DBPass,
89
- database: config.DBName
90
- });
91
- })
92
- .catch(function(err) {
93
- debug('read config err:%o', err);
94
-
95
- return options.mysql;
96
- });
97
- } else {
98
- getConfigPromise = Promise.resolve(options.mysql);
99
- }
100
-
101
- return getConfigPromise.then(function(config) {
102
- debug('mysql config:%o', config);
103
- if (
104
- !config ||
105
- !config.host ||
106
- !config.user ||
107
- !config.password ||
108
- !config.database
109
- ) {
110
- return;
111
- }
112
-
113
- return (
114
- client.cache.mysqlPool ||
115
- (client.cache.mysqlPool = mysql.createPool(config))
116
- );
117
- });
118
- }
119
-
120
- exports._test.translateValues = translateValues;
121
- function translateValues(mainValues, mainKeys, subKeys) {
122
- const result = [];
123
- if (!mainValues || !mainKeys) return result;
124
-
125
- mainKeys.forEach(function(name) {
126
- const keys = subKeys && subKeys[name];
127
- let value = mainValues[name];
128
- if (keys) {
129
- const isValArray = Array.isArray(value);
130
- if (!isValArray) value = [value];
131
-
132
- const subVals = [];
133
- value.forEach(function(params) {
134
- if (!params) return;
135
-
136
- const tmpVal = [];
137
- subVals.push(tmpVal);
138
- keys.forEach(function(name) {
139
- tmpVal.push(params[name]);
140
- });
141
- });
142
-
143
- if (isValArray) result.push(subVals);
144
- else result.push(subVals[0] || []);
145
- } else {
146
- result.push(value);
147
- }
148
- });
149
-
150
- return result;
151
- }