@pi-r/redis 0.6.5 → 0.7.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/README.md CHANGED
@@ -1,10 +1,7 @@
1
- # @pi-r/redis
1
+ ### @pi-r/redis
2
2
 
3
- ## Documentation
3
+ https://e-mc.readthedocs.io/en/latest/db/redis.html
4
4
 
5
- - [E-mc](https://e-mc.readthedocs.io/en/latest/db/redis.html)
6
- - [squared](https://squared.readthedocs.io)
7
-
8
- ## LICENSE
5
+ ### LICENSE
9
6
 
10
7
  MIT
package/client/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
4
3
  const redis = require("redis");
5
4
  const util_1 = require("@e-mc/db/util");
@@ -23,7 +22,7 @@ class RedisPool extends DbPool {
23
22
  const POOL_STATE = {};
24
23
  const POOL_ACTIVE = new WeakSet();
25
24
  async function getClient(db, options, poolKey) {
26
- const socket = options.socket || (options.socket = {});
25
+ const socket = options.socket ||= {};
27
26
  if (!poolKey) {
28
27
  socket.keepAlive = false;
29
28
  }
@@ -37,7 +36,7 @@ async function getClient(db, options, poolKey) {
37
36
  delete POOL_STATE[poolKey];
38
37
  });
39
38
  }
40
- client.connect().then(() => resolve(client)).catch(err => reject(err));
39
+ client.connect().then(() => resolve(client)).catch((err) => reject(err));
41
40
  });
42
41
  }
43
42
  function removePoolProperties(options) {
@@ -49,7 +48,7 @@ function removePoolProperties(options) {
49
48
  const getPoolKey = (options) => options.url && JSON.stringify(options);
50
49
  async function setCredential(item) {
51
50
  const credential = this.getCredential(item);
52
- const options = item.options || (item.options = {});
51
+ const options = item.options ||= {};
53
52
  const client = (0, types_1.isPlainObject)(options.client) ? options.client : options.client = {};
54
53
  let { uri, username, password, database = 0 } = item;
55
54
  if (database > 0) {
@@ -57,21 +56,12 @@ async function setCredential(item) {
57
56
  }
58
57
  if (credential) {
59
58
  const auth = (0, util_1.parseServerAuth)(credential, 6379);
60
- if (!client.database) {
61
- if ('database' in credential) {
62
- const value = credential.database;
63
- if (+value > 0) {
64
- database = +value;
65
- }
66
- delete credential.database;
67
- }
68
- if (database > 0) {
69
- client.database = database;
70
- }
59
+ if (auth.database && (database = +auth.database) > 0) {
60
+ client.database ||= database;
71
61
  }
72
- uri || (uri = (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379)));
73
- username || (username = auth.username);
74
- password || (password = auth.password);
62
+ uri ||= (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379));
63
+ username ||= auth.username;
64
+ password ||= auth.password;
75
65
  }
76
66
  else {
77
67
  delete item.credential;
@@ -110,26 +100,26 @@ async function setCredential(item) {
110
100
  }
111
101
  }
112
102
  const config = this.getPoolConfig("redis", password);
113
- const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
103
+ const poolOptions = client.isolationPoolOptions ||= {};
114
104
  if (config) {
115
105
  const { min, max, idle, queue_max, queue_idle, timeout } = config;
116
106
  if (min >= 0) {
117
- poolOptions.min ?? (poolOptions.min = min);
107
+ poolOptions.min ??= min;
118
108
  }
119
109
  if (max > 0) {
120
- poolOptions.max ?? (poolOptions.max = max);
110
+ poolOptions.max ??= max;
121
111
  }
122
112
  if (idle > 0) {
123
- poolOptions.idleTimeoutMillis ?? (poolOptions.idleTimeoutMillis = idle);
113
+ poolOptions.idleTimeoutMillis ??= idle;
124
114
  }
125
115
  if (queue_max >= 0) {
126
- poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
116
+ poolOptions.maxWaitingClients ??= queue_max;
127
117
  }
128
118
  if (queue_idle > 0) {
129
- poolOptions.softIdleTimeoutMillis ?? (poolOptions.softIdleTimeoutMillis = queue_idle);
119
+ poolOptions.softIdleTimeoutMillis ??= queue_idle;
130
120
  }
131
121
  if (timeout > 0) {
132
- poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = timeout);
122
+ poolOptions.acquireTimeoutMillis ??= timeout;
133
123
  }
134
124
  }
135
125
  const poolKey = getPoolKey(client);
@@ -175,7 +165,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
175
165
  parallel = !batch.some(item => item.parallel === false);
176
166
  }
177
167
  if (!parallel) {
178
- outResult || (outResult = new Array(length));
168
+ outResult ||= new Array(length);
179
169
  }
180
170
  const caching = this.hasCache("redis", sessionKey);
181
171
  const tasks = new Array(length);
@@ -196,7 +186,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
196
186
  if (connectOnce && parallel) {
197
187
  const size = length - redisCache;
198
188
  if (size > 1) {
199
- (credential.isolationPoolOptions || (credential.isolationPoolOptions = {})).max = size;
189
+ (credential.isolationPoolOptions ||= {}).max = size;
200
190
  }
201
191
  }
202
192
  clients.push(client = await getClient(redis, credential));
@@ -234,7 +224,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
234
224
  }
235
225
  item.transactionState = 1;
236
226
  const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
237
- const uuidKey = (credential.uuidKey || (credential.uuidKey = (onceCredential ? batch[0] : item).credential?.uuidKey));
227
+ const uuidKey = (credential.uuidKey ||= (onceCredential ? batch[0] : item).credential?.uuidKey);
238
228
  const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
239
229
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
240
230
  let queryString = '', rows;
@@ -307,10 +297,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
307
297
  failKey();
308
298
  return;
309
299
  }
310
- if (this.hasCoerce("redis", 'options', uuidKey)) {
311
- if ((0, types_1.isString)(v) && v.startsWith('new')) {
312
- ({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
313
- }
300
+ if (this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.isString)(v) && v.startsWith('new')) {
301
+ ({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
314
302
  }
315
303
  let { path = '$', command: cmd, start, stop, index } = target;
316
304
  const name = ((0, types_1.isString)(cmd) ? cmd.toUpperCase() : 'SET');
@@ -350,8 +338,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
350
338
  const data = (Array.isArray(v) ? v : [v])
351
339
  .filter((m) => m.value !== undefined)
352
340
  .map((m) => {
353
- m.key || (m.key = k);
354
- m.path || (m.path = path);
341
+ m.key ||= k;
342
+ m.path ||= path;
355
343
  return m;
356
344
  });
357
345
  if (data.length) {
@@ -476,7 +464,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
476
464
  }
477
465
  }
478
466
  if (ttl) {
479
- PX = await client.pTTL(k).catch(err => {
467
+ PX = await client.pTTL(k).catch((err) => {
480
468
  failed(err);
481
469
  return 0;
482
470
  });
@@ -494,13 +482,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
494
482
  failed((0, types_1.errorMessage)(f, "Invalid value", has(EX) ? 'EX: ' + EX : has(PX) ? 'PX: ' + PX : has(EXAT) ? 'EXAT: ' + EX : 'PXAT: ' + PXAT));
495
483
  }
496
484
  })
497
- .catch(err => failed(err));
485
+ .catch((err) => failed(err));
498
486
  }
499
487
  else {
500
488
  success(target);
501
489
  }
502
490
  })
503
- .catch(err => failed(err));
491
+ .catch((err) => failed(err));
504
492
  }
505
493
  else {
506
494
  failValue();
@@ -516,7 +504,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
516
504
  }
517
505
  }
518
506
  })
519
- .catch(err => {
507
+ .catch((err) => {
520
508
  if (this.handleFail(err, item, { errorQuery, commandType })) {
521
509
  reject(err);
522
510
  }
@@ -547,7 +535,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
547
535
  }
548
536
  finally {
549
537
  if (idx && idx !== index) {
550
- client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536, fatal: false }));
538
+ client.ft.dropIndex(idx).catch((err) => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536, fatal: false }));
551
539
  }
552
540
  }
553
541
  }
@@ -630,7 +618,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
630
618
  }
631
619
  }
632
620
  return this.processRows(batch, tasks, {
633
- disconnect: () => clients.forEach(async (item) => item.disconnect()),
621
+ disconnect: () => clients.forEach(item => {
622
+ item.disconnect();
623
+ }),
634
624
  parallel
635
625
  }, outResult);
636
626
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/redis",
3
- "version": "0.6.5",
3
+ "version": "0.7.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.8.6",
25
- "@e-mc/types": "^0.8.6",
24
+ "@e-mc/db": "^0.9.0",
25
+ "@e-mc/types": "^0.9.0",
26
26
  "redis": "^4.6.13"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -9,7 +9,7 @@ import type { RedisCommandArgument } from '@redis/client/dist/lib/commands';
9
9
  import type { RedisJSON } from '@redis/json/dist/commands';
10
10
  import type { RediSearchSchema, SearchOptions, SetOptions } from 'redis';
11
11
 
12
- export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, string | RedisCredential, string>, AuthValue, CascadeAction {
12
+ export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, RedisCredential, string>, CascadeAction, AuthValue {
13
13
  source: "redis";
14
14
  key?: ArrayOf<RedisCommandArgument>;
15
15
  field?: RedisCommandArgument;
@@ -55,9 +55,9 @@ export interface RedisSetValue extends RedisCommand<"HASH", RedisCommandArgument
55
55
  export interface RedisJSONValue extends RedisCommand<"JSON", string, ArrayOf<RedisJSON>> {
56
56
  command?: RedisCommandJSON;
57
57
  path?: string;
58
- start?: NumString;
59
- stop?: NumString;
60
- index?: NumString;
58
+ start?: number | string;
59
+ stop?: number | string;
60
+ index?: number | string;
61
61
  }
62
62
 
63
63
  export interface RedisQuery {
@@ -78,6 +78,6 @@ export type RedisCommandJSON = "ARRAPPEND" | "ARRINDEX" | "ARRINSERT" | "ARRPOP"
78
78
  export type RedisExpireCondition = "NX" | "XX" | "GT" | "LT";
79
79
  export type RedisCommandValue = RedisCommandArgument | number;
80
80
  export type RedisCommandOptions = CommandOptions<ClientCommandOptions>;
81
- export type RedisHSETObject = Record<NumString, RedisCommandValue>;
81
+ export type RedisHSETObject = Record<number | string, RedisCommandValue>;
82
82
 
83
83
  export type { RedisCommandArgument, RedisJSON };