@pi-r/mssql 0.6.6 → 0.7.0
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 +3 -6
- package/client/index.js +45 -33
- package/package.json +3 -3
- package/types/index.d.ts +10 -9
- package/types/pool.d.ts +69 -0
package/README.md
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
### @pi-r/mssql
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
https://e-mc.readthedocs.io/en/latest/db/mssql.html
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- [squared](https://squared.readthedocs.io)
|
|
7
|
-
|
|
8
|
-
## LICENSE
|
|
5
|
+
### LICENSE
|
|
9
6
|
|
|
10
7
|
MIT
|
package/client/index.js
CHANGED
|
@@ -57,7 +57,9 @@ function convertParameters(sql, params, output) {
|
|
|
57
57
|
}
|
|
58
58
|
const columns = value.columns;
|
|
59
59
|
if ((0, types_1.isArray)(columns)) {
|
|
60
|
-
columns.forEach(col =>
|
|
60
|
+
columns.forEach(col => {
|
|
61
|
+
col.type = convertType(sql, col.type, col.value, col.options ||= {});
|
|
62
|
+
});
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
this[output ? 'addOutputParameter' : 'addParameter'](name, type, value, options);
|
|
@@ -95,18 +97,18 @@ function convertType(sql, type, value, options) {
|
|
|
95
97
|
return sql.Int;
|
|
96
98
|
}
|
|
97
99
|
return sql.BigInt;
|
|
98
|
-
case 'string':
|
|
99
|
-
return sql.VarChar;
|
|
100
100
|
case 'object':
|
|
101
101
|
if (value instanceof Date) {
|
|
102
102
|
return sql.DateTime2;
|
|
103
103
|
}
|
|
104
104
|
if (Buffer.isBuffer(value)) {
|
|
105
|
-
options
|
|
105
|
+
if (Array.isArray(options)) {
|
|
106
|
+
options.length = Infinity;
|
|
107
|
+
}
|
|
106
108
|
return sql.VarBinary;
|
|
107
109
|
}
|
|
108
110
|
default:
|
|
109
|
-
return sql.
|
|
111
|
+
return sql.VarChar;
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
114
|
function getPoolKey(credential) {
|
|
@@ -154,24 +156,24 @@ function setCredential(item) {
|
|
|
154
156
|
}
|
|
155
157
|
const errors = [];
|
|
156
158
|
if (hostname) {
|
|
157
|
-
credential.server
|
|
159
|
+
credential.server ||= hostname;
|
|
158
160
|
}
|
|
159
161
|
if (!credential.server) {
|
|
160
162
|
errors.push('server');
|
|
161
163
|
}
|
|
162
|
-
const auth = credential.authentication
|
|
163
|
-
const authOptions = auth.options
|
|
164
|
-
auth.type
|
|
164
|
+
const auth = credential.authentication ||= { options: {} };
|
|
165
|
+
const authOptions = auth.options ||= {};
|
|
166
|
+
auth.type ||= 'default';
|
|
165
167
|
if (database || port) {
|
|
166
|
-
const options = credential.options
|
|
167
|
-
options.database
|
|
168
|
+
const options = credential.options ||= {};
|
|
169
|
+
options.database ||= database;
|
|
168
170
|
if (port) {
|
|
169
|
-
options.port
|
|
171
|
+
options.port ||= +port;
|
|
170
172
|
}
|
|
171
173
|
}
|
|
172
174
|
if (username || password) {
|
|
173
|
-
authOptions.userName
|
|
174
|
-
authOptions.password
|
|
175
|
+
authOptions.userName ||= username;
|
|
176
|
+
authOptions.password ||= password;
|
|
175
177
|
}
|
|
176
178
|
if (!authOptions.userName) {
|
|
177
179
|
errors.push('authentication.options.userName');
|
|
@@ -221,16 +223,16 @@ function setCredential(item) {
|
|
|
221
223
|
if (config) {
|
|
222
224
|
const { min, max, idle, queue_idle } = config;
|
|
223
225
|
if (min >= 0) {
|
|
224
|
-
poolConfig.min
|
|
226
|
+
poolConfig.min ??= min;
|
|
225
227
|
}
|
|
226
228
|
if (max > 0) {
|
|
227
|
-
poolConfig.max
|
|
229
|
+
poolConfig.max ??= max;
|
|
228
230
|
}
|
|
229
231
|
if (idle >= 0) {
|
|
230
|
-
poolConfig.idleTimeout
|
|
232
|
+
poolConfig.idleTimeout ??= idle;
|
|
231
233
|
}
|
|
232
234
|
if (queue_idle >= 0) {
|
|
233
|
-
poolConfig.acquireTimeout
|
|
235
|
+
poolConfig.acquireTimeout ??= queue_idle;
|
|
234
236
|
}
|
|
235
237
|
}
|
|
236
238
|
const poolKey = getPoolKey(credential);
|
|
@@ -268,7 +270,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
268
270
|
parallel = !batch.some(item => item.parallel === false);
|
|
269
271
|
}
|
|
270
272
|
if (!parallel) {
|
|
271
|
-
outResult
|
|
273
|
+
outResult ||= new Array(length);
|
|
272
274
|
}
|
|
273
275
|
const caching = this.hasCache("mssql", sessionKey);
|
|
274
276
|
const tasks = new Array(length);
|
|
@@ -359,7 +361,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
359
361
|
continue;
|
|
360
362
|
}
|
|
361
363
|
item.transactionState = 1;
|
|
362
|
-
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mssql", 'options',
|
|
364
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mssql", 'options', credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
|
|
363
365
|
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
364
366
|
let params = item.params, queryString = '';
|
|
365
367
|
if (caching && ignoreCache !== true && !streamRow) {
|
|
@@ -461,26 +463,32 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
461
463
|
}
|
|
462
464
|
rows.push(result);
|
|
463
465
|
})
|
|
464
|
-
.on('returnValue', (name, value) => (output
|
|
466
|
+
.on('returnValue', (name, value) => (output ||= {})[name] = value)
|
|
465
467
|
.on('done', rowCount => {
|
|
466
|
-
if (
|
|
467
|
-
|
|
468
|
+
if (rowCount !== undefined) {
|
|
469
|
+
if (resume && rowCount >= 0) {
|
|
470
|
+
++recordset;
|
|
471
|
+
}
|
|
472
|
+
resume = false;
|
|
468
473
|
}
|
|
469
|
-
resume = false;
|
|
470
474
|
})
|
|
471
475
|
.on('doneProc', rowCount => {
|
|
472
|
-
if (
|
|
473
|
-
|
|
476
|
+
if (rowCount !== undefined) {
|
|
477
|
+
if (resume && rowCount >= 0) {
|
|
478
|
+
++recordset;
|
|
479
|
+
}
|
|
480
|
+
resume = false;
|
|
474
481
|
}
|
|
475
|
-
resume = false;
|
|
476
482
|
})
|
|
477
483
|
.on('doneInProc', rowCount => {
|
|
478
|
-
if (
|
|
479
|
-
|
|
484
|
+
if (rowCount !== undefined) {
|
|
485
|
+
if (resume && rowCount >= 0) {
|
|
486
|
+
++recordset;
|
|
487
|
+
}
|
|
488
|
+
resume = false;
|
|
480
489
|
}
|
|
481
|
-
resume = false;
|
|
482
490
|
})
|
|
483
|
-
.on('error', err => error
|
|
491
|
+
.on('error', err => error ||= err);
|
|
484
492
|
if (item.storedProc) {
|
|
485
493
|
client.callProcedure(request);
|
|
486
494
|
}
|
|
@@ -509,8 +517,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
509
517
|
}
|
|
510
518
|
return this.processRows(batch, tasks, {
|
|
511
519
|
disconnect: () => {
|
|
512
|
-
clients.forEach(item =>
|
|
513
|
-
|
|
520
|
+
clients.forEach(item => {
|
|
521
|
+
item.close();
|
|
522
|
+
});
|
|
523
|
+
pools.forEach(item => {
|
|
524
|
+
item.release();
|
|
525
|
+
});
|
|
514
526
|
},
|
|
515
527
|
parallel
|
|
516
528
|
}, outResult);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mssql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "MSSQL client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
24
|
+
"@e-mc/db": "^0.9.0",
|
|
25
|
+
"@e-mc/types": "^0.9.0",
|
|
26
26
|
"tedious": "^16.7.1",
|
|
27
27
|
"tedious-connection-pool2": "^2.1.0"
|
|
28
28
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -2,17 +2,17 @@ import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
|
2
2
|
|
|
3
3
|
import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
import type { PoolConfig } from 'tedious-connection-pool';
|
|
5
|
+
import type { ConnectionConfiguration, TYPES } from 'tedious';
|
|
6
|
+
import type { DataType, ParameterData } from 'tedious/lib/data-type';
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
import type { PoolConfig } from './pool';
|
|
9
|
+
|
|
10
|
+
export interface MSSQLDataSource extends DbDataSource<string, unknown, unknown, MSSQLCredential, string | PoolConfig>, ExecuteAction<MSSQLRequestParameters | MSSQLRequestWithOutputParameters> {
|
|
11
11
|
source: "mssql";
|
|
12
12
|
storedProc?: boolean;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export interface MSSQLCredential extends ServerAuth,
|
|
15
|
+
export interface MSSQLCredential extends ServerAuth, Omit<ConnectionConfiguration, "server"> {}
|
|
16
16
|
|
|
17
17
|
export interface MSSQLRequestWithOutputParameters {
|
|
18
18
|
input?: MSSQLRequestParameters;
|
|
@@ -22,8 +22,9 @@ export interface MSSQLRequestWithOutputParameters {
|
|
|
22
22
|
export interface MSSQLRequestParameterValue {
|
|
23
23
|
name?: string;
|
|
24
24
|
value?: unknown;
|
|
25
|
-
type?:
|
|
26
|
-
options?:
|
|
25
|
+
type?: DataType;
|
|
26
|
+
options?: ParameterData;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
export type MSSQLRequestParameters = MSSQLRequestParameterValue[] |
|
|
29
|
+
export type MSSQLRequestParameters = Null<MSSQLRequestParameterValue[] | ObjectMap<Omit<MSSQLRequestParameterValue, "name">>>;
|
|
30
|
+
export type MSSQLDataType = keyof typeof TYPES;
|
package/types/pool.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/* eslint @typescript-eslint/consistent-type-imports: "off" */
|
|
2
|
+
|
|
3
|
+
import type { Connection, ConnectionConfiguration } from 'tedious';
|
|
4
|
+
|
|
5
|
+
import type * as EventEmitter from 'events';
|
|
6
|
+
|
|
7
|
+
declare class Pool extends EventEmitter {
|
|
8
|
+
/**
|
|
9
|
+
* Connection Pool constructor
|
|
10
|
+
* @param poolConfig the pool configuration
|
|
11
|
+
* @param connectionConfig the connection configuration
|
|
12
|
+
*/
|
|
13
|
+
constructor(poolConfig: Pool.PoolConfig, connectionConfig: ConnectionConfiguration);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* acquires a connection from the pool
|
|
17
|
+
* @param callback invoked when the connection is retrieved and ready
|
|
18
|
+
*/
|
|
19
|
+
acquire(callback: (err: Error, connection: Pool.PooledConnection) => void): void;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* closes opened connections
|
|
23
|
+
*/
|
|
24
|
+
drain(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare namespace Pool {
|
|
28
|
+
|
|
29
|
+
export class PooledConnection extends Connection {
|
|
30
|
+
/**
|
|
31
|
+
* If the connection is issued from a connection pool returns the connection to the pool.
|
|
32
|
+
*/
|
|
33
|
+
release(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PoolConfig {
|
|
37
|
+
/**
|
|
38
|
+
* Minimum concurrent connections
|
|
39
|
+
*/
|
|
40
|
+
min?: number | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Maximum concurrent connections
|
|
44
|
+
*/
|
|
45
|
+
max?: number | undefined;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Defines if logging is activated
|
|
49
|
+
*/
|
|
50
|
+
log?: boolean | undefined;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Idle timeout
|
|
54
|
+
*/
|
|
55
|
+
idleTimeout?: number | undefined;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Retry delay
|
|
59
|
+
*/
|
|
60
|
+
retryDelay?: number | undefined;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Acquire timeout
|
|
64
|
+
*/
|
|
65
|
+
acquireTimeout?: number | undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export = Pool;
|