@pi-r/mssql 0.6.5 → 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 CHANGED
@@ -1,10 +1,7 @@
1
- # @pi-r/mssql
1
+ ### @pi-r/mssql
2
2
 
3
- ## Documentation
3
+ https://e-mc.readthedocs.io/en/latest/db/mssql.html
4
4
 
5
- - [E-mc](https://e-mc.readthedocs.io/en/latest/db/mssql.html)
6
- - [squared](https://squared.readthedocs.io)
7
-
8
- ## LICENSE
5
+ ### LICENSE
9
6
 
10
7
  MIT
package/client/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
4
3
  const tedious = require("tedious");
5
4
  const types_1 = require("@e-mc/types");
@@ -58,7 +57,9 @@ function convertParameters(sql, params, output) {
58
57
  }
59
58
  const columns = value.columns;
60
59
  if ((0, types_1.isArray)(columns)) {
61
- columns.forEach(col => col.type = convertType(sql, col.type, col.value, col.options || (col.options = {})));
60
+ columns.forEach(col => {
61
+ col.type = convertType(sql, col.type, col.value, col.options ||= {});
62
+ });
62
63
  }
63
64
  }
64
65
  this[output ? 'addOutputParameter' : 'addParameter'](name, type, value, options);
@@ -96,18 +97,18 @@ function convertType(sql, type, value, options) {
96
97
  return sql.Int;
97
98
  }
98
99
  return sql.BigInt;
99
- case 'string':
100
- return sql.VarChar;
101
100
  case 'object':
102
101
  if (value instanceof Date) {
103
102
  return sql.DateTime2;
104
103
  }
105
104
  if (Buffer.isBuffer(value)) {
106
- options.length = Infinity;
105
+ if (Array.isArray(options)) {
106
+ options.length = Infinity;
107
+ }
107
108
  return sql.VarBinary;
108
109
  }
109
110
  default:
110
- return sql.Null;
111
+ return sql.VarChar;
111
112
  }
112
113
  }
113
114
  function getPoolKey(credential) {
@@ -155,24 +156,24 @@ function setCredential(item) {
155
156
  }
156
157
  const errors = [];
157
158
  if (hostname) {
158
- credential.server || (credential.server = hostname);
159
+ credential.server ||= hostname;
159
160
  }
160
161
  if (!credential.server) {
161
162
  errors.push('server');
162
163
  }
163
- const auth = credential.authentication || (credential.authentication = { options: {} });
164
- const authOptions = auth.options || (auth.options = {});
165
- auth.type || (auth.type = 'default');
164
+ const auth = credential.authentication ||= { options: {} };
165
+ const authOptions = auth.options ||= {};
166
+ auth.type ||= 'default';
166
167
  if (database || port) {
167
- const options = credential.options || (credential.options = {});
168
- options.database || (options.database = database);
168
+ const options = credential.options ||= {};
169
+ options.database ||= database;
169
170
  if (port) {
170
- options.port || (options.port = +port);
171
+ options.port ||= +port;
171
172
  }
172
173
  }
173
174
  if (username || password) {
174
- authOptions.userName || (authOptions.userName = username);
175
- authOptions.password || (authOptions.password = password);
175
+ authOptions.userName ||= username;
176
+ authOptions.password ||= password;
176
177
  }
177
178
  if (!authOptions.userName) {
178
179
  errors.push('authentication.options.userName');
@@ -222,16 +223,16 @@ function setCredential(item) {
222
223
  if (config) {
223
224
  const { min, max, idle, queue_idle } = config;
224
225
  if (min >= 0) {
225
- poolConfig.min ?? (poolConfig.min = min);
226
+ poolConfig.min ??= min;
226
227
  }
227
228
  if (max > 0) {
228
- poolConfig.max ?? (poolConfig.max = max);
229
+ poolConfig.max ??= max;
229
230
  }
230
231
  if (idle >= 0) {
231
- poolConfig.idleTimeout ?? (poolConfig.idleTimeout = idle);
232
+ poolConfig.idleTimeout ??= idle;
232
233
  }
233
234
  if (queue_idle >= 0) {
234
- poolConfig.acquireTimeout ?? (poolConfig.acquireTimeout = queue_idle);
235
+ poolConfig.acquireTimeout ??= queue_idle;
235
236
  }
236
237
  }
237
238
  const poolKey = getPoolKey(credential);
@@ -269,7 +270,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
269
270
  parallel = !batch.some(item => item.parallel === false);
270
271
  }
271
272
  if (!parallel) {
272
- outResult || (outResult = new Array(length));
273
+ outResult ||= new Array(length);
273
274
  }
274
275
  const caching = this.hasCache("mssql", sessionKey);
275
276
  const tasks = new Array(length);
@@ -360,7 +361,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
360
361
  continue;
361
362
  }
362
363
  item.transactionState = 1;
363
- const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mssql", 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
364
+ const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mssql", 'options', credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
364
365
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
365
366
  let params = item.params, queryString = '';
366
367
  if (caching && ignoreCache !== true && !streamRow) {
@@ -462,26 +463,32 @@ async function executeBatchQuery(batch, options = '', outResult) {
462
463
  }
463
464
  rows.push(result);
464
465
  })
465
- .on('returnValue', (name, value) => (output || (output = {}))[name] = value)
466
+ .on('returnValue', (name, value) => (output ||= {})[name] = value)
466
467
  .on('done', rowCount => {
467
- if (resume && rowCount >= 0) {
468
- ++recordset;
468
+ if (rowCount !== undefined) {
469
+ if (resume && rowCount >= 0) {
470
+ ++recordset;
471
+ }
472
+ resume = false;
469
473
  }
470
- resume = false;
471
474
  })
472
475
  .on('doneProc', rowCount => {
473
- if (resume && rowCount >= 0) {
474
- ++recordset;
476
+ if (rowCount !== undefined) {
477
+ if (resume && rowCount >= 0) {
478
+ ++recordset;
479
+ }
480
+ resume = false;
475
481
  }
476
- resume = false;
477
482
  })
478
483
  .on('doneInProc', rowCount => {
479
- if (resume && rowCount >= 0) {
480
- ++recordset;
484
+ if (rowCount !== undefined) {
485
+ if (resume && rowCount >= 0) {
486
+ ++recordset;
487
+ }
488
+ resume = false;
481
489
  }
482
- resume = false;
483
490
  })
484
- .on('error', err => error || (error = err));
491
+ .on('error', err => error ||= err);
485
492
  if (item.storedProc) {
486
493
  client.callProcedure(request);
487
494
  }
@@ -510,8 +517,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
510
517
  }
511
518
  return this.processRows(batch, tasks, {
512
519
  disconnect: () => {
513
- clients.forEach(item => item.close());
514
- pools.forEach(item => item.release());
520
+ clients.forEach(item => {
521
+ item.close();
522
+ });
523
+ pools.forEach(item => {
524
+ item.release();
525
+ });
515
526
  },
516
527
  parallel
517
528
  }, outResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mssql",
3
- "version": "0.6.5",
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.8.6",
25
- "@e-mc/types": "^0.8.6",
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
- // @ts-ignore
6
- import type { ConnectionConfig, ParameterOptions, TediousType, TediousTypes } from 'tedious';
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
- export interface MSSQLDataSource extends DbDataSource<string, unknown, unknown, string | MSSQLCredential, string | PoolConfig>, ExecuteAction<MSSQLRequestParameters | MSSQLRequestWithOutputParameters> {
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, ConnectionConfig {}
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?: keyof TediousTypes | TediousType;
26
- options?: ParameterOptions;
25
+ type?: DataType;
26
+ options?: ParameterData;
27
27
  }
28
28
 
29
- export type MSSQLRequestParameters = MSSQLRequestParameterValue[] | Record<string, Omit<MSSQLRequestParameterValue, "name">> | null;
29
+ export type MSSQLRequestParameters = Null<MSSQLRequestParameterValue[] | ObjectMap<Omit<MSSQLRequestParameterValue, "name">>>;
30
+ export type MSSQLDataType = keyof typeof TYPES;
@@ -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;