@technicity/data-service-generator 0.11.0 → 0.11.1

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,18 @@
1
- import { RedisClientType } from 'redis';
2
- import { TResolveParams } from './IRuntime';
3
- import Stats from './Stats';
1
+ import { RedisClientType } 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
+ };
4
10
  declare class Cache {
5
11
  client: RedisClientType;
6
12
  waiting?: Set<() => void> | undefined;
7
13
  logs?: boolean;
8
14
  stats?: Stats;
9
- constructor(url: string, debug?: string[]);
15
+ constructor(redisConfig: RedisConfig, debug?: string[]);
10
16
  log(message: string): void;
11
17
  pending(): Promise<void>;
12
18
  from(input: TResolveParams): Promise<{
@@ -5,16 +5,24 @@ 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, tls, db, socketTimeout } = redisConfig;
13
+ const scheme = tls ? "rediss://" : "redis://";
14
+ const client = (this.client = (0, redis_1.createClient)({
15
+ url: `${scheme}${url}/${db}`,
16
+ socket: {
17
+ tls,
18
+ rejectUnauthorized: false,
19
+ connectTimeout: socketTimeout
20
+ }
21
+ }));
14
22
  client.connect();
15
23
  client.on("connect", () => {
16
24
  if (this.waiting)
17
- this.waiting.forEach(x => x());
25
+ this.waiting.forEach((x) => x());
18
26
  this.waiting = undefined;
19
27
  });
20
28
  if (debug?.includes("Stats"))
@@ -26,16 +34,17 @@ class Cache {
26
34
  }
27
35
  async pending() {
28
36
  if (this.waiting)
29
- return new Promise(res => this.waiting.add(res));
37
+ return new Promise((res) => this.waiting.add(res));
30
38
  }
31
39
  async from(input) {
32
40
  let { action, args, fields, resource } = input;
33
41
  const request = JSON.stringify({
34
- action, resource, args, fields
42
+ action,
43
+ resource,
44
+ args,
45
+ fields
35
46
  });
36
- const key = (0, crypto_1.createHash)('sha256')
37
- .update(request)
38
- .digest('hex');
47
+ const key = (0, crypto_1.createHash)("sha256").update(request).digest("hex");
39
48
  const cached = await this.read(key);
40
49
  return {
41
50
  request: key,
@@ -47,7 +56,7 @@ class Cache {
47
56
  const json = JSON.stringify(payload);
48
57
  const regex = /"uuid":"(.+?)"/g;
49
58
  const pending = [];
50
- for (let result; result = regex.exec(json);) {
59
+ for (let result; (result = regex.exec(json));) {
51
60
  const uuid = result[1];
52
61
  if (!uuid)
53
62
  continue;
@@ -56,10 +65,7 @@ class Cache {
56
65
  if (!pending.length)
57
66
  return;
58
67
  this.log(`insert: ${key.substring(0, 6)}`);
59
- await Promise.all([
60
- redis.set(`cache:${key}`, json),
61
- ...pending
62
- ]);
68
+ await Promise.all([redis.set(`cache:${key}`, json), ...pending]);
63
69
  }
64
70
  async read(key) {
65
71
  await this.pending();
@@ -76,13 +82,13 @@ class Cache {
76
82
  }
77
83
  async purge(...uuids) {
78
84
  const redis = this.client;
79
- await (0, utility_1.mapAsync)(uuids, uuid => {
85
+ await (0, utility_1.mapAsync)(uuids, (uuid) => {
80
86
  const getDependancies = redis.sMembers(`deps:${uuid}`);
81
87
  return [
82
- (0, utility_1.mapAsync)(getDependancies, key => {
88
+ (0, utility_1.mapAsync)(getDependancies, (key) => {
83
89
  const getDependants = redis.sMembers(`cached:${key}`);
84
90
  return [
85
- (0, utility_1.mapAsync)(getDependants, uuid => (redis.sRem(`deps:${uuid}`, [key]))),
91
+ (0, utility_1.mapAsync)(getDependants, (uuid) => redis.sRem(`deps:${uuid}`, [key])),
86
92
  redis.del(`cache:${key}`),
87
93
  redis.del(`cached:${key}`)
88
94
  ];
@@ -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");
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.1",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"