@obisey/nest 0.1.23 → 0.1.25

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.23",
3
+ "version": "0.1.25",
4
4
  "description": "NestJS utilities and base classes by Obisey",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -141,30 +141,78 @@ function BaseService(modelo) {
141
141
  });
142
142
  return instance;
143
143
  })), //Revisar
144
- (resultado) => Promise.resolve(resultado.map((n) => n.get({ plain: true }))), //transformar
144
+ (resultado) => {
145
+ console.log(`[TRANSFORMAR] ${modelo}: transforming ${resultado.length} items`);
146
+ const plain = resultado.map((n) => n.get({ plain: true }));
147
+ if (plain.length > 0) {
148
+ console.log(`[TRANSFORMAR-FIRST] ${modelo}:`, plain[0]);
149
+ }
150
+ return Promise.resolve(plain);
151
+ }, //transformar
145
152
  () => new Promise((resolve) => {
146
- resolve(repository.findAll(value)); //Pedir
147
- }), () => new Promise(async (resolve) => {
153
+ repository.findAll(value).then(result => {
154
+ console.log(`[PEDIR-DB] ${modelo}: found ${result.length} items from DB`);
155
+ if (result.length > 0) {
156
+ console.log(`[PEDIR-FIRST-ITEM] ${modelo}:`, {
157
+ id: result[0].id,
158
+ hasDataValues: !!result[0].dataValues,
159
+ });
160
+ }
161
+ resolve(result);
162
+ });
163
+ }), //Pedir
164
+ () => new Promise(async (resolve) => {
165
+ console.log(`[OBSERVAR-START] ${modelo}: checking Redis for nkey=${nkey.substring(0, 50)}`);
148
166
  let e = await this.redis.hExists(nkey, peticion);
167
+ console.log(`[OBSERVAR-EXISTS] ${modelo}: exists=${e}`);
149
168
  if (e) {
150
169
  let ids = await this.redis.hGet(nkey, peticion);
170
+ console.log(`[OBSERVAR-IDS] ${modelo}: ids from Redis = ${ids ? ids.substring(0, 60) : 'undefined'}`);
151
171
  if (ids) {
152
172
  Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hExists(pkey(id), 'item'))).then((exists) => {
153
173
  if (!exists.includes(0)) {
154
- Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hGet(pkey(id), 'item'))).then(resolve);
174
+ console.log(`[OBSERVAR-ALL-EXIST] ${modelo}: all items exist, fetching from Redis`);
175
+ Promise.all(JSON.parse(ids).map(async (id) => await this.redis.hGet(pkey(id), 'item'))).then(items => {
176
+ console.log(`[OBSERVAR-RESOLVE-ITEMS] ${modelo}: resolving ${items.length} items from Redis`);
177
+ console.log(`[OBSERVAR-ITEMS-CONTENT] ${modelo}: [0]=${items[0] ? items[0].substring(0, 80) : 'NULL'}`);
178
+ resolve(items);
179
+ });
155
180
  }
156
- else
181
+ else {
182
+ console.log(`[OBSERVAR-INCOMPLETE] ${modelo}: some items missing in Redis, fallback to DB`);
157
183
  resolve(undefined);
184
+ }
158
185
  });
159
186
  }
160
- else
187
+ else {
188
+ console.log(`[OBSERVAR-NO-IDS] ${modelo}: no IDs in Redis, fallback to DB`);
161
189
  resolve(undefined);
190
+ }
162
191
  }
163
- else
192
+ else {
193
+ console.log(`[OBSERVAR-NOT-EXISTS] ${modelo}: nkey not in Redis, fallback to DB`);
164
194
  resolve(undefined);
195
+ }
165
196
  }), //observar
166
197
  (__, resultado) => {
167
- return this.redis.hSet(nkey, peticion, JSON.stringify(resultado.map((n) => (0, getModelId_1.getModelId)(n))));
198
+ console.log(`[ACTUALIZAR-START] ${modelo}: saving ${resultado.length} items to Redis`);
199
+ if (resultado.length > 0) {
200
+ console.log(`[ACTUALIZAR-FIRST-ITEM] ${modelo}: resultado[0]=`, {
201
+ id: resultado[0].id,
202
+ plainObj: resultado[0].get({ plain: true }),
203
+ });
204
+ }
205
+ const idsToSave = resultado.map((n) => (0, getModelId_1.getModelId)(n));
206
+ console.log(`[ACTUALIZAR-IDS] ${modelo}: IDs=[${idsToSave}]`);
207
+ // SAVE INDIVIDUAL ITEMS
208
+ const promises = resultado.map((item) => {
209
+ const id = (0, getModelId_1.getModelId)(item);
210
+ const plainData = item.get({ plain: true });
211
+ return this.redis.hSet(pkey(id), 'item', JSON.stringify(plainData));
212
+ });
213
+ return Promise.all(promises).then(() => {
214
+ return this.redis.hSet(nkey, peticion, JSON.stringify(idsToSave));
215
+ });
168
216
  });
169
217
  await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.map((n) => n.get({ plain: true })));
170
218
  // if (isDev)