@obisey/nest 0.1.12 → 0.1.14
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 +1 -1
- package/prototipos/baseService.d.ts +9 -5
- package/prototipos/baseService.js +18 -17
package/package.json
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Cache } from 'cache-manager';
|
|
2
|
+
import { createClient } 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
|
-
|
|
18
|
+
readonly redis: ReturnType<typeof createClient>;
|
|
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<
|
|
52
|
-
}>(cacheManager:
|
|
53
|
-
|
|
54
|
+
eliminarCache(items: string[], borrarSSR?: boolean): Promise<boolean[][]>;
|
|
55
|
+
}>(redis: ReturnType<typeof createClient>, cacheManager: Cache): {
|
|
56
|
+
readonly redis: ReturnType<typeof createClient>;
|
|
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<
|
|
92
|
+
eliminarCache(items: string[], borrarSSR?: boolean): Promise<boolean[][]>;
|
|
89
93
|
};
|
|
90
94
|
};
|
|
91
95
|
export {};
|
|
@@ -33,7 +33,8 @@ var stringifyInclude = ({ where, as, include }) => ({
|
|
|
33
33
|
});
|
|
34
34
|
function BaseService(modelo) {
|
|
35
35
|
return class BaseService {
|
|
36
|
-
constructor(cacheManager) {
|
|
36
|
+
constructor(redis, cacheManager) {
|
|
37
|
+
this.redis = redis;
|
|
37
38
|
this.cacheManager = cacheManager;
|
|
38
39
|
}
|
|
39
40
|
buscarXid({ value: { id, cacheable }, key, Clase, repository, pagina, }) {
|
|
@@ -61,19 +62,19 @@ function BaseService(modelo) {
|
|
|
61
62
|
() => repository.findByPk(id), //Pedir
|
|
62
63
|
() => new Promise(async (resolve) => {
|
|
63
64
|
//observar
|
|
64
|
-
let e = await this.
|
|
65
|
+
let e = await this.redis.hExists(nkey, 'item');
|
|
65
66
|
if (e) {
|
|
66
|
-
let item = await this.
|
|
67
|
+
let item = await this.redis.hGet(nkey, 'item');
|
|
67
68
|
resolve(item);
|
|
68
69
|
}
|
|
69
70
|
else {
|
|
70
71
|
resolve(undefined);
|
|
71
72
|
}
|
|
72
73
|
}), (__, resultado) => {
|
|
73
|
-
if (resultado)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
if (resultado) {
|
|
75
|
+
this.redis.hSet(nkey, 'item', resultado);
|
|
76
|
+
}
|
|
77
|
+
return Promise.resolve(false);
|
|
77
78
|
});
|
|
78
79
|
if (resultado)
|
|
79
80
|
await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.get({ plain: true }));
|
|
@@ -131,13 +132,13 @@ function BaseService(modelo) {
|
|
|
131
132
|
() => new Promise((resolve) => {
|
|
132
133
|
resolve(repository.findAll(value)); //Pedir
|
|
133
134
|
}), () => new Promise(async (resolve) => {
|
|
134
|
-
let e = await this.
|
|
135
|
+
let e = await this.redis.hExists(nkey, peticion);
|
|
135
136
|
if (e) {
|
|
136
|
-
let ids = await this.
|
|
137
|
+
let ids = await this.redis.hGet(nkey, peticion);
|
|
137
138
|
if (ids) {
|
|
138
|
-
Promise.all(JSON.parse(ids).map(async (id) => await this.
|
|
139
|
+
Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hExists(pkey(id), 'item'))).then((exists) => {
|
|
139
140
|
if (!exists.includes(0)) {
|
|
140
|
-
Promise.all(JSON.parse(ids).map(async (id) => await this.
|
|
141
|
+
Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hGet(pkey(id), 'item'))).then(resolve);
|
|
141
142
|
}
|
|
142
143
|
else
|
|
143
144
|
resolve(undefined);
|
|
@@ -150,7 +151,7 @@ function BaseService(modelo) {
|
|
|
150
151
|
resolve(undefined);
|
|
151
152
|
}), //observar
|
|
152
153
|
(__, resultado) => {
|
|
153
|
-
return this.
|
|
154
|
+
return this.redis.hSet(nkey, peticion, JSON.stringify(resultado.map((n) => n.id)));
|
|
154
155
|
});
|
|
155
156
|
await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.map((n) => n.get({ plain: true })));
|
|
156
157
|
// if (isDev)
|
|
@@ -244,9 +245,9 @@ function BaseService(modelo) {
|
|
|
244
245
|
(resultado) => Promise.resolve(JSON.stringify(resultado)), //transformar
|
|
245
246
|
() => repository.count({ ...value, distinct: false }), //Pedir
|
|
246
247
|
() => new Promise(async (resolve) => {
|
|
247
|
-
let e = await this.
|
|
248
|
+
let e = await this.redis.hExists(nkey, peticion);
|
|
248
249
|
if (e) {
|
|
249
|
-
let item = await this.
|
|
250
|
+
let item = await this.redis.hGet(nkey, peticion);
|
|
250
251
|
if (item) {
|
|
251
252
|
resolve(item);
|
|
252
253
|
}
|
|
@@ -259,7 +260,7 @@ function BaseService(modelo) {
|
|
|
259
260
|
}
|
|
260
261
|
}), //observar
|
|
261
262
|
(__, resultado) => {
|
|
262
|
-
return this.
|
|
263
|
+
return this.redis.hSet(nkey, peticion, resultado);
|
|
263
264
|
});
|
|
264
265
|
// let key$ : string = transformarKey({ nombre : 'contar', ...key, include, where : stringifWhere(where) })
|
|
265
266
|
// const cache = await this.cacheManager.get(key$)
|
|
@@ -326,7 +327,7 @@ function BaseService(modelo) {
|
|
|
326
327
|
try {
|
|
327
328
|
// imprimir(`${modelo} ${value.id} destruir`);
|
|
328
329
|
let resultado = await repository.destroy(value);
|
|
329
|
-
this.
|
|
330
|
+
this.redis
|
|
330
331
|
.keys(`*:${modelo}*`) //Obten todos los keys
|
|
331
332
|
.then((keys) => Promise.all(keys.map((key) => this.cacheManager.del(key))));
|
|
332
333
|
resolve(resultado);
|
|
@@ -387,7 +388,7 @@ function BaseService(modelo) {
|
|
|
387
388
|
// borrarSSR,
|
|
388
389
|
// ),
|
|
389
390
|
// );
|
|
390
|
-
return Promise.all(items.map(async (item) => this.
|
|
391
|
+
return Promise.all(items.map(async (item) => this.redis
|
|
391
392
|
.keys(`*${item}*`)
|
|
392
393
|
.then((keys) => Promise.all(keys.map((key) => this.cacheManager.del(key))))));
|
|
393
394
|
}
|