@e-mc/db 0.7.0 → 0.8.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.
Files changed (3) hide show
  1. package/index.js +42 -13
  2. package/package.json +4 -4
  3. package/pool.js +5 -2
package/index.js CHANGED
@@ -62,7 +62,7 @@ class Db extends core_1.ClientDb {
62
62
  static getPoolConfig(source) {
63
63
  return POOL_CONFIG.get(source);
64
64
  }
65
- setCredential(item) {
65
+ async setCredential(item) {
66
66
  try {
67
67
  return this.getClient(item.source).setCredential.call(this, item);
68
68
  }
@@ -93,7 +93,36 @@ class Db extends core_1.ClientDb {
93
93
  return false;
94
94
  }
95
95
  }
96
- executeQuery(item, options) {
96
+ applyCommand(...items) {
97
+ let settings;
98
+ for (let i = 0, length = items.length; i < length; ++i) {
99
+ const item = items[i];
100
+ const command = item.withCommand;
101
+ if (!command) {
102
+ continue;
103
+ }
104
+ if (!settings && !(0, types_1.isPlainObject)(settings = this.getUserSettings()?.[item.source]?.commands)) {
105
+ return;
106
+ }
107
+ let name, table;
108
+ const [group, procedure] = Array.isArray(command) ? command : ({ name, table } = item, [(name || table ? name && table ? name + ':' + table : table || name : typeof item.document === 'string' && item.document) || '', command]);
109
+ const data = settings[group]?.[procedure];
110
+ if (!(0, types_1.isPlainObject)(data)) {
111
+ continue;
112
+ }
113
+ if ('uri' in data) {
114
+ delete data.uri;
115
+ }
116
+ if ('credential' in data) {
117
+ delete data.credential;
118
+ }
119
+ if ('usePool' in data) {
120
+ delete data.usePool;
121
+ }
122
+ Object.assign(item, data);
123
+ }
124
+ }
125
+ async executeQuery(item, options) {
97
126
  if (this.aborted) {
98
127
  return Promise.reject((0, types_1.createAbortError)());
99
128
  }
@@ -104,7 +133,7 @@ class Db extends core_1.ClientDb {
104
133
  return Promise.reject(err);
105
134
  }
106
135
  }
107
- executeBatchQuery(batch, options, outResult) {
136
+ async executeBatchQuery(batch, options, outResult) {
108
137
  if (this.aborted) {
109
138
  return Promise.reject((0, types_1.createAbortError)());
110
139
  }
@@ -115,7 +144,7 @@ class Db extends core_1.ClientDb {
115
144
  return Promise.reject(err);
116
145
  }
117
146
  }
118
- processRows(batch, tasks, parallel, outResult) {
147
+ async processRows(batch, tasks, parallel, outResult) {
119
148
  let disconnect;
120
149
  if ((0, types_1.isObject)(parallel)) {
121
150
  ({ disconnect, parallel } = parallel);
@@ -142,11 +171,11 @@ class Db extends core_1.ClientDb {
142
171
  return [];
143
172
  };
144
173
  if (tasks.length === 0) {
145
- return Promise.resolve(cleanup());
174
+ return cleanup();
146
175
  }
147
176
  if (!parallel && outResult) {
148
177
  terminate();
149
- return Promise.resolve(outResult);
178
+ return outResult;
150
179
  }
151
180
  return Promise.all(tasks)
152
181
  .then(result => {
@@ -167,31 +196,31 @@ class Db extends core_1.ClientDb {
167
196
  if (options && typeof options.errorQuery === 'function') {
168
197
  if (options.errorQuery(err, item, options.commandType)) {
169
198
  if (item.willAbort) {
170
- this.abort();
199
+ this.abort(err);
171
200
  }
172
201
  return true;
173
202
  }
174
203
  }
175
204
  else {
176
205
  if (item.willAbort) {
177
- this.abort();
206
+ this.abort(err);
178
207
  }
179
208
  this.writeFail(["Unable to execute query" /* ERR_DB.EXEC_QUERY */, item.source], err, 65536 /* LOG_TYPE.DB */);
180
209
  }
181
210
  return false;
182
211
  }
183
- commit(items) {
212
+ async commit(items) {
184
213
  if (this.aborted) {
185
214
  return Promise.reject((0, types_1.createAbortError)());
186
215
  }
187
- const tasks = (items || this.pending).map(data => {
216
+ const tasks = (items || this.pending).map(async (data) => {
188
217
  data.ignoreCache ?? (data.ignoreCache = true);
189
218
  return this.executeQuery(data).catch(() => {
190
219
  this.applyState([data], 16 /* DB_TRANSACTION.ABORT */);
191
220
  return [];
192
221
  });
193
222
  });
194
- return tasks.length === 0 ? Promise.resolve(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" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
195
224
  }
196
225
  readTLSCert(value, cache) {
197
226
  if ((0, types_1.isString)(value)) {
@@ -226,10 +255,10 @@ class Db extends core_1.ClientDb {
226
255
  }
227
256
  resolveSource(source, folder) {
228
257
  let result;
229
- if (source[0] !== '@') {
258
+ if (!source.startsWith('@')) {
230
259
  result = this.settings.imports?.[source] || util_1.IMPORTS[source];
231
260
  }
232
- else if (!folder && source.indexOf('/') === -1) {
261
+ else if (!folder && !source.includes('/')) {
233
262
  folder = 'client';
234
263
  }
235
264
  return (result || source) + (folder ? '/' + folder : '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/db",
3
- "version": "0.7.0",
3
+ "version": "0.8.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.7.0",
24
- "@e-mc/request": "0.7.0",
25
- "@e-mc/types": "0.7.0"
23
+ "@e-mc/core": "0.8.0",
24
+ "@e-mc/request": "0.8.0",
25
+ "@e-mc/types": "0.8.0"
26
26
  }
27
27
  }
package/pool.js CHANGED
@@ -88,9 +88,12 @@ class DbPool {
88
88
  delete parent[this.poolKey];
89
89
  }
90
90
  }
91
- detach(force) {
91
+ async detach(force) {
92
92
  this.remove();
93
- return this.closed ? Promise.resolve() : force ? this.close().catch(() => { }) : this.close();
93
+ if (this.closed) {
94
+ return;
95
+ }
96
+ return force ? this.close().catch(() => { }) : this.close();
94
97
  }
95
98
  isIdle(timeout) {
96
99
  if (this.closed) {