@e-mc/db 0.9.9 → 0.10.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
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.9/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.10.1/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.9/lib/squared.d.ts
204
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/core.d.ts
205
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/db.d.ts
206
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/http.d.ts
207
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/settings.d.ts
207
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/squared.d.ts
208
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/core.d.ts
209
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/db.d.ts
210
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/http.d.ts
211
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/settings.d.ts
208
212
 
209
213
  * https://www.npmjs.com/package/@types/node
210
214
 
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;
@@ -184,6 +174,12 @@ class Db extends core_1.ClientDb {
184
174
  }
185
175
  return Promise.all(tasks)
186
176
  .then(result => {
177
+ for (let i = 0, length = batch.length; i < length; ++i) {
178
+ const flat = batch[i].flat || 0;
179
+ if (flat > 0 && Array.isArray(result[i])) {
180
+ result[i] = result[i].flat(flat);
181
+ }
182
+ }
187
183
  terminate();
188
184
  if (outResult) {
189
185
  for (let i = 0, length = outResult.length; i < length; ++i) {
@@ -193,12 +189,12 @@ class Db extends core_1.ClientDb {
193
189
  }
194
190
  return result;
195
191
  })
196
- .catch(() => cleanup());
192
+ .catch(cleanup);
197
193
  }
198
194
  handleFail(err, item, options) {
199
195
  this.add(item, 32);
200
196
  item.transactionFail = true;
201
- if (options && typeof options.errorQuery === 'function') {
197
+ if (typeof options?.errorQuery === 'function') {
202
198
  if (options.errorQuery(err, item, options.commandType)) {
203
199
  if (item.willAbort) {
204
200
  this.abort(err);
@@ -243,10 +239,10 @@ class Db extends core_1.ClientDb {
243
239
  if ((0, types_1.isPlainObject)(options)) {
244
240
  const { ca, cert, key } = options;
245
241
  if (ca) {
246
- options.ca = setCert.call(this, ca);
242
+ options.ca = setCert(this, ca);
247
243
  }
248
244
  if (cert) {
249
- options.cert = setCert.call(this, cert);
245
+ options.cert = setCert(this, cert);
250
246
  }
251
247
  if (key) {
252
248
  if ((0, types_1.isString)(key)) {
@@ -261,7 +257,7 @@ class Db extends core_1.ClientDb {
261
257
  resolveSource(source, folder) {
262
258
  let result;
263
259
  if (!source.startsWith('@')) {
264
- result = this.settings.imports?.[source] || util_1.IMPORTS[source];
260
+ result = this.settings.imports?.[source] || types_1.IMPORT_MAP[source];
265
261
  }
266
262
  else if (!folder && !source.includes('/')) {
267
263
  folder = 'client';
@@ -316,5 +312,4 @@ Db.STORE_RESULT_PARTITION_SIZE = 16;
316
312
  Db.STORE_RESULT_PARTITION_MULT = 2;
317
313
  Object.freeze(types_1.DB_TYPE);
318
314
  Object.freeze(util_1.SQL_COMMAND);
319
-
320
315
  module.exports = Db;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/db",
3
- "version": "0.9.9",
3
+ "version": "0.10.1",
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.9",
24
- "@e-mc/request": "0.9.9",
25
- "@e-mc/types": "0.9.9"
23
+ "@e-mc/core": "0.10.1",
24
+ "@e-mc/request": "0.10.1",
25
+ "@e-mc/types": "0.10.1"
26
26
  }
27
27
  }
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;