@technicity/data-service-generator 0.11.0 → 0.11.2

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.
@@ -1,12 +1,19 @@
1
- import { RedisClientType } from 'redis';
2
- import { TResolveParams } from './IRuntime';
3
- import Stats from './Stats';
1
+ import { RedisClientType, RedisClusterType } from "redis";
2
+ import { TResolveParams } from "./IRuntime";
3
+ import Stats from "./Stats";
4
+ export declare type RedisConfig = {
5
+ url: string;
6
+ tls?: boolean;
7
+ db?: number;
8
+ socketTimeout?: number;
9
+ clusterMode?: boolean;
10
+ };
4
11
  declare class Cache {
5
- client: RedisClientType;
12
+ client: RedisClientType | RedisClusterType;
6
13
  waiting?: Set<() => void> | undefined;
7
14
  logs?: boolean;
8
15
  stats?: Stats;
9
- constructor(url: string, debug?: string[]);
16
+ constructor(redisConfig: RedisConfig, debug?: string[]);
10
17
  log(message: string): void;
11
18
  pending(): Promise<void>;
12
19
  from(input: TResolveParams): Promise<{
@@ -5,16 +5,48 @@ const redis_1 = require("redis");
5
5
  const utility_1 = require("./lib/utility");
6
6
  const Stats_1 = require("./Stats");
7
7
  class Cache {
8
- constructor(url, debug) {
8
+ constructor(redisConfig, debug) {
9
9
  this.waiting = new Set();
10
10
  if (debug?.includes("Cache"))
11
11
  this.logs = true;
12
- const client = this.client =
13
- (0, redis_1.createClient)({ url: `redis://${url}` });
12
+ const { url } = redisConfig;
13
+ const tls = redisConfig.tls ? true : false;
14
+ const clusterMode = redisConfig.clusterMode ? true : false;
15
+ const scheme = tls ? "rediss://" : "redis://";
16
+ const db = redisConfig.db == null ? 0 : redisConfig.db;
17
+ const socketTimeout = redisConfig.socketTimeout == null ? 50000 : redisConfig.socketTimeout;
18
+ let client = undefined;
19
+ if (clusterMode) {
20
+ client = (0, redis_1.createCluster)({
21
+ rootNodes: [
22
+ {
23
+ url: `${scheme}${url}`
24
+ }
25
+ ],
26
+ defaults: {
27
+ socket: {
28
+ tls,
29
+ rejectUnauthorized: false,
30
+ connectTimeout: socketTimeout
31
+ }
32
+ }
33
+ });
34
+ }
35
+ else {
36
+ client = (0, redis_1.createClient)({
37
+ url: `${scheme}${url}/${db}`,
38
+ socket: {
39
+ tls,
40
+ rejectUnauthorized: false,
41
+ connectTimeout: socketTimeout
42
+ }
43
+ });
44
+ }
45
+ this.client = client;
14
46
  client.connect();
15
47
  client.on("connect", () => {
16
48
  if (this.waiting)
17
- this.waiting.forEach(x => x());
49
+ this.waiting.forEach((x) => x());
18
50
  this.waiting = undefined;
19
51
  });
20
52
  if (debug?.includes("Stats"))
@@ -26,16 +58,17 @@ class Cache {
26
58
  }
27
59
  async pending() {
28
60
  if (this.waiting)
29
- return new Promise(res => this.waiting.add(res));
61
+ return new Promise((res) => this.waiting.add(res));
30
62
  }
31
63
  async from(input) {
32
64
  let { action, args, fields, resource } = input;
33
65
  const request = JSON.stringify({
34
- action, resource, args, fields
66
+ action,
67
+ resource,
68
+ args,
69
+ fields
35
70
  });
36
- const key = (0, crypto_1.createHash)('sha256')
37
- .update(request)
38
- .digest('hex');
71
+ const key = (0, crypto_1.createHash)("sha256").update(request).digest("hex");
39
72
  const cached = await this.read(key);
40
73
  return {
41
74
  request: key,
@@ -47,7 +80,7 @@ class Cache {
47
80
  const json = JSON.stringify(payload);
48
81
  const regex = /"uuid":"(.+?)"/g;
49
82
  const pending = [];
50
- for (let result; result = regex.exec(json);) {
83
+ for (let result; (result = regex.exec(json));) {
51
84
  const uuid = result[1];
52
85
  if (!uuid)
53
86
  continue;
@@ -56,10 +89,7 @@ class Cache {
56
89
  if (!pending.length)
57
90
  return;
58
91
  this.log(`insert: ${key.substring(0, 6)}`);
59
- await Promise.all([
60
- redis.set(`cache:${key}`, json),
61
- ...pending
62
- ]);
92
+ await Promise.all([redis.set(`cache:${key}`, json), ...pending]);
63
93
  }
64
94
  async read(key) {
65
95
  await this.pending();
@@ -76,13 +106,13 @@ class Cache {
76
106
  }
77
107
  async purge(...uuids) {
78
108
  const redis = this.client;
79
- await (0, utility_1.mapAsync)(uuids, uuid => {
109
+ await (0, utility_1.mapAsync)(uuids, (uuid) => {
80
110
  const getDependancies = redis.sMembers(`deps:${uuid}`);
81
111
  return [
82
- (0, utility_1.mapAsync)(getDependancies, key => {
112
+ (0, utility_1.mapAsync)(getDependancies, (key) => {
83
113
  const getDependants = redis.sMembers(`cached:${key}`);
84
114
  return [
85
- (0, utility_1.mapAsync)(getDependants, uuid => (redis.sRem(`deps:${uuid}`, [key]))),
115
+ (0, utility_1.mapAsync)(getDependants, (uuid) => redis.sRem(`deps:${uuid}`, [key])),
86
116
  redis.del(`cache:${key}`),
87
117
  redis.del(`cached:${key}`)
88
118
  ];
@@ -1,3 +1,4 @@
1
+ import { RedisConfig } from "./Cache";
1
2
  import type { IRuntime, TMiddleware, TResolveParams, IArtifacts, ISupplementClientOpts } from "./IRuntime";
2
3
  export declare class RuntimeMySQL implements IRuntime {
3
4
  #private;
@@ -5,7 +6,7 @@ export declare class RuntimeMySQL implements IRuntime {
5
6
  [k: string]: any;
6
7
  }, otherOpts: {
7
8
  supplementClientOpts?: ISupplementClientOpts;
8
- redisHost?: string;
9
+ redis?: RedisConfig;
9
10
  }, artifacts: IArtifacts);
10
11
  resolve(input: TResolveParams): Promise<any>;
11
12
  $queryRaw(sql: string, values?: any[]): Promise<any>;
@@ -24,8 +24,8 @@ class RuntimeMySQL {
24
24
  _RuntimeMySQL_clientCache.set(this, void 0);
25
25
  _RuntimeMySQL_middlewareHandler.set(this, void 0);
26
26
  __classPrivateFieldSet(this, _RuntimeMySQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
27
- if (otherOpts.redisHost)
28
- __classPrivateFieldSet(this, _RuntimeMySQL_clientCache, new Cache_1.default(otherOpts.redisHost, clientOpts?.debug), "f");
27
+ if (otherOpts.redis)
28
+ __classPrivateFieldSet(this, _RuntimeMySQL_clientCache, new Cache_1.default(otherOpts.redis, clientOpts?.debug), "f");
29
29
  if (otherOpts.supplementClientOpts) {
30
30
  clientOpts = {
31
31
  supportBigNumbers: true,
@@ -48,7 +48,7 @@ class RuntimeMySQL {
48
48
  }
49
49
  return next();
50
50
  },
51
- ...clientOpts,
51
+ ...clientOpts
52
52
  };
53
53
  }
54
54
  else {
@@ -62,7 +62,7 @@ class RuntimeMySQL {
62
62
  }
63
63
  return next();
64
64
  },
65
- ...clientOpts,
65
+ ...clientOpts
66
66
  };
67
67
  }
68
68
  __classPrivateFieldSet(this, _RuntimeMySQL_mysqlClient, new MySQL_1.MySQL(clientOpts), "f");
@@ -1,7 +1,7 @@
1
- import { RedisClientType } from '@redis/client';
1
+ import { RedisClientType, RedisClusterType } from "@redis/client";
2
2
  declare class Stats {
3
- client: RedisClientType<any>;
4
- constructor(client: RedisClientType<any>);
3
+ client: RedisClientType<any> | RedisClusterType<any>;
4
+ constructor(client: RedisClientType<any> | RedisClusterType<any>);
5
5
  updateStats: (ms: number, category: string) => Promise<void>;
6
6
  }
7
7
  declare namespace timer {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"