|
4 | 4 | * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
5 | 5 | */ |
6 | 6 |
|
7 | | -import {DynamoDBClient} from '@aws-sdk/client-dynamodb'; |
8 | | -import {DynamoDBDocumentClient, GetCommand, type GetCommandOutput} from '@aws-sdk/lib-dynamodb'; |
9 | | - |
10 | | -import {logMRTError} from '../utils/utils.js'; |
11 | | - |
12 | | -export class DataStoreNotFoundError extends Error { |
13 | | - constructor(message: string) { |
14 | | - super(message); |
15 | | - this.name = 'DataStoreNotFoundError'; |
16 | | - Object.setPrototypeOf(this, DataStoreNotFoundError.prototype); |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -export class DataStoreServiceError extends Error { |
21 | | - constructor(message: string) { |
22 | | - super(message); |
23 | | - this.name = 'DataStoreServiceError'; |
24 | | - Object.setPrototypeOf(this, DataStoreServiceError.prototype); |
25 | | - } |
26 | | -} |
27 | | - |
28 | | -export class DataStoreUnavailableError extends Error { |
29 | | - constructor(message: string) { |
30 | | - super(message); |
31 | | - this.name = 'DataStoreUnavailableError'; |
32 | | - Object.setPrototypeOf(this, DataStoreUnavailableError.prototype); |
33 | | - } |
34 | | -} |
35 | | - |
36 | 7 | /** |
37 | | - * A class for reading entries from the data store. |
38 | | - * |
39 | | - * This class uses a singleton pattern. |
40 | | - * Use DataStore.getDataStore() to get the singleton instance. |
| 8 | + * @deprecated Import data-store symbols from `@salesforce/mrt-utilities/data-store`. |
| 9 | + * This compatibility path is kept for backward compatibility. |
41 | 10 | */ |
42 | | -export class DataStore { |
43 | | - private _tableName: string = ''; |
44 | | - private _ddb: DynamoDBDocumentClient | null = null; |
45 | | - private static _instance: DataStore | null = null; |
46 | | - |
47 | | - /** @internal Test hook: inject a document client for unit tests */ |
48 | | - static _testDocumentClient: DynamoDBDocumentClient | null = null; |
49 | | - /** @internal Test hook: inject logMRTError for unit tests */ |
50 | | - static _testLogMRTError: ((namespace: string, err: unknown, context?: Record<string, unknown>) => void) | null = null; |
51 | | - |
52 | | - private constructor() { |
53 | | - // Private constructor for singleton; use DataStore.getDataStore() instead. |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Get or create a DynamoDB document client (for abstraction of attribute values). |
58 | | - * |
59 | | - * @private |
60 | | - * @returns The DynamoDB document client |
61 | | - * @throws {DataStoreUnavailableError} The data store is unavailable |
62 | | - */ |
63 | | - private getClient(): DynamoDBDocumentClient { |
64 | | - if (!this.isDataStoreAvailable()) { |
65 | | - throw new DataStoreUnavailableError('The data store is unavailable.'); |
66 | | - } |
67 | | - |
68 | | - if (DataStore._testDocumentClient) { |
69 | | - this._tableName = `DataAccessLayer-${process.env.AWS_REGION}`; |
70 | | - return DataStore._testDocumentClient; |
71 | | - } |
72 | | - |
73 | | - if (!this._ddb) { |
74 | | - this._tableName = `DataAccessLayer-${process.env.AWS_REGION}`; |
75 | | - this._ddb = DynamoDBDocumentClient.from( |
76 | | - new DynamoDBClient({ |
77 | | - region: process.env.AWS_REGION, |
78 | | - }), |
79 | | - ); |
80 | | - } |
81 | | - |
82 | | - return this._ddb; |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * Get or create the singleton DataStore instance. |
87 | | - * |
88 | | - * @returns The singleton DataStore instance |
89 | | - */ |
90 | | - static getDataStore(): DataStore { |
91 | | - if (!DataStore._instance) { |
92 | | - DataStore._instance = new DataStore(); |
93 | | - } |
94 | | - return DataStore._instance; |
95 | | - } |
96 | | - |
97 | | - /** |
98 | | - * Whether the data store can be used in the current environment. |
99 | | - * |
100 | | - * @returns true if the data store is available, false otherwise |
101 | | - */ |
102 | | - isDataStoreAvailable(): boolean { |
103 | | - return Boolean(process.env.AWS_REGION && process.env.MOBIFY_PROPERTY_ID && process.env.DEPLOY_TARGET); |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Fetch an entry from the data store. |
108 | | - * |
109 | | - * @param key The data store entry's key |
110 | | - * @returns An object containing the entry's key and value |
111 | | - * @throws {DataStoreUnavailableError} The data store is unavailable |
112 | | - * @throws {DataStoreNotFoundError} An entry with the given key cannot be found |
113 | | - * @throws {DataStoreServiceError} An internal error occurred |
114 | | - */ |
115 | | - async getEntry(key: string): Promise<Record<string, unknown> | undefined> { |
116 | | - if (!this.isDataStoreAvailable()) { |
117 | | - throw new DataStoreUnavailableError('The data store is unavailable.'); |
118 | | - } |
119 | | - |
120 | | - const ddb = this.getClient(); |
121 | | - let response: GetCommandOutput; |
122 | | - try { |
123 | | - response = await ddb.send( |
124 | | - new GetCommand({ |
125 | | - TableName: this._tableName, |
126 | | - Key: { |
127 | | - projectEnvironment: `${process.env.MOBIFY_PROPERTY_ID} ${process.env.DEPLOY_TARGET}`, |
128 | | - key, |
129 | | - }, |
130 | | - }), |
131 | | - ); |
132 | | - } catch (error) { |
133 | | - const logFn = DataStore._testLogMRTError ?? logMRTError; |
134 | | - logFn('data_store', error, {key, tableName: this._tableName}); |
135 | | - throw new DataStoreServiceError('Data store request failed.'); |
136 | | - } |
137 | | - |
138 | | - if (!response.Item?.value) { |
139 | | - throw new DataStoreNotFoundError(`Data store entry '${key}' not found.`); |
140 | | - } |
141 | | - |
142 | | - return {key, value: response.Item.value}; |
143 | | - } |
144 | | -} |
| 11 | +export * from '../data-store/index.js'; |
0 commit comments