@obisey/nest 0.1.31 → 0.1.33

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.31",
3
+ "version": "0.1.33",
4
4
  "description": "NestJS utilities and base classes by Obisey",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -412,11 +412,7 @@ function BaseResolver(modelo) {
412
412
  { nombre: modelo, id: item.id },
413
413
  { nombre: planeta },
414
414
  ],
415
- }), (cache) => {
416
- const parsed = JSON.parse(cache);
417
- const instance = Clase.build(parsed, { raw: false, isNewRecord: false });
418
- return Promise.resolve(Object.assign(instance, parsed));
419
- }, //Revisar
415
+ }), (cache) => Promise.resolve(new Clase(JSON.parse(cache))), //Revisar
420
416
  (resultado) => Promise.resolve(resultado ? resultado.get({ plain: true }) : null), //transformar
421
417
  () => item.$get(planeta, { where: (0, formarwhere_1.formarWhere)(where) }), //Pedir
422
418
  () => new Promise(async (resolve) => {
@@ -482,11 +478,7 @@ function BaseResolver(modelo) {
482
478
  { nombre: modelo, id: item.id },
483
479
  { nombre: planeta },
484
480
  ],
485
- }), (cache) => Promise.resolve(cache.map((n) => {
486
- const parsed = JSON.parse(n);
487
- const instance = Clase.build(parsed, { raw: false, isNewRecord: false });
488
- return Object.assign(instance, parsed);
489
- })), //Revisar
481
+ }), (cache) => Promise.resolve(cache.map((n) => new Clase(JSON.parse(n)))), //Revisar
490
482
  (resultado) => Promise.resolve(resultado.map((n) => n.get({ plain: true }))), //transformar
491
483
  () => item.$get(planeta, { where: (0, formarwhere_1.formarWhere)(where) }), //Pedir
492
484
  () => new Promise(async (resolve) => {
@@ -10,7 +10,6 @@ const cifrado_1 = require("./utils/cifrado");
10
10
  const keyInterface_1 = require("./utils/keyInterface");
11
11
  exports.redis_on = true;
12
12
  const paginar_1 = require("./utils/paginar");
13
- const getModelId_1 = require("./utils/getModelId");
14
13
  var isDev = false;
15
14
  var stringifWhere = (where) => (0, lodash_1.isObject)(where)
16
15
  ? JSON.stringify(Object.entries(where).reduce((ac, [a, v]) => {
@@ -153,38 +152,7 @@ function BaseService(modelo) {
153
152
  () => new Promise((resolve) => {
154
153
  repository.findAll(value).then(result => {
155
154
  console.log(`[PEDIR-DB] ${modelo}: found ${result.length} items from DB`);
156
- if (result.length > 0) {
157
- console.log(`[PEDIR-FIRST-ITEM] ${modelo}:`, {
158
- id: result[0].id,
159
- hasDataValues: !!result[0].dataValues,
160
- });
161
- }
162
- // SAVE ITEMS HERE (before transform)
163
- console.log(`[PEDIR-SAVING-ITEMS] ${modelo}: saving ${result.length} items to Redis`);
164
- const savePromises = result.map((item, idx) => {
165
- const id = (0, getModelId_1.getModelId)(item);
166
- const plainData = item.get({ plain: true });
167
- console.log(`[PEDIR-ITEM-${idx}] ${modelo}: id=${id}, saving to pkey=${pkey(id).substring(0, 50)}`);
168
- return this.redis.hSet(pkey(id), 'item', JSON.stringify(plainData))
169
- .then(res => {
170
- console.log(`[PEDIR-ITEM-${idx}-SAVED] ${modelo}: id=${id}, result=${res}`);
171
- return res;
172
- })
173
- .catch(err => {
174
- console.log(`[PEDIR-ITEM-${idx}-ERROR] ${modelo}: id=${id}, error=${err.message}`);
175
- throw err;
176
- });
177
- });
178
- Promise.all(savePromises)
179
- .then(() => {
180
- console.log(`[PEDIR-ITEMS-SAVED] ${modelo}: all ${result.length} items saved successfully`);
181
- console.log(`[PEDIR-BEFORE-RESOLVE] ${modelo}: about to resolve with ${result.length} items`);
182
- resolve(result);
183
- })
184
- .catch(err => {
185
- console.log(`[PEDIR-SAVE-ERROR] ${modelo}: error=${err.message}, but resolving anyway with ${result.length} items`);
186
- resolve(result);
187
- });
155
+ resolve(result);
188
156
  });
189
157
  }), //Pedir
190
158
  () => new Promise(async (resolve) => {
@@ -202,8 +170,8 @@ function BaseService(modelo) {
202
170
  console.log(`[OBSERVAR-EXISTS-${idx}] ${modelo}: id=${id}, hExists(pkey(id), 'item')=${exists}`);
203
171
  return exists;
204
172
  })).then((exists) => {
205
- console.log(`[OBSERVAR-EXISTS-ARRAY] ${modelo}: all checks done, exists=[${exists}], includes(0)=${exists.includes(0)}`);
206
- if (!exists.includes(0)) {
173
+ console.log(`[OBSERVAR-EXISTS-ARRAY] ${modelo}: all checks done, exists=[${exists}], includes(false)=${exists.includes(false)}`);
174
+ if (!exists.includes(false)) {
207
175
  console.log(`[OBSERVAR-ALL-EXIST] ${modelo}: all items exist, fetching from Redis`);
208
176
  Promise.all(idArray.map(async (id, idx) => {
209
177
  const item = await this.redis.hGet(pkey(id), 'item');
@@ -217,7 +185,7 @@ function BaseService(modelo) {
217
185
  });
218
186
  }
219
187
  else {
220
- console.log(`[OBSERVAR-INCOMPLETE] ${modelo}: some items missing. exists.includes(0)=true, fallback to DB`);
188
+ console.log(`[OBSERVAR-INCOMPLETE] ${modelo}: some items missing. exists.includes(false)=true, fallback to DB`);
221
189
  resolve(undefined);
222
190
  }
223
191
  });
@@ -233,22 +201,8 @@ function BaseService(modelo) {
233
201
  }
234
202
  }), //observar
235
203
  (__, resultado) => {
236
- console.log(`[ACTUALIZAR-START] ${modelo}: saving IDs to Redis, nkey=${nkey.substring(0, 50)}, peticion=${peticion.substring(0, 30)}`);
237
- const idsToSave = resultado.map((n) => {
238
- // resultado is already transformed to plain JSON
239
- return n.id || n;
240
- });
241
- console.log(`[ACTUALIZAR-IDS] ${modelo}: IDs=[${idsToSave}], count=${idsToSave.length}`);
242
- console.log(`[ACTUALIZAR-BEFORE-HSET] ${modelo}: about to hSet(${nkey.substring(0, 40)}, ${peticion.substring(0, 20)}, [IDs])`);
243
- return this.redis.hSet(nkey, peticion, JSON.stringify(idsToSave))
244
- .then(res => {
245
- console.log(`[ACTUALIZAR-HSET-SUCCESS] ${modelo}: result=${res}`);
246
- return res;
247
- })
248
- .catch(err => {
249
- console.log(`[ACTUALIZAR-HSET-ERROR] ${modelo}: error=${err.message}`);
250
- throw err;
251
- });
204
+ // LEGACY PATTERN: Save individual items FIRST, then save IDs
205
+ return Promise.all(resultado.map((n) => this.redis.hSet(pkey(n.id || n), 'item', JSON.stringify(n)))).then(() => this.redis.hSet(nkey, peticion, JSON.stringify(resultado.map((n) => n.id || n))));
252
206
  });
253
207
  await (0, paginar_1.paginar)(this.cacheManager, key.IdEmpresa, pagina, modelo, resultado.map((n) => n.get({ plain: true })));
254
208
  // if (isDev)