@pi-r/redis 0.6.3 → 0.6.5

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,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
2
2
 
3
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:
4
4
 
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
- ### @pi-r/redis
1
+ # @pi-r/redis
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/db/redis.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/db/redis.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { RedisDataSource } from '@e-mc/types/lib/db';
2
-
3
1
  import type { IDbSourceClient } from '@e-mc/db/types';
4
2
 
3
+ import type { RedisDataSource } from '../types';
4
+
5
5
  declare const Redis: IDbSourceClient<RedisDataSource>;
6
6
 
7
7
  export = Redis;
package/client/index.js CHANGED
@@ -21,6 +21,7 @@ class RedisPool extends DbPool {
21
21
  }
22
22
  }
23
23
  const POOL_STATE = {};
24
+ const POOL_ACTIVE = new WeakSet();
24
25
  async function getClient(db, options, poolKey) {
25
26
  const socket = options.socket || (options.socket = {});
26
27
  if (!poolKey) {
@@ -40,7 +41,7 @@ async function getClient(db, options, poolKey) {
40
41
  });
41
42
  }
42
43
  function removePoolProperties(options) {
43
- if (!options.uuidKey && ('socket' in options || 'isolationPoolOptions' in options)) {
44
+ if (!options.uuidKey && ('socket' in options || 'isolationPoolOptions' in options) && POOL_ACTIVE.has(options)) {
44
45
  return { ...options, socket: options.socket?.tls ? { tls: true } : undefined, isolationPoolOptions: undefined };
45
46
  }
46
47
  return options;
@@ -55,7 +56,7 @@ async function setCredential(item) {
55
56
  client.database = database;
56
57
  }
57
58
  if (credential) {
58
- const auth = (0, util_1.parseServerAuth)(credential, 6379 /* VALUES.PORT */);
59
+ const auth = (0, util_1.parseServerAuth)(credential, 6379);
59
60
  if (!client.database) {
60
61
  if ('database' in credential) {
61
62
  const value = credential.database;
@@ -68,7 +69,7 @@ async function setCredential(item) {
68
69
  client.database = database;
69
70
  }
70
71
  }
71
- uri || (uri = (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379 /* VALUES.PORT */)));
72
+ uri || (uri = (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379)));
72
73
  username || (username = auth.username);
73
74
  password || (password = auth.password);
74
75
  }
@@ -76,7 +77,7 @@ async function setCredential(item) {
76
77
  delete item.credential;
77
78
  }
78
79
  if (!uri) {
79
- throw (0, types_1.errorMessage)("redis" /* STRINGS.MODULE_NAME */, 'Not defined - uri');
80
+ throw (0, types_1.errorMessage)("redis", 'Not defined - uri');
80
81
  }
81
82
  if (client.socket?.tls) {
82
83
  this.readTLSConfig(client.socket);
@@ -108,7 +109,7 @@ async function setCredential(item) {
108
109
  return;
109
110
  }
110
111
  }
111
- const config = this.getPoolConfig("redis" /* STRINGS.MODULE_NAME */, password);
112
+ const config = this.getPoolConfig("redis", password);
112
113
  const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
113
114
  if (config) {
114
115
  const { min, max, idle, queue_max, queue_idle, timeout } = config;
@@ -176,18 +177,19 @@ async function executeBatchQuery(batch, options = '', outResult) {
176
177
  if (!parallel) {
177
178
  outResult || (outResult = new Array(length));
178
179
  }
179
- const caching = this.hasCache("redis" /* STRINGS.MODULE_NAME */, sessionKey);
180
+ const caching = this.hasCache("redis", sessionKey);
180
181
  const tasks = new Array(length);
181
182
  const clients = [];
182
183
  let redisClient, redisCredential, redisCache = 0, onceCredential = connectOnce ? batch[0].options?.client : undefined;
183
184
  const getConnection = async (item, credential) => {
184
- item.transactionState = 64 /* DB_TRANSACTION.AUTH */;
185
+ item.transactionState = 64;
185
186
  let client;
186
187
  if (item.usePool) {
187
188
  const pool = DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
188
189
  if (pool) {
189
190
  client = pool.client;
190
191
  pool.connected = true;
192
+ POOL_ACTIVE.add(credential);
191
193
  }
192
194
  }
193
195
  if (!client) {
@@ -203,7 +205,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
203
205
  redisClient = client;
204
206
  redisCredential = credential;
205
207
  }
206
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
208
+ item.transactionState &= ~64;
207
209
  return client;
208
210
  };
209
211
  if (this.host?.username) {
@@ -213,7 +215,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
213
215
  const item = batch[i];
214
216
  const { source, uri, key, format = 'HASH', search, aggregate, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
215
217
  let credential = (redisCredential || onceCredential), error;
216
- if (!credential && !(0, types_1.isPlainObject)(credential = clientOptions.client) && (error = (0, types_1.errorMessage)(source, "Invalid credentials" /* ERR_DB.CREDENTIALS */)) || !((0, types_1.isString)(key) || (0, types_1.isArray)(key) || (0, types_1.isPlainObject)(search) && ((0, types_1.isPlainObject)(search.schema) || (0, types_1.isString)(search.query)) || (0, types_1.isPlainObject)(aggregate) && ((0, types_1.isPlainObject)(aggregate.schema) || (0, types_1.isString)(aggregate.query))) && (error = (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */, uri))) {
218
+ if (!credential && !(0, types_1.isPlainObject)(credential = clientOptions.client) && (error = (0, types_1.errorMessage)(source, "Invalid credentials")) || !((0, types_1.isString)(key) || (0, types_1.isArray)(key) || (0, types_1.isPlainObject)(search) && ((0, types_1.isPlainObject)(search.schema) || (0, types_1.isString)(search.query)) || (0, types_1.isPlainObject)(aggregate) && ((0, types_1.isPlainObject)(aggregate.schema) || (0, types_1.isString)(aggregate.query))) && (error = (0, types_1.errorMessage)(source, "Missing database query", uri))) {
217
219
  ++redisCache;
218
220
  if (this.handleFail(error, item, { errorQuery })) {
219
221
  if (!parallel) {
@@ -230,10 +232,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
230
232
  }
231
233
  continue;
232
234
  }
233
- item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
235
+ item.transactionState = 1;
234
236
  const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
235
237
  const uuidKey = (credential.uuidKey || (credential.uuidKey = (onceCredential ? batch[0] : item).credential?.uuidKey));
236
- const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis" /* STRINGS.MODULE_NAME */, 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
238
+ const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
237
239
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
238
240
  let queryString = '', rows;
239
241
  if (caching && ignoreCache !== true) {
@@ -261,7 +263,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
261
263
  else {
262
264
  outResult[i] = rows;
263
265
  }
264
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */ | 128 /* DB_TRANSACTION.CACHE */);
266
+ this.add(item, 4 | 128);
265
267
  continue;
266
268
  }
267
269
  if (!ignoreCache && outCacheMiss) {
@@ -296,8 +298,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
296
298
  return;
297
299
  }
298
300
  const has = (n) => typeof n === 'number' && n > 0;
299
- const failKey = () => failed((0, types_1.errorMessage)(f, "Invalid key" /* ERR_DB.KEY */, Db.asString(k) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
300
- const failValue = () => failed((0, types_1.errorMessage)(f, "Invalid value" /* ERR_DB.VALUE */, Db.asString(v) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
301
+ const failKey = () => failed((0, types_1.errorMessage)(f, "Invalid key", Db.asString(k) || "Unknown"));
302
+ const failValue = () => failed((0, types_1.errorMessage)(f, "Invalid value", Db.asString(v) || "Unknown"));
301
303
  const commandSet = (0, types_1.isPlainObject)(setOptions.command) && redis.commandOptions(setOptions.command);
302
304
  let pending, codeMin = 0, EX, PX, EXAT, PXAT, KEEPTTL;
303
305
  if (f === 'JSON') {
@@ -305,7 +307,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
305
307
  failKey();
306
308
  return;
307
309
  }
308
- if (this.hasCoerce("redis" /* STRINGS.MODULE_NAME */, 'options', uuidKey)) {
310
+ if (this.hasCoerce("redis", 'options', uuidKey)) {
309
311
  if ((0, types_1.isString)(v) && v.startsWith('new')) {
310
312
  ({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
311
313
  }
@@ -489,7 +491,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
489
491
  success(target);
490
492
  }
491
493
  else {
492
- failed((0, types_1.errorMessage)(f, "Invalid value" /* ERR_DB.VALUE */, has(EX) ? 'EX: ' + EX : has(PX) ? 'PX: ' + PX : has(EXAT) ? 'EXAT: ' + EX : 'PXAT: ' + PXAT));
494
+ failed((0, types_1.errorMessage)(f, "Invalid value", has(EX) ? 'EX: ' + EX : has(PX) ? 'PX: ' + PX : has(EXAT) ? 'EXAT: ' + EX : 'PXAT: ' + PXAT));
493
495
  }
494
496
  })
495
497
  .catch(err => failed(err));
@@ -545,7 +547,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
545
547
  }
546
548
  finally {
547
549
  if (idx && idx !== index) {
548
- 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 }));
550
+ client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536, fatal: false }));
549
551
  }
550
552
  }
551
553
  }
@@ -591,16 +593,16 @@ async function executeBatchQuery(batch, options = '', outResult) {
591
593
  rows = (targetObject ? targetObject(item, data) : data);
592
594
  }
593
595
  if (rows === undefined) {
594
- throw (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */);
596
+ throw (0, types_1.errorMessage)(source, "Missing database query");
595
597
  }
596
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
598
+ this.add(item, 4);
597
599
  resolve(this.setQueryResult(source, removePoolProperties(redisCredential || credential), queryString, rows, cacheValue));
598
600
  }
599
601
  catch (err) {
600
602
  if (err instanceof Error) {
601
603
  switch (err.message) {
602
- case "The client is closed" /* STRINGS.ERR_CLIENT_CLOSE */:
603
- case "Disconnects client" /* STRINGS.ERR_CLIENT_DISCONNECT */: {
604
+ case "The client is closed":
605
+ case "Disconnects client": {
604
606
  const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential));
605
607
  if (pool) {
606
608
  await pool.detach(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/redis",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
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.8.3",
25
- "@e-mc/types": "^0.8.3",
26
- "redis": "^4.6.12"
24
+ "@e-mc/db": "^0.8.6",
25
+ "@e-mc/types": "^0.8.6",
26
+ "redis": "^4.6.13"
27
27
  }
28
28
  }