@pi-r/mssql 0.6.7 → 0.7.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
@@ -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
@@ -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 => 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
+ });
61
63
  }
62
64
  }
63
65
  this[output ? 'addOutputParameter' : 'addParameter'](name, type, value, options);
@@ -95,27 +97,26 @@ 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.length = Infinity;
105
+ if (Array.isArray(options)) {
106
+ options.length = Infinity;
107
+ }
106
108
  return sql.VarBinary;
107
109
  }
108
110
  default:
109
- return sql.Null;
111
+ return sql.VarChar;
110
112
  }
111
113
  }
112
114
  function getPoolKey(credential) {
113
- var _a, _b;
114
- const auth = (_a = credential.authentication) === null || _a === void 0 ? void 0 : _a.options;
115
- if (!credential.server || !((auth === null || auth === void 0 ? void 0 : auth.userName) && auth.password)) {
115
+ const auth = credential.authentication?.options;
116
+ if (!credential.server || !(auth?.userName && auth.password)) {
116
117
  return;
117
118
  }
118
- const tls = (_b = credential.options) === null || _b === void 0 ? void 0 : _b.cryptoCredentialsDetails;
119
+ const tls = credential.options?.cryptoCredentialsDetails;
119
120
  if (tls) {
120
121
  if (!(0, types_1.isObject)(tls)) {
121
122
  delete credential.options.cryptoCredentialsDetails;
@@ -138,7 +139,6 @@ function getPoolKey(credential) {
138
139
  }
139
140
  const isOutputParameters = (params) => (0, types_1.isPlainObject)(params) && Object.keys(params).length === 2 && 'input' in params && 'output' in params;
140
141
  function setCredential(item) {
141
- var _a, _b, _c, _d;
142
142
  let credential = this.getCredential(item), hostname, username, password, port, database;
143
143
  if (credential) {
144
144
  ({ username, password, hostname, port, database } = (0, util_1.parseServerAuth)(credential, true));
@@ -156,24 +156,24 @@ function setCredential(item) {
156
156
  }
157
157
  const errors = [];
158
158
  if (hostname) {
159
- credential.server || (credential.server = hostname);
159
+ credential.server ||= hostname;
160
160
  }
161
161
  if (!credential.server) {
162
162
  errors.push('server');
163
163
  }
164
- const auth = credential.authentication || (credential.authentication = { options: {} });
165
- const authOptions = auth.options || (auth.options = {});
166
- auth.type || (auth.type = 'default');
164
+ const auth = credential.authentication ||= { options: {} };
165
+ const authOptions = auth.options ||= {};
166
+ auth.type ||= 'default';
167
167
  if (database || port) {
168
- const options = credential.options || (credential.options = {});
169
- options.database || (options.database = database);
168
+ const options = credential.options ||= {};
169
+ options.database ||= database;
170
170
  if (port) {
171
- options.port || (options.port = +port);
171
+ options.port ||= +port;
172
172
  }
173
173
  }
174
174
  if (username || password) {
175
- authOptions.userName || (authOptions.userName = username);
176
- authOptions.password || (authOptions.password = password);
175
+ authOptions.userName ||= username;
176
+ authOptions.password ||= password;
177
177
  }
178
178
  if (!authOptions.userName) {
179
179
  errors.push('authentication.options.userName');
@@ -223,16 +223,16 @@ function setCredential(item) {
223
223
  if (config) {
224
224
  const { min, max, idle, queue_idle } = config;
225
225
  if (min >= 0) {
226
- (_a = poolConfig.min) !== null && _a !== void 0 ? _a : (poolConfig.min = min);
226
+ poolConfig.min ??= min;
227
227
  }
228
228
  if (max > 0) {
229
- (_b = poolConfig.max) !== null && _b !== void 0 ? _b : (poolConfig.max = max);
229
+ poolConfig.max ??= max;
230
230
  }
231
231
  if (idle >= 0) {
232
- (_c = poolConfig.idleTimeout) !== null && _c !== void 0 ? _c : (poolConfig.idleTimeout = idle);
232
+ poolConfig.idleTimeout ??= idle;
233
233
  }
234
234
  if (queue_idle >= 0) {
235
- (_d = poolConfig.acquireTimeout) !== null && _d !== void 0 ? _d : (poolConfig.acquireTimeout = queue_idle);
235
+ poolConfig.acquireTimeout ??= queue_idle;
236
236
  }
237
237
  }
238
238
  const poolKey = getPoolKey(credential);
@@ -248,7 +248,6 @@ async function executeQuery(item, options) {
248
248
  }
249
249
  exports.executeQuery = executeQuery;
250
250
  async function executeBatchQuery(batch, options = '', outResult) {
251
- var _a;
252
251
  const length = batch.length;
253
252
  if (length === 0) {
254
253
  return [];
@@ -271,7 +270,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
271
270
  parallel = !batch.some(item => item.parallel === false);
272
271
  }
273
272
  if (!parallel) {
274
- outResult || (outResult = new Array(length));
273
+ outResult ||= new Array(length);
275
274
  }
276
275
  const caching = this.hasCache("mssql", sessionKey);
277
276
  const tasks = new Array(length);
@@ -338,7 +337,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
338
337
  item.transactionState &= ~64;
339
338
  return client;
340
339
  };
341
- if ((_a = this.host) === null || _a === void 0 ? void 0 : _a.username) {
340
+ if (this.host?.username) {
342
341
  this.applyCommand(...batch);
343
342
  }
344
343
  for (let i = 0; i < length; ++i) {
@@ -362,7 +361,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
362
361
  continue;
363
362
  }
364
363
  item.transactionState = 1;
365
- 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;
366
365
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
367
366
  let params = item.params, queryString = '';
368
367
  if (caching && ignoreCache !== true && !streamRow) {
@@ -464,26 +463,32 @@ async function executeBatchQuery(batch, options = '', outResult) {
464
463
  }
465
464
  rows.push(result);
466
465
  })
467
- .on('returnValue', (name, value) => (output || (output = {}))[name] = value)
466
+ .on('returnValue', (name, value) => (output ||= {})[name] = value)
468
467
  .on('done', rowCount => {
469
- if (resume && rowCount >= 0) {
470
- ++recordset;
468
+ if (rowCount !== undefined) {
469
+ if (resume && rowCount >= 0) {
470
+ ++recordset;
471
+ }
472
+ resume = false;
471
473
  }
472
- resume = false;
473
474
  })
474
475
  .on('doneProc', rowCount => {
475
- if (resume && rowCount >= 0) {
476
- ++recordset;
476
+ if (rowCount !== undefined) {
477
+ if (resume && rowCount >= 0) {
478
+ ++recordset;
479
+ }
480
+ resume = false;
477
481
  }
478
- resume = false;
479
482
  })
480
483
  .on('doneInProc', rowCount => {
481
- if (resume && rowCount >= 0) {
482
- ++recordset;
484
+ if (rowCount !== undefined) {
485
+ if (resume && rowCount >= 0) {
486
+ ++recordset;
487
+ }
488
+ resume = false;
483
489
  }
484
- resume = false;
485
490
  })
486
- .on('error', err => error || (error = err));
491
+ .on('error', err => error ||= err);
487
492
  if (item.storedProc) {
488
493
  client.callProcedure(request);
489
494
  }
@@ -512,8 +517,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
512
517
  }
513
518
  return this.processRows(batch, tasks, {
514
519
  disconnect: () => {
515
- clients.forEach(item => item.close());
516
- pools.forEach(item => item.release());
520
+ clients.forEach(item => {
521
+ item.close();
522
+ });
523
+ pools.forEach(item => {
524
+ item.release();
525
+ });
517
526
  },
518
527
  parallel
519
528
  }, outResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mssql",
3
- "version": "0.6.7",
3
+ "version": "0.7.1",
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.8",
25
- "@e-mc/types": "^0.8.8",
24
+ "@e-mc/db": "^0.9.2",
25
+ "@e-mc/types": "^0.9.2",
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;