@obisey/nest 0.1.12 → 0.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@obisey/nest",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "NestJS utilities and base classes by Obisey",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,3 +1,5 @@
1
+ import { Cache } from 'cache-manager';
2
+ import { RedisClientType } from 'redis';
1
3
  import { Key } from './utils/keyInterface';
2
4
  export declare const redis_on = true;
3
5
  type Ctor = new (...a: any[]) => any;
@@ -13,7 +15,8 @@ interface Peticion {
13
15
  }
14
16
  export declare function BaseService(modelo: string): {
15
17
  new <T extends {
16
- cacheManager: any;
18
+ readonly redis: RedisClientType;
19
+ readonly cacheManager: Cache;
17
20
  buscarXid<Ctor>({ value: { id, cacheable }, key, Clase, repository, pagina, }: Peticion): Promise<Ctor>;
18
21
  buscarTodos<Ctor>({ value, key, Clase, repository, pagina, cacheable, }: Peticion): Promise<Ctor[]>;
19
22
  crear<Ctor>({ value, key, Clase, repository, borrarSSR, }: Peticion): Promise<Ctor>;
@@ -48,9 +51,10 @@ export declare function BaseService(modelo: string): {
48
51
  value: any;
49
52
  repository: any;
50
53
  }): Promise<Ctor>;
51
- eliminarCache(items: string[], borrarSSR?: boolean): Promise<any[]>;
52
- }>(cacheManager: any): {
53
- cacheManager: any;
54
+ eliminarCache(items: string[], borrarSSR?: boolean): Promise<boolean[][]>;
55
+ }>(redis: RedisClientType, cacheManager: Cache): {
56
+ readonly redis: RedisClientType;
57
+ readonly cacheManager: Cache;
54
58
  buscarXid<Ctor>({ value: { id, cacheable }, key, Clase, repository, pagina, }: Peticion): Promise<Ctor>;
55
59
  buscarTodos<Ctor>({ value, key, Clase, repository, pagina, cacheable, }: Peticion): Promise<Ctor[]>;
56
60
  crear<Ctor>({ value, key, Clase, repository, borrarSSR, }: Peticion): Promise<Ctor>;
@@ -85,7 +89,7 @@ export declare function BaseService(modelo: string): {
85
89
  value: any;
86
90
  repository: any;
87
91
  }): Promise<Ctor>;
88
- eliminarCache(items: string[], borrarSSR?: boolean): Promise<any[]>;
92
+ eliminarCache(items: string[], borrarSSR?: boolean): Promise<boolean[][]>;
89
93
  };
90
94
  };
91
95
  export {};
@@ -33,7 +33,9 @@ var stringifyInclude = ({ where, as, include }) => ({
33
33
  });
34
34
  function BaseService(modelo) {
35
35
  return class BaseService {
36
- constructor(cacheManager) {
36
+ constructor(redis, // o ReturnType<typeof createClient>
37
+ cacheManager) {
38
+ this.redis = redis;
37
39
  this.cacheManager = cacheManager;
38
40
  }
39
41
  buscarXid({ value: { id, cacheable }, key, Clase, repository, pagina, }) {
@@ -61,19 +63,19 @@ function BaseService(modelo) {
61
63
  () => repository.findByPk(id), //Pedir
62
64
  () => new Promise(async (resolve) => {
63
65
  //observar
64
- let e = await this.cacheManager.store.hexists(nkey, 'item');
66
+ let e = await this.redis.hExists(nkey, 'item');
65
67
  if (e) {
66
- let item = await this.cacheManager.store.hget(nkey, 'item');
68
+ let item = await this.redis.hGet(nkey, 'item');
67
69
  resolve(item);
68
70
  }
69
71
  else {
70
72
  resolve(undefined);
71
73
  }
72
74
  }), (__, resultado) => {
73
- if (resultado)
74
- return this.cacheManager.store.hset(nkey, 'item', resultado);
75
- else
76
- return false;
75
+ if (resultado) {
76
+ this.redis.hSet(nkey, 'item', resultado);
77
+ }
78
+ return Promise.resolve(false);
77
79
  });
78
80
  if (resultado)
79
81
  await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.get({ plain: true }));
@@ -131,13 +133,13 @@ function BaseService(modelo) {
131
133
  () => new Promise((resolve) => {
132
134
  resolve(repository.findAll(value)); //Pedir
133
135
  }), () => new Promise(async (resolve) => {
134
- let e = await this.cacheManager.store.hexists(nkey, peticion);
136
+ let e = await this.redis.hExists(nkey, peticion);
135
137
  if (e) {
136
- let ids = await this.cacheManager.store.hget(nkey, peticion);
138
+ let ids = await this.redis.hGet(nkey, peticion);
137
139
  if (ids) {
138
- Promise.all(JSON.parse(ids).map(async (id) => await this.cacheManager.store.hexists(pkey(id), 'item'))).then((exists) => {
140
+ Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hExists(pkey(id), 'item'))).then((exists) => {
139
141
  if (!exists.includes(0)) {
140
- Promise.all(JSON.parse(ids).map(async (id) => await this.cacheManager.store.hget(pkey(id), 'item'))).then(resolve);
142
+ Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hGet(pkey(id), 'item'))).then(resolve);
141
143
  }
142
144
  else
143
145
  resolve(undefined);
@@ -150,7 +152,7 @@ function BaseService(modelo) {
150
152
  resolve(undefined);
151
153
  }), //observar
152
154
  (__, resultado) => {
153
- return this.cacheManager.store.hset(nkey, peticion, JSON.stringify(resultado.map((n) => n.id)));
155
+ return this.redis.hSet(nkey, peticion, JSON.stringify(resultado.map((n) => n.id)));
154
156
  });
155
157
  await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.map((n) => n.get({ plain: true })));
156
158
  // if (isDev)
@@ -244,9 +246,9 @@ function BaseService(modelo) {
244
246
  (resultado) => Promise.resolve(JSON.stringify(resultado)), //transformar
245
247
  () => repository.count({ ...value, distinct: false }), //Pedir
246
248
  () => new Promise(async (resolve) => {
247
- let e = await this.cacheManager.store.hexists(nkey, peticion);
249
+ let e = await this.redis.hExists(nkey, peticion);
248
250
  if (e) {
249
- let item = await this.cacheManager.store.hget(nkey, peticion);
251
+ let item = await this.redis.hGet(nkey, peticion);
250
252
  if (item) {
251
253
  resolve(item);
252
254
  }
@@ -259,7 +261,7 @@ function BaseService(modelo) {
259
261
  }
260
262
  }), //observar
261
263
  (__, resultado) => {
262
- return this.cacheManager.store.hset(nkey, peticion, resultado);
264
+ return this.redis.hSet(nkey, peticion, resultado);
263
265
  });
264
266
  // let key$ : string = transformarKey({ nombre : 'contar', ...key, include, where : stringifWhere(where) })
265
267
  // const cache = await this.cacheManager.get(key$)
@@ -326,7 +328,7 @@ function BaseService(modelo) {
326
328
  try {
327
329
  // imprimir(`${modelo} ${value.id} destruir`);
328
330
  let resultado = await repository.destroy(value);
329
- this.cacheManager
331
+ this.redis
330
332
  .keys(`*:${modelo}*`) //Obten todos los keys
331
333
  .then((keys) => Promise.all(keys.map((key) => this.cacheManager.del(key))));
332
334
  resolve(resultado);
@@ -387,7 +389,7 @@ function BaseService(modelo) {
387
389
  // borrarSSR,
388
390
  // ),
389
391
  // );
390
- return Promise.all(items.map(async (item) => this.cacheManager
392
+ return Promise.all(items.map(async (item) => this.redis
391
393
  .keys(`*${item}*`)
392
394
  .then((keys) => Promise.all(keys.map((key) => this.cacheManager.del(key))))));
393
395
  }