@encodeagent/platform-helper-data 1.2510.1231638 → 1.2511.1011152
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/cosmosdb/index.d.ts +8 -1
- package/dist/cosmosdb/index.d.ts.map +1 -1
- package/dist/cosmosdb/index.js +146 -7
- package/dist/cosmosdb/index.js.map +1 -1
- package/dist/dynamodb/index.d.ts +11 -4
- package/dist/dynamodb/index.d.ts.map +1 -1
- package/dist/dynamodb/index.js +260 -14
- package/dist/dynamodb/index.js.map +1 -1
- package/dist/firestore/index.d.ts +5 -3
- package/dist/firestore/index.d.ts.map +1 -1
- package/dist/firestore/index.js +192 -12
- package/dist/firestore/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/mongodb/index.d.ts +8 -1
- package/dist/mongodb/index.d.ts.map +1 -1
- package/dist/mongodb/index.js +188 -26
- package/dist/mongodb/index.js.map +1 -1
- package/dist/util.d.ts +35 -14
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +20 -5
- package/dist/util.js.map +1 -1
- package/dist/wrapper/index.d.ts +21 -1
- package/dist/wrapper/index.d.ts.map +1 -1
- package/dist/wrapper/index.js +237 -16
- package/dist/wrapper/index.js.map +1 -1
- package/package.json +2 -2
package/dist/cosmosdb/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Azure Cosmos DB implementation
|
|
3
3
|
*/
|
|
4
|
-
import { IConnectionForServiceProps, IConnectionResult, IRetrieveResult,
|
|
4
|
+
import { IConnectionForServiceProps, IConnectionResult, IRetrieveResult, IRetrieveProps, ICreateProps, ICreateResult, IQueryProps, IQueryResult, IUpsertProps, IUpsertResult, IUpdateProps, IUpdateResult } from '../util';
|
|
5
5
|
/**
|
|
6
6
|
* Create Azure Cosmos DB connection
|
|
7
7
|
* @param props Connection properties with URI and key
|
|
@@ -16,4 +16,11 @@ export declare function connect(props: IConnectionForServiceProps): Promise<ICon
|
|
|
16
16
|
export declare function retrieve(props: IRetrieveProps): Promise<IRetrieveResult>;
|
|
17
17
|
export declare function query(props: IQueryProps): Promise<IQueryResult>;
|
|
18
18
|
export declare function create(props: ICreateProps): Promise<ICreateResult>;
|
|
19
|
+
/**
|
|
20
|
+
* Upsert (create or update) a document in Azure Cosmos DB
|
|
21
|
+
* @param props Upsert properties with client, databaseId, containerId, record, and optional partitionKey
|
|
22
|
+
* @returns Promise<IUpsertResult>
|
|
23
|
+
*/
|
|
24
|
+
export declare function upsert(props: IUpsertProps): Promise<IUpsertResult>;
|
|
25
|
+
export declare function update(props: IUpdateProps): Promise<IUpdateResult>;
|
|
19
26
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cosmosdb/index.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cosmosdb/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,0BAA0B,EAAE,iBAAiB,EAC7C,eAAe,EAAE,cAAc,EAC/B,YAAY,EAAE,aAAa,EAC3B,WAAW,EAAE,YAAY,EACzB,YAAY,EAAE,aAAa,EAC3B,YAAY,EAAE,aAAa,EAG9B,MAAM,SAAS,CAAC;AAWjB;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA8C3F;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAuD9E;AAGD,wBAAsB,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,CAsDrE;AAED,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAyCxE;AAED;;;;GAIG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAkDxE;AAGD,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAsDxE"}
|
package/dist/cosmosdb/index.js
CHANGED
|
@@ -7,11 +7,17 @@ exports.connect = connect;
|
|
|
7
7
|
exports.retrieve = retrieve;
|
|
8
8
|
exports.query = query;
|
|
9
9
|
exports.create = create;
|
|
10
|
-
|
|
10
|
+
exports.upsert = upsert;
|
|
11
|
+
exports.update = update;
|
|
11
12
|
const util_1 = require("../util");
|
|
13
|
+
const platform_helper_util_1 = require("@encodeagent/platform-helper-util");
|
|
12
14
|
const cosmos_1 = require("@azure/cosmos");
|
|
13
15
|
const query_builder_1 = require("./query-builder");
|
|
14
16
|
const ENGINE = "cosmosdb";
|
|
17
|
+
const ERROR_INFO = {
|
|
18
|
+
package: util_1.PACKAGE,
|
|
19
|
+
module: "database-cosmosdb"
|
|
20
|
+
};
|
|
15
21
|
/**
|
|
16
22
|
* Create Azure Cosmos DB connection
|
|
17
23
|
* @param props Connection properties with URI and key
|
|
@@ -23,13 +29,23 @@ async function connect(props) {
|
|
|
23
29
|
if (!uri) {
|
|
24
30
|
return {
|
|
25
31
|
engine: ENGINE,
|
|
26
|
-
error:
|
|
32
|
+
error: {
|
|
33
|
+
...ERROR_INFO,
|
|
34
|
+
source: "connect",
|
|
35
|
+
code: "require-db-uri",
|
|
36
|
+
message: "Azure Cosmos DB URI is required"
|
|
37
|
+
}
|
|
27
38
|
};
|
|
28
39
|
}
|
|
29
40
|
if (!key) {
|
|
30
41
|
return {
|
|
31
42
|
engine: ENGINE,
|
|
32
|
-
error:
|
|
43
|
+
error: {
|
|
44
|
+
...ERROR_INFO,
|
|
45
|
+
source: "connect",
|
|
46
|
+
code: "require-db-key",
|
|
47
|
+
message: "Azure Cosmos DB key is required"
|
|
48
|
+
}
|
|
33
49
|
};
|
|
34
50
|
}
|
|
35
51
|
// Create Azure Cosmos DB client
|
|
@@ -42,7 +58,12 @@ async function connect(props) {
|
|
|
42
58
|
catch (error) {
|
|
43
59
|
return {
|
|
44
60
|
engine: ENGINE,
|
|
45
|
-
error:
|
|
61
|
+
error: {
|
|
62
|
+
...ERROR_INFO,
|
|
63
|
+
source: "connect",
|
|
64
|
+
code: "database-connect",
|
|
65
|
+
message: error instanceof Error ? error.message : 'Failed to connect to Azure Cosmos DB'
|
|
66
|
+
}
|
|
46
67
|
};
|
|
47
68
|
}
|
|
48
69
|
}
|
|
@@ -55,6 +76,8 @@ async function retrieve(props) {
|
|
|
55
76
|
try {
|
|
56
77
|
const { client, id, partitionKey, databaseId, containerId } = props;
|
|
57
78
|
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
79
|
+
engine: ENGINE,
|
|
80
|
+
...ERROR_INFO,
|
|
58
81
|
databaseId,
|
|
59
82
|
containerId,
|
|
60
83
|
checkDatabaseId: true,
|
|
@@ -91,7 +114,12 @@ async function retrieve(props) {
|
|
|
91
114
|
catch (error) {
|
|
92
115
|
return {
|
|
93
116
|
engine: ENGINE,
|
|
94
|
-
error:
|
|
117
|
+
error: {
|
|
118
|
+
...ERROR_INFO,
|
|
119
|
+
source: "retrieve",
|
|
120
|
+
code: "database-retrieve",
|
|
121
|
+
message: error instanceof Error ? error.message : 'Failed to retrieve document from Cosmos DB'
|
|
122
|
+
}
|
|
95
123
|
};
|
|
96
124
|
}
|
|
97
125
|
}
|
|
@@ -100,6 +128,8 @@ async function query(props) {
|
|
|
100
128
|
const { client, databaseId, containerId, query } = props;
|
|
101
129
|
try {
|
|
102
130
|
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
131
|
+
engine: ENGINE,
|
|
132
|
+
...ERROR_INFO,
|
|
103
133
|
databaseId,
|
|
104
134
|
containerId,
|
|
105
135
|
checkDatabaseId: true,
|
|
@@ -130,7 +160,12 @@ async function query(props) {
|
|
|
130
160
|
engine: ENGINE,
|
|
131
161
|
query,
|
|
132
162
|
dbQuery,
|
|
133
|
-
error:
|
|
163
|
+
error: {
|
|
164
|
+
...ERROR_INFO,
|
|
165
|
+
source: "query",
|
|
166
|
+
code: "database-query",
|
|
167
|
+
message: error instanceof Error ? error.message : 'Failed to query documents from Cosmos DB'
|
|
168
|
+
}
|
|
134
169
|
};
|
|
135
170
|
}
|
|
136
171
|
}
|
|
@@ -138,6 +173,8 @@ async function create(props) {
|
|
|
138
173
|
try {
|
|
139
174
|
const { client, databaseId, containerId, record } = props;
|
|
140
175
|
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
176
|
+
engine: ENGINE,
|
|
177
|
+
...ERROR_INFO,
|
|
141
178
|
databaseId,
|
|
142
179
|
containerId,
|
|
143
180
|
checkDatabaseId: true,
|
|
@@ -160,7 +197,109 @@ async function create(props) {
|
|
|
160
197
|
catch (error) {
|
|
161
198
|
return {
|
|
162
199
|
engine: ENGINE,
|
|
163
|
-
error:
|
|
200
|
+
error: {
|
|
201
|
+
...ERROR_INFO,
|
|
202
|
+
source: "create",
|
|
203
|
+
code: "database-create",
|
|
204
|
+
message: error instanceof Error ? error.message : 'Failed to create record in Cosmos DB'
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Upsert (create or update) a document in Azure Cosmos DB
|
|
211
|
+
* @param props Upsert properties with client, databaseId, containerId, record, and optional partitionKey
|
|
212
|
+
* @returns Promise<IUpsertResult>
|
|
213
|
+
*/
|
|
214
|
+
async function upsert(props) {
|
|
215
|
+
try {
|
|
216
|
+
const { client, databaseId, containerId, record } = props;
|
|
217
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
218
|
+
engine: ENGINE,
|
|
219
|
+
...ERROR_INFO,
|
|
220
|
+
databaseId,
|
|
221
|
+
containerId,
|
|
222
|
+
checkDatabaseId: true,
|
|
223
|
+
checkContainerId: true
|
|
224
|
+
});
|
|
225
|
+
if (errorResult) {
|
|
226
|
+
return errorResult;
|
|
227
|
+
}
|
|
228
|
+
const database = client.database(databaseId);
|
|
229
|
+
const container = database.container(containerId);
|
|
230
|
+
// Ensure the record has an id
|
|
231
|
+
const recordWithId = {
|
|
232
|
+
...record,
|
|
233
|
+
id: record.id ?? (0, platform_helper_util_1.newGuid)()
|
|
234
|
+
};
|
|
235
|
+
// Use upsert operation which creates if not exists or replaces if exists
|
|
236
|
+
const response = await container.items.upsert(recordWithId);
|
|
237
|
+
// Check if this was a new document or an update
|
|
238
|
+
// Cosmos DB doesn't directly tell us, but we can infer from the response
|
|
239
|
+
const isNew = response.statusCode === 201;
|
|
240
|
+
return {
|
|
241
|
+
engine: ENGINE,
|
|
242
|
+
data: response.resource,
|
|
243
|
+
isNew
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
return {
|
|
248
|
+
engine: ENGINE,
|
|
249
|
+
error: {
|
|
250
|
+
...ERROR_INFO,
|
|
251
|
+
source: "upsert",
|
|
252
|
+
code: "database-upsert",
|
|
253
|
+
message: error instanceof Error ? error.message : 'Failed to upsert record in Cosmos DB'
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async function update(props) {
|
|
259
|
+
try {
|
|
260
|
+
const { client, databaseId, containerId, record, partitionKey } = props;
|
|
261
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
262
|
+
engine: ENGINE,
|
|
263
|
+
...ERROR_INFO,
|
|
264
|
+
databaseId,
|
|
265
|
+
containerId,
|
|
266
|
+
checkDatabaseId: true,
|
|
267
|
+
checkContainerId: true
|
|
268
|
+
});
|
|
269
|
+
if (errorResult) {
|
|
270
|
+
return errorResult;
|
|
271
|
+
}
|
|
272
|
+
if (!record?.id || !(0, platform_helper_util_1.isNonEmptyString)(String(record.id))) {
|
|
273
|
+
return {
|
|
274
|
+
engine: ENGINE,
|
|
275
|
+
error: {
|
|
276
|
+
...ERROR_INFO,
|
|
277
|
+
source: "update",
|
|
278
|
+
code: "require-record-id",
|
|
279
|
+
message: "Record id is required for update"
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const database = client.database(databaseId);
|
|
284
|
+
const container = database.container(containerId);
|
|
285
|
+
const item = (0, platform_helper_util_1.isNonEmptyString)(partitionKey)
|
|
286
|
+
? container.item(record.id, partitionKey)
|
|
287
|
+
: container.item(record.id);
|
|
288
|
+
const response = await item.replace(record);
|
|
289
|
+
return {
|
|
290
|
+
engine: ENGINE,
|
|
291
|
+
data: response.resource
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
return {
|
|
296
|
+
engine: ENGINE,
|
|
297
|
+
error: {
|
|
298
|
+
...ERROR_INFO,
|
|
299
|
+
source: "update",
|
|
300
|
+
code: "database-update",
|
|
301
|
+
message: error instanceof Error ? error.message : 'Failed to update record in Cosmos DB'
|
|
302
|
+
}
|
|
164
303
|
};
|
|
165
304
|
}
|
|
166
305
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cosmosdb/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cosmosdb/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;AA2BH,0BA8CC;AAOD,4BAuDC;AAGD,sBAsDC;AAED,wBAyCC;AAOD,wBAkDC;AAGD,wBAsDC;AA3VD,kCASiB;AACjB,4EAA8E;AAC9E,0CAA6C;AAC7C,mDAA+C;AAE/C,MAAM,MAAM,GAAG,UAAU,CAAC;AAC1B,MAAM,UAAU,GAAG;IACf,OAAO,EAAE,cAAO;IAChB,MAAM,EAAE,mBAAmB;CAC9B,CAAA;AAED;;;;GAIG;AACI,KAAK,UAAU,OAAO,CAAC,K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CAAC,EAAE,CAAC,CAAC;QAEhC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAE5C,OAAO;YACH,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,QAAQ,CAAC,QAAQ;SAC1B,CAAC;IAEN,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO;YACH,MAAM,EAAE,MAAM;YACd,KAAK,EAAE;gBACH,GAAG,UAAU;gBACb,MAAM,EAAE,QAAQ;gBAChB,IAAI,EAAE,iBAAiB;gBACvB,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,sCAAsC;aAC3F;SACJ,CAAC;IACN,CAAC;AACL,CAAC"}
|
package/dist/dynamodb/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview AWS DynamoDB implementation
|
|
3
3
|
*/
|
|
4
|
-
import { IConnectionForServiceProps, IConnectionResult, IRetrieveResult, IRetrieveProps, ICreateProps } from '../util';
|
|
4
|
+
import { IConnectionForServiceProps, IConnectionResult, IRetrieveResult, IRetrieveProps, ICreateProps, ICreateResult, IUpsertProps, IUpsertResult, IUpdateProps, IUpdateResult } from '../util';
|
|
5
5
|
/**
|
|
6
6
|
* Create AWS DynamoDB connection
|
|
7
7
|
* @param props Connection properties with URI and key
|
|
@@ -11,8 +11,15 @@ export declare function connect(props: IConnectionForServiceProps): Promise<ICon
|
|
|
11
11
|
/**
|
|
12
12
|
* Retrieve document by ID from AWS DynamoDB
|
|
13
13
|
* @param props Retrieve properties with client, collection (table), and id
|
|
14
|
-
* @returns Promise<IRetrieveResult
|
|
14
|
+
* @returns Promise<IRetrieveResult>
|
|
15
15
|
*/
|
|
16
|
-
export declare function retrieve
|
|
17
|
-
export declare function create
|
|
16
|
+
export declare function retrieve(props: IRetrieveProps): Promise<IRetrieveResult>;
|
|
17
|
+
export declare function create(props: ICreateProps): Promise<ICreateResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Upsert (create or update) a document in AWS DynamoDB
|
|
20
|
+
* @param props Upsert properties with client, containerId (table name), and record
|
|
21
|
+
* @returns Promise<IUpsertResult>
|
|
22
|
+
*/
|
|
23
|
+
export declare function upsert(props: IUpsertProps): Promise<IUpsertResult>;
|
|
24
|
+
export declare function update(props: IUpdateProps): Promise<IUpdateResult>;
|
|
18
25
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dynamodb/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dynamodb/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAEH,0BAA0B,EAAE,iBAAiB,EAC7C,eAAe,EAAE,cAAc,EAC/B,YAAY,EAAE,aAAa,EAC3B,YAAY,EAAE,aAAa,EAC3B,YAAY,EAAE,aAAa,EAC9B,MAAM,SAAS,CAAC;AAYjB;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAuE3F;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAmD9E;AAED,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAqExE;AAED;;;;GAIG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAoFxE;AAED,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CA4GxE"}
|
package/dist/dynamodb/index.js
CHANGED
|
@@ -6,10 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.connect = connect;
|
|
7
7
|
exports.retrieve = retrieve;
|
|
8
8
|
exports.create = create;
|
|
9
|
+
exports.upsert = upsert;
|
|
10
|
+
exports.update = update;
|
|
11
|
+
const util_1 = require("../util");
|
|
9
12
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
10
13
|
const util_dynamodb_1 = require("@aws-sdk/util-dynamodb");
|
|
11
14
|
const platform_helper_util_1 = require("@encodeagent/platform-helper-util");
|
|
15
|
+
const util_2 = require("../util");
|
|
12
16
|
const ENGINE = "dynamodb";
|
|
17
|
+
const ERROR_INFO = {
|
|
18
|
+
package: util_2.PACKAGE,
|
|
19
|
+
module: "database-dynamodb"
|
|
20
|
+
};
|
|
13
21
|
/**
|
|
14
22
|
* Create AWS DynamoDB connection
|
|
15
23
|
* @param props Connection properties with URI and key
|
|
@@ -20,14 +28,24 @@ async function connect(props) {
|
|
|
20
28
|
const { uri, key } = props;
|
|
21
29
|
if (!uri) {
|
|
22
30
|
return {
|
|
23
|
-
engine:
|
|
24
|
-
error:
|
|
31
|
+
engine: ENGINE,
|
|
32
|
+
error: {
|
|
33
|
+
...ERROR_INFO,
|
|
34
|
+
source: "connect",
|
|
35
|
+
code: "require-db-uri",
|
|
36
|
+
message: "AWS DynamoDB URI is required"
|
|
37
|
+
}
|
|
25
38
|
};
|
|
26
39
|
}
|
|
27
40
|
if (!key) {
|
|
28
41
|
return {
|
|
29
42
|
engine: ENGINE,
|
|
30
|
-
error:
|
|
43
|
+
error: {
|
|
44
|
+
...ERROR_INFO,
|
|
45
|
+
source: "connect",
|
|
46
|
+
code: "require-db-key",
|
|
47
|
+
message: "AWS DynamoDB access key is required"
|
|
48
|
+
}
|
|
31
49
|
};
|
|
32
50
|
}
|
|
33
51
|
// Parse AWS credentials from key (format: accessKeyId:secretAccessKey)
|
|
@@ -35,7 +53,12 @@ async function connect(props) {
|
|
|
35
53
|
if (!accessKeyId || !secretAccessKey) {
|
|
36
54
|
return {
|
|
37
55
|
engine: ENGINE,
|
|
38
|
-
error:
|
|
56
|
+
error: {
|
|
57
|
+
...ERROR_INFO,
|
|
58
|
+
source: "connect",
|
|
59
|
+
code: "invalid-db-key",
|
|
60
|
+
message: "AWS key must be in format: accessKeyId:secretAccessKey"
|
|
61
|
+
}
|
|
39
62
|
};
|
|
40
63
|
}
|
|
41
64
|
// Create DynamoDB client configuration
|
|
@@ -56,18 +79,34 @@ async function connect(props) {
|
|
|
56
79
|
catch (error) {
|
|
57
80
|
return {
|
|
58
81
|
engine: ENGINE,
|
|
59
|
-
error:
|
|
82
|
+
error: {
|
|
83
|
+
...ERROR_INFO,
|
|
84
|
+
source: "connect",
|
|
85
|
+
code: "database-connect",
|
|
86
|
+
message: error instanceof Error ? error.message : 'Failed to connect to AWS DynamoDB'
|
|
87
|
+
}
|
|
60
88
|
};
|
|
61
89
|
}
|
|
62
90
|
}
|
|
63
91
|
/**
|
|
64
92
|
* Retrieve document by ID from AWS DynamoDB
|
|
65
93
|
* @param props Retrieve properties with client, collection (table), and id
|
|
66
|
-
* @returns Promise<IRetrieveResult
|
|
94
|
+
* @returns Promise<IRetrieveResult>
|
|
67
95
|
*/
|
|
68
96
|
async function retrieve(props) {
|
|
69
97
|
try {
|
|
70
|
-
const { client, id, containerId } = props;
|
|
98
|
+
const { client, id, containerId, databaseId } = props;
|
|
99
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
100
|
+
engine: ENGINE,
|
|
101
|
+
...ERROR_INFO,
|
|
102
|
+
databaseId,
|
|
103
|
+
containerId,
|
|
104
|
+
checkDatabaseId: true,
|
|
105
|
+
checkContainerId: true
|
|
106
|
+
});
|
|
107
|
+
if (errorResult) {
|
|
108
|
+
return errorResult;
|
|
109
|
+
}
|
|
71
110
|
// Create GetItem command
|
|
72
111
|
const command = new client_dynamodb_1.GetItemCommand({
|
|
73
112
|
TableName: containerId,
|
|
@@ -77,8 +116,7 @@ async function retrieve(props) {
|
|
|
77
116
|
const response = await client.send(command);
|
|
78
117
|
if (!response.Item) {
|
|
79
118
|
return {
|
|
80
|
-
engine: ENGINE
|
|
81
|
-
error: `Document with ID '${id}' not found in table '${containerId}'`
|
|
119
|
+
engine: ENGINE
|
|
82
120
|
};
|
|
83
121
|
}
|
|
84
122
|
// Unmarshall the DynamoDB item to regular object
|
|
@@ -91,23 +129,49 @@ async function retrieve(props) {
|
|
|
91
129
|
catch (error) {
|
|
92
130
|
return {
|
|
93
131
|
engine: ENGINE,
|
|
94
|
-
error:
|
|
132
|
+
error: {
|
|
133
|
+
...ERROR_INFO,
|
|
134
|
+
source: "retrieve",
|
|
135
|
+
code: "database-retrieve",
|
|
136
|
+
message: error instanceof Error ? error.message : 'Failed to retrieve document from DynamoDB'
|
|
137
|
+
}
|
|
95
138
|
};
|
|
96
139
|
}
|
|
97
140
|
}
|
|
98
141
|
async function create(props) {
|
|
99
142
|
try {
|
|
100
|
-
const { client, containerId, record } = props;
|
|
143
|
+
const { client, containerId, record, databaseId } = props;
|
|
144
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
145
|
+
engine: ENGINE,
|
|
146
|
+
...ERROR_INFO,
|
|
147
|
+
databaseId,
|
|
148
|
+
containerId,
|
|
149
|
+
checkDatabaseId: true,
|
|
150
|
+
checkContainerId: true
|
|
151
|
+
});
|
|
152
|
+
if (errorResult) {
|
|
153
|
+
return errorResult;
|
|
154
|
+
}
|
|
101
155
|
if (!containerId) {
|
|
102
156
|
return {
|
|
103
157
|
engine: ENGINE,
|
|
104
|
-
error:
|
|
158
|
+
error: {
|
|
159
|
+
...ERROR_INFO,
|
|
160
|
+
source: "create",
|
|
161
|
+
code: "require-container-id",
|
|
162
|
+
message: "The containerId (table name) is required"
|
|
163
|
+
}
|
|
105
164
|
};
|
|
106
165
|
}
|
|
107
166
|
if (!record) {
|
|
108
167
|
return {
|
|
109
168
|
engine: ENGINE,
|
|
110
|
-
error:
|
|
169
|
+
error: {
|
|
170
|
+
...ERROR_INFO,
|
|
171
|
+
source: "create",
|
|
172
|
+
code: "require-record",
|
|
173
|
+
message: "The record is required"
|
|
174
|
+
}
|
|
111
175
|
};
|
|
112
176
|
}
|
|
113
177
|
const item = {
|
|
@@ -127,7 +191,189 @@ async function create(props) {
|
|
|
127
191
|
catch (error) {
|
|
128
192
|
return {
|
|
129
193
|
engine: ENGINE,
|
|
130
|
-
error:
|
|
194
|
+
error: {
|
|
195
|
+
...ERROR_INFO,
|
|
196
|
+
source: "create",
|
|
197
|
+
code: "database-create",
|
|
198
|
+
message: error instanceof Error ? error.message : 'Failed to create record in DynamoDB'
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Upsert (create or update) a document in AWS DynamoDB
|
|
205
|
+
* @param props Upsert properties with client, containerId (table name), and record
|
|
206
|
+
* @returns Promise<IUpsertResult>
|
|
207
|
+
*/
|
|
208
|
+
async function upsert(props) {
|
|
209
|
+
try {
|
|
210
|
+
const { client, containerId, record, databaseId } = props;
|
|
211
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
212
|
+
engine: ENGINE,
|
|
213
|
+
...ERROR_INFO,
|
|
214
|
+
databaseId,
|
|
215
|
+
containerId,
|
|
216
|
+
checkDatabaseId: true,
|
|
217
|
+
checkContainerId: true
|
|
218
|
+
});
|
|
219
|
+
if (errorResult) {
|
|
220
|
+
return errorResult;
|
|
221
|
+
}
|
|
222
|
+
if (!containerId) {
|
|
223
|
+
return {
|
|
224
|
+
engine: ENGINE,
|
|
225
|
+
error: {
|
|
226
|
+
...ERROR_INFO,
|
|
227
|
+
source: "upsert",
|
|
228
|
+
code: "require-container-id",
|
|
229
|
+
message: "The containerId (table name) is required"
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (!record) {
|
|
234
|
+
return {
|
|
235
|
+
engine: ENGINE,
|
|
236
|
+
error: {
|
|
237
|
+
...ERROR_INFO,
|
|
238
|
+
source: "upsert",
|
|
239
|
+
code: "require-record",
|
|
240
|
+
message: "The record is required"
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
// Ensure the record has an id
|
|
245
|
+
const recordWithId = {
|
|
246
|
+
...record,
|
|
247
|
+
id: record.id ?? (0, platform_helper_util_1.newGuid)()
|
|
248
|
+
};
|
|
249
|
+
// Check if the item exists to determine if this is a new record
|
|
250
|
+
let isNew = true;
|
|
251
|
+
if (record.id) {
|
|
252
|
+
const getCommand = new client_dynamodb_1.GetItemCommand({
|
|
253
|
+
TableName: containerId,
|
|
254
|
+
Key: (0, util_dynamodb_1.marshall)({ id: record.id })
|
|
255
|
+
});
|
|
256
|
+
const getResponse = await client.send(getCommand);
|
|
257
|
+
isNew = !getResponse.Item;
|
|
258
|
+
}
|
|
259
|
+
// Use PutItem which will create or replace the item
|
|
260
|
+
const putCommand = new client_dynamodb_1.PutItemCommand({
|
|
261
|
+
TableName: containerId,
|
|
262
|
+
Item: (0, util_dynamodb_1.marshall)(recordWithId)
|
|
263
|
+
});
|
|
264
|
+
await client.send(putCommand);
|
|
265
|
+
return {
|
|
266
|
+
engine: ENGINE,
|
|
267
|
+
data: recordWithId,
|
|
268
|
+
isNew
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
return {
|
|
273
|
+
engine: ENGINE,
|
|
274
|
+
error: {
|
|
275
|
+
...ERROR_INFO,
|
|
276
|
+
source: "upsert",
|
|
277
|
+
code: "database-upsert",
|
|
278
|
+
message: error instanceof Error ? error.message : 'Failed to upsert record in DynamoDB'
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async function update(props) {
|
|
284
|
+
try {
|
|
285
|
+
const { client, containerId, record } = props;
|
|
286
|
+
const errorResult = (0, util_1.validateDatabaseSetting)({
|
|
287
|
+
engine: ENGINE,
|
|
288
|
+
...ERROR_INFO,
|
|
289
|
+
containerId,
|
|
290
|
+
checkDatabaseId: true,
|
|
291
|
+
checkContainerId: true
|
|
292
|
+
});
|
|
293
|
+
if (errorResult) {
|
|
294
|
+
return errorResult;
|
|
295
|
+
}
|
|
296
|
+
if (!containerId) {
|
|
297
|
+
return {
|
|
298
|
+
engine: ENGINE,
|
|
299
|
+
error: {
|
|
300
|
+
...ERROR_INFO,
|
|
301
|
+
source: "update",
|
|
302
|
+
code: "require-container-id",
|
|
303
|
+
message: "The containerId (table name) is required"
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (!record) {
|
|
308
|
+
return {
|
|
309
|
+
engine: ENGINE,
|
|
310
|
+
error: {
|
|
311
|
+
...ERROR_INFO,
|
|
312
|
+
source: "update",
|
|
313
|
+
code: "require-record",
|
|
314
|
+
message: "The record is required"
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
const id = record.id;
|
|
319
|
+
if (!id) {
|
|
320
|
+
return {
|
|
321
|
+
engine: ENGINE,
|
|
322
|
+
error: {
|
|
323
|
+
...ERROR_INFO,
|
|
324
|
+
source: "update",
|
|
325
|
+
code: "require-record-id",
|
|
326
|
+
message: "The record.id is required for update"
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
const entries = Object.entries(record).filter(([k]) => k !== 'id');
|
|
331
|
+
if (entries.length === 0) {
|
|
332
|
+
return {
|
|
333
|
+
engine: ENGINE,
|
|
334
|
+
error: {
|
|
335
|
+
...ERROR_INFO,
|
|
336
|
+
source: "update",
|
|
337
|
+
code: "no-update-fields",
|
|
338
|
+
message: "No fields to update"
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
const ExpressionAttributeNames = {};
|
|
343
|
+
const ExpressionAttributeValues = {};
|
|
344
|
+
const assignments = [];
|
|
345
|
+
entries.forEach(([key, value], idx) => {
|
|
346
|
+
const nameKey = `#n${idx}`;
|
|
347
|
+
const valueKey = `:v${idx}`;
|
|
348
|
+
ExpressionAttributeNames[nameKey] = key;
|
|
349
|
+
ExpressionAttributeValues[valueKey] = value;
|
|
350
|
+
assignments.push(`${nameKey} = ${valueKey}`);
|
|
351
|
+
});
|
|
352
|
+
const UpdateExpression = `SET ${assignments.join(', ')}`;
|
|
353
|
+
const command = new client_dynamodb_1.UpdateItemCommand({
|
|
354
|
+
TableName: containerId,
|
|
355
|
+
Key: (0, util_dynamodb_1.marshall)({ id }),
|
|
356
|
+
UpdateExpression,
|
|
357
|
+
ExpressionAttributeNames,
|
|
358
|
+
ExpressionAttributeValues: (0, util_dynamodb_1.marshall)(ExpressionAttributeValues),
|
|
359
|
+
ReturnValues: 'ALL_NEW'
|
|
360
|
+
});
|
|
361
|
+
const resp = await client.send(command);
|
|
362
|
+
const data = resp.Attributes ? ((0, util_dynamodb_1.unmarshall)(resp.Attributes)) : (record);
|
|
363
|
+
return {
|
|
364
|
+
engine: ENGINE,
|
|
365
|
+
data
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
return {
|
|
370
|
+
engine: ENGINE,
|
|
371
|
+
error: {
|
|
372
|
+
...ERROR_INFO,
|
|
373
|
+
source: "update",
|
|
374
|
+
code: "database-update",
|
|
375
|
+
message: error instanceof Error ? error.message : 'Failed to update record in DynamoDB'
|
|
376
|
+
}
|
|
131
377
|
};
|
|
132
378
|
}
|
|
133
379
|
}
|