@pi-r/redis 0.3.0 → 0.6.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 (2) hide show
  1. package/client/index.js +67 -60
  2. package/package.json +4 -4
package/client/index.js CHANGED
@@ -8,10 +8,10 @@ const Db = require("@e-mc/db");
8
8
  const DbPool = require('@e-mc/db/pool');
9
9
  const REDIS_V4 = Db.checkSemVer("redis" /* STRINGS.MODULE_NAME */, 4);
10
10
  class RedisPool extends DbPool {
11
- getConnection() {
12
- return Promise.resolve(this.client);
11
+ async getConnection() {
12
+ return this.client;
13
13
  }
14
- close() {
14
+ async close() {
15
15
  return this.client.disconnect();
16
16
  }
17
17
  isEmpty() {
@@ -22,7 +22,7 @@ class RedisPool extends DbPool {
22
22
  }
23
23
  }
24
24
  const POOL_STATE = {};
25
- function getClient(db, options, poolKey) {
25
+ async function getClient(db, options, poolKey) {
26
26
  const socket = options.socket || (options.socket = {});
27
27
  if (!poolKey) {
28
28
  socket.keepAlive = false;
@@ -93,53 +93,56 @@ async function setCredential(item) {
93
93
  client.password = password;
94
94
  }
95
95
  const usePool = item.usePool;
96
- if (usePool) {
97
- let pool;
98
- username = undefined;
99
- password = undefined;
100
- if (typeof usePool === 'string' && (username = client.username || (0, util_1.parseConnectionString)(uri)?.username)) {
101
- [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
102
- if (pool) {
103
- pool.add(item, password);
104
- return;
105
- }
96
+ if (!usePool) {
97
+ return;
98
+ }
99
+ let pool;
100
+ username = undefined;
101
+ password = undefined;
102
+ if (typeof usePool === 'string' && (username = client.username || (0, util_1.parseConnectionString)(uri)?.username)) {
103
+ [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
104
+ if (pool) {
105
+ pool.add(item, password);
106
+ return;
106
107
  }
107
- const poolKey = getPoolKey(client);
108
- if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
109
- const config = this.getPoolConfig("redis" /* STRINGS.MODULE_NAME */, password);
110
- const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
111
- if (config) {
112
- const { min, max, idle, queue_max, queue_idle } = config;
113
- if (min >= 0) {
114
- poolOptions.min ?? (poolOptions.min = min);
115
- }
116
- if (max > 0) {
117
- poolOptions.max ?? (poolOptions.max = max);
118
- }
119
- if (idle > 0) {
120
- poolOptions.idleTimeoutMillis ?? (poolOptions.idleTimeoutMillis = idle);
121
- }
122
- if (queue_max >= 0) {
123
- poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
124
- }
125
- if (queue_idle > 0) {
126
- poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = queue_idle);
127
- }
128
- }
129
- try {
130
- const target = new RedisPool(await getClient(redis, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
131
- target.parent = POOL_STATE;
132
- target.success = 1;
133
- }
134
- catch {
135
- item.usePool = false;
136
- delete client.isolationPoolOptions;
137
- }
108
+ }
109
+ const poolKey = getPoolKey(client);
110
+ if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
111
+ pool.add(item);
112
+ return;
113
+ }
114
+ const config = this.getPoolConfig("redis" /* STRINGS.MODULE_NAME */, password);
115
+ const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
116
+ if (config) {
117
+ const { min, max, idle, queue_max, queue_idle, timeout } = config;
118
+ if (min >= 0) {
119
+ poolOptions.min ?? (poolOptions.min = min);
120
+ }
121
+ if (max > 0) {
122
+ poolOptions.max ?? (poolOptions.max = max);
123
+ }
124
+ if (idle > 0) {
125
+ poolOptions.idleTimeoutMillis ?? (poolOptions.idleTimeoutMillis = idle);
126
+ }
127
+ if (queue_max >= 0) {
128
+ poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
129
+ }
130
+ if (queue_idle > 0) {
131
+ poolOptions.softIdleTimeoutMillis ?? (poolOptions.softIdleTimeoutMillis = queue_idle);
138
132
  }
139
- else {
140
- pool.add(item);
133
+ if (timeout > 0) {
134
+ poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = timeout);
141
135
  }
142
136
  }
137
+ try {
138
+ const target = new RedisPool(await getClient(redis, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
139
+ target.parent = POOL_STATE;
140
+ target.success = 1;
141
+ }
142
+ catch {
143
+ item.usePool = false;
144
+ delete client.isolationPoolOptions;
145
+ }
143
146
  }
144
147
  exports.setCredential = setCredential;
145
148
  async function executeQuery(item, options) {
@@ -201,6 +204,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
201
204
  item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
202
205
  return client;
203
206
  };
207
+ if (this.host?.username) {
208
+ this.applyCommand(...batch);
209
+ }
204
210
  for (let i = 0; i < length; ++i) {
205
211
  const item = batch[i];
206
212
  const { source, uri, key, format = 'HASH', search, aggregate, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
@@ -237,10 +243,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
237
243
  queryString = Db.asString(aggregate, true);
238
244
  }
239
245
  else if (!targetObject) {
240
- queryString = Db.asString(key);
246
+ queryString = Db.asString(key, true);
241
247
  }
242
248
  else if (cacheObjectKey) {
243
- queryString = Db.asString(key) + '_' + targetObject.toString() + cacheObjectKey;
249
+ queryString = Db.asString(key, true) + '_' + targetObject.toString() + cacheObjectKey;
244
250
  }
245
251
  if (queryString) {
246
252
  queryString += '_' + format;
@@ -280,7 +286,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
280
286
  const b = (arg) => typeof arg === 'number' || Buffer.isBuffer(arg) ? arg.toString() : arg;
281
287
  if (update) {
282
288
  commandType = this.commandType.UPDATE;
283
- const values = (!Array.isArray(update) ? [update] : update).map(target => {
289
+ const values = (!Array.isArray(update) ? [update] : update).map(async (target) => {
284
290
  return new Promise(async (success, failed) => {
285
291
  let { key: k, value: v, format: f = 'HASH', NX, XX, EX = 0, PX = 0, EXAT = 0, PXAT = 0, options: setOptions = {} } = target;
286
292
  if (v === undefined) {
@@ -457,13 +463,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
457
463
  }
458
464
  }
459
465
  catch (err) {
460
- error = err;
461
- }
462
- if (idx && idx !== index) {
463
- client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }));
466
+ if (err instanceof Error) {
467
+ throw err;
468
+ }
464
469
  }
465
- if (error instanceof Error) {
466
- throw error;
470
+ finally {
471
+ if (idx && idx !== index) {
472
+ client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }));
473
+ }
467
474
  }
468
475
  }
469
476
  else if (key) {
@@ -471,10 +478,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
471
478
  if (Array.isArray(key)) {
472
479
  switch (format.toUpperCase()) {
473
480
  case 'HKEYS':
474
- data = await Promise.all(key.map(k => client.hKeys(...command ? [command, k] : [k])));
481
+ data = await Promise.all(key.map(async (k) => client.hKeys(...command ? [command, k] : [k])));
475
482
  break;
476
483
  case 'HVALS':
477
- data = await Promise.all(key.map(k => client.hVals(...command ? [command, k] : [k])));
484
+ data = await Promise.all(key.map(async (k) => client.hVals(...command ? [command, k] : [k])));
478
485
  break;
479
486
  case 'JSON':
480
487
  data = (await (command ? client.json.mGet(command, key.map(c => b(c)), item.path || '$') : client.json.mGet(key.map(c => b(c)), item.path || '$'))).flat();
@@ -545,12 +552,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
545
552
  }
546
553
  }
547
554
  return this.processRows(batch, tasks, {
548
- disconnect: () => clients.forEach(item => item.disconnect()),
555
+ disconnect: () => clients.forEach(async (item) => item.disconnect()),
549
556
  parallel
550
557
  }, outResult);
551
558
  }
552
559
  exports.executeBatchQuery = executeBatchQuery;
553
- function checkTimeout(value, limit = 0) {
560
+ async function checkTimeout(value, limit = 0) {
554
561
  return DbPool.checkTimeout(POOL_STATE, value, limit);
555
562
  }
556
563
  exports.checkTimeout = checkTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/redis",
3
- "version": "0.3.0",
3
+ "version": "0.6.0",
4
4
  "description": "Redis client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -21,8 +21,8 @@
21
21
  "license": "MIT",
22
22
  "homepage": "https://github.com/anpham6/pi-r#readme",
23
23
  "dependencies": {
24
- "@e-mc/db": "^0.6.0",
25
- "@e-mc/types": "^0.6.0",
26
- "redis": "^4.6.7"
24
+ "@e-mc/db": "^0.8.0",
25
+ "@e-mc/types": "^0.8.0",
26
+ "redis": "^4.6.12"
27
27
  }
28
28
  }