@pi-r/mssql 0.7.2 → 0.7.4

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,7 +1,7 @@
1
- ### @pi-r/mssql
2
-
3
- https://e-mc.readthedocs.io/en/latest/db/mssql.html
4
-
5
- ### LICENSE
6
-
1
+ ### @pi-r/mssql
2
+
3
+ https://e-mc.readthedocs.io/en/latest/db/mssql.html
4
+
5
+ ### LICENSE
6
+
7
7
  MIT
package/client/index.js CHANGED
@@ -171,15 +171,58 @@ function setCredential(item) {
171
171
  options.port ||= +port;
172
172
  }
173
173
  }
174
- if (username || password) {
175
- authOptions.userName ||= username;
176
- authOptions.password ||= password;
174
+ let flags = 0;
175
+ switch (auth.type) {
176
+ case 'default':
177
+ flags = 1;
178
+ break;
179
+ case 'ntlm':
180
+ flags = 1;
181
+ if (!(0, types_1.isString)(authOptions.domain)) {
182
+ errors.push('authentication.options.domain');
183
+ }
184
+ break;
185
+ case 'azure-active-directory-password':
186
+ flags = 1 | 4;
187
+ break;
188
+ case 'azure-active-directory-access-token':
189
+ if (!(0, types_1.isString)(authOptions.token)) {
190
+ errors.push('authentication.options.token');
191
+ }
192
+ break;
193
+ case 'azure-active-directory-msi-vm':
194
+ flags = 8;
195
+ break;
196
+ case 'azure-active-directory-msi-app-service':
197
+ flags = 4 | 8;
198
+ if (!(0, types_1.isString)(authOptions.msiSecret)) {
199
+ errors.push('authentication.options.msiSecret');
200
+ }
201
+ break;
202
+ case 'azure-active-directory-service-principal-secret':
203
+ flags = 4;
204
+ if (!(0, types_1.isString)(authOptions.clientSecret)) {
205
+ errors.push('authentication.options.clientSecret');
206
+ }
207
+ break;
208
+ }
209
+ if (flags & 1) {
210
+ if (username || password) {
211
+ authOptions.userName ||= username;
212
+ authOptions.password ||= password;
213
+ }
214
+ if (!(0, types_1.isString)(authOptions.userName)) {
215
+ errors.push('authentication.options.userName');
216
+ }
217
+ if (!(0, types_1.isString)(authOptions.password)) {
218
+ errors.push('authentication.options.password');
219
+ }
177
220
  }
178
- if (!authOptions.userName) {
179
- errors.push('authentication.options.userName');
221
+ if ((flags & 4) && !(0, types_1.isString)(authOptions.clientId)) {
222
+ errors.push('authentication.options.clientId');
180
223
  }
181
- if (!authOptions.password) {
182
- errors.push('authentication.options.password');
224
+ if ((flags & 8) && !(0, types_1.isString)(authOptions.msiEndpoint)) {
225
+ errors.push('authentication.options.msiEndpoint');
183
226
  }
184
227
  if (errors.length) {
185
228
  throw (0, types_1.errorMessage)("mssql", 'Not defined - ' + errors.join(' | '));
package/client/pool.js ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ const util_1 = require("@e-mc/db/util");
3
+ const types_1 = require("@e-mc/types");
4
+ const DbPool = require('@e-mc/db/pool');
5
+ class MSSQLPool extends DbPool {
6
+ static asString(credential) {
7
+ const auth = credential.authentication?.options;
8
+ if (!credential.server || !(auth?.userName && auth.password)) {
9
+ return super.asString(credential);
10
+ }
11
+ const options = credential.options;
12
+ let tls;
13
+ if (options && (tls = options.cryptoCredentialsDetails)) {
14
+ if (!(0, types_1.isObject)(tls)) {
15
+ delete options.cryptoCredentialsDetails;
16
+ }
17
+ else if (tls.ca || tls.cert || tls.key) {
18
+ credential = { ...credential };
19
+ if ('uuidKey' in credential) {
20
+ delete credential.uuidKey;
21
+ }
22
+ credential.options = { ...options };
23
+ delete credential.options.cryptoCredentialsDetails;
24
+ return JSON.stringify(credential) + '_tls';
25
+ }
26
+ }
27
+ if ('uuidKey' in credential) {
28
+ credential = { ...credential };
29
+ delete credential.uuidKey;
30
+ }
31
+ return JSON.stringify(credential);
32
+ }
33
+ async getConnection() {
34
+ return new Promise((resolve, reject) => {
35
+ this.client.acquire((err, connection) => {
36
+ if (err) {
37
+ reject(err);
38
+ }
39
+ else {
40
+ resolve(connection);
41
+ }
42
+ });
43
+ });
44
+ }
45
+ async close() {
46
+ return new Promise((resolve, reject) => {
47
+ try {
48
+ this.client.drain();
49
+ resolve();
50
+ }
51
+ catch (err) {
52
+ reject(err);
53
+ }
54
+ });
55
+ }
56
+ isEmpty() {
57
+ return this.closed || (0, util_1.checkEmpty)(this.client.connections) && (0, util_1.checkEmpty)(this.client.waiting);
58
+ }
59
+ get closed() {
60
+ return !!this.client.drained;
61
+ }
62
+ }
63
+ module.exports = MSSQLPool;
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
- {
2
- "name": "@pi-r/mssql",
3
- "version": "0.7.2",
4
- "description": "MSSQL client driver for E-mc.",
5
- "main": "client/index.js",
6
- "types": "client/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/anpham6/pi-r.git",
13
- "directory": "src/db/mssql"
14
- },
15
- "keywords": [
16
- "squared",
17
- "e-mc",
18
- "squared-functions"
19
- ],
20
- "author": "An Pham <anpham6@gmail.com>",
21
- "license": "MIT",
22
- "homepage": "https://github.com/anpham6/pi-r#readme",
23
- "dependencies": {
24
- "@e-mc/db": "^0.9.4",
25
- "@e-mc/types": "^0.9.4",
26
- "tedious": "^16.7.1",
27
- "tedious-connection-pool2": "^2.1.0"
28
- }
29
- }
1
+ {
2
+ "name": "@pi-r/mssql",
3
+ "version": "0.7.4",
4
+ "description": "MSSQL client driver for E-mc.",
5
+ "main": "client/index.js",
6
+ "types": "client/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/anpham6/pi-r.git",
13
+ "directory": "src/db/mssql"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "e-mc",
18
+ "squared-functions"
19
+ ],
20
+ "author": "An Pham <anpham6@gmail.com>",
21
+ "license": "MIT",
22
+ "homepage": "https://github.com/anpham6/pi-r#readme",
23
+ "dependencies": {
24
+ "@e-mc/db": "^0.9.12",
25
+ "@e-mc/types": "^0.9.12",
26
+ "tedious": "^16.7.1",
27
+ "tedious-connection-pool2": "^2.1.0"
28
+ }
29
+ }
package/types/index.d.ts CHANGED
@@ -1,30 +1,30 @@
1
- import type { DbDataSource } from '@e-mc/types/lib/squared';
2
-
3
- import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
4
-
5
- import type { ConnectionConfiguration, TYPES } from 'tedious';
6
- import type { DataType, ParameterData } from 'tedious/lib/data-type';
7
-
8
- import type { PoolConfig } from './pool';
9
-
10
- export interface MSSQLDataSource extends DbDataSource<string, unknown, unknown, MSSQLCredential, string | PoolConfig>, ExecuteAction<MSSQLRequestParameters | MSSQLRequestWithOutputParameters> {
11
- source: "mssql";
12
- storedProc?: boolean;
13
- }
14
-
15
- export interface MSSQLCredential extends ServerAuth, Omit<ConnectionConfiguration, "server"> {}
16
-
17
- export interface MSSQLRequestWithOutputParameters {
18
- input?: MSSQLRequestParameters;
19
- output?: MSSQLRequestParameters;
20
- }
21
-
22
- export interface MSSQLRequestParameterValue {
23
- name?: string;
24
- value?: unknown;
25
- type?: DataType;
26
- options?: ParameterData;
27
- }
28
-
29
- export type MSSQLRequestParameters = Null<MSSQLRequestParameterValue[] | ObjectMap<Omit<MSSQLRequestParameterValue, "name">>>;
1
+ import type { DbDataSource } from '@e-mc/types/lib/squared';
2
+
3
+ import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
4
+
5
+ import type { ConnectionConfiguration, TYPES } from 'tedious';
6
+ import type { DataType, ParameterData } from 'tedious/lib/data-type';
7
+
8
+ import type { PoolConfig } from './pool';
9
+
10
+ export interface MSSQLDataSource extends DbDataSource<string, unknown, unknown, MSSQLCredential, string | PoolConfig>, ExecuteAction<MSSQLRequestParameters | MSSQLRequestWithOutputParameters> {
11
+ source: "mssql";
12
+ storedProc?: boolean;
13
+ }
14
+
15
+ export interface MSSQLCredential extends ServerAuth, Omit<ConnectionConfiguration, "server"> {}
16
+
17
+ export interface MSSQLRequestWithOutputParameters {
18
+ input?: MSSQLRequestParameters;
19
+ output?: MSSQLRequestParameters;
20
+ }
21
+
22
+ export interface MSSQLRequestParameterValue {
23
+ name?: string;
24
+ value?: unknown;
25
+ type?: DataType;
26
+ options?: ParameterData;
27
+ }
28
+
29
+ export type MSSQLRequestParameters = Null<MSSQLRequestParameterValue[] | ObjectMap<Omit<MSSQLRequestParameterValue, "name">>>;
30
30
  export type MSSQLDataType = keyof typeof TYPES;
package/types/pool.d.ts CHANGED
@@ -1,69 +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
-
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
69
  export = Pool;