@things-factory/integration-base 7.0.10 → 7.0.13
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/assets/images/mssql-procedure-example1.png +0 -0
- package/dist-server/engine/connector/mssql-connector.d.ts +10 -0
- package/dist-server/engine/connector/mssql-connector.js +42 -11
- package/dist-server/engine/connector/mssql-connector.js.map +1 -1
- package/dist-server/engine/task/index.d.ts +1 -0
- package/dist-server/engine/task/index.js +1 -0
- package/dist-server/engine/task/index.js.map +1 -1
- package/dist-server/engine/task/mssql-procedure.d.ts +1 -0
- package/dist-server/engine/task/mssql-procedure.js +94 -0
- package/dist-server/engine/task/mssql-procedure.js.map +1 -0
- package/dist-server/engine/task/oracle-procedure.js +4 -1
- package/dist-server/engine/task/oracle-procedure.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/helps/integration/task/mqtt-subscribe.ms.md +127 -11
- package/helps/integration/task/mssql-procedure.ja.md +105 -0
- package/helps/integration/task/mssql-procedure.ko.md +89 -0
- package/helps/integration/task/mssql-procedure.md +89 -0
- package/helps/integration/task/mssql-procedure.ms.md +89 -0
- package/helps/integration/task/mssql-procedure.zh.md +103 -0
- package/helps/integration/task/oracle-procedure.ko.md +0 -2
- package/package.json +2 -2
- package/server/engine/connector/mssql-connector.ts +43 -12
- package/server/engine/task/index.ts +1 -0
- package/server/engine/task/mssql-procedure.ts +128 -0
- package/server/engine/task/oracle-procedure.ts +4 -1
@@ -5,7 +5,7 @@ import { Connector } from '../types'
|
|
5
5
|
import { InputConnection } from '../../service/connection/connection-type'
|
6
6
|
|
7
7
|
try {
|
8
|
-
var
|
8
|
+
var mssql = require('mssql')
|
9
9
|
} catch (err) {
|
10
10
|
logger.error('mssql module loading failed', err)
|
11
11
|
}
|
@@ -20,24 +20,23 @@ export class MssqlConnector implements Connector {
|
|
20
20
|
async connect(connection: InputConnection) {
|
21
21
|
const {
|
22
22
|
endpoint,
|
23
|
-
params: { user, password, database, encrypt = 'Y', trustServerCertificate }
|
23
|
+
params: { user, password, database, poolMin = 0, poolMax = 4, encrypt = 'Y', trustServerCertificate }
|
24
24
|
} = connection
|
25
25
|
|
26
26
|
const [host, port = 1433] = endpoint.split(':')
|
27
27
|
|
28
|
-
if (!
|
28
|
+
if (!mssql) {
|
29
29
|
throw new Error('mssql module loading failed')
|
30
30
|
}
|
31
31
|
|
32
|
-
const
|
33
|
-
// client is pool
|
32
|
+
const pool = await mssql.connect({
|
34
33
|
user,
|
35
34
|
password,
|
36
35
|
server: host,
|
37
36
|
database,
|
38
37
|
pool: {
|
39
|
-
max:
|
40
|
-
min: 0,
|
38
|
+
max: Math.max(2, poolMax),
|
39
|
+
min: Math.max(0, poolMin),
|
41
40
|
idleTimeoutMillis: 30000
|
42
41
|
},
|
43
42
|
options: {
|
@@ -48,12 +47,30 @@ export class MssqlConnector implements Connector {
|
|
48
47
|
|
49
48
|
ConnectionManager.addConnectionInstance(connection, {
|
50
49
|
query: async (query, params) => {
|
51
|
-
var result = await
|
50
|
+
var result = await pool.request().query(query)
|
52
51
|
return result.recordset
|
53
|
-
|
54
|
-
// return (await client.query(query))
|
55
52
|
},
|
56
|
-
|
53
|
+
execute: async (procedure, params) => {
|
54
|
+
let request = pool.request()
|
55
|
+
|
56
|
+
Object.keys(params).forEach(key => {
|
57
|
+
let { dir, type, val } = params[key]
|
58
|
+
|
59
|
+
if (dir === 'in' || dir === 'inout') {
|
60
|
+
request.input(key, type, val)
|
61
|
+
} else if (dir === 'out') {
|
62
|
+
request.output(key, type)
|
63
|
+
} else {
|
64
|
+
console.error(`Invalid parameter direction(${dir}) for mssql stored procedure`)
|
65
|
+
}
|
66
|
+
})
|
67
|
+
|
68
|
+
var result = await request.execute(procedure)
|
69
|
+
return result
|
70
|
+
},
|
71
|
+
close: () => {
|
72
|
+
pool.close()
|
73
|
+
}
|
57
74
|
})
|
58
75
|
|
59
76
|
ConnectionManager.logger.info(`MSSql Database(${connection.name}:${database}) at ${endpoint} connected.`)
|
@@ -97,6 +114,20 @@ export class MssqlConnector implements Connector {
|
|
97
114
|
options: ['Y', 'N']
|
98
115
|
}
|
99
116
|
},
|
117
|
+
{
|
118
|
+
type: 'number',
|
119
|
+
name: 'poolMin',
|
120
|
+
placeholder: 'minimum connection-pool size',
|
121
|
+
label: 'pool-min',
|
122
|
+
value: 0
|
123
|
+
},
|
124
|
+
{
|
125
|
+
type: 'number',
|
126
|
+
name: 'poolMax',
|
127
|
+
placeholder: 'maximum connection-pool size',
|
128
|
+
label: 'pool-max',
|
129
|
+
value: 4
|
130
|
+
},
|
100
131
|
{
|
101
132
|
type: 'select',
|
102
133
|
name: 'trustServerCertificate',
|
@@ -109,7 +140,7 @@ export class MssqlConnector implements Connector {
|
|
109
140
|
}
|
110
141
|
|
111
142
|
get taskPrefixes() {
|
112
|
-
return ['database']
|
143
|
+
return ['database', 'mssql']
|
113
144
|
}
|
114
145
|
|
115
146
|
get help() {
|
@@ -0,0 +1,128 @@
|
|
1
|
+
import { logger } from '@things-factory/env'
|
2
|
+
import { access } from '@things-factory/utils'
|
3
|
+
import { ConnectionManager } from '../connection-manager'
|
4
|
+
import { TaskRegistry } from '../task-registry'
|
5
|
+
import { InputStep } from '../../service/step/step-type'
|
6
|
+
import { Context } from '../types'
|
7
|
+
import 'ses'
|
8
|
+
|
9
|
+
try {
|
10
|
+
var mssql = require('mssql')
|
11
|
+
} catch (err) {
|
12
|
+
logger.error('mssql module loading failed', err)
|
13
|
+
}
|
14
|
+
|
15
|
+
type ProcedureParameterType = {
|
16
|
+
name: string
|
17
|
+
dir: string
|
18
|
+
type: string
|
19
|
+
val?: any
|
20
|
+
accessor?: string
|
21
|
+
maxSize?: number
|
22
|
+
}
|
23
|
+
|
24
|
+
type ValueType = {
|
25
|
+
code?: string
|
26
|
+
procedure?: string
|
27
|
+
parameters?: ProcedureParameterType[]
|
28
|
+
}
|
29
|
+
|
30
|
+
const DIR = {
|
31
|
+
In: 'in',
|
32
|
+
Out: 'out',
|
33
|
+
Inout: 'inout' /* 초기값이 있는 out 파라미터 */
|
34
|
+
}
|
35
|
+
|
36
|
+
const NUMBER_TYPES = [
|
37
|
+
'TINYINT',
|
38
|
+
'SMALLINT',
|
39
|
+
'INT',
|
40
|
+
'BIGINT',
|
41
|
+
'FLOAT',
|
42
|
+
'READ',
|
43
|
+
'DECIMAL',
|
44
|
+
'NUMERIC',
|
45
|
+
'MONEY',
|
46
|
+
'SMALLMONEY'
|
47
|
+
]
|
48
|
+
|
49
|
+
const DATE_TYPES = ['DATE', 'TIME', 'DATETIME', 'SMALLDATETIME', 'DATETIME2']
|
50
|
+
const PARAMETERIZED_STRINGS = ['NCHAR', 'NVARCHAR', 'NTEXT', 'DECIMAL', 'NUMERIC']
|
51
|
+
|
52
|
+
async function MssqlProcedure(step: InputStep, context: Context) {
|
53
|
+
var { domain, user, data, variables, lng } = context
|
54
|
+
var { connection: connectionName, params } = step
|
55
|
+
|
56
|
+
var { code = '', procedure = '', parameters = [] } = params.parameters as ValueType
|
57
|
+
|
58
|
+
var dbconnection = ConnectionManager.getConnectionInstanceByName(domain, connectionName)
|
59
|
+
|
60
|
+
if (!code) {
|
61
|
+
throw 'procedure code not defined'
|
62
|
+
}
|
63
|
+
|
64
|
+
const compartment = new Compartment({
|
65
|
+
domain,
|
66
|
+
user,
|
67
|
+
lng,
|
68
|
+
data,
|
69
|
+
variables,
|
70
|
+
console
|
71
|
+
})
|
72
|
+
|
73
|
+
let evalCode
|
74
|
+
try {
|
75
|
+
evalCode = compartment.evaluate('`' + code + '`')
|
76
|
+
} catch (err) {
|
77
|
+
throw new Error(`Failed to evaluate code: ${err.message}`)
|
78
|
+
}
|
79
|
+
|
80
|
+
code = evalCode
|
81
|
+
|
82
|
+
const procedureParameters =
|
83
|
+
parameters &&
|
84
|
+
parameters.reduce((sum, { name, val, dir, type, accessor, maxSize }) => {
|
85
|
+
sum[name] = {
|
86
|
+
dir: DIR[dir],
|
87
|
+
type
|
88
|
+
}
|
89
|
+
|
90
|
+
const calculated = accessor ? access(accessor, data) || val : val
|
91
|
+
|
92
|
+
if (calculated !== undefined) {
|
93
|
+
sum[name].val = NUMBER_TYPES.includes(type)
|
94
|
+
? Number(calculated)
|
95
|
+
: DATE_TYPES.includes(type)
|
96
|
+
? new Date(calculated)
|
97
|
+
: String(calculated)
|
98
|
+
}
|
99
|
+
|
100
|
+
if ((dir == DIR.In || dir == DIR.Inout) && maxSize > 0 && type == 'String') {
|
101
|
+
sum[name].type = mssql?.VarChar(maxSize)
|
102
|
+
sum[name].maxSize = maxSize
|
103
|
+
}
|
104
|
+
|
105
|
+
return sum
|
106
|
+
}, {})
|
107
|
+
|
108
|
+
const result = await dbconnection.execute(procedure, procedureParameters)
|
109
|
+
|
110
|
+
return {
|
111
|
+
data: result
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
MssqlProcedure.parameterSpec = [
|
116
|
+
{
|
117
|
+
type: 'procedure-parameters',
|
118
|
+
name: 'parameters',
|
119
|
+
label: '',
|
120
|
+
property: {
|
121
|
+
dbtype: 'mssql'
|
122
|
+
}
|
123
|
+
}
|
124
|
+
]
|
125
|
+
|
126
|
+
MssqlProcedure.help = 'integration/task/mssql-procedure'
|
127
|
+
|
128
|
+
TaskRegistry.registerTaskHandler('mssql-procedure', MssqlProcedure)
|