axe-api 1.0.1 → 1.0.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.
@@ -338,6 +338,9 @@ const putCache = (context, data) => __awaiter(void 0, void 0, void 0, function*
338
338
  }
339
339
  // Getting the redis service
340
340
  const redis = yield Services_1.IoCService.use("Redis");
341
+ if (!redis.isReady()) {
342
+ return;
343
+ }
341
344
  // Generating the cache key
342
345
  const key = (0, exports.toCacheKey)(context);
343
346
  // Setting the tags if the cache configuration of the model has been set as
@@ -357,12 +360,18 @@ const putCache = (context, data) => __awaiter(void 0, void 0, void 0, function*
357
360
  exports.putCache = putCache;
358
361
  const deleteCacheTagMembers = (key) => __awaiter(void 0, void 0, void 0, function* () {
359
362
  const redis = yield Services_1.IoCService.use("Redis");
363
+ if (!redis.isReady()) {
364
+ return;
365
+ }
360
366
  const members = yield redis.getTagMemebers(key);
361
367
  yield redis.delete(members);
362
368
  });
363
369
  exports.deleteCacheTagMembers = deleteCacheTagMembers;
364
370
  const cleanRelatedCachedObjectByModel = (model, config) => __awaiter(void 0, void 0, void 0, function* () {
365
371
  const redis = yield Services_1.IoCService.use("Redis");
372
+ if (!redis.isReady()) {
373
+ return;
374
+ }
366
375
  const prefix = (0, exports.toCachePrefix)(config === null || config === void 0 ? void 0 : config.cachePrefix);
367
376
  const tagPrefix = (config === null || config === void 0 ? void 0 : config.tagPrefix) ? config === null || config === void 0 ? void 0 : config.tagPrefix : "";
368
377
  const modelName = model.name.toLowerCase();
@@ -376,6 +385,9 @@ const cleanRelatedCachedObjectByModel = (model, config) => __awaiter(void 0, voi
376
385
  exports.cleanRelatedCachedObjectByModel = cleanRelatedCachedObjectByModel;
377
386
  const clearCacheTags = (tag) => __awaiter(void 0, void 0, void 0, function* () {
378
387
  const redis = yield Services_1.IoCService.use("Redis");
388
+ if (!redis.isReady()) {
389
+ return;
390
+ }
379
391
  const members = yield redis.getTagMemebers(tag);
380
392
  if (members.length > 0) {
381
393
  yield redis.delete(members);
@@ -4,7 +4,10 @@ type RedisClientOptions = Parameters<typeof createClient>[0];
4
4
  declare class RedisAdaptor implements ICacheAdaptor {
5
5
  private client;
6
6
  private prefix;
7
+ private isConnected;
7
8
  constructor(options: RedisClientOptions | undefined, prefix: string);
9
+ isReady(): boolean;
10
+ connect(): Promise<void>;
8
11
  get(key: string): Promise<string | null>;
9
12
  set(key: string, value: string, ttl: number): Promise<void>;
10
13
  tags(keys: string[], value: string): Promise<void>;
@@ -10,11 +10,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const redis_1 = require("redis");
13
+ const Services_1 = require("../../Services");
13
14
  class RedisAdaptor {
14
15
  constructor(options, prefix) {
15
16
  this.client = (0, redis_1.createClient)(options);
16
17
  this.prefix = prefix;
17
- this.client.connect();
18
+ this.isConnected = false;
19
+ }
20
+ isReady() {
21
+ return this.isConnected;
22
+ }
23
+ connect() {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ try {
26
+ yield this.client.connect();
27
+ this.isConnected = true;
28
+ }
29
+ catch (error) {
30
+ Services_1.LogService.error(`Redis Connection Error: ${error.message}`);
31
+ }
32
+ });
18
33
  }
19
34
  get(key) {
20
35
  return __awaiter(this, void 0, void 0, function* () {
@@ -19,6 +19,9 @@ exports.default = (context) => __awaiter(void 0, void 0, void 0, function* () {
19
19
  if (config === null || config === void 0 ? void 0 : config.enable) {
20
20
  // Getting the redis service
21
21
  const redis = yield Services_1.IoCService.use("Redis");
22
+ if (!redis.isReady()) {
23
+ return;
24
+ }
22
25
  // Generating the cache key
23
26
  const key = (0, Helpers_1.toCacheKey)(context);
24
27
  // Try to fetch the value via Redis
@@ -49,6 +49,7 @@ class ModelResolver {
49
49
  setCacheOptions(modelList) {
50
50
  return __awaiter(this, void 0, void 0, function* () {
51
51
  const api = Services_1.APIService.getInstance();
52
+ let needRedisConnection = false;
52
53
  // For each model should be analyzed
53
54
  for (const model of modelList.get()) {
54
55
  // For each cachable handler, developers are able to set a different
@@ -57,10 +58,18 @@ class ModelResolver {
57
58
  // API configuration, version configuration and the handler type are in
58
59
  // order. The following function gets the correct configuration
59
60
  const configuration = (0, Helpers_1.getModelCacheConfiguration)(model, api.config.cache, this.version.config.cache, handler);
61
+ // We should try to connect to Redis
62
+ if (configuration.enable) {
63
+ needRedisConnection = true;
64
+ }
60
65
  // We need to set this to use late in action
61
66
  model.setCacheConfiguration(handler, configuration);
62
67
  }
63
68
  }
69
+ if (needRedisConnection) {
70
+ const redis = yield Services_1.IoCService.use("Redis");
71
+ redis.connect();
72
+ }
64
73
  });
65
74
  }
66
75
  setModelRelations(modelList) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axe-api",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "AXE API is a simple tool to create Rest APIs quickly.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",