@e-mc/db 0.0.3 → 0.1.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/index.js CHANGED
@@ -5,8 +5,8 @@ const types_1 = require("../types");
5
5
  const core_1 = require("../core");
6
6
  const request_1 = require("../request");
7
7
  const util_1 = require("./util");
8
- const DB_CLIENT = {};
9
- const POOL_CONFIG = {};
8
+ const DB_CLIENT = new Map();
9
+ const POOL_CONFIG = new Map();
10
10
  function sanitizePoolConfig(value) {
11
11
  value.min ?? (value.min = -1);
12
12
  value.max ?? (value.max = -1);
@@ -33,14 +33,13 @@ class Db extends core_1.ClientDb {
33
33
  static async purgeMemory(percent = 1, limit = 0, parent) {
34
34
  let result = 0;
35
35
  if (percent > 0 && percent <= 1) {
36
- for (const name in DB_CLIENT) {
37
- const client = DB_CLIENT[name];
38
- const { purge } = POOL_CONFIG[name];
39
- if (purge > 0) {
36
+ for (const [name, client] of DB_CLIENT) {
37
+ const config = POOL_CONFIG.get(name);
38
+ if (config && config.purge > 0) {
40
39
  const checkTimeout = client.checkTimeout?.bind(client);
41
40
  if (checkTimeout) {
42
41
  try {
43
- result += await checkTimeout(purge, limit);
42
+ result += await checkTimeout(config.purge, limit);
44
43
  }
45
44
  catch {
46
45
  }
@@ -54,12 +53,12 @@ class Db extends core_1.ClientDb {
54
53
  for (const name in value) {
55
54
  const source = value[name];
56
55
  if ((0, types_1.isPlainObject)(source)) {
57
- POOL_CONFIG[name] = sanitizePoolConfig(source);
56
+ POOL_CONFIG.set(name, sanitizePoolConfig(source));
58
57
  }
59
58
  }
60
59
  }
61
60
  static getPoolConfig(source) {
62
- return POOL_CONFIG[source];
61
+ return POOL_CONFIG.get(source);
63
62
  }
64
63
  setCredential(item) {
65
64
  try {
@@ -120,7 +119,7 @@ class Db extends core_1.ClientDb {
120
119
  ({ disconnect, parallel } = parallel);
121
120
  }
122
121
  const terminate = () => {
123
- this.applyState(batch, 8 /* TRANSACTION_DB.TERMINATE */);
122
+ this.applyState(batch, 8 /* DB_TRANSACTION.TERMINATE */);
124
123
  if (typeof disconnect === 'function') {
125
124
  try {
126
125
  disconnect();
@@ -130,7 +129,7 @@ class Db extends core_1.ClientDb {
130
129
  }
131
130
  };
132
131
  const cleanup = () => {
133
- this.applyState(batch, 16 /* TRANSACTION_DB.ABORT */);
132
+ this.applyState(batch, 16 /* DB_TRANSACTION.ABORT */);
134
133
  terminate();
135
134
  if (outResult) {
136
135
  for (let i = 0, length = outResult.length; i < length; ++i) {
@@ -161,7 +160,7 @@ class Db extends core_1.ClientDb {
161
160
  .catch(() => cleanup());
162
161
  }
163
162
  handleFail(err, item, options) {
164
- this.add(item, 32 /* TRANSACTION_DB.FAIL */);
163
+ this.add(item, 32 /* DB_TRANSACTION.FAIL */);
165
164
  item.transactionFail = true;
166
165
  if (options && typeof options.errorQuery === 'function') {
167
166
  if (options.errorQuery(err, item, options.commandType)) {
@@ -186,7 +185,7 @@ class Db extends core_1.ClientDb {
186
185
  const tasks = (items || this.pending).map(data => {
187
186
  data.ignoreCache ?? (data.ignoreCache = true);
188
187
  return this.executeQuery(data).catch(() => {
189
- this.applyState([data], 16 /* TRANSACTION_DB.ABORT */);
188
+ this.applyState([data], 16 /* DB_TRANSACTION.ABORT */);
190
189
  return [];
191
190
  });
192
191
  });
@@ -224,18 +223,26 @@ class Db extends core_1.ClientDb {
224
223
  }
225
224
  }
226
225
  resolveSource(source, folder) {
227
- let result, sep = path.sep;
228
- if (source[0] === '@') {
229
- if ((result = source).indexOf('/') === -1) {
230
- folder || (folder = 'client');
226
+ let result;
227
+ if (source[0] !== '@') {
228
+ switch (source) {
229
+ case 'mongodb':
230
+ case 'mssql':
231
+ case 'mysql':
232
+ case 'oracle':
233
+ case 'postgres':
234
+ case 'redis':
235
+ result = '@e-mc2/' + source;
236
+ break;
237
+ default:
238
+ result = source;
239
+ break;
231
240
  }
232
- sep = '/';
233
241
  }
234
- else if (!Db.isDir(result = path.join(__dirname, source))) {
235
- result = source;
236
- sep = '/';
242
+ else if ((result = source).indexOf('/') === -1) {
243
+ folder || (folder = 'client');
237
244
  }
238
- return result + (folder ? sep + folder : '');
245
+ return result + (folder ? '/' + folder : '');
239
246
  }
240
247
  getPoolConfig(source, uuidKey) {
241
248
  const config = Db.getPoolConfig(source);
@@ -262,15 +269,16 @@ class Db extends core_1.ClientDb {
262
269
  return util_1.SQL_COMMAND;
263
270
  }
264
271
  getClient(source) {
265
- let client;
266
- if (client = DB_CLIENT[source]) {
272
+ let client = DB_CLIENT.get(source);
273
+ if (client) {
267
274
  return client;
268
275
  }
269
276
  try {
270
277
  client = require(this.resolveSource(source));
271
278
  if (client?.DB_SOURCE_CLIENT) {
272
279
  client.DB_SOURCE_NAME = source;
273
- return DB_CLIENT[source] = client;
280
+ DB_CLIENT.set(source, client);
281
+ return client;
274
282
  }
275
283
  }
276
284
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/db",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "description": "DB modules for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +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.0.3",
24
- "@e-mc/request": "0.0.3"
23
+ "@e-mc/core": "0.1.0",
24
+ "@e-mc/request": "0.1.0",
25
+ "@e-mc/types": "0.1.0"
25
26
  }
26
27
  }
@@ -1,12 +0,0 @@
1
- import type { MongoDBDataSource, MongoDBFilterValue, MongoDBSortValue } from '../../types/lib/db';
2
-
3
- import type { IDbSourceClient } from '../types';
4
-
5
- import type { CommandOperationOptions, Document, Filter, Sort, SortDirection } from 'mongodb';
6
-
7
- declare const MongoDB: IDbSourceClient<MongoDBDataSource> & {
8
- getFilterValue: (query: Filter<Document> | MongoDBFilterValue, coerce?: boolean) => [Filter<Document>, CommandOperationOptions?];
9
- getSortValue: (sort: Sort | string | MongoDBSortValue, coerce?: boolean) => [Sort | string, SortDirection?];
10
- };
11
-
12
- export = MongoDB;