@e-mc/db 0.8.5 → 0.8.7

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/LICENSE CHANGED
@@ -1,7 +1,11 @@
1
- Copyright 2024 Mile Square Park
1
+ Copyright 2024 An Pham
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
4
 
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
6
 
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,7 +1,105 @@
1
1
  # @e-mc/db
2
2
 
3
- PEP 402
3
+ * NodeJS 14
4
+ * ES2020
5
+
6
+ ## General Usage
7
+
8
+ * [Read the Docs](https://e-mc.readthedocs.io)
9
+
10
+ ## Interface
11
+
12
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/index.d.ts
13
+
14
+ ```typescript
15
+ import type { DbDataSource } from "./squared";
16
+
17
+ import type { IHost } from "./index";
18
+ import type { ClientDbConstructor, IClientDb } from "./core";
19
+ import type { DB_TYPE, SQL_COMMAND, BatchQueryResult, ExecuteBatchQueryOptions, ExecuteQueryOptions, HandleFailOptions, PoolConfig, ProcessRowsOptions, QueryResult } from "./db";
20
+ import type { DbCoerceSettings, DbModule, DbSourceOptions } from "./settings";
21
+
22
+ import type { SecureContextOptions } from "tls";
23
+
24
+ interface IDb extends IClientDb<IHost, DbModule, DbDataSource, DbSourceOptions, DbCoerceSettings> {
25
+ setCredential(item: DbDataSource): Promise<void>;
26
+ getCredential(item: DbDataSource): Record<string | number | symbol, unknown>;
27
+ hasSource(source: string, ...type: number[]): boolean;
28
+ applyCommand(...items: DbDataSource[]): void;
29
+ executeQuery(item: DbDataSource, sessionKey: string): Promise<QueryResult>;
30
+ executeQuery(item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
31
+ executeBatchQuery(batch: DbDataSource[], sessionKey: string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
32
+ executeBatchQuery(batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
33
+ processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], parallel: boolean): Promise<BatchQueryResult>;
34
+ processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], options?: ProcessRowsOptions, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
35
+ handleFail(err: unknown, item: DbDataSource, options?: HandleFailOptions): boolean;
36
+ readTLSCert(value: unknown, cache?: boolean): string;
37
+ readTLSConfig(options: SecureContextOptions, cache?: boolean): void;
38
+ settingsOf(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
39
+ settingsOf(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
40
+ settingsKey(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
41
+ settingsKey(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
42
+ getPoolConfig(source: string, uuidKey?: string): Required<PoolConfig> | undefined;
43
+ get sourceType(): DB_TYPE;
44
+ get commandType(): SQL_COMMAND;
45
+ }
46
+
47
+ interface DbConstructor extends ClientDbConstructor<IHost> {
48
+ setPoolConfig(value: Record<string, PoolConfig>): void;
49
+ getPoolConfig(source: string): Required<PoolConfig> | undefined;
50
+ readonly prototype: IDb;
51
+ new(module?: DbModule, database?: DbDataSource[], ...args: unknown[]): IDb;
52
+ }
53
+
54
+ interface IDbSourceClient {
55
+ DB_SOURCE_NAME: string;
56
+ DB_SOURCE_CLIENT: boolean;
57
+ DB_SOURCE_TYPE: number;
58
+ setCredential(this: IDb, item: DbDataSource): Promise<void>;
59
+ executeQuery(this: IDb, item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
60
+ executeBatchQuery(this: IDb, batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
61
+ checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
62
+ }
63
+
64
+ interface IDbPool {
65
+ client: unknown;
66
+ lastAccessed: number;
67
+ success: number;
68
+ failed: number;
69
+ poolKey: string;
70
+ uuidKey: AuthValue | null;
71
+ add(item: DbDataSource, uuidKey?: string): this;
72
+ has(item: DbDataSource): boolean;
73
+ getConnection(): Promise<unknown>;
74
+ remove(): void;
75
+ detach(force?: boolean): Promise<void>;
76
+ close(): Promise<void>;
77
+ isIdle(timeout: number): boolean;
78
+ isEmpty(): boolean;
79
+ set connected(value: boolean);
80
+ get persist(): boolean;
81
+ get closed(): boolean;
82
+ get closeable(): boolean;
83
+ set parent(value: Record<string, IDbPool>);
84
+ }
85
+
86
+ interface DbPoolConstructor {
87
+ findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
88
+ validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
89
+ checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
90
+ readonly prototype: IDbPool;
91
+ new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
92
+ }
93
+ ```
94
+
95
+ ## References
96
+
97
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/squared.d.ts
98
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/core.d.ts
99
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/db.d.ts
100
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/object.d.ts
101
+ - https://www.unpkg.com/@e-mc/types@0.8.7/lib/settings.d.ts
4
102
 
5
103
  ## LICENSE
6
104
 
7
- MIT
105
+ BSD 3-Clause
package/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const path = require("path");
4
- const types_1 = require("../types");
5
- const core_1 = require("../core");
6
- const request_1 = require("../request");
7
- const util_1 = require("./util");
4
+ const types_1 = require("@e-mc/types");
5
+ const core_1 = require("@e-mc/core");
6
+ const request_1 = require("@e-mc/request");
7
+ const util_1 = require("@e-mc/db/util");
8
8
  const DB_CLIENT = new Map();
9
9
  const POOL_CONFIG = new Map();
10
10
  function sanitizePoolConfig(value) {
@@ -67,7 +67,7 @@ class Db extends core_1.ClientDb {
67
67
  return this.getClient(item.source).setCredential.call(this, item);
68
68
  }
69
69
  catch (err) {
70
- return Promise.reject(err instanceof Error ? err : new Error(Db.asString(err) || "Invalid credentials" /* ERR_DB.CREDENTIALS */));
70
+ return Promise.reject(err instanceof Error ? err : new Error(Db.asString(err) || "Invalid credentials"));
71
71
  }
72
72
  }
73
73
  getCredential(item) {
@@ -150,7 +150,7 @@ class Db extends core_1.ClientDb {
150
150
  ({ disconnect, parallel } = parallel);
151
151
  }
152
152
  const terminate = () => {
153
- this.applyState(batch, 8 /* DB_TRANSACTION.TERMINATE */);
153
+ this.applyState(batch, 8);
154
154
  if (typeof disconnect === 'function') {
155
155
  try {
156
156
  disconnect();
@@ -160,7 +160,7 @@ class Db extends core_1.ClientDb {
160
160
  }
161
161
  };
162
162
  const cleanup = () => {
163
- this.applyState(batch, 16 /* DB_TRANSACTION.ABORT */);
163
+ this.applyState(batch, 16);
164
164
  terminate();
165
165
  if (outResult) {
166
166
  for (let i = 0, length = outResult.length; i < length; ++i) {
@@ -191,7 +191,7 @@ class Db extends core_1.ClientDb {
191
191
  .catch(() => cleanup());
192
192
  }
193
193
  handleFail(err, item, options) {
194
- this.add(item, 32 /* DB_TRANSACTION.FAIL */);
194
+ this.add(item, 32);
195
195
  item.transactionFail = true;
196
196
  if (options && typeof options.errorQuery === 'function') {
197
197
  if (options.errorQuery(err, item, options.commandType)) {
@@ -205,7 +205,7 @@ class Db extends core_1.ClientDb {
205
205
  if (item.willAbort) {
206
206
  this.abort(err);
207
207
  }
208
- this.writeFail(["Unable to execute query" /* ERR_DB.EXEC_QUERY */, item.source], err, 65536 /* LOG_TYPE.DB */);
208
+ this.writeFail(["Unable to execute query", item.source], err, 65536);
209
209
  }
210
210
  return false;
211
211
  }
@@ -216,11 +216,11 @@ class Db extends core_1.ClientDb {
216
216
  const tasks = (items || this.pending).map(async (data) => {
217
217
  data.ignoreCache ?? (data.ignoreCache = true);
218
218
  return this.executeQuery(data).catch(() => {
219
- this.applyState([data], 16 /* DB_TRANSACTION.ABORT */);
219
+ this.applyState([data], 16);
220
220
  return [];
221
221
  });
222
222
  });
223
- return tasks.length === 0 ? false : this.allSettled(tasks, ["Execute unassigned queries" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
223
+ return tasks.length === 0 ? false : this.allSettled(tasks, ["Execute unassigned queries", this.moduleName]).then(result => result.length > 0).catch(() => false);
224
224
  }
225
225
  readTLSCert(value, cache) {
226
226
  if ((0, types_1.isString)(value)) {
@@ -304,11 +304,11 @@ class Db extends core_1.ClientDb {
304
304
  }
305
305
  catch {
306
306
  }
307
- throw (0, types_1.errorMessage)(source, "Database provider not found" /* ERR_DB.PROVIDER_NOTFOUND */);
307
+ throw (0, types_1.errorMessage)(source, "Database provider not found");
308
308
  }
309
309
  }
310
- Db.STORE_RESULT_PARTITION_SIZE = 16 /* CACHE_SIZE.DB_PARTITION_SIZE */;
311
- Db.STORE_RESULT_PARTITION_MULT = 2 /* CACHE_SIZE.DB_PARTITION_MULT */;
310
+ Db.STORE_RESULT_PARTITION_SIZE = 16;
311
+ Db.STORE_RESULT_PARTITION_MULT = 2;
312
312
  Object.freeze(types_1.DB_TYPE);
313
313
  Object.freeze(util_1.SQL_COMMAND);
314
314
  exports.default = Db;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/db",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "description": "DB modules for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -17,11 +17,11 @@
17
17
  "squared-functions"
18
18
  ],
19
19
  "author": "An Pham <anpham6@gmail.com>",
20
- "license": "MIT",
20
+ "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.8.5",
24
- "@e-mc/request": "0.8.5",
25
- "@e-mc/types": "0.8.5"
23
+ "@e-mc/core": "0.8.7",
24
+ "@e-mc/request": "0.8.7",
25
+ "@e-mc/types": "0.8.7"
26
26
  }
27
27
  }
package/pool.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  var _a, _b, _c;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- const types_1 = require("../types");
5
- const util_1 = require("./util");
4
+ const types_1 = require("@e-mc/types");
5
+ const util_1 = require("@e-mc/db/util");
6
6
  const kItems = Symbol('items');
7
7
  const kParent = Symbol('parent');
8
8
  const kIdlePrevious = Symbol('idlePrevious');
@@ -116,7 +116,7 @@ class DbPool {
116
116
  }
117
117
  previous.success = success;
118
118
  previous.failed = failed;
119
- return Date.now() - timeout >= this.lastAccessed && (this.closeable || count >= (this.uuidKey ? 10 /* IDLE_THRESHOLD.UUID */ : 5 /* IDLE_THRESHOLD.CONFIG */)) || error >= (this.uuidKey ? 3 /* IDLE_THRESHOLD.UUID_ERROR */ : 2 /* IDLE_THRESHOLD.CONFIG_ERROR */);
119
+ return Date.now() - timeout >= this.lastAccessed && (this.closeable || count >= (this.uuidKey ? 10 : 5)) || error >= (this.uuidKey ? 3 : 2);
120
120
  }
121
121
  get persist() {
122
122
  return this.uuidKey ? this.success > this.failed : false;
package/util.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hasBasicAuth = exports.getBasicAuth = exports.setUUIDKey = exports.checkEmpty = exports.parseConnectionString = exports.parseServerAuth = exports.IMPORTS = exports.SQL_COMMAND = void 0;
4
- const types_1 = require("../types");
5
- const util_1 = require("../request/util");
4
+ const types_1 = require("@e-mc/types");
5
+ const util_1 = require("@e-mc/request/util");
6
6
  Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
7
7
  Object.defineProperty(exports, "hasBasicAuth", { enumerable: true, get: function () { return util_1.hasBasicAuth; } });
8
8
  var SQL_COMMAND;