@e-mc/db 0.9.6 → 0.10.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
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.6/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.10.0/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DbDataSource } from "./squared";
@@ -73,7 +73,7 @@ interface IDbPool {
73
73
  uuidKey: AuthValue | null;
74
74
  add(item: DbDataSource, uuidKey?: string): this;
75
75
  has(item: DbDataSource): boolean;
76
- getConnection(): Promise<unknown>;
76
+ getConnection(credential?: unknown): Promise<unknown>;
77
77
  remove(): void;
78
78
  detach(force?: boolean): Promise<void>;
79
79
  close(): Promise<void>;
@@ -87,6 +87,10 @@ interface IDbPool {
87
87
  }
88
88
 
89
89
  interface DbPoolConstructor {
90
+ CACHE_UNUSED: readonly string[];
91
+ asString(credential: unknown): string;
92
+ sanitize(credential: unknown): unknown;
93
+ removeUUIDKey(credential: unknown): unknown;
90
94
  findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
91
95
  validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
92
96
  checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
@@ -200,11 +204,11 @@ const [rows1, rows2] = await instance.executeBatchQuery([
200
204
 
201
205
  ## References
202
206
 
203
- - https://www.unpkg.com/@e-mc/types@0.9.6/lib/squared.d.ts
204
- - https://www.unpkg.com/@e-mc/types@0.9.6/lib/core.d.ts
205
- - https://www.unpkg.com/@e-mc/types@0.9.6/lib/db.d.ts
206
- - https://www.unpkg.com/@e-mc/types@0.9.6/lib/http.d.ts
207
- - https://www.unpkg.com/@e-mc/types@0.9.6/lib/settings.d.ts
207
+ - https://www.unpkg.com/@e-mc/types@0.10.0/lib/squared.d.ts
208
+ - https://www.unpkg.com/@e-mc/types@0.10.0/lib/core.d.ts
209
+ - https://www.unpkg.com/@e-mc/types@0.10.0/lib/db.d.ts
210
+ - https://www.unpkg.com/@e-mc/types@0.10.0/lib/http.d.ts
211
+ - https://www.unpkg.com/@e-mc/types@0.10.0/lib/settings.d.ts
208
212
 
209
213
  * https://www.npmjs.com/package/@types/node
210
214
 
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { DbConstructor } from '../types/lib';
2
-
3
- declare const Db: DbConstructor;
4
-
1
+ import type { DbConstructor } from '../types/lib';
2
+
3
+ declare const Db: DbConstructor;
4
+
5
5
  export = Db;
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  const path = require("path");
3
- const types_1 = require("@e-mc/types");
4
3
  const core_1 = require("@e-mc/core");
5
4
  const request_1 = require("@e-mc/request");
5
+ const types_1 = require("@e-mc/types");
6
6
  const util_1 = require("@e-mc/db/util");
7
7
  const DB_CLIENT = new Map();
8
8
  const POOL_CONFIG = new Map();
@@ -17,12 +17,12 @@ function sanitizePoolConfig(value) {
17
17
  value.socket_timeout ?? (value.socket_timeout = -1);
18
18
  return value;
19
19
  }
20
- function setCert(items, cache) {
20
+ function setCert(instance, items, cache) {
21
21
  if (Array.isArray(items)) {
22
- return items.map(cert => (0, types_1.isString)(cert) ? this.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
22
+ return items.map(cert => (0, types_1.isString)(cert) ? instance.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
23
23
  }
24
24
  if ((0, types_1.isString)(items)) {
25
- return this.readTLSCert(items, cache);
25
+ return instance.readTLSCert(items, cache);
26
26
  }
27
27
  }
28
28
  class Db extends core_1.ClientDb {
@@ -128,12 +128,7 @@ class Db extends core_1.ClientDb {
128
128
  if (typeof options === 'function') {
129
129
  options = { errorQuery: options };
130
130
  }
131
- try {
132
- return this.getClient(item.source).executeQuery.call(this, item, options);
133
- }
134
- catch (err) {
135
- return Promise.reject(err);
136
- }
131
+ return this.getClient(item.source).executeQuery.call(this, item, options);
137
132
  }
138
133
  async executeBatchQuery(batch, options, outResult) {
139
134
  if (this.aborted) {
@@ -142,12 +137,7 @@ class Db extends core_1.ClientDb {
142
137
  if (typeof options === 'function') {
143
138
  options = { errorQuery: options };
144
139
  }
145
- try {
146
- return this.getClient(batch[0].source).executeBatchQuery.call(this, batch, options, outResult);
147
- }
148
- catch (err) {
149
- return Promise.reject(err);
150
- }
140
+ return this.getClient(batch[0].source).executeBatchQuery.call(this, batch, options, outResult);
151
141
  }
152
142
  async processRows(batch, tasks, parallel, outResult) {
153
143
  let disconnect;
@@ -198,7 +188,7 @@ class Db extends core_1.ClientDb {
198
188
  handleFail(err, item, options) {
199
189
  this.add(item, 32);
200
190
  item.transactionFail = true;
201
- if (options && typeof options.errorQuery === 'function') {
191
+ if (typeof options?.errorQuery === 'function') {
202
192
  if (options.errorQuery(err, item, options.commandType)) {
203
193
  if (item.willAbort) {
204
194
  this.abort(err);
@@ -243,10 +233,10 @@ class Db extends core_1.ClientDb {
243
233
  if ((0, types_1.isPlainObject)(options)) {
244
234
  const { ca, cert, key } = options;
245
235
  if (ca) {
246
- options.ca = setCert.call(this, ca);
236
+ options.ca = setCert(this, ca);
247
237
  }
248
238
  if (cert) {
249
- options.cert = setCert.call(this, cert);
239
+ options.cert = setCert(this, cert);
250
240
  }
251
241
  if (key) {
252
242
  if ((0, types_1.isString)(key)) {
@@ -261,7 +251,7 @@ class Db extends core_1.ClientDb {
261
251
  resolveSource(source, folder) {
262
252
  let result;
263
253
  if (!source.startsWith('@')) {
264
- result = this.settings.imports?.[source] || util_1.IMPORTS[source];
254
+ result = this.settings.imports?.[source] || types_1.IMPORT_MAP[source];
265
255
  }
266
256
  else if (!folder && !source.includes('/')) {
267
257
  folder = 'client';
@@ -316,5 +306,4 @@ Db.STORE_RESULT_PARTITION_SIZE = 16;
316
306
  Db.STORE_RESULT_PARTITION_MULT = 2;
317
307
  Object.freeze(types_1.DB_TYPE);
318
308
  Object.freeze(util_1.SQL_COMMAND);
319
-
320
309
  module.exports = Db;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/db",
3
- "version": "0.9.6",
3
+ "version": "0.10.0",
4
4
  "description": "DB modules for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.9.6",
24
- "@e-mc/request": "0.9.6",
25
- "@e-mc/types": "0.9.6"
23
+ "@e-mc/core": "0.10.0",
24
+ "@e-mc/request": "0.10.0",
25
+ "@e-mc/types": "0.10.0"
26
26
  }
27
27
  }
package/pool.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { DbPoolConstructor } from '../types/lib/db';
2
-
3
- declare const DbPool: DbPoolConstructor;
4
-
1
+ import type { DbPoolConstructor } from '../types/lib/db';
2
+
3
+ declare const DbPool: DbPoolConstructor;
4
+
5
5
  export = DbPool;
package/pool.js CHANGED
@@ -1,11 +1,34 @@
1
1
  "use strict";
2
2
  var _a, _b, _c;
3
+ const crypto = require("crypto");
3
4
  const types_1 = require("@e-mc/types");
4
5
  const util_1 = require("@e-mc/db/util");
5
6
  const kItems = Symbol('items');
6
7
  const kParent = Symbol('parent');
7
8
  const kIdlePrevious = Symbol('idlePrevious');
8
9
  class DbPool {
10
+ static asString(credential) {
11
+ return crypto.randomUUID();
12
+ }
13
+ static sanitize(credential) {
14
+ const properties = this.CACHE_UNUSED;
15
+ let result;
16
+ for (let i = 0, length = properties.length; i < length; ++i) {
17
+ const attr = properties[i];
18
+ if (attr in credential) {
19
+ result || (result = { ...credential });
20
+ result[attr] = undefined;
21
+ }
22
+ }
23
+ return result || credential;
24
+ }
25
+ static removeUUIDKey(credential) {
26
+ if ('uuidKey' in credential) {
27
+ credential = { ...credential };
28
+ delete credential.uuidKey;
29
+ }
30
+ return credential;
31
+ }
9
32
  static findKey(pools, uuidKey, poolKey, ...items) {
10
33
  if (uuidKey) {
11
34
  let pool;
@@ -20,7 +43,7 @@ class DbPool {
20
43
  }
21
44
  if (pool || poolKey && (pool = pools[poolKey])) {
22
45
  if (!pool.closed) {
23
- return items.length && !items.some(item => pool.has(item)) ? null : pool;
46
+ return items.length > 0 && !items.some(item => pool.has(item)) ? null : pool;
24
47
  }
25
48
  pool.remove();
26
49
  }
@@ -136,5 +159,5 @@ class DbPool {
136
159
  }
137
160
  }
138
161
  _a = kItems, _b = kParent, _c = kIdlePrevious;
139
-
162
+ DbPool.CACHE_UNUSED = [];
140
163
  module.exports = DbPool;
package/util.d.ts CHANGED
@@ -5,6 +5,7 @@ import type { AuthValue } from '../types/lib/http';
5
5
 
6
6
  declare namespace util {
7
7
  const SQL_COMMAND: SQL_COMMAND;
8
+ /** @deprecated Types.IMPORT_MAP */
8
9
  const IMPORTS: Record<string, string | undefined>;
9
10
  function getBasicAuth(auth: AuthValue): string;
10
11
  function getBasicAuth(username: unknown, password?: unknown): string;
package/util.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
- exports.hasBasicAuth = exports.getBasicAuth = exports.setUUIDKey = exports.checkEmpty = exports.parseConnectionString = exports.parseServerAuth = exports.IMPORTS = exports.SQL_COMMAND = void 0;
2
+ exports.hasBasicAuth = exports.getBasicAuth = exports.IMPORTS = exports.SQL_COMMAND = void 0;
3
+ exports.parseServerAuth = parseServerAuth;
4
+ exports.parseConnectionString = parseConnectionString;
5
+ exports.checkEmpty = checkEmpty;
6
+ exports.setUUIDKey = setUUIDKey;
3
7
  const types_1 = require("@e-mc/types");
8
+ Object.defineProperty(exports, "IMPORTS", { enumerable: true, get: function () { return types_1.IMPORT_MAP; } });
4
9
  const util_1 = require("@e-mc/request/util");
5
10
  Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
6
11
  Object.defineProperty(exports, "hasBasicAuth", { enumerable: true, get: function () { return util_1.hasBasicAuth; } });
@@ -11,15 +16,6 @@ var SQL_COMMAND;
11
16
  SQL_COMMAND[SQL_COMMAND["UPDATE"] = 3] = "UPDATE";
12
17
  SQL_COMMAND[SQL_COMMAND["DELETE"] = 4] = "DELETE";
13
18
  })(SQL_COMMAND || (exports.SQL_COMMAND = SQL_COMMAND = {}));
14
- exports.IMPORTS = {
15
- "mongodb": "@pi-r/mongodb",
16
- "redis": "@pi-r/redis",
17
- "mysql": "@pi-r/mysql",
18
- "postgres": "@pi-r/postgres",
19
- "mssql": "@pi-r/mssql",
20
- "oracle": "@pi-r/oracle",
21
- "mariadb": "@pi-r/mariadb"
22
- };
23
19
  function parseServerAuth(credential, port, all) {
24
20
  if (typeof port === 'boolean') {
25
21
  all = port;
@@ -83,7 +79,6 @@ function parseServerAuth(credential, port, all) {
83
79
  }
84
80
  return { protocol, server, hostname, port, username, password, database };
85
81
  }
86
- exports.parseServerAuth = parseServerAuth;
87
82
  function parseConnectionString(value, scheme = 'http') {
88
83
  if (!/^[^:]+:\/\//.test(value)) {
89
84
  value = scheme + '://' + value;
@@ -97,11 +92,9 @@ function parseConnectionString(value, scheme = 'http') {
97
92
  return null;
98
93
  }
99
94
  }
100
- exports.parseConnectionString = parseConnectionString;
101
95
  function checkEmpty(value) {
102
96
  return value === undefined || value === null ? false : value === 0 || (value instanceof Set || value instanceof Map ? value.size === 0 : (Array.isArray(value) || (0, types_1.isObject)(value)) && value.length === 0);
103
97
  }
104
- exports.checkEmpty = checkEmpty;
105
98
  function setUUIDKey(item, value) {
106
99
  var _a;
107
100
  if ((0, types_1.isString)(value)) {
@@ -111,4 +104,3 @@ function setUUIDKey(item, value) {
111
104
  (_a = item.credential).uuidKey || (_a.uuidKey = value);
112
105
  }
113
106
  }
114
- exports.setUUIDKey = setUUIDKey;