@pi-r/redis 0.2.2 → 0.3.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,7 +106,6 @@ 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) {
@@ -127,9 +127,9 @@ async function setCredential(item) {
127
127
  }
128
128
  }
129
129
  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;
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
133
  }
134
134
  catch {
135
135
  item.usePool = false;
@@ -151,7 +151,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
151
151
  if (length === 0) {
152
152
  return [];
153
153
  }
154
- const db = require("redis" /* STRINGS.PACKAGE_NAME */);
155
154
  let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
156
155
  if ((0, types_1.isPlainObject)(options)) {
157
156
  ({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -193,7 +192,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
193
192
  (credential.isolationPoolOptions || (credential.isolationPoolOptions = {})).max = size;
194
193
  }
195
194
  }
196
- clients.push(client = await getClient(db, credential));
195
+ clients.push(client = await getClient(redis, credential));
197
196
  }
198
197
  if (connectOnce) {
199
198
  redisClient = client;
@@ -204,9 +203,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
204
203
  };
205
204
  for (let i = 0; i < length; ++i) {
206
205
  const item = batch[i];
207
- const { source, uri, key, format = 'HASH', search, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
206
+ const { source, uri, key, format = 'HASH', search, aggregate, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
208
207
  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))) {
208
+ 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
209
  ++redisCache;
211
210
  if (this.handleFail(error, item, { errorQuery })) {
212
211
  if (!parallel) {
@@ -228,12 +227,15 @@ async function executeBatchQuery(batch, options = '', outResult) {
228
227
  const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis" /* STRINGS.MODULE_NAME */, 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
229
228
  const renewCache = ignoreCache === 0;
230
229
  const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
231
- const command = (0, types_1.isPlainObject)(clientOptions.command) ? db.commandOptions(clientOptions.command) : null;
230
+ const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
232
231
  let queryString = '', rows;
233
232
  if (caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) {
234
233
  if ((0, types_1.isObject)(search)) {
235
234
  queryString = Db.asString(search, true);
236
235
  }
236
+ else if ((0, types_1.isObject)(aggregate)) {
237
+ queryString = Db.asString(aggregate, true);
238
+ }
237
239
  else if (!targetObject) {
238
240
  queryString = Db.asString(key);
239
241
  }
@@ -287,7 +289,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
287
289
  }
288
290
  const failKey = () => failed((0, types_1.errorMessage)(f, "Invalid key" /* ERR_DB.KEY */, Db.asString(k) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
289
291
  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);
292
+ const commandSet = (0, types_1.isPlainObject)(setOptions.command) && redis.commandOptions(setOptions.command);
291
293
  let pending;
292
294
  if ((f = f.toUpperCase()) === 'JSON') {
293
295
  if (typeof k !== 'string') {
@@ -437,22 +439,28 @@ async function executeBatchQuery(batch, options = '', outResult) {
437
439
  });
438
440
  }
439
441
  commandType = this.commandType.SELECT;
440
- if ((0, types_1.isObject)(search)) {
441
- const { query = '', schema, index } = search;
442
+ const target = (0, types_1.isObject)(search) ? search : (0, types_1.isObject)(aggregate) ? aggregate : null;
443
+ if (target) {
444
+ const { query = '', schema, index } = target;
442
445
  let idx = '';
443
446
  if ((0, types_1.isObject)(schema)) {
444
447
  idx = index || (0, types_1.generateUUID)();
445
- await (command ? client.ft.create(command, idx, schema, search.options) : client.ft.create(idx, schema, search.options));
448
+ await (command ? client.ft.create(command, idx, schema, target.options) : client.ft.create(idx, schema, target.options));
446
449
  }
447
450
  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);
451
+ if (target === search) {
452
+ const reply = await (command ? client.ft.search(command, index || idx, query, clientOptions.search) : client.ft.search(index || idx, query, clientOptions.search));
453
+ rows = reply.documents.map(doc => (doc.value.__id__ = doc.id) && doc.value);
454
+ }
455
+ else {
456
+ ({ results: rows } = await (command ? client.ft.aggregate(command, index || idx, query, clientOptions.aggregate) : client.ft.aggregate(index || idx, query, clientOptions.aggregate)));
457
+ }
450
458
  }
451
459
  catch (err) {
452
460
  error = err;
453
461
  }
454
462
  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 }));
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 }));
456
464
  }
457
465
  if (error instanceof Error) {
458
466
  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.3.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.6.0",
25
+ "@e-mc/types": "^0.6.0",
26
+ "redis": "^4.6.7"
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;