@roit/roit-data-firestore 1.2.39 → 1.2.41
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/dist/archive/ArchiveService.d.ts +0 -1
- package/dist/archive/ArchiveService.js +113 -136
- package/dist/cache/CacheResolver.js +80 -104
- package/dist/cache/providers/RedisCacheArchiveProvider.d.ts +0 -1
- package/dist/cache/providers/RedisCacheArchiveProvider.js +43 -69
- package/dist/cache/providers/RedisCacheProvider.js +37 -52
- package/dist/config/ArchiveConfig.d.ts +4 -0
- package/dist/config/ArchiveConfig.js +20 -1
- package/dist/config/BaseRepository.js +13 -30
- package/dist/config/ReadonlyRepository.js +7 -20
- package/dist/decorators/Repository.js +3 -12
- package/dist/exception/RepositoryException.js +1 -1
- package/dist/exception/handle/ValidatorDataHandle.js +12 -24
- package/dist/firestore-read-audit/FirestoreReadAuditResolver.js +15 -18
- package/dist/firestore-read-audit/providers/BigQueryFirestoreReadAuditProvider.js +53 -66
- package/dist/firestore-read-audit/providers/PubSubFirestoreReadAuditProvider.js +15 -26
- package/dist/model/CacheProviders.js +1 -1
- package/dist/query/ManualQueryHelper.js +132 -150
- package/dist/query/QueryCreatorConfig.js +30 -42
- package/dist/query/QueryPredicateFunctionTransform.js +10 -10
- package/dist/query/TransformMethodFromQuery.js +2 -2
- package/dist/tracer/Tracer.js +5 -14
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/tsconfig.json +2 -2
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.BigQueryFirestoreReadAuditProvider = void 0;
|
|
13
4
|
const PlatformTools_1 = require("../../platform/PlatformTools");
|
|
@@ -35,67 +26,63 @@ class BigQueryFirestoreReadAuditProvider {
|
|
|
35
26
|
}
|
|
36
27
|
}
|
|
37
28
|
}
|
|
38
|
-
createFirestoreAuditDatasetAndTableIfNecessary() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
field: 'insertAt'
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
const handleBigQueryError = (e) => {
|
|
63
|
-
if (!JSON.stringify(e.errors).includes('Exists')) {
|
|
64
|
-
console.log('Error [createFirestoreAuditDatasetAndTableIfNecessary]', e);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
try {
|
|
68
|
-
yield this.bigQuery.createDataset(dataset);
|
|
69
|
-
}
|
|
70
|
-
catch (e) {
|
|
71
|
-
handleBigQueryError(e);
|
|
29
|
+
async createFirestoreAuditDatasetAndTableIfNecessary() {
|
|
30
|
+
const TWO_DAYS_IN_MS = '172800000';
|
|
31
|
+
const schema = [
|
|
32
|
+
{ name: 'collection', type: 'STRING' },
|
|
33
|
+
{ name: 'service', type: 'STRING' },
|
|
34
|
+
{ name: 'projectId', type: 'STRING' },
|
|
35
|
+
{ name: 'env', type: 'STRING' },
|
|
36
|
+
{ name: 'repositoryClassName', type: 'STRING' },
|
|
37
|
+
{ name: 'functionSignature', type: 'STRING' },
|
|
38
|
+
{ name: 'params', type: 'STRING' },
|
|
39
|
+
{ name: 'queryResult', type: 'STRING' },
|
|
40
|
+
{ name: 'queryResultLength', type: 'INTEGER' },
|
|
41
|
+
{ name: 'insertAt', type: 'DATETIME' },
|
|
42
|
+
];
|
|
43
|
+
const options = {
|
|
44
|
+
schema,
|
|
45
|
+
location: 'US',
|
|
46
|
+
timePartitioning: {
|
|
47
|
+
type: 'DAY',
|
|
48
|
+
expirationMS: TWO_DAYS_IN_MS,
|
|
49
|
+
field: 'insertAt'
|
|
72
50
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
51
|
+
};
|
|
52
|
+
const handleBigQueryError = (e) => {
|
|
53
|
+
if (!JSON.stringify(e.errors).includes('Exists')) {
|
|
54
|
+
console.log('Error [createFirestoreAuditDatasetAndTableIfNecessary]', e);
|
|
77
55
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
56
|
+
};
|
|
57
|
+
try {
|
|
58
|
+
await this.bigQuery.createDataset(dataset);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
handleBigQueryError(e);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
await this.bigQuery
|
|
65
|
+
.dataset(dataset)
|
|
66
|
+
.createTable(table, options);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
handleBigQueryError(e);
|
|
70
|
+
}
|
|
82
71
|
}
|
|
83
|
-
persistFirestoreRead(props) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
yield this.createFirestoreAuditDatasetAndTableIfNecessary();
|
|
88
|
-
}
|
|
89
|
-
this.isTableCreated = true;
|
|
90
|
-
yield this.bigQuery
|
|
91
|
-
.dataset(dataset)
|
|
92
|
-
.table(table)
|
|
93
|
-
.insert([props]);
|
|
94
|
-
}
|
|
95
|
-
catch (error) {
|
|
96
|
-
console.log(JSON.stringify(error.errors));
|
|
72
|
+
async persistFirestoreRead(props) {
|
|
73
|
+
try {
|
|
74
|
+
if (!this.isTableCreated) {
|
|
75
|
+
await this.createFirestoreAuditDatasetAndTableIfNecessary();
|
|
97
76
|
}
|
|
98
|
-
|
|
77
|
+
this.isTableCreated = true;
|
|
78
|
+
await this.bigQuery
|
|
79
|
+
.dataset(dataset)
|
|
80
|
+
.table(table)
|
|
81
|
+
.insert([props]);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
console.log(JSON.stringify(error.errors));
|
|
85
|
+
}
|
|
99
86
|
}
|
|
100
87
|
loadBigQuery() {
|
|
101
88
|
try {
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.PubSubFirestoreReadAuditProvider = void 0;
|
|
13
4
|
const PlatformTools_1 = require("../../platform/PlatformTools");
|
|
@@ -27,23 +18,21 @@ class PubSubFirestoreReadAuditProvider {
|
|
|
27
18
|
}
|
|
28
19
|
this.topic = this.pubsub.topic(envTopic, { batching: { maxMessages: 1 } });
|
|
29
20
|
}
|
|
30
|
-
persistFirestoreRead(params) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
});
|
|
21
|
+
async persistFirestoreRead(params) {
|
|
22
|
+
const limitBytes = 9900000;
|
|
23
|
+
let buffer = Buffer.from(JSON.stringify(params));
|
|
24
|
+
if (buffer.length > limitBytes) {
|
|
25
|
+
params.queryResult = 'too-big-to-save';
|
|
26
|
+
buffer = Buffer.from(JSON.stringify(params));
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
await this.topic.publishMessage({
|
|
30
|
+
data: buffer
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
console.log(JSON.stringify(error));
|
|
35
|
+
}
|
|
47
36
|
}
|
|
48
37
|
loadPubSub() {
|
|
49
38
|
try {
|
|
@@ -6,4 +6,4 @@ var CacheProviders;
|
|
|
6
6
|
CacheProviders["LOCAL"] = "LOCAL";
|
|
7
7
|
CacheProviders["REDIS"] = "REDIS";
|
|
8
8
|
CacheProviders["REDIS_ARCHIVE"] = "REDIS_ARCHIVE";
|
|
9
|
-
})(CacheProviders
|
|
9
|
+
})(CacheProviders || (exports.CacheProviders = CacheProviders = {}));
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.ManualQueryHelper = void 0;
|
|
13
4
|
const FirestoreInstance_1 = require("../config/FirestoreInstance");
|
|
@@ -17,162 +8,153 @@ const QueryPredicateFunctionTransform_1 = require("./QueryPredicateFunctionTrans
|
|
|
17
8
|
const ArchiveService_1 = require("../archive/ArchiveService");
|
|
18
9
|
// import { ArchiveConfig } from "../config/ArchiveConfig";
|
|
19
10
|
class ManualQueryHelper {
|
|
20
|
-
static executeQueryManual(className, config, queryRef = false) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return data;
|
|
28
|
-
});
|
|
11
|
+
static async executeQueryManual(className, config, queryRef = false) {
|
|
12
|
+
const result = await this.handleExecuteQueryManual(className, config, { showCount: false }, queryRef);
|
|
13
|
+
if (queryRef) {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
const { data } = result;
|
|
17
|
+
return data;
|
|
29
18
|
}
|
|
30
|
-
static executeQueryManualPaginated(className, config) {
|
|
31
|
-
return
|
|
32
|
-
return this.handleExecuteQueryManual(className, config, { showCount: true });
|
|
33
|
-
});
|
|
19
|
+
static async executeQueryManualPaginated(className, config) {
|
|
20
|
+
return this.handleExecuteQueryManual(className, config, { showCount: true });
|
|
34
21
|
}
|
|
35
22
|
/**
|
|
36
23
|
* Processa documentos arquivados, recuperando seus dados completos do Cloud Storage
|
|
37
24
|
*/
|
|
38
|
-
static processArchivedDocuments(docs, collectionName) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
static async processArchivedDocuments(docs, collectionName) {
|
|
26
|
+
const archiveService = await ArchiveService_1.ArchiveService.getInstance();
|
|
27
|
+
// Verifica se o arquivamento está habilitado
|
|
28
|
+
if (!archiveService.isEnabled()) {
|
|
29
|
+
return docs;
|
|
30
|
+
}
|
|
31
|
+
const docsMap = new Map();
|
|
32
|
+
const recoveryPromises = docs.map(async (doc) => {
|
|
33
|
+
docsMap.set(doc.id, doc);
|
|
34
|
+
if (!archiveService.isDocumentArchived(doc)) {
|
|
35
|
+
return null;
|
|
44
36
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// O ArchiveService agora gerencia o cache internamente baseado na configuração
|
|
53
|
-
const archivedData = yield archiveService.getArchivedDocument(collectionName, doc);
|
|
54
|
-
if (archivedData) {
|
|
55
|
-
// Remove o flag de arquivamento e mescla os dados
|
|
56
|
-
// const { fbArchivedAt, ...archivedDataWithoutFlag } = archivedData;
|
|
57
|
-
return Object.assign(Object.assign({}, doc), archivedData);
|
|
58
|
-
}
|
|
59
|
-
return doc;
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
console.warn(`Erro ao recuperar documento arquivado ${doc.id}:`, error);
|
|
63
|
-
return doc;
|
|
37
|
+
try {
|
|
38
|
+
// O ArchiveService agora gerencia o cache internamente baseado na configuração
|
|
39
|
+
const archivedData = await archiveService.getArchivedDocument(collectionName, doc);
|
|
40
|
+
if (archivedData) {
|
|
41
|
+
// Remove o flag de arquivamento e mescla os dados
|
|
42
|
+
// const { fbArchivedAt, ...archivedDataWithoutFlag } = archivedData;
|
|
43
|
+
return { ...doc, ...archivedData };
|
|
64
44
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
});
|
|
72
|
-
return Array.from(docsMap.values());
|
|
45
|
+
return doc;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.warn(`Erro ao recuperar documento arquivado ${doc.id}:`, error);
|
|
49
|
+
return doc;
|
|
50
|
+
}
|
|
73
51
|
});
|
|
52
|
+
const recoveredDocs = await Promise.all(recoveryPromises);
|
|
53
|
+
recoveredDocs.forEach(doc => {
|
|
54
|
+
if (doc) {
|
|
55
|
+
docsMap.set(doc.id, doc);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return Array.from(docsMap.values());
|
|
74
59
|
}
|
|
75
|
-
static handleExecuteQueryManual(className, config, options, queryRef = false) {
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (Object.keys(query).length === 1) {
|
|
98
|
-
return this.convertToMQuery(query);
|
|
99
|
-
}
|
|
100
|
-
return query;
|
|
101
|
-
});
|
|
102
|
-
const queryInit = queryList[0];
|
|
103
|
-
queryExecute = collection.where(queryInit.field, queryInit.operator, queryInit.value);
|
|
104
|
-
pushTraceQuery(queryInit);
|
|
105
|
-
queryList.shift();
|
|
106
|
-
queryList.forEach(que => {
|
|
107
|
-
queryExecute = queryExecute.where(que.field, que.operator, que.value);
|
|
108
|
-
pushTraceQuery(que);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (config && (config === null || config === void 0 ? void 0 : config.select)) {
|
|
112
|
-
if (queryExecute) {
|
|
113
|
-
queryExecute = queryExecute.select(...config.select);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
queryExecute = collection.select(...config.select);
|
|
60
|
+
static async handleExecuteQueryManual(className, config, options, queryRef = false) {
|
|
61
|
+
return await global.instances.startTracer('firestore.query', async (span) => {
|
|
62
|
+
try {
|
|
63
|
+
const cacheResolver = global.instances.cacheResolver;
|
|
64
|
+
const result = await cacheResolver.getCacheResult(className, 'any', JSON.stringify({ ...config, options }));
|
|
65
|
+
if (result) {
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
const repositoryOptions = QueryPredicateFunctionTransform_1.QueryPredicateFunctionTransform.classConfig.get(className);
|
|
69
|
+
const traceQuery = [];
|
|
70
|
+
const pushTraceQuery = (query) => {
|
|
71
|
+
traceQuery.push({ field: query.field, operator: query.operator, value: '?' });
|
|
72
|
+
};
|
|
73
|
+
if (repositoryOptions) {
|
|
74
|
+
const firestoreInstance = FirestoreInstance_1.FirestoreInstance.getInstance();
|
|
75
|
+
const collection = firestoreInstance.collection(repositoryOptions.collection);
|
|
76
|
+
let queryList;
|
|
77
|
+
let queryExecute = collection;
|
|
78
|
+
if (config?.query && config?.query?.length > 0) {
|
|
79
|
+
queryList = config.query.map(query => {
|
|
80
|
+
if (Object.keys(query).length === 1) {
|
|
81
|
+
return this.convertToMQuery(query);
|
|
117
82
|
}
|
|
83
|
+
return query;
|
|
84
|
+
});
|
|
85
|
+
const queryInit = queryList[0];
|
|
86
|
+
queryExecute = collection.where(queryInit.field, queryInit.operator, queryInit.value);
|
|
87
|
+
pushTraceQuery(queryInit);
|
|
88
|
+
queryList.shift();
|
|
89
|
+
queryList.forEach(que => {
|
|
90
|
+
queryExecute = queryExecute.where(que.field, que.operator, que.value);
|
|
91
|
+
pushTraceQuery(que);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (config && config?.select) {
|
|
95
|
+
if (queryExecute) {
|
|
96
|
+
queryExecute = queryExecute.select(...config.select);
|
|
118
97
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
queryExecute = queryExecute.orderBy(config.orderBy.field, config.orderBy.direction);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
queryExecute = collection.orderBy(config.orderBy.field, config.orderBy.direction);
|
|
125
|
-
}
|
|
98
|
+
else {
|
|
99
|
+
queryExecute = collection.select(...config.select);
|
|
126
100
|
}
|
|
101
|
+
}
|
|
102
|
+
if (config && config?.orderBy) {
|
|
127
103
|
if (queryExecute) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
count = totalItens;
|
|
133
|
-
}
|
|
134
|
-
if (queryRef) {
|
|
135
|
-
return queryExecute;
|
|
136
|
-
}
|
|
137
|
-
const snapshot = yield queryExecute.get();
|
|
138
|
-
const data = this.getData(snapshot);
|
|
139
|
-
// PROCESSAR DOCUMENTOS ARQUIVADOS
|
|
140
|
-
const processedData = yield this.processArchivedDocuments(data, repositoryOptions.collection);
|
|
141
|
-
yield cacheResolver.cacheResult(className, 'any', { data: processedData, count }, JSON.stringify(Object.assign(Object.assign({}, config), { options })));
|
|
142
|
-
const firestoreReadAuditResolver = global.instances.firestoreReadAuditResolver;
|
|
143
|
-
yield firestoreReadAuditResolver.persistFirestoreRead({
|
|
144
|
-
collection: repositoryOptions.collection,
|
|
145
|
-
repositoryClassName: className,
|
|
146
|
-
functionSignature: 'manual-query',
|
|
147
|
-
params: JSON.stringify(config),
|
|
148
|
-
queryResult: data
|
|
149
|
-
});
|
|
150
|
-
span.setAttributes({
|
|
151
|
-
'firestore.operation.name': 'query',
|
|
152
|
-
'firestore.operation.query': JSON.stringify(traceQuery),
|
|
153
|
-
'firestore.collection.name': repositoryOptions.collection,
|
|
154
|
-
'firestore.operation.size': data.length,
|
|
155
|
-
});
|
|
156
|
-
return {
|
|
157
|
-
data: processedData,
|
|
158
|
-
totalItens: count
|
|
159
|
-
};
|
|
104
|
+
queryExecute = queryExecute.orderBy(config.orderBy.field, config.orderBy.direction);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
queryExecute = collection.orderBy(config.orderBy.field, config.orderBy.direction);
|
|
160
108
|
}
|
|
161
109
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
110
|
+
if (queryExecute) {
|
|
111
|
+
let count = null;
|
|
112
|
+
if (config?.paging) {
|
|
113
|
+
const { documentRef, totalItens } = await new QueryCreatorConfig_1.QueryCreatorConfig().buildPaging(queryExecute, config.paging, options);
|
|
114
|
+
queryExecute = documentRef;
|
|
115
|
+
count = totalItens;
|
|
116
|
+
}
|
|
117
|
+
if (queryRef) {
|
|
118
|
+
return queryExecute;
|
|
119
|
+
}
|
|
120
|
+
const snapshot = await queryExecute.get();
|
|
121
|
+
const data = this.getData(snapshot);
|
|
122
|
+
// PROCESSAR DOCUMENTOS ARQUIVADOS
|
|
123
|
+
const processedData = await this.processArchivedDocuments(data, repositoryOptions.collection);
|
|
124
|
+
await cacheResolver.cacheResult(className, 'any', { data: processedData, count }, JSON.stringify({ ...config, options }));
|
|
125
|
+
const firestoreReadAuditResolver = global.instances.firestoreReadAuditResolver;
|
|
126
|
+
await firestoreReadAuditResolver.persistFirestoreRead({
|
|
127
|
+
collection: repositoryOptions.collection,
|
|
128
|
+
repositoryClassName: className,
|
|
129
|
+
functionSignature: 'manual-query',
|
|
130
|
+
params: JSON.stringify(config),
|
|
131
|
+
queryResult: data
|
|
132
|
+
});
|
|
133
|
+
span.setAttributes({
|
|
134
|
+
'firestore.operation.name': 'query',
|
|
135
|
+
'firestore.operation.query': JSON.stringify(traceQuery),
|
|
136
|
+
'firestore.collection.name': repositoryOptions.collection,
|
|
137
|
+
'firestore.operation.size': data.length,
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
data: processedData,
|
|
141
|
+
totalItens: count
|
|
142
|
+
};
|
|
143
|
+
}
|
|
174
144
|
}
|
|
175
|
-
|
|
145
|
+
return {
|
|
146
|
+
data: [],
|
|
147
|
+
totalItens: 0
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
span.setStatus({
|
|
152
|
+
code: 2,
|
|
153
|
+
message: error.message
|
|
154
|
+
});
|
|
155
|
+
span.recordException(error);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
176
158
|
});
|
|
177
159
|
}
|
|
178
160
|
static convertToMQuery(query) {
|
|
@@ -188,7 +170,7 @@ class ManualQueryHelper {
|
|
|
188
170
|
let items = [];
|
|
189
171
|
try {
|
|
190
172
|
snapshot.forEach((doc) => {
|
|
191
|
-
let element =
|
|
173
|
+
let element = { ...doc.data() };
|
|
192
174
|
element.id = doc.id;
|
|
193
175
|
items.push(element);
|
|
194
176
|
});
|
|
@@ -1,49 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.QueryCreatorConfig = void 0;
|
|
13
4
|
class QueryCreatorConfig {
|
|
14
|
-
buildPaging(collectionRef, paging, options) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
totalItens
|
|
45
|
-
};
|
|
46
|
-
});
|
|
5
|
+
async buildPaging(collectionRef, paging, options) {
|
|
6
|
+
const orderByDirection = paging?.orderByDirection || 'asc';
|
|
7
|
+
const limit = paging?.limit ?? 1000;
|
|
8
|
+
let totalItens = null;
|
|
9
|
+
if (options?.showCount) {
|
|
10
|
+
const itensQuery = await collectionRef.count().get();
|
|
11
|
+
totalItens = itensQuery.data().count;
|
|
12
|
+
}
|
|
13
|
+
let documentRef = collectionRef.limit(limit);
|
|
14
|
+
if (paging?.orderBy) {
|
|
15
|
+
const ordersBy = Array.isArray(paging.orderBy) ? paging.orderBy : [paging.orderBy];
|
|
16
|
+
ordersBy.forEach(order => documentRef = documentRef.orderBy(order, orderByDirection));
|
|
17
|
+
}
|
|
18
|
+
if (paging?.select) {
|
|
19
|
+
documentRef.select(...paging.select);
|
|
20
|
+
}
|
|
21
|
+
if (paging?.cursor) {
|
|
22
|
+
const startAfter = Array.isArray(paging.cursor) ? paging.cursor : [paging.cursor];
|
|
23
|
+
documentRef = documentRef.startAfter(...startAfter);
|
|
24
|
+
}
|
|
25
|
+
if (paging?.startAt) {
|
|
26
|
+
documentRef = documentRef.startAt(paging.startAt);
|
|
27
|
+
}
|
|
28
|
+
if (paging?.endAt) {
|
|
29
|
+
documentRef = documentRef.endAt(paging.endAt);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
documentRef,
|
|
33
|
+
totalItens
|
|
34
|
+
};
|
|
47
35
|
}
|
|
48
36
|
}
|
|
49
37
|
exports.QueryCreatorConfig = QueryCreatorConfig;
|