@celerity-sdk/datastore 0.3.1 → 0.5.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.
- package/README.md +5 -5
- package/dist/chunk-3I3BMI7I.js +21 -0
- package/dist/chunk-3I3BMI7I.js.map +1 -0
- package/dist/chunk-7QVYU63E.js +7 -0
- package/dist/chunk-7QVYU63E.js.map +1 -0
- package/dist/chunk-HHZJK2AK.js +30 -0
- package/dist/chunk-HHZJK2AK.js.map +1 -0
- package/dist/config-IXTAYSFF.js +8 -0
- package/dist/config-IXTAYSFF.js.map +1 -0
- package/dist/dynamodb-datastore-client-LQH3GS6X.js +463 -0
- package/dist/dynamodb-datastore-client-LQH3GS6X.js.map +1 -0
- package/dist/index.cjs +715 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +370 -1
- package/dist/index.js +156 -0
- package/dist/index.js.map +1 -1
- package/package.json +18 -7
- package/dist/index.d.cts +0 -2
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __esm = (fn, res) => function __init() {
|
|
10
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
6
16
|
var __copyProps = (to, from, except, desc) => {
|
|
7
17
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
18
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,9 +21,714 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
21
|
}
|
|
12
22
|
return to;
|
|
13
23
|
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
14
32
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
33
|
|
|
34
|
+
// src/errors.ts
|
|
35
|
+
var DatastoreError, ConditionalCheckFailedError;
|
|
36
|
+
var init_errors = __esm({
|
|
37
|
+
"src/errors.ts"() {
|
|
38
|
+
"use strict";
|
|
39
|
+
DatastoreError = class extends Error {
|
|
40
|
+
static {
|
|
41
|
+
__name(this, "DatastoreError");
|
|
42
|
+
}
|
|
43
|
+
table;
|
|
44
|
+
constructor(message, table, options) {
|
|
45
|
+
super(message, options), this.table = table;
|
|
46
|
+
this.name = "DatastoreError";
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
ConditionalCheckFailedError = class extends DatastoreError {
|
|
50
|
+
static {
|
|
51
|
+
__name(this, "ConditionalCheckFailedError");
|
|
52
|
+
}
|
|
53
|
+
constructor(table, options) {
|
|
54
|
+
super(`Conditional check failed on table "${table}"`, table, options);
|
|
55
|
+
this.name = "ConditionalCheckFailedError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// src/providers/dynamodb/expressions.ts
|
|
62
|
+
function buildKeyConditionExpression(key, range) {
|
|
63
|
+
const names = {};
|
|
64
|
+
const values = {};
|
|
65
|
+
let counter = 0;
|
|
66
|
+
const pkName = `#k${counter}`;
|
|
67
|
+
const pkValue = `:k${counter}`;
|
|
68
|
+
names[pkName] = key.name;
|
|
69
|
+
values[pkValue] = key.value;
|
|
70
|
+
counter++;
|
|
71
|
+
let expression = `${pkName} = ${pkValue}`;
|
|
72
|
+
if (range) {
|
|
73
|
+
const skName = `#k${counter}`;
|
|
74
|
+
names[skName] = range.name;
|
|
75
|
+
switch (range.operator) {
|
|
76
|
+
case "eq":
|
|
77
|
+
case "lt":
|
|
78
|
+
case "le":
|
|
79
|
+
case "gt":
|
|
80
|
+
case "ge": {
|
|
81
|
+
const skValue = `:k${counter}`;
|
|
82
|
+
values[skValue] = range.value;
|
|
83
|
+
expression += ` AND ${skName} ${COMPARISON_OPERATORS[range.operator]} ${skValue}`;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "between": {
|
|
87
|
+
const lowVal = `:k${counter}a`;
|
|
88
|
+
const highVal = `:k${counter}b`;
|
|
89
|
+
values[lowVal] = range.low;
|
|
90
|
+
values[highVal] = range.high;
|
|
91
|
+
expression += ` AND ${skName} BETWEEN ${lowVal} AND ${highVal}`;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case "startsWith": {
|
|
95
|
+
const skValue = `:k${counter}`;
|
|
96
|
+
values[skValue] = range.value;
|
|
97
|
+
expression += ` AND begins_with(${skName}, ${skValue})`;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
expression,
|
|
104
|
+
names,
|
|
105
|
+
values
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function buildFilterExpression(conditions) {
|
|
109
|
+
const condArray = Array.isArray(conditions) ? conditions : [
|
|
110
|
+
conditions
|
|
111
|
+
];
|
|
112
|
+
const names = {};
|
|
113
|
+
const values = {};
|
|
114
|
+
const parts = [];
|
|
115
|
+
let counter = 0;
|
|
116
|
+
for (const cond of condArray) {
|
|
117
|
+
const attrName = `#f${counter}`;
|
|
118
|
+
names[attrName] = cond.name;
|
|
119
|
+
switch (cond.operator) {
|
|
120
|
+
case "eq":
|
|
121
|
+
case "ne":
|
|
122
|
+
case "lt":
|
|
123
|
+
case "le":
|
|
124
|
+
case "gt":
|
|
125
|
+
case "ge": {
|
|
126
|
+
const valKey = `:f${counter}`;
|
|
127
|
+
values[valKey] = cond.value;
|
|
128
|
+
parts.push(`${attrName} ${COMPARISON_OPERATORS[cond.operator]} ${valKey}`);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case "between": {
|
|
132
|
+
const lowVal = `:f${counter}a`;
|
|
133
|
+
const highVal = `:f${counter}b`;
|
|
134
|
+
values[lowVal] = cond.low;
|
|
135
|
+
values[highVal] = cond.high;
|
|
136
|
+
parts.push(`${attrName} BETWEEN ${lowVal} AND ${highVal}`);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "startsWith":
|
|
140
|
+
case "contains": {
|
|
141
|
+
const valKey = `:f${counter}`;
|
|
142
|
+
values[valKey] = cond.value;
|
|
143
|
+
const fnName = cond.operator === "startsWith" ? "begins_with" : "contains";
|
|
144
|
+
parts.push(`${fnName}(${attrName}, ${valKey})`);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "exists":
|
|
148
|
+
parts.push(`attribute_exists(${attrName})`);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
counter++;
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
expression: parts.join(" AND "),
|
|
155
|
+
names,
|
|
156
|
+
values
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
var COMPARISON_OPERATORS;
|
|
160
|
+
var init_expressions = __esm({
|
|
161
|
+
"src/providers/dynamodb/expressions.ts"() {
|
|
162
|
+
"use strict";
|
|
163
|
+
COMPARISON_OPERATORS = {
|
|
164
|
+
eq: "=",
|
|
165
|
+
ne: "<>",
|
|
166
|
+
lt: "<",
|
|
167
|
+
le: "<=",
|
|
168
|
+
gt: ">",
|
|
169
|
+
ge: ">="
|
|
170
|
+
};
|
|
171
|
+
__name(buildKeyConditionExpression, "buildKeyConditionExpression");
|
|
172
|
+
__name(buildFilterExpression, "buildFilterExpression");
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// src/providers/dynamodb/dynamodb-item-listing.ts
|
|
177
|
+
function encodeCursor(lastEvaluatedKey) {
|
|
178
|
+
const state = {
|
|
179
|
+
lastEvaluatedKey
|
|
180
|
+
};
|
|
181
|
+
return Buffer.from(JSON.stringify(state)).toString("base64url");
|
|
182
|
+
}
|
|
183
|
+
function decodeCursor(cursor) {
|
|
184
|
+
return JSON.parse(Buffer.from(cursor, "base64url").toString("utf-8"));
|
|
185
|
+
}
|
|
186
|
+
var import_debug, import_lib_dynamodb, debug, DynamoDBItemListing;
|
|
187
|
+
var init_dynamodb_item_listing = __esm({
|
|
188
|
+
"src/providers/dynamodb/dynamodb-item-listing.ts"() {
|
|
189
|
+
"use strict";
|
|
190
|
+
import_debug = __toESM(require("debug"), 1);
|
|
191
|
+
import_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
|
|
192
|
+
init_errors();
|
|
193
|
+
init_expressions();
|
|
194
|
+
debug = (0, import_debug.default)("celerity:datastore:dynamodb");
|
|
195
|
+
__name(encodeCursor, "encodeCursor");
|
|
196
|
+
__name(decodeCursor, "decodeCursor");
|
|
197
|
+
DynamoDBItemListing = class {
|
|
198
|
+
static {
|
|
199
|
+
__name(this, "DynamoDBItemListing");
|
|
200
|
+
}
|
|
201
|
+
client;
|
|
202
|
+
tableName;
|
|
203
|
+
mode;
|
|
204
|
+
options;
|
|
205
|
+
tracer;
|
|
206
|
+
_cursor;
|
|
207
|
+
constructor(client, tableName, mode, options, tracer) {
|
|
208
|
+
this.client = client;
|
|
209
|
+
this.tableName = tableName;
|
|
210
|
+
this.mode = mode;
|
|
211
|
+
this.options = options;
|
|
212
|
+
this.tracer = tracer;
|
|
213
|
+
this._cursor = options.cursor;
|
|
214
|
+
}
|
|
215
|
+
get cursor() {
|
|
216
|
+
return this._cursor;
|
|
217
|
+
}
|
|
218
|
+
async *[Symbol.asyncIterator]() {
|
|
219
|
+
const cursorState = this._cursor ? decodeCursor(this._cursor) : void 0;
|
|
220
|
+
let exclusiveStartKey = cursorState?.lastEvaluatedKey;
|
|
221
|
+
do {
|
|
222
|
+
debug("%s page %s key=%o", this.mode, this.tableName, exclusiveStartKey ?? "(start)");
|
|
223
|
+
const response = await this.fetchPage(exclusiveStartKey);
|
|
224
|
+
for (const item of response.Items ?? []) {
|
|
225
|
+
yield item;
|
|
226
|
+
}
|
|
227
|
+
exclusiveStartKey = response.LastEvaluatedKey;
|
|
228
|
+
if (exclusiveStartKey) {
|
|
229
|
+
this._cursor = encodeCursor(exclusiveStartKey);
|
|
230
|
+
} else {
|
|
231
|
+
this._cursor = void 0;
|
|
232
|
+
}
|
|
233
|
+
} while (exclusiveStartKey);
|
|
234
|
+
}
|
|
235
|
+
async fetchPage(exclusiveStartKey) {
|
|
236
|
+
const command = this.mode === "query" ? this.buildQueryCommand(exclusiveStartKey) : this.buildScanCommand(exclusiveStartKey);
|
|
237
|
+
const doFetch = /* @__PURE__ */ __name(async () => {
|
|
238
|
+
try {
|
|
239
|
+
return await this.client.send(command);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
throw new DatastoreError(`Failed to ${this.mode} table "${this.tableName}"`, this.tableName, {
|
|
242
|
+
cause: error
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}, "doFetch");
|
|
246
|
+
if (!this.tracer) return doFetch();
|
|
247
|
+
return this.tracer.withSpan(`celerity.datastore.${this.mode}_page`, () => doFetch(), {
|
|
248
|
+
"datastore.table": this.tableName
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
buildQueryCommand(exclusiveStartKey) {
|
|
252
|
+
const opts = this.options;
|
|
253
|
+
const keyExpr = buildKeyConditionExpression(opts.key, opts.range);
|
|
254
|
+
const filterExpr = opts.filter ? buildFilterExpression(opts.filter) : void 0;
|
|
255
|
+
return new import_lib_dynamodb.QueryCommand({
|
|
256
|
+
TableName: this.tableName,
|
|
257
|
+
IndexName: opts.indexName,
|
|
258
|
+
KeyConditionExpression: keyExpr.expression,
|
|
259
|
+
FilterExpression: filterExpr?.expression,
|
|
260
|
+
ExpressionAttributeNames: {
|
|
261
|
+
...keyExpr.names,
|
|
262
|
+
...filterExpr?.names
|
|
263
|
+
},
|
|
264
|
+
ExpressionAttributeValues: {
|
|
265
|
+
...keyExpr.values,
|
|
266
|
+
...filterExpr?.values
|
|
267
|
+
},
|
|
268
|
+
ScanIndexForward: opts.sortAscending,
|
|
269
|
+
Limit: opts.maxResults,
|
|
270
|
+
ExclusiveStartKey: exclusiveStartKey,
|
|
271
|
+
ConsistentRead: opts.consistentRead
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
buildScanCommand(exclusiveStartKey) {
|
|
275
|
+
const opts = this.options;
|
|
276
|
+
const filterExpr = opts.filter ? buildFilterExpression(opts.filter) : void 0;
|
|
277
|
+
return new import_lib_dynamodb.ScanCommand({
|
|
278
|
+
TableName: this.tableName,
|
|
279
|
+
IndexName: opts.indexName,
|
|
280
|
+
FilterExpression: filterExpr?.expression,
|
|
281
|
+
ExpressionAttributeNames: filterExpr?.names && Object.keys(filterExpr.names).length > 0 ? filterExpr.names : void 0,
|
|
282
|
+
ExpressionAttributeValues: filterExpr?.values && Object.keys(filterExpr.values).length > 0 ? filterExpr.values : void 0,
|
|
283
|
+
Limit: opts.maxResults,
|
|
284
|
+
ExclusiveStartKey: exclusiveStartKey,
|
|
285
|
+
ConsistentRead: opts.consistentRead
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// src/providers/dynamodb/errors.ts
|
|
293
|
+
function isConditionalCheckFailedError(error) {
|
|
294
|
+
if (!(error instanceof Error)) return false;
|
|
295
|
+
return error.name === "ConditionalCheckFailedException";
|
|
296
|
+
}
|
|
297
|
+
var init_errors2 = __esm({
|
|
298
|
+
"src/providers/dynamodb/errors.ts"() {
|
|
299
|
+
"use strict";
|
|
300
|
+
__name(isConditionalCheckFailedError, "isConditionalCheckFailedError");
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// src/providers/dynamodb/dynamodb-datastore.ts
|
|
305
|
+
var import_debug2, import_lib_dynamodb2, debug2, DynamoDBDatastore;
|
|
306
|
+
var init_dynamodb_datastore = __esm({
|
|
307
|
+
"src/providers/dynamodb/dynamodb-datastore.ts"() {
|
|
308
|
+
"use strict";
|
|
309
|
+
import_debug2 = __toESM(require("debug"), 1);
|
|
310
|
+
import_lib_dynamodb2 = require("@aws-sdk/lib-dynamodb");
|
|
311
|
+
init_errors();
|
|
312
|
+
init_dynamodb_item_listing();
|
|
313
|
+
init_expressions();
|
|
314
|
+
init_errors2();
|
|
315
|
+
debug2 = (0, import_debug2.default)("celerity:datastore:dynamodb");
|
|
316
|
+
DynamoDBDatastore = class {
|
|
317
|
+
static {
|
|
318
|
+
__name(this, "DynamoDBDatastore");
|
|
319
|
+
}
|
|
320
|
+
tableName;
|
|
321
|
+
client;
|
|
322
|
+
tracer;
|
|
323
|
+
constructor(tableName, client, tracer) {
|
|
324
|
+
this.tableName = tableName;
|
|
325
|
+
this.client = client;
|
|
326
|
+
this.tracer = tracer;
|
|
327
|
+
}
|
|
328
|
+
async getItem(key, options) {
|
|
329
|
+
debug2("getItem %s %o", this.tableName, key);
|
|
330
|
+
return this.traced("celerity.datastore.get_item", {
|
|
331
|
+
"datastore.table": this.tableName
|
|
332
|
+
}, async () => {
|
|
333
|
+
try {
|
|
334
|
+
const response = await this.client.send(new import_lib_dynamodb2.GetCommand({
|
|
335
|
+
TableName: this.tableName,
|
|
336
|
+
Key: key,
|
|
337
|
+
ConsistentRead: options?.consistentRead
|
|
338
|
+
}));
|
|
339
|
+
return response.Item ?? null;
|
|
340
|
+
} catch (error) {
|
|
341
|
+
throw new DatastoreError(`Failed to get item from table "${this.tableName}"`, this.tableName, {
|
|
342
|
+
cause: error
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
async putItem(item, options) {
|
|
348
|
+
debug2("putItem %s", this.tableName);
|
|
349
|
+
return this.traced("celerity.datastore.put_item", {
|
|
350
|
+
"datastore.table": this.tableName
|
|
351
|
+
}, async () => {
|
|
352
|
+
try {
|
|
353
|
+
const conditionParams = options?.condition ? buildFilterExpression(options.condition) : void 0;
|
|
354
|
+
await this.client.send(new import_lib_dynamodb2.PutCommand({
|
|
355
|
+
TableName: this.tableName,
|
|
356
|
+
Item: item,
|
|
357
|
+
ConditionExpression: conditionParams?.expression,
|
|
358
|
+
ExpressionAttributeNames: conditionParams?.names,
|
|
359
|
+
ExpressionAttributeValues: conditionParams?.values
|
|
360
|
+
}));
|
|
361
|
+
} catch (error) {
|
|
362
|
+
if (isConditionalCheckFailedError(error)) {
|
|
363
|
+
throw new ConditionalCheckFailedError(this.tableName, {
|
|
364
|
+
cause: error
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
throw new DatastoreError(`Failed to put item in table "${this.tableName}"`, this.tableName, {
|
|
368
|
+
cause: error
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async deleteItem(key, options) {
|
|
374
|
+
debug2("deleteItem %s %o", this.tableName, key);
|
|
375
|
+
return this.traced("celerity.datastore.delete_item", {
|
|
376
|
+
"datastore.table": this.tableName
|
|
377
|
+
}, async () => {
|
|
378
|
+
try {
|
|
379
|
+
const conditionParams = options?.condition ? buildFilterExpression(options.condition) : void 0;
|
|
380
|
+
await this.client.send(new import_lib_dynamodb2.DeleteCommand({
|
|
381
|
+
TableName: this.tableName,
|
|
382
|
+
Key: key,
|
|
383
|
+
ConditionExpression: conditionParams?.expression,
|
|
384
|
+
ExpressionAttributeNames: conditionParams?.names,
|
|
385
|
+
ExpressionAttributeValues: conditionParams?.values
|
|
386
|
+
}));
|
|
387
|
+
} catch (error) {
|
|
388
|
+
if (isConditionalCheckFailedError(error)) {
|
|
389
|
+
throw new ConditionalCheckFailedError(this.tableName, {
|
|
390
|
+
cause: error
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
throw new DatastoreError(`Failed to delete item from table "${this.tableName}"`, this.tableName, {
|
|
394
|
+
cause: error
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
query(options) {
|
|
400
|
+
debug2("query %s pk=%s", this.tableName, options.key.name);
|
|
401
|
+
return new DynamoDBItemListing(this.client, this.tableName, "query", options, this.tracer);
|
|
402
|
+
}
|
|
403
|
+
scan(options) {
|
|
404
|
+
debug2("scan %s", this.tableName);
|
|
405
|
+
return new DynamoDBItemListing(this.client, this.tableName, "scan", options ?? {}, this.tracer);
|
|
406
|
+
}
|
|
407
|
+
async batchGetItems(keys, options) {
|
|
408
|
+
debug2("batchGetItems %s count=%d", this.tableName, keys.length);
|
|
409
|
+
return this.traced("celerity.datastore.batch_get_items", {
|
|
410
|
+
"datastore.table": this.tableName,
|
|
411
|
+
"datastore.batch_size": keys.length
|
|
412
|
+
}, async () => {
|
|
413
|
+
try {
|
|
414
|
+
const response = await this.client.send(new import_lib_dynamodb2.BatchGetCommand({
|
|
415
|
+
RequestItems: {
|
|
416
|
+
[this.tableName]: {
|
|
417
|
+
Keys: keys,
|
|
418
|
+
ConsistentRead: options?.consistentRead
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}));
|
|
422
|
+
const items = response.Responses?.[this.tableName] ?? [];
|
|
423
|
+
const unprocessedKeys = response.UnprocessedKeys?.[this.tableName]?.Keys ?? [];
|
|
424
|
+
return {
|
|
425
|
+
items,
|
|
426
|
+
unprocessedKeys
|
|
427
|
+
};
|
|
428
|
+
} catch (error) {
|
|
429
|
+
throw new DatastoreError(`Failed to batch get items from table "${this.tableName}"`, this.tableName, {
|
|
430
|
+
cause: error
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
async batchWriteItems(operations) {
|
|
436
|
+
debug2("batchWriteItems %s count=%d", this.tableName, operations.length);
|
|
437
|
+
return this.traced("celerity.datastore.batch_write_items", {
|
|
438
|
+
"datastore.table": this.tableName,
|
|
439
|
+
"datastore.batch_size": operations.length
|
|
440
|
+
}, async () => {
|
|
441
|
+
try {
|
|
442
|
+
const writeRequests = operations.map((op) => {
|
|
443
|
+
if (op.type === "put") {
|
|
444
|
+
return {
|
|
445
|
+
PutRequest: {
|
|
446
|
+
Item: op.item
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
return {
|
|
451
|
+
DeleteRequest: {
|
|
452
|
+
Key: op.key
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
const response = await this.client.send(new import_lib_dynamodb2.BatchWriteCommand({
|
|
457
|
+
RequestItems: {
|
|
458
|
+
[this.tableName]: writeRequests
|
|
459
|
+
}
|
|
460
|
+
}));
|
|
461
|
+
const unprocessedRequests = response.UnprocessedItems?.[this.tableName] ?? [];
|
|
462
|
+
const unprocessedOperations = unprocessedRequests.map((req) => {
|
|
463
|
+
if (req.PutRequest) {
|
|
464
|
+
return {
|
|
465
|
+
type: "put",
|
|
466
|
+
item: req.PutRequest.Item
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
type: "delete",
|
|
471
|
+
key: req.DeleteRequest.Key
|
|
472
|
+
};
|
|
473
|
+
});
|
|
474
|
+
return {
|
|
475
|
+
unprocessedOperations
|
|
476
|
+
};
|
|
477
|
+
} catch (error) {
|
|
478
|
+
throw new DatastoreError(`Failed to batch write items to table "${this.tableName}"`, this.tableName, {
|
|
479
|
+
cause: error
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
traced(name, attributes, fn) {
|
|
485
|
+
if (!this.tracer) return fn();
|
|
486
|
+
return this.tracer.withSpan(name, (span) => fn(span), attributes);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// src/providers/dynamodb/config.ts
|
|
493
|
+
var config_exports = {};
|
|
494
|
+
__export(config_exports, {
|
|
495
|
+
captureDynamoDBConfig: () => captureDynamoDBConfig
|
|
496
|
+
});
|
|
497
|
+
function captureDynamoDBConfig() {
|
|
498
|
+
return {
|
|
499
|
+
region: process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION,
|
|
500
|
+
endpoint: process.env.CELERITY_AWS_DYNAMODB_ENDPOINT ?? process.env.AWS_ENDPOINT_URL,
|
|
501
|
+
credentials: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY ? {
|
|
502
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
503
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
504
|
+
} : void 0
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
var init_config = __esm({
|
|
508
|
+
"src/providers/dynamodb/config.ts"() {
|
|
509
|
+
"use strict";
|
|
510
|
+
__name(captureDynamoDBConfig, "captureDynamoDBConfig");
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
// src/providers/dynamodb/dynamodb-datastore-client.ts
|
|
515
|
+
var dynamodb_datastore_client_exports = {};
|
|
516
|
+
__export(dynamodb_datastore_client_exports, {
|
|
517
|
+
DynamoDBDatastoreClient: () => DynamoDBDatastoreClient
|
|
518
|
+
});
|
|
519
|
+
var import_client_dynamodb, import_lib_dynamodb3, DynamoDBDatastoreClient;
|
|
520
|
+
var init_dynamodb_datastore_client = __esm({
|
|
521
|
+
"src/providers/dynamodb/dynamodb-datastore-client.ts"() {
|
|
522
|
+
"use strict";
|
|
523
|
+
import_client_dynamodb = require("@aws-sdk/client-dynamodb");
|
|
524
|
+
import_lib_dynamodb3 = require("@aws-sdk/lib-dynamodb");
|
|
525
|
+
init_dynamodb_datastore();
|
|
526
|
+
init_config();
|
|
527
|
+
DynamoDBDatastoreClient = class {
|
|
528
|
+
static {
|
|
529
|
+
__name(this, "DynamoDBDatastoreClient");
|
|
530
|
+
}
|
|
531
|
+
tracer;
|
|
532
|
+
client = null;
|
|
533
|
+
docClient = null;
|
|
534
|
+
config;
|
|
535
|
+
constructor(config, tracer) {
|
|
536
|
+
this.tracer = tracer;
|
|
537
|
+
this.config = config ?? captureDynamoDBConfig();
|
|
538
|
+
}
|
|
539
|
+
datastore(name) {
|
|
540
|
+
return new DynamoDBDatastore(name, this.getDocClient(), this.tracer);
|
|
541
|
+
}
|
|
542
|
+
close() {
|
|
543
|
+
this.docClient?.destroy();
|
|
544
|
+
this.client?.destroy();
|
|
545
|
+
this.docClient = null;
|
|
546
|
+
this.client = null;
|
|
547
|
+
}
|
|
548
|
+
getDocClient() {
|
|
549
|
+
if (!this.docClient) {
|
|
550
|
+
this.client = new import_client_dynamodb.DynamoDBClient({
|
|
551
|
+
region: this.config.region,
|
|
552
|
+
endpoint: this.config.endpoint,
|
|
553
|
+
credentials: this.config.credentials
|
|
554
|
+
});
|
|
555
|
+
this.docClient = import_lib_dynamodb3.DynamoDBDocumentClient.from(this.client, {
|
|
556
|
+
marshallOptions: {
|
|
557
|
+
removeUndefinedValues: true
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
return this.docClient;
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
|
|
16
567
|
// src/index.ts
|
|
17
568
|
var index_exports = {};
|
|
569
|
+
__export(index_exports, {
|
|
570
|
+
ConditionalCheckFailedError: () => ConditionalCheckFailedError,
|
|
571
|
+
DEFAULT_DATASTORE_TOKEN: () => DEFAULT_DATASTORE_TOKEN,
|
|
572
|
+
Datastore: () => Datastore,
|
|
573
|
+
DatastoreClient: () => DatastoreClient,
|
|
574
|
+
DatastoreError: () => DatastoreError,
|
|
575
|
+
DatastoreLayer: () => DatastoreLayer,
|
|
576
|
+
createDatastoreClient: () => createDatastoreClient,
|
|
577
|
+
datastoreToken: () => datastoreToken,
|
|
578
|
+
getDatastore: () => getDatastore
|
|
579
|
+
});
|
|
18
580
|
module.exports = __toCommonJS(index_exports);
|
|
581
|
+
|
|
582
|
+
// src/types.ts
|
|
583
|
+
var DatastoreClient = /* @__PURE__ */ Symbol.for("DatastoreClient");
|
|
584
|
+
|
|
585
|
+
// src/factory.ts
|
|
586
|
+
var import_config2 = require("@celerity-sdk/config");
|
|
587
|
+
async function createDatastoreClient(options) {
|
|
588
|
+
const resolved = (0, import_config2.resolveConfig)("datastore");
|
|
589
|
+
const provider = options?.provider ?? resolved.provider;
|
|
590
|
+
switch (provider) {
|
|
591
|
+
case "aws": {
|
|
592
|
+
const { DynamoDBDatastoreClient: DynamoDBDatastoreClient2 } = await Promise.resolve().then(() => (init_dynamodb_datastore_client(), dynamodb_datastore_client_exports));
|
|
593
|
+
return new DynamoDBDatastoreClient2(options?.aws, options?.tracer);
|
|
594
|
+
}
|
|
595
|
+
case "local":
|
|
596
|
+
return createLocalClient(options);
|
|
597
|
+
default:
|
|
598
|
+
throw new Error(`Unsupported datastore provider: "${provider}"`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
__name(createDatastoreClient, "createDatastoreClient");
|
|
602
|
+
async function createLocalClient(options) {
|
|
603
|
+
const deployTarget = options?.deployTarget?.toLowerCase();
|
|
604
|
+
switch (deployTarget) {
|
|
605
|
+
case "aws":
|
|
606
|
+
case "aws-serverless":
|
|
607
|
+
case void 0: {
|
|
608
|
+
const { captureDynamoDBConfig: captureDynamoDBConfig2 } = await Promise.resolve().then(() => (init_config(), config_exports));
|
|
609
|
+
const { DynamoDBDatastoreClient: DynamoDBDatastoreClient2 } = await Promise.resolve().then(() => (init_dynamodb_datastore_client(), dynamodb_datastore_client_exports));
|
|
610
|
+
const localConfig = {
|
|
611
|
+
...captureDynamoDBConfig2(),
|
|
612
|
+
...options?.aws
|
|
613
|
+
};
|
|
614
|
+
return new DynamoDBDatastoreClient2(localConfig, options?.tracer);
|
|
615
|
+
}
|
|
616
|
+
// case "gcloud":
|
|
617
|
+
// case "gcloud-serverless":
|
|
618
|
+
// v1: Firestore emulator
|
|
619
|
+
// case "azure":
|
|
620
|
+
// case "azure-serverless":
|
|
621
|
+
// v1: Cosmos DB emulator
|
|
622
|
+
default:
|
|
623
|
+
throw new Error(`Unsupported local datastore deploy target: "${deployTarget}". Only AWS is supported in v0.`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
__name(createLocalClient, "createLocalClient");
|
|
627
|
+
|
|
628
|
+
// src/decorators.ts
|
|
629
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
630
|
+
var import_common = require("@celerity-sdk/common");
|
|
631
|
+
function datastoreToken(resourceName) {
|
|
632
|
+
return /* @__PURE__ */ Symbol.for(`celerity:datastore:${resourceName}`);
|
|
633
|
+
}
|
|
634
|
+
__name(datastoreToken, "datastoreToken");
|
|
635
|
+
var DEFAULT_DATASTORE_TOKEN = /* @__PURE__ */ Symbol.for("celerity:datastore:default");
|
|
636
|
+
function Datastore(resourceName) {
|
|
637
|
+
return (target, _propertyKey, parameterIndex) => {
|
|
638
|
+
const token = resourceName ? datastoreToken(resourceName) : DEFAULT_DATASTORE_TOKEN;
|
|
639
|
+
const existing = Reflect.getOwnMetadata(import_common.INJECT_METADATA, target) ?? /* @__PURE__ */ new Map();
|
|
640
|
+
existing.set(parameterIndex, token);
|
|
641
|
+
Reflect.defineMetadata(import_common.INJECT_METADATA, existing, target);
|
|
642
|
+
if (resourceName) {
|
|
643
|
+
const resources = Reflect.getOwnMetadata(import_common.USE_RESOURCE_METADATA, target) ?? [];
|
|
644
|
+
if (!resources.includes(resourceName)) {
|
|
645
|
+
Reflect.defineMetadata(import_common.USE_RESOURCE_METADATA, [
|
|
646
|
+
...resources,
|
|
647
|
+
resourceName
|
|
648
|
+
], target);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
__name(Datastore, "Datastore");
|
|
654
|
+
|
|
655
|
+
// src/helpers.ts
|
|
656
|
+
function getDatastore(container, resourceName) {
|
|
657
|
+
const token = resourceName ? datastoreToken(resourceName) : DEFAULT_DATASTORE_TOKEN;
|
|
658
|
+
return container.resolve(token);
|
|
659
|
+
}
|
|
660
|
+
__name(getDatastore, "getDatastore");
|
|
661
|
+
|
|
662
|
+
// src/layer.ts
|
|
663
|
+
var import_debug3 = __toESM(require("debug"), 1);
|
|
664
|
+
var import_common2 = require("@celerity-sdk/common");
|
|
665
|
+
var import_config3 = require("@celerity-sdk/config");
|
|
666
|
+
var debug3 = (0, import_debug3.default)("celerity:datastore");
|
|
667
|
+
function captureDatastoreLayerConfig() {
|
|
668
|
+
return {
|
|
669
|
+
deployTarget: process.env.CELERITY_DEPLOY_TARGET
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
__name(captureDatastoreLayerConfig, "captureDatastoreLayerConfig");
|
|
673
|
+
var DatastoreLayer = class {
|
|
674
|
+
static {
|
|
675
|
+
__name(this, "DatastoreLayer");
|
|
676
|
+
}
|
|
677
|
+
initialized = false;
|
|
678
|
+
config = null;
|
|
679
|
+
async handle(context, next) {
|
|
680
|
+
if (!this.initialized) {
|
|
681
|
+
this.config = captureDatastoreLayerConfig();
|
|
682
|
+
const tracer = context.container.has(import_common2.TRACER_TOKEN) ? await context.container.resolve(import_common2.TRACER_TOKEN) : void 0;
|
|
683
|
+
const client = await createDatastoreClient({
|
|
684
|
+
tracer,
|
|
685
|
+
deployTarget: this.config.deployTarget
|
|
686
|
+
});
|
|
687
|
+
debug3("registering DatastoreClient");
|
|
688
|
+
context.container.register("DatastoreClient", {
|
|
689
|
+
useValue: client
|
|
690
|
+
});
|
|
691
|
+
const links = (0, import_config3.captureResourceLinks)();
|
|
692
|
+
const datastoreLinks = (0, import_config3.getLinksOfType)(links, "datastore");
|
|
693
|
+
if (datastoreLinks.size > 0) {
|
|
694
|
+
const configService = await context.container.resolve(import_common2.CONFIG_SERVICE_TOKEN);
|
|
695
|
+
const resourceConfig = configService.namespace(import_config3.RESOURCE_CONFIG_NAMESPACE);
|
|
696
|
+
for (const [resourceName, configKey] of datastoreLinks) {
|
|
697
|
+
const actualName = await resourceConfig.getOrThrow(configKey);
|
|
698
|
+
debug3("registered datastore resource %s \u2192 %s", resourceName, actualName);
|
|
699
|
+
context.container.register(datastoreToken(resourceName), {
|
|
700
|
+
useValue: client.datastore(actualName)
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
if (datastoreLinks.size === 1) {
|
|
704
|
+
const [, configKey] = [
|
|
705
|
+
...datastoreLinks.entries()
|
|
706
|
+
][0];
|
|
707
|
+
const actualName = await resourceConfig.getOrThrow(configKey);
|
|
708
|
+
debug3("registered default datastore \u2192 %s", actualName);
|
|
709
|
+
context.container.register(DEFAULT_DATASTORE_TOKEN, {
|
|
710
|
+
useValue: client.datastore(actualName)
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
this.initialized = true;
|
|
715
|
+
}
|
|
716
|
+
return next();
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
// src/index.ts
|
|
721
|
+
init_errors();
|
|
722
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
723
|
+
0 && (module.exports = {
|
|
724
|
+
ConditionalCheckFailedError,
|
|
725
|
+
DEFAULT_DATASTORE_TOKEN,
|
|
726
|
+
Datastore,
|
|
727
|
+
DatastoreClient,
|
|
728
|
+
DatastoreError,
|
|
729
|
+
DatastoreLayer,
|
|
730
|
+
createDatastoreClient,
|
|
731
|
+
datastoreToken,
|
|
732
|
+
getDatastore
|
|
733
|
+
});
|
|
19
734
|
//# sourceMappingURL=index.cjs.map
|