@pi-r/redis 0.6.6 → 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
@@ -22,7 +22,7 @@ class RedisPool extends DbPool {
22
22
  const POOL_STATE = {};
23
23
  const POOL_ACTIVE = new WeakSet();
24
24
  async function getClient(db, options, poolKey) {
25
- const socket = options.socket || (options.socket = {});
25
+ const socket = options.socket ||= {};
26
26
  if (!poolKey) {
27
27
  socket.keepAlive = false;
28
28
  }
@@ -36,7 +36,7 @@ async function getClient(db, options, poolKey) {
36
36
  delete POOL_STATE[poolKey];
37
37
  });
38
38
  }
39
- client.connect().then(() => resolve(client)).catch(err => reject(err));
39
+ client.connect().then(() => resolve(client)).catch((err) => reject(err));
40
40
  });
41
41
  }
42
42
  function removePoolProperties(options) {
@@ -48,7 +48,7 @@ function removePoolProperties(options) {
48
48
  const getPoolKey = (options) => options.url && JSON.stringify(options);
49
49
  async function setCredential(item) {
50
50
  const credential = this.getCredential(item);
51
- const options = item.options || (item.options = {});
51
+ const options = item.options ||= {};
52
52
  const client = (0, types_1.isPlainObject)(options.client) ? options.client : options.client = {};
53
53
  let { uri, username, password, database = 0 } = item;
54
54
  if (database > 0) {
@@ -56,21 +56,12 @@ async function setCredential(item) {
56
56
  }
57
57
  if (credential) {
58
58
  const auth = (0, util_1.parseServerAuth)(credential, 6379);
59
- if (!client.database) {
60
- if ('database' in credential) {
61
- const value = credential.database;
62
- if (+value > 0) {
63
- database = +value;
64
- }
65
- delete credential.database;
66
- }
67
- if (database > 0) {
68
- client.database = database;
69
- }
59
+ if (auth.database && (database = +auth.database) > 0) {
60
+ client.database ||= database;
70
61
  }
71
- uri || (uri = (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379)));
72
- username || (username = auth.username);
73
- 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;
74
65
  }
75
66
  else {
76
67
  delete item.credential;
@@ -109,26 +100,26 @@ async function setCredential(item) {
109
100
  }
110
101
  }
111
102
  const config = this.getPoolConfig("redis", password);
112
- const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
103
+ const poolOptions = client.isolationPoolOptions ||= {};
113
104
  if (config) {
114
105
  const { min, max, idle, queue_max, queue_idle, timeout } = config;
115
106
  if (min >= 0) {
116
- poolOptions.min ?? (poolOptions.min = min);
107
+ poolOptions.min ??= min;
117
108
  }
118
109
  if (max > 0) {
119
- poolOptions.max ?? (poolOptions.max = max);
110
+ poolOptions.max ??= max;
120
111
  }
121
112
  if (idle > 0) {
122
- poolOptions.idleTimeoutMillis ?? (poolOptions.idleTimeoutMillis = idle);
113
+ poolOptions.idleTimeoutMillis ??= idle;
123
114
  }
124
115
  if (queue_max >= 0) {
125
- poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
116
+ poolOptions.maxWaitingClients ??= queue_max;
126
117
  }
127
118
  if (queue_idle > 0) {
128
- poolOptions.softIdleTimeoutMillis ?? (poolOptions.softIdleTimeoutMillis = queue_idle);
119
+ poolOptions.softIdleTimeoutMillis ??= queue_idle;
129
120
  }
130
121
  if (timeout > 0) {
131
- poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = timeout);
122
+ poolOptions.acquireTimeoutMillis ??= timeout;
132
123
  }
133
124
  }
134
125
  const poolKey = getPoolKey(client);
@@ -174,7 +165,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
174
165
  parallel = !batch.some(item => item.parallel === false);
175
166
  }
176
167
  if (!parallel) {
177
- outResult || (outResult = new Array(length));
168
+ outResult ||= new Array(length);
178
169
  }
179
170
  const caching = this.hasCache("redis", sessionKey);
180
171
  const tasks = new Array(length);
@@ -195,7 +186,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
195
186
  if (connectOnce && parallel) {
196
187
  const size = length - redisCache;
197
188
  if (size > 1) {
198
- (credential.isolationPoolOptions || (credential.isolationPoolOptions = {})).max = size;
189
+ (credential.isolationPoolOptions ||= {}).max = size;
199
190
  }
200
191
  }
201
192
  clients.push(client = await getClient(redis, credential));
@@ -233,7 +224,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
233
224
  }
234
225
  item.transactionState = 1;
235
226
  const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
236
- const uuidKey = (credential.uuidKey || (credential.uuidKey = (onceCredential ? batch[0] : item).credential?.uuidKey));
227
+ const uuidKey = (credential.uuidKey ||= (onceCredential ? batch[0] : item).credential?.uuidKey);
237
228
  const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
238
229
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
239
230
  let queryString = '', rows;
@@ -306,10 +297,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
306
297
  failKey();
307
298
  return;
308
299
  }
309
- if (this.hasCoerce("redis", 'options', uuidKey)) {
310
- if ((0, types_1.isString)(v) && v.startsWith('new')) {
311
- ({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
312
- }
300
+ if (this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.isString)(v) && v.startsWith('new')) {
301
+ ({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
313
302
  }
314
303
  let { path = '$', command: cmd, start, stop, index } = target;
315
304
  const name = ((0, types_1.isString)(cmd) ? cmd.toUpperCase() : 'SET');
@@ -349,8 +338,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
349
338
  const data = (Array.isArray(v) ? v : [v])
350
339
  .filter((m) => m.value !== undefined)
351
340
  .map((m) => {
352
- m.key || (m.key = k);
353
- m.path || (m.path = path);
341
+ m.key ||= k;
342
+ m.path ||= path;
354
343
  return m;
355
344
  });
356
345
  if (data.length) {
@@ -475,7 +464,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
475
464
  }
476
465
  }
477
466
  if (ttl) {
478
- PX = await client.pTTL(k).catch(err => {
467
+ PX = await client.pTTL(k).catch((err) => {
479
468
  failed(err);
480
469
  return 0;
481
470
  });
@@ -493,13 +482,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
493
482
  failed((0, types_1.errorMessage)(f, "Invalid value", has(EX) ? 'EX: ' + EX : has(PX) ? 'PX: ' + PX : has(EXAT) ? 'EXAT: ' + EX : 'PXAT: ' + PXAT));
494
483
  }
495
484
  })
496
- .catch(err => failed(err));
485
+ .catch((err) => failed(err));
497
486
  }
498
487
  else {
499
488
  success(target);
500
489
  }
501
490
  })
502
- .catch(err => failed(err));
491
+ .catch((err) => failed(err));
503
492
  }
504
493
  else {
505
494
  failValue();
@@ -515,7 +504,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
515
504
  }
516
505
  }
517
506
  })
518
- .catch(err => {
507
+ .catch((err) => {
519
508
  if (this.handleFail(err, item, { errorQuery, commandType })) {
520
509
  reject(err);
521
510
  }
@@ -546,7 +535,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
546
535
  }
547
536
  finally {
548
537
  if (idx && idx !== index) {
549
- 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 }));
550
539
  }
551
540
  }
552
541
  }
@@ -629,7 +618,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
629
618
  }
630
619
  }
631
620
  return this.processRows(batch, tasks, {
632
- disconnect: () => clients.forEach(async (item) => item.disconnect()),
621
+ disconnect: () => clients.forEach(item => {
622
+ item.disconnect();
623
+ }),
633
624
  parallel
634
625
  }, outResult);
635
626
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/redis",
3
- "version": "0.6.6",
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.7",
25
- "@e-mc/types": "^0.8.7",
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 };