@pi-r/redis 0.2.2 → 0.5.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/client/index.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
4
+ const redis = require("redis");
4
5
  const util_1 = require("@e-mc/db/util");
5
6
  const types_1 = require("@e-mc/types");
6
7
  const Db = require("@e-mc/db");
7
8
  const DbPool = require('@e-mc/db/pool');
8
- const REDIS_V4 = Db.checkSemVer("redis" /* STRINGS.PACKAGE_NAME */, 4);
9
+ const REDIS_V4 = Db.checkSemVer("redis" /* STRINGS.MODULE_NAME */, 4);
9
10
  class RedisPool extends DbPool {
10
11
  getConnection() {
11
12
  return Promise.resolve(this.client);
@@ -39,16 +40,16 @@ function getClient(db, options, poolKey) {
39
40
  client.connect().then(() => resolve(client)).catch(err => reject(err));
40
41
  });
41
42
  }
42
- const removePoolProperties = (options) => {
43
+ function removePoolProperties(options) {
43
44
  if (!options.uuidKey && ('socket' in options || 'isolationPoolOptions' in options)) {
44
45
  return { ...options, socket: options.socket?.tls ? { tls: true } : undefined, isolationPoolOptions: undefined };
45
46
  }
46
47
  return options;
47
- };
48
+ }
48
49
  const getPoolKey = (options) => options.url + '_' + (options.username || '') + '_' + (options.password || '') + '_' + (options.database ?? '') + '_' + (options.socket?.tls ? '1' : '0') + '_' + (options.legacyMode ? '1' : '0');
49
50
  async function setCredential(item) {
50
51
  if (!REDIS_V4) {
51
- throw (0, types_1.errorValue)("Upgrade client to latest version" /* ERR_DB.UPGRADE */, "redis" /* STRINGS.PACKAGE_NAME */ + '@' + Db.getPackageVersion("redis" /* STRINGS.PACKAGE_NAME */));
52
+ throw (0, types_1.errorValue)("Upgrade client to latest version" /* ERR_DB.UPGRADE */, "redis" /* STRINGS.MODULE_NAME */ + '@' + Db.getPackageVersion("redis" /* STRINGS.MODULE_NAME */));
52
53
  }
53
54
  const credential = this.getCredential(item);
54
55
  const options = item.options || (item.options = {});
@@ -105,11 +106,10 @@ async function setCredential(item) {
105
106
  }
106
107
  const poolKey = getPoolKey(client);
107
108
  if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
108
- const db = require("redis" /* STRINGS.PACKAGE_NAME */);
109
109
  const config = this.getPoolConfig("redis" /* STRINGS.MODULE_NAME */, password);
110
110
  const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
111
111
  if (config) {
112
- const { min, max, idle, queue_max, queue_idle } = config;
112
+ const { min, max, idle, queue_max, queue_idle, timeout } = config;
113
113
  if (min >= 0) {
114
114
  poolOptions.min ?? (poolOptions.min = min);
115
115
  }
@@ -123,13 +123,16 @@ async function setCredential(item) {
123
123
  poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
124
124
  }
125
125
  if (queue_idle > 0) {
126
- poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = queue_idle);
126
+ poolOptions.softIdleTimeoutMillis ?? (poolOptions.softIdleTimeoutMillis = queue_idle);
127
+ }
128
+ if (timeout > 0) {
129
+ poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = timeout);
127
130
  }
128
131
  }
129
132
  try {
130
- const redis = new RedisPool(await getClient(db, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
131
- redis.parent = POOL_STATE;
132
- redis.success = 1;
133
+ const target = new RedisPool(await getClient(redis, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
134
+ target.parent = POOL_STATE;
135
+ target.success = 1;
133
136
  }
134
137
  catch {
135
138
  item.usePool = false;
@@ -151,7 +154,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
151
154
  if (length === 0) {
152
155
  return [];
153
156
  }
154
- const db = require("redis" /* STRINGS.PACKAGE_NAME */);
155
157
  let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
156
158
  if ((0, types_1.isPlainObject)(options)) {
157
159
  ({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -193,7 +195,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
193
195
  (credential.isolationPoolOptions || (credential.isolationPoolOptions = {})).max = size;
194
196
  }
195
197
  }
196
- clients.push(client = await getClient(db, credential));
198
+ clients.push(client = await getClient(redis, credential));
197
199
  }
198
200
  if (connectOnce) {
199
201
  redisClient = client;
@@ -204,9 +206,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
204
206
  };
205
207
  for (let i = 0; i < length; ++i) {
206
208
  const item = batch[i];
207
- const { source, uri, key, format = 'HASH', search, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
209
+ const { source, uri, key, format = 'HASH', search, aggregate, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
208
210
  let credential = (redisCredential || onceCredential), error;
209
- 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))) && (error = (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */, uri))) {
211
+ 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))) {
210
212
  ++redisCache;
211
213
  if (this.handleFail(error, item, { errorQuery })) {
212
214
  if (!parallel) {
@@ -228,12 +230,15 @@ async function executeBatchQuery(batch, options = '', outResult) {
228
230
  const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis" /* STRINGS.MODULE_NAME */, 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
229
231
  const renewCache = ignoreCache === 0;
230
232
  const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
231
- const command = (0, types_1.isPlainObject)(clientOptions.command) ? db.commandOptions(clientOptions.command) : null;
233
+ const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
232
234
  let queryString = '', rows;
233
235
  if (caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) {
234
236
  if ((0, types_1.isObject)(search)) {
235
237
  queryString = Db.asString(search, true);
236
238
  }
239
+ else if ((0, types_1.isObject)(aggregate)) {
240
+ queryString = Db.asString(aggregate, true);
241
+ }
237
242
  else if (!targetObject) {
238
243
  queryString = Db.asString(key);
239
244
  }
@@ -287,7 +292,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
287
292
  }
288
293
  const failKey = () => failed((0, types_1.errorMessage)(f, "Invalid key" /* ERR_DB.KEY */, Db.asString(k) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
289
294
  const failValue = () => failed((0, types_1.errorMessage)(f, "Invalid value" /* ERR_DB.VALUE */, Db.asString(v) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
290
- const commandSet = (0, types_1.isPlainObject)(setOptions.command) && db.commandOptions(setOptions.command);
295
+ const commandSet = (0, types_1.isPlainObject)(setOptions.command) && redis.commandOptions(setOptions.command);
291
296
  let pending;
292
297
  if ((f = f.toUpperCase()) === 'JSON') {
293
298
  if (typeof k !== 'string') {
@@ -437,22 +442,28 @@ async function executeBatchQuery(batch, options = '', outResult) {
437
442
  });
438
443
  }
439
444
  commandType = this.commandType.SELECT;
440
- if ((0, types_1.isObject)(search)) {
441
- const { query = '', schema, index } = search;
445
+ const target = (0, types_1.isObject)(search) ? search : (0, types_1.isObject)(aggregate) ? aggregate : null;
446
+ if (target) {
447
+ const { query = '', schema, index } = target;
442
448
  let idx = '';
443
449
  if ((0, types_1.isObject)(schema)) {
444
450
  idx = index || (0, types_1.generateUUID)();
445
- await (command ? client.ft.create(command, idx, schema, search.options) : client.ft.create(idx, schema, search.options));
451
+ await (command ? client.ft.create(command, idx, schema, target.options) : client.ft.create(idx, schema, target.options));
446
452
  }
447
453
  try {
448
- const reply = await (command ? client.ft.search(command, index || idx, query) : client.ft.search(index || idx, query));
449
- rows = reply.documents.map(doc => (doc.value['__id__'] = doc.id) && doc.value);
454
+ if (target === search) {
455
+ const reply = await (command ? client.ft.search(command, index || idx, query, clientOptions.search) : client.ft.search(index || idx, query, clientOptions.search));
456
+ rows = reply.documents.map(doc => (doc.value.__id__ = doc.id) && doc.value);
457
+ }
458
+ else {
459
+ ({ results: rows } = await (command ? client.ft.aggregate(command, index || idx, query, clientOptions.aggregate) : client.ft.aggregate(index || idx, query, clientOptions.aggregate)));
460
+ }
450
461
  }
451
462
  catch (err) {
452
463
  error = err;
453
464
  }
454
465
  if (idx && idx !== index) {
455
- client.ft.dropIndex(idx).catch(err => this.writeFail(['Unable to drop search index', idx], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }));
466
+ 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 }));
456
467
  }
457
468
  if (error instanceof Error) {
458
469
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/redis",
3
- "version": "0.2.2",
3
+ "version": "0.5.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.5.3",
25
- "@e-mc/types": "^0.5.3",
26
- "redis": "^4.6.6"
24
+ "@e-mc/db": "^0.7.0",
25
+ "@e-mc/types": "^0.7.0",
26
+ "redis": "^4.6.11"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import type { ClientCommandOptions } from '@redis/client/dist/lib/client';
7
7
  import type { RedisClientOptions } from '@redis/client';
8
8
  import type { RedisCommandArgument } from '@redis/client/dist/lib/commands';
9
9
  import type { RedisJSON } from '@redis/json/dist/commands';
10
- import type { RediSearchSchema } from 'redis';
10
+ import type { AggregateOptions, RediSearchSchema, SearchOptions } from 'redis';
11
11
 
12
12
  export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, string | RedisCredential, string>, CascadeAction, AuthValue {
13
13
  source: "redis";
@@ -15,16 +15,14 @@ export interface RedisDataSource extends DbDataSource<string, PlainObject, Array
15
15
  field?: RedisCommandArgument;
16
16
  path?: string;
17
17
  format?: RedisFormat | "HKEYS" | "HVALS";
18
- search?: {
19
- index?: string;
20
- schema?: RediSearchSchema | string;
21
- query?: string;
22
- options?: PlainObject;
23
- };
18
+ search?: RedisQuery;
19
+ aggregate?: RedisQuery;
24
20
  options?: {
25
21
  client?: RedisClientOptions;
26
- get?: PlainObject;
27
22
  command?: RedisCommandOptions;
23
+ get?: PlainObject;
24
+ search?: SearchOptions;
25
+ aggregate?: AggregateOptions;
28
26
  };
29
27
  database?: number;
30
28
  }
@@ -53,6 +51,13 @@ export interface RedisJSONValue extends Omit<RedisSetValue<"JSON", string, Redis
53
51
  path?: string;
54
52
  }
55
53
 
54
+ export interface RedisQuery {
55
+ index?: string;
56
+ schema?: RediSearchSchema | string;
57
+ query?: string;
58
+ options?: PlainObject;
59
+ }
60
+
56
61
  export type RedisFormat = "HASH" | "JSON";
57
62
  export type RedisExpireCondition = "NX" | "XX" | "GT" | "LT";
58
63
  export type RedisCommandValue = RedisCommandArgument | number;