@boostercloud/framework-provider-azure 2.18.8 → 2.19.0
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.
|
@@ -1,15 +1,3 @@
|
|
|
1
1
|
import { BoosterConfig, EventEnvelope, NonPersistedEventEnvelope } from '@boostercloud/framework-types';
|
|
2
2
|
import { CosmosClient } from '@azure/cosmos';
|
|
3
|
-
/**
|
|
4
|
-
* Limits: The Azure Cosmos DB request size limit constrains the size of the TransactionalBatch payload to not exceed 2 MB,
|
|
5
|
-
* and the maximum execution time is 5 seconds. There's a current limit of 100 operations per TransactionalBatch to ensure
|
|
6
|
-
* the performance is as expected and within SLAs.
|
|
7
|
-
*
|
|
8
|
-
* Errors: If there's a failure, the failed operation will have a status code of its corresponding error. All the other
|
|
9
|
-
* operations will have a 424 status code (failed dependency). The status code enables one to identify the cause of transaction failure.
|
|
10
|
-
*
|
|
11
|
-
* @param cosmosDb
|
|
12
|
-
* @param eventEnvelopes
|
|
13
|
-
* @param config
|
|
14
|
-
*/
|
|
15
3
|
export declare function storeEvents(cosmosDb: CosmosClient, eventEnvelopes: Array<NonPersistedEventEnvelope>, config: BoosterConfig): Promise<Array<EventEnvelope>>;
|
|
@@ -4,7 +4,33 @@ exports.storeEvents = void 0;
|
|
|
4
4
|
const framework_common_helpers_1 = require("@boostercloud/framework-common-helpers");
|
|
5
5
|
const partition_keys_1 = require("./partition-keys");
|
|
6
6
|
const constants_1 = require("../constants");
|
|
7
|
-
|
|
7
|
+
async function storeEvents(cosmosDb, eventEnvelopes, config) {
|
|
8
|
+
var _a;
|
|
9
|
+
if ((_a = config.azureConfiguration) === null || _a === void 0 ? void 0 : _a.enableEventBatching) {
|
|
10
|
+
return storeEventsInBatch(cosmosDb, eventEnvelopes, config);
|
|
11
|
+
}
|
|
12
|
+
const logger = (0, framework_common_helpers_1.getLogger)(config, 'store-events-adapter#storeEvents');
|
|
13
|
+
logger.debug('Storing EventEnvelopes with eventEnvelopes:', eventEnvelopes);
|
|
14
|
+
const persistableEvents = [];
|
|
15
|
+
for (const eventEnvelope of eventEnvelopes) {
|
|
16
|
+
const persistableEvent = {
|
|
17
|
+
...eventEnvelope,
|
|
18
|
+
createdAt: new Date().toISOString(),
|
|
19
|
+
};
|
|
20
|
+
await cosmosDb
|
|
21
|
+
.database(config.resourceNames.applicationStack)
|
|
22
|
+
.container(config.resourceNames.eventsStore)
|
|
23
|
+
.items.create({
|
|
24
|
+
...persistableEvent,
|
|
25
|
+
[constants_1.eventsStoreAttributes.partitionKey]: (0, partition_keys_1.partitionKeyForEvent)(eventEnvelope.entityTypeName, eventEnvelope.entityID),
|
|
26
|
+
[constants_1.eventsStoreAttributes.sortKey]: persistableEvent.createdAt,
|
|
27
|
+
});
|
|
28
|
+
persistableEvents.push(persistableEvent);
|
|
29
|
+
}
|
|
30
|
+
logger.debug('EventEnvelopes stored:');
|
|
31
|
+
return persistableEvents;
|
|
32
|
+
}
|
|
33
|
+
exports.storeEvents = storeEvents;
|
|
8
34
|
/**
|
|
9
35
|
* Limits: The Azure Cosmos DB request size limit constrains the size of the TransactionalBatch payload to not exceed 2 MB,
|
|
10
36
|
* and the maximum execution time is 5 seconds. There's a current limit of 100 operations per TransactionalBatch to ensure
|
|
@@ -17,13 +43,15 @@ const DEFAULT_CHUNK_SIZE = 100;
|
|
|
17
43
|
* @param eventEnvelopes
|
|
18
44
|
* @param config
|
|
19
45
|
*/
|
|
20
|
-
async function
|
|
21
|
-
|
|
46
|
+
async function storeEventsInBatch(cosmosDb, eventEnvelopes, config) {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
const logger = (0, framework_common_helpers_1.getLogger)(config, 'store-events-adapter#storeEventsInBatch');
|
|
22
49
|
logger.debug('Storing EventEnvelopes with eventEnvelopes:', eventEnvelopes);
|
|
23
50
|
const envelopesWithCreatedAt = [];
|
|
24
51
|
const eventsPerPartitionKey = eventEnvelopes.reduce(groupByPartitionKey, {});
|
|
25
52
|
for (const [partitionKey, eventsInPartitionKey] of Object.entries(eventsPerPartitionKey)) {
|
|
26
|
-
const
|
|
53
|
+
const chunkSize = (_b = (_a = config.azureConfiguration) === null || _a === void 0 ? void 0 : _a.cosmos.batchSize) !== null && _b !== void 0 ? _b : 100;
|
|
54
|
+
const chunksOfEventsInPartitionKey = chunkEvents(eventsInPartitionKey, chunkSize);
|
|
27
55
|
for (const eventListInChunk of chunksOfEventsInPartitionKey) {
|
|
28
56
|
const eventEnvelopesChunkWithCreatedAt = toEventEnvelopes(eventListInChunk);
|
|
29
57
|
envelopesWithCreatedAt.push(...eventEnvelopesChunkWithCreatedAt);
|
|
@@ -40,7 +68,6 @@ async function storeEvents(cosmosDb, eventEnvelopes, config) {
|
|
|
40
68
|
}
|
|
41
69
|
return envelopesWithCreatedAt;
|
|
42
70
|
}
|
|
43
|
-
exports.storeEvents = storeEvents;
|
|
44
71
|
function toEventEnvelopes(events) {
|
|
45
72
|
return events.map((eventEnvelope) => ({
|
|
46
73
|
...eventEnvelope,
|
|
@@ -57,13 +84,14 @@ function toInputOperations(eventEnvelopesChunkWithCreatedAt, config) {
|
|
|
57
84
|
});
|
|
58
85
|
}
|
|
59
86
|
async function batchEvents(cosmosDb, config, inputOperations, partitionKey) {
|
|
87
|
+
var _a, _b;
|
|
60
88
|
const logger = (0, framework_common_helpers_1.getLogger)(config, 'store-events-adapter#batchEvents');
|
|
61
89
|
try {
|
|
62
90
|
logger.debug('Storing EventEnvelopes with inputOperations:', inputOperations);
|
|
63
91
|
return await cosmosDb
|
|
64
92
|
.database(config.resourceNames.applicationStack)
|
|
65
93
|
.container(config.resourceNames.eventsStore)
|
|
66
|
-
.items.batch(inputOperations, partitionKey);
|
|
94
|
+
.items.batch(inputOperations, partitionKey, { ...(_b = (_a = config.azureConfiguration) === null || _a === void 0 ? void 0 : _a.cosmos) === null || _b === void 0 ? void 0 : _b.requestOptions });
|
|
67
95
|
}
|
|
68
96
|
catch (e) {
|
|
69
97
|
logger.error('Unexpected error storing events', e);
|
|
@@ -102,7 +130,7 @@ function storableResource(config, eventEnvelope) {
|
|
|
102
130
|
}
|
|
103
131
|
}
|
|
104
132
|
/**
|
|
105
|
-
* Split events in chunks of size
|
|
133
|
+
* Split events in chunks of size 'size'
|
|
106
134
|
*
|
|
107
135
|
* @param arr
|
|
108
136
|
* @param size
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boostercloud/framework-provider-azure",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.0",
|
|
4
4
|
"description": "Handle Booster's integration with Azure",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"framework-provider-azure"
|
|
@@ -27,14 +27,14 @@
|
|
|
27
27
|
"@azure/functions": "^1.2.2",
|
|
28
28
|
"@azure/identity": "~2.1.0",
|
|
29
29
|
"@azure/event-hubs": "5.11.1",
|
|
30
|
-
"@boostercloud/framework-common-helpers": "^2.
|
|
31
|
-
"@boostercloud/framework-types": "^2.
|
|
30
|
+
"@boostercloud/framework-common-helpers": "^2.19.0",
|
|
31
|
+
"@boostercloud/framework-types": "^2.19.0",
|
|
32
32
|
"tslib": "^2.4.0",
|
|
33
33
|
"@effect-ts/core": "^0.60.4",
|
|
34
34
|
"@azure/web-pubsub": "~1.1.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@boostercloud/eslint-config": "^2.
|
|
37
|
+
"@boostercloud/eslint-config": "^2.19.0",
|
|
38
38
|
"@types/chai": "4.2.18",
|
|
39
39
|
"@types/chai-as-promised": "7.1.4",
|
|
40
40
|
"@types/faker": "5.1.5",
|