@roit/roit-data-firestore 1.2.43 → 1.2.45

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.
Files changed (36) hide show
  1. package/dist/archive/ArchivePluginRegistry.d.ts +78 -0
  2. package/dist/archive/ArchivePluginRegistry.js +135 -0
  3. package/dist/archive/ArchiveService.d.ts +61 -11
  4. package/dist/archive/ArchiveService.js +138 -120
  5. package/dist/archive/IArchivePlugin.d.ts +102 -0
  6. package/dist/archive/IArchivePlugin.js +12 -0
  7. package/dist/archive/index.d.ts +2 -0
  8. package/dist/archive/index.js +11 -0
  9. package/dist/cache/CacheResolver.js +6 -5
  10. package/dist/config/ArchiveConfig.d.ts +17 -12
  11. package/dist/config/ArchiveConfig.js +25 -41
  12. package/dist/config/BaseRepository.js +1 -1
  13. package/dist/config/ReadonlyRepository.js +1 -1
  14. package/dist/exception/RepositoryException.js +1 -1
  15. package/dist/index.d.ts +4 -0
  16. package/dist/index.js +10 -1
  17. package/dist/model/CacheProviders.d.ts +1 -2
  18. package/dist/model/CacheProviders.js +1 -2
  19. package/dist/query/ManualQueryHelper.js +0 -1
  20. package/dist/query/QueryPredicateFunctionTransform.js +4 -1
  21. package/dist/template/FunctionAggregationTemplate.txt +1 -1
  22. package/dist/template/FunctionAverageTemplate.txt +1 -1
  23. package/dist/template/FunctionCountTemplate.txt +20 -25
  24. package/dist/template/FunctionCreateOrUpdateTemplate.txt +69 -20
  25. package/dist/template/FunctionCreateTemplate.txt +15 -13
  26. package/dist/template/FunctionDeleteTemplate.txt +45 -13
  27. package/dist/template/FunctionFindAllTemplate.txt +39 -23
  28. package/dist/template/FunctionFindByIdTemplate.txt +24 -14
  29. package/dist/template/FunctionQueryTemplate.txt +67 -41
  30. package/dist/template/FunctionSumTemplate.txt +48 -32
  31. package/dist/template/FunctionUpdatePartialTemplate.txt +76 -21
  32. package/dist/template/FunctionUpdateTemplate.txt +64 -17
  33. package/dist/tsconfig.build.tsbuildinfo +1 -1
  34. package/package.json +1 -1
  35. package/dist/cache/providers/RedisCacheArchiveProvider.d.ts +0 -19
  36. package/dist/cache/providers/RedisCacheArchiveProvider.js +0 -115
@@ -19,48 +19,95 @@ update(items) {
19
19
  const getTtlTimestamp = global.instances.getTtlTimestamp;
20
20
  const collection = db.collection('<COLLECTION_REPLACE>');
21
21
  const validatorDataHandle = global.instances.validatorDataHandle;
22
+ const archiveService = yield global.instances.archiveService;
22
23
  const batch = db.batch();
24
+
25
+ // Validar IDs antes de processar
23
26
  for (const item of items) {
24
- yield validatorDataHandle.validateModel(modelName, item, validatorOptions);
25
27
  if (!item.id) {
26
28
  throw new RepositoryBusinessException_1.RepositoryBusinessException(`Id is required`, []);
27
29
  }
30
+ }
31
+
32
+ // Buscar documentos existentes em lote (otimização)
33
+ const docRefs = items.map(item => collection.doc(item.id));
34
+ const existingDocs = (archiveService.isEnabled() && docRefs.length > 0)
35
+ ? yield db.getAll(...docRefs)
36
+ : [];
37
+
38
+ for (let i = 0; i < items.length; i++) {
39
+ const item = items[i];
40
+ yield validatorDataHandle.validateModel(modelName, item, validatorOptions);
41
+
42
+ const docRef = docRefs[i];
43
+ let shouldUnarchive = false;
44
+
45
+ // VERIFICAÇÃO DE DOCUMENTO ARQUIVADO
46
+ if (archiveService.isEnabled() && existingDocs[i]?.exists) {
47
+ const currentData = existingDocs[i].data();
48
+
49
+ if (currentData && archiveService.isDocumentArchived(currentData)) {
50
+ const archivePath = currentData.fbArchivePath;
51
+ const updateResult = yield archiveService.updateArchivedDocument(
52
+ '<COLLECTION_REPLACE>',
53
+ item.id,
54
+ item,
55
+ archivePath,
56
+ { unarchive: true }
57
+ );
58
+
59
+ if (updateResult.result.success && updateResult.mergedData) {
60
+ const originalItem = Object.assign({}, item);
61
+ Object.assign(item, updateResult.mergedData, originalItem);
62
+ shouldUnarchive = true;
63
+ } else {
64
+ throw new Error('Failed to update archived document; aborting update to prevent inconsistent state and stale archive overwriting user changes.');
65
+ }
66
+ }
67
+ }
68
+
28
69
  item.updateAt = newDate();
29
70
  item.updateTimestampAt = new Date(item.updateAt).getTime();
30
71
  item.lastServiceModify = process.env.SERVICE || 'PROJECT_UNDEFINED';
31
- const docRef = collection.doc(item.id);
32
- if (ttlExpirationIn && item?.ttlExpirationAt) {
33
- delete item.ttlExpirationAt;
34
- }
35
- batch.set(docRef, JSON.parse(JSON.stringify(item)), { merge: true });
72
+
73
+ // Preparar dados para persistir (consolidado)
74
+ const itemData = JSON.parse(JSON.stringify(item));
36
75
  if (ttlExpirationIn && ttlUnit && ttlUpdate) {
37
- const ttl = getTtlTimestamp(ttlExpirationIn, ttlUnit);
38
- batch.set(docRef, {
39
- ttlExpirationAt: ttl,
40
- }, { merge: true });
76
+ itemData.ttlExpirationAt = getTtlTimestamp(ttlExpirationIn, ttlUnit);
77
+ }
78
+
79
+ if (shouldUnarchive) {
80
+ const FieldValue = global.instances.FieldValue;
81
+ const ARCHIVE_FIELDS = global.instances.ARCHIVE_FIELDS;
82
+ itemData[ARCHIVE_FIELDS.ARCHIVED_AT] = FieldValue.delete();
83
+ itemData[ARCHIVE_FIELDS.ARCHIVE_PATH] = FieldValue.delete();
84
+ itemData[ARCHIVE_FIELDS.ARCHIVE_HASH] = FieldValue.delete();
41
85
  }
86
+
87
+ batch.set(docRef, itemData, { merge: true });
42
88
  }
89
+
43
90
  if (!environmentUtil.areWeTesting()) {
44
91
  yield batch.commit();
45
92
  yield this.revokeCache();
46
- }
47
- else {
93
+ } else {
48
94
  console.log('It was decreed that it is being executed try, no operation or effective transaction will be performed');
49
95
  }
96
+
50
97
  span.setAttributes({
51
98
  'firestore.operation.name': 'update',
52
99
  'firestore.collection.name': '<COLLECTION_REPLACE>',
53
100
  'firestore.operation.size': items.length,
54
- })
101
+ });
55
102
  return items;
56
103
  } catch (error) {
57
104
  span.setStatus({
58
105
  code: 2,
59
106
  message: error.message
60
- })
61
- span.recordException(error)
62
- throw error
107
+ });
108
+ span.recordException(error);
109
+ throw error;
63
110
  }
64
- })
111
+ });
65
112
  });
66
113
  }