@squiz/db-lib 1.76.0 → 1.77.1

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 (38) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/dynamodb/AbstractDynamoDbRepository.d.ts +1 -1
  3. package/lib/dynamodb/AbstractDynamoDbRepository.d.ts.map +1 -1
  4. package/lib/dynamodb/AbstractDynamoDbRepository.js +6 -5
  5. package/lib/dynamodb/AbstractDynamoDbRepository.js.map +1 -1
  6. package/lib/dynamodb/AbstractDynamoDbRepository.spec.d.ts.map +1 -1
  7. package/lib/dynamodb/AbstractDynamoDbRepository.spec.js +15 -14
  8. package/lib/dynamodb/AbstractDynamoDbRepository.spec.js.map +1 -1
  9. package/lib/dynamodb/getDynamoDbOptions.d.ts.map +1 -1
  10. package/lib/externalized/ExternalizedDynamoDbRepository.d.ts +166 -0
  11. package/lib/externalized/ExternalizedDynamoDbRepository.d.ts.map +1 -0
  12. package/lib/externalized/ExternalizedDynamoDbRepository.js +535 -0
  13. package/lib/externalized/ExternalizedDynamoDbRepository.js.map +1 -0
  14. package/lib/externalized/ExternalizedDynamoDbRepository.spec.d.ts +2 -0
  15. package/lib/externalized/ExternalizedDynamoDbRepository.spec.d.ts.map +1 -0
  16. package/lib/externalized/ExternalizedDynamoDbRepository.spec.js +431 -0
  17. package/lib/externalized/ExternalizedDynamoDbRepository.spec.js.map +1 -0
  18. package/lib/index.d.ts +2 -0
  19. package/lib/index.d.ts.map +1 -1
  20. package/lib/index.js +3 -0
  21. package/lib/index.js.map +1 -1
  22. package/lib/s3/S3ExternalStorage.d.ts +66 -0
  23. package/lib/s3/S3ExternalStorage.d.ts.map +1 -0
  24. package/lib/s3/S3ExternalStorage.js +84 -0
  25. package/lib/s3/S3ExternalStorage.js.map +1 -0
  26. package/lib/s3/S3ExternalStorage.spec.d.ts +12 -0
  27. package/lib/s3/S3ExternalStorage.spec.d.ts.map +1 -0
  28. package/lib/s3/S3ExternalStorage.spec.js +130 -0
  29. package/lib/s3/S3ExternalStorage.spec.js.map +1 -0
  30. package/package.json +7 -6
  31. package/src/dynamodb/AbstractDynamoDbRepository.spec.ts +2 -1
  32. package/src/dynamodb/AbstractDynamoDbRepository.ts +3 -1
  33. package/src/externalized/ExternalizedDynamoDbRepository.spec.ts +539 -0
  34. package/src/externalized/ExternalizedDynamoDbRepository.ts +625 -0
  35. package/src/index.ts +4 -0
  36. package/src/s3/S3ExternalStorage.spec.ts +181 -0
  37. package/src/s3/S3ExternalStorage.ts +118 -0
  38. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,166 @@
1
+ /*!
2
+ * @file ExternalizedDynamoDbRepository.ts
3
+ * @description This file contains the ExternalizedDynamoDbRepository class, which is used to store and retrieve externalized items from DynamoDB and S3.
4
+ * @author Dean Heffernan
5
+ * @copyright 2025 Squiz
6
+ */
7
+ import { AbstractDynamoDbRepository, EntityDefinition, QueryOptions } from '../dynamodb/AbstractDynamoDbRepository';
8
+ import { DynamoDbManager, Transaction } from '../dynamodb/DynamoDbManager';
9
+ import { S3ExternalStorage, S3StorageLocation } from '../s3/S3ExternalStorage';
10
+ /**
11
+ * The ExternalizedDynamoDbRepository class is used to store and retrieve externalized items from DynamoDB and S3.
12
+ * @class ExternalizedDynamoDbRepository
13
+ * @extends {AbstractDynamoDbRepository<SHAPE, DATA_CLASS>}
14
+ * @param {string} tableName - The name of the DynamoDB table to use for storing externalized items.
15
+ * @param {ContentDynamodbDbManager} dbManager - The DynamoDB database manager to use for storing externalized items.
16
+ * @param {string} entityName - The name of the entity to store externalized items for.
17
+ * @param {EntityDefinition} entityDefinition - The entity definition to use for storing externalized items.
18
+ * @param {DATA_CLASS} classRef - The class to use for storing externalized items.
19
+ * @param {S3ExternalStorage} storage - The S3 storage to use for storing externalized items.
20
+ * @returns {ExternalizedDynamoDbRepository} A new instance of the ExternalizedDynamoDbRepository class.
21
+ */
22
+ export declare abstract class ExternalizedDynamoDbRepository<SHAPE extends object, DATA_CLASS extends SHAPE & {
23
+ storageLocation?: S3StorageLocation;
24
+ }> extends AbstractDynamoDbRepository<SHAPE, DATA_CLASS> {
25
+ private readonly storage;
26
+ /**
27
+ * Creates a new instance of the ExternalizedDynamoDbRepository class.
28
+ * @constructor
29
+ * @param {string} tableName - The name of the DynamoDB table to use for storing externalized items.
30
+ * @param {DynamoDbManager} dbManager - The DynamoDB database manager to use for storing externalized items.
31
+ * @param {string} entityName - The name of the entity to store externalized items for.
32
+ * @param {EntityDefinition} entityDefinition - The entity definition to use for storing externalized items.
33
+ * @param {DATA_CLASS} classRef - The class to use for storing externalized items.
34
+ * @param {S3ExternalStorage} storage - The S3 storage to use for storing externalized items.
35
+ * @returns {ExternalizedDynamoDbRepository} A new instance of the ExternalizedDynamoDbRepository class.
36
+ */
37
+ constructor(tableName: string, dbManager: DynamoDbManager<any>, entityName: string, entityDefinition: EntityDefinition, classRef: {
38
+ new (data?: Record<string, unknown>): DATA_CLASS;
39
+ }, storage: S3ExternalStorage);
40
+ /**
41
+ * Removes storageLocation from the response.
42
+ * storageLocation is an internal S3 storage implementation detail that should never be exposed to API consumers.
43
+ * @param {DATA_CLASS} item - The item to process.
44
+ * @returns {DATA_CLASS} The item without storageLocation.
45
+ */
46
+ private removeStorageLocationFromResponse;
47
+ /**
48
+ * Creates a new item in the repository.
49
+ * If the item exceeds DynamoDB's size limit, it will automatically externalize the content to S3.
50
+ * @param {DATA_CLASS} value - The value to create the item for.
51
+ * @param {Transaction} transaction - The transaction to use for creating the item.
52
+ * @param {Partial<SHAPE>} additionalValue - Additional value to use for creating the item.
53
+ * @param {Object} options - The options to use for creating the item.
54
+ * @returns {Promise<DATA_CLASS>} A promise that resolves to the created item.
55
+ */
56
+ createItem(value: DATA_CLASS, transaction?: Transaction, additionalValue?: Partial<SHAPE>, options?: {
57
+ overrideExisting?: boolean;
58
+ }): Promise<DATA_CLASS>;
59
+ /**
60
+ * Internal method to create an item in the repository.
61
+ * @param {DATA_CLASS} value - The value to create the item for.
62
+ * @param {Transaction} transaction - The transaction to use for creating the item.
63
+ * @param {Partial<SHAPE>} additionalValue - Additional value to use for creating the item.
64
+ * @param {Object} options - The options to use for creating the item.
65
+ * @returns {Promise<DATA_CLASS>} A promise that resolves to the created item.
66
+ */
67
+ private createItemInternal;
68
+ /**
69
+ * Updates an item in the repository.
70
+ * If the item exceeds DynamoDB's size limit, it will automatically externalize the content to S3.
71
+ * @param {Partial<SHAPE>} value - The value to update the item with.
72
+ * @returns {Promise<DATA_CLASS | undefined>} A promise that resolves to the updated item.
73
+ */
74
+ updateItem(value: Partial<SHAPE>): Promise<DATA_CLASS | undefined>;
75
+ /**
76
+ * Internal method to update an item in the repository.
77
+ * @param {Partial<SHAPE>} value - The value to update the item with.
78
+ * @returns {Promise<DATA_CLASS | undefined>} A promise that resolves to the updated item.
79
+ */
80
+ private updateItemInternal;
81
+ /**
82
+ * Gets an item from the repository.
83
+ * @param {Partial<SHAPE>} item - The item to get.
84
+ * @returns {Promise<DATA_CLASS | undefined>} A promise that resolves to the item.
85
+ */
86
+ getItem(item: Partial<SHAPE>): Promise<DATA_CLASS | undefined>;
87
+ /**
88
+ * Gets items from the repository.
89
+ * @param {Partial<SHAPE>[]} items - The items to get.
90
+ * @returns {Promise<DATA_CLASS[]>} A promise that resolves to the items.
91
+ */
92
+ getItems(items: Partial<SHAPE>[]): Promise<DATA_CLASS[]>;
93
+ /**
94
+ * Queries items from the repository.
95
+ * @param {Partial<SHAPE>} item - The item to query.
96
+ * @param {QueryOptions} options - The options to use for querying the items.
97
+ * @returns {Promise<DATA_CLASS[]>} A promise that resolves to the items.
98
+ */
99
+ queryItems(item: Partial<SHAPE>, options?: QueryOptions): Promise<DATA_CLASS[]>;
100
+ /**
101
+ * Deletes an item from the repository.
102
+ * @param {Partial<SHAPE>} partialItem - The item to delete.
103
+ * @param {Transaction} transaction - The transaction to use for deleting the item.
104
+ * @returns {Promise<number>} A promise that resolves to the number of items deleted.
105
+ */
106
+ deleteItem(partialItem: Partial<SHAPE>, transaction?: Transaction): Promise<number>;
107
+ /**
108
+ * Deletes items from the repository.
109
+ * @param {Partial<SHAPE>[]} items - The items to delete.
110
+ * @returns {Promise<void>} A promise that resolves when the items are deleted.
111
+ */
112
+ deleteItems(items: Partial<SHAPE>[]): Promise<void>;
113
+ /**
114
+ * Prepares a value for storage by externalizing large content to S3.
115
+ * @param {DATA_CLASS} value - The value to prepare for storage.
116
+ * @returns {Promise<DATA_CLASS>} A promise that resolves to the prepared value for DynamoDB.
117
+ * @throws {Error} If S3 storage is not configured or if saving to S3 fails.
118
+ */
119
+ prepareValueForStorage(value: DATA_CLASS): Promise<DATA_CLASS>;
120
+ /**
121
+ * Extracts only the key field values from an object based on the entity definition.
122
+ * Large content fields (defined in fieldsAsJsonString) are set to empty values.
123
+ * All other fields (key fields and small metadata) are preserved.
124
+ *
125
+ * @param {Record<string, unknown>} obj - The source object to extract key fields from.
126
+ * @returns {Record<string, unknown>} An object containing only the key field values.
127
+ */
128
+ protected getKeyFieldsValues(obj: Record<string, unknown>): Record<string, unknown>;
129
+ /**
130
+ * Override parent's hydrateItem to preserve storageLocation
131
+ * The parent's hydrateItem creates a new instance which strips storageLocation
132
+ */
133
+ protected hydrateItem(item: Record<string, unknown>): DATA_CLASS;
134
+ /**
135
+ * Checks if content is actually stored in S3 by examining if large content fields are empty.
136
+ * When content is externalized to S3, fieldsAsJsonString fields are set to empty objects/arrays.
137
+ * @param {DATA_CLASS} item - The item to check.
138
+ * @returns {boolean} True if content is in S3, false if content is in DynamoDB.
139
+ */
140
+ private isContentInS3;
141
+ /**
142
+ * Hydrates a record from external storage.
143
+ * @param {DATA_CLASS} record - The record to hydrate.
144
+ * @returns {Promise<DATA_CLASS | undefined>} A promise that resolves to the hydrated record (without storageLocation).
145
+ */
146
+ private hydrateFromExternalStorage;
147
+ /**
148
+ * Fetches the stored location of an item.
149
+ * @param {Partial<SHAPE>} partialItem - The item to fetch the stored location for.
150
+ * @returns {Promise<S3StorageLocation | undefined>} A promise that resolves to the stored location.
151
+ */
152
+ private fetchStoredLocation;
153
+ /**
154
+ * Check if the error is due to DynamoDB item size limit being exceeded.
155
+ * @param {unknown} error - The error to check.
156
+ * @returns {boolean} True if the error is due to DynamoDB item size limit being exceeded, false otherwise.
157
+ */
158
+ protected isDynamoItemSizeLimitError(error: unknown): boolean;
159
+ /**
160
+ * Check if the message contains the DynamoDB size keyword.
161
+ * @param {string} message - The message to check.
162
+ * @returns {boolean} True if the message contains the DynamoDB size keyword, false otherwise.
163
+ */
164
+ private hasDynamoSizeKeyword;
165
+ }
166
+ //# sourceMappingURL=ExternalizedDynamoDbRepository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExternalizedDynamoDbRepository.d.ts","sourceRoot":"","sources":["../../src/externalized/ExternalizedDynamoDbRepository.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAC;AACpH,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG3E,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE/E;;;;;;;;;;;GAWG;AACH,8BAAsB,8BAA8B,CAClD,KAAK,SAAS,MAAM,EACpB,UAAU,SAAS,KAAK,GAAG;IAAE,eAAe,CAAC,EAAE,iBAAiB,CAAA;CAAE,CAClE,SAAQ,0BAA0B,CAAC,KAAK,EAAE,UAAU,CAAC;IAkBnD,OAAO,CAAC,QAAQ,CAAC,OAAO;IAjB1B;;;;;;;;;;OAUG;gBAED,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,eAAe,CAAC,GAAG,CAAC,EAC/B,UAAU,EAAE,MAAM,EAClB,gBAAgB,EAAE,gBAAgB,EAClC,QAAQ,EAAE;QAAE,KAAK,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,G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