@celerity-sdk/datastore 0.4.0 → 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/dist/index.cjs CHANGED
@@ -6,6 +6,9 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
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
+ };
9
12
  var __export = (target, all) => {
10
13
  for (var name in all)
11
14
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -28,67 +31,34 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
31
  ));
29
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
33
 
31
- // src/index.ts
32
- var index_exports = {};
33
- __export(index_exports, {
34
- ConditionalCheckFailedError: () => ConditionalCheckFailedError,
35
- DEFAULT_DATASTORE_TOKEN: () => DEFAULT_DATASTORE_TOKEN,
36
- Datastore: () => Datastore,
37
- DatastoreClient: () => DatastoreClient,
38
- DatastoreError: () => DatastoreError,
39
- DatastoreLayer: () => DatastoreLayer,
40
- DynamoDBDatastoreClient: () => DynamoDBDatastoreClient,
41
- createDatastoreClient: () => createDatastoreClient,
42
- datastoreToken: () => datastoreToken,
43
- getDatastore: () => getDatastore
44
- });
45
- module.exports = __toCommonJS(index_exports);
46
-
47
- // src/types.ts
48
- var DatastoreClient = /* @__PURE__ */ Symbol.for("DatastoreClient");
49
-
50
- // src/providers/dynamodb/dynamodb-datastore-client.ts
51
- var import_client_dynamodb = require("@aws-sdk/client-dynamodb");
52
- var import_lib_dynamodb3 = require("@aws-sdk/lib-dynamodb");
53
-
54
- // src/providers/dynamodb/dynamodb-datastore.ts
55
- var import_debug2 = __toESM(require("debug"), 1);
56
- var import_lib_dynamodb2 = require("@aws-sdk/lib-dynamodb");
57
-
58
34
  // src/errors.ts
59
- var DatastoreError = class extends Error {
60
- static {
61
- __name(this, "DatastoreError");
62
- }
63
- table;
64
- constructor(message, table, options) {
65
- super(message, options), this.table = table;
66
- this.name = "DatastoreError";
67
- }
68
- };
69
- var ConditionalCheckFailedError = class extends DatastoreError {
70
- static {
71
- __name(this, "ConditionalCheckFailedError");
72
- }
73
- constructor(table, options) {
74
- super(`Conditional check failed on table "${table}"`, table, options);
75
- this.name = "ConditionalCheckFailedError";
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
+ };
76
58
  }
77
- };
78
-
79
- // src/providers/dynamodb/dynamodb-item-listing.ts
80
- var import_debug = __toESM(require("debug"), 1);
81
- var import_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
59
+ });
82
60
 
83
61
  // src/providers/dynamodb/expressions.ts
84
- var COMPARISON_OPERATORS = {
85
- eq: "=",
86
- ne: "<>",
87
- lt: "<",
88
- le: "<=",
89
- gt: ">",
90
- ge: ">="
91
- };
92
62
  function buildKeyConditionExpression(key, range) {
93
63
  const names = {};
94
64
  const values = {};
@@ -135,7 +105,6 @@ function buildKeyConditionExpression(key, range) {
135
105
  values
136
106
  };
137
107
  }
138
- __name(buildKeyConditionExpression, "buildKeyConditionExpression");
139
108
  function buildFilterExpression(conditions) {
140
109
  const condArray = Array.isArray(conditions) ? conditions : [
141
110
  conditions
@@ -187,357 +156,442 @@ function buildFilterExpression(conditions) {
187
156
  values
188
157
  };
189
158
  }
190
- __name(buildFilterExpression, "buildFilterExpression");
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
+ });
191
175
 
192
176
  // src/providers/dynamodb/dynamodb-item-listing.ts
193
- var debug = (0, import_debug.default)("celerity:datastore:dynamodb");
194
177
  function encodeCursor(lastEvaluatedKey) {
195
178
  const state = {
196
179
  lastEvaluatedKey
197
180
  };
198
181
  return Buffer.from(JSON.stringify(state)).toString("base64url");
199
182
  }
200
- __name(encodeCursor, "encodeCursor");
201
183
  function decodeCursor(cursor) {
202
184
  return JSON.parse(Buffer.from(cursor, "base64url").toString("utf-8"));
203
185
  }
204
- __name(decodeCursor, "decodeCursor");
205
- var DynamoDBItemListing = class {
206
- static {
207
- __name(this, "DynamoDBItemListing");
208
- }
209
- client;
210
- tableName;
211
- mode;
212
- options;
213
- tracer;
214
- _cursor;
215
- constructor(client, tableName, mode, options, tracer) {
216
- this.client = client;
217
- this.tableName = tableName;
218
- this.mode = mode;
219
- this.options = options;
220
- this.tracer = tracer;
221
- this._cursor = options.cursor;
222
- }
223
- get cursor() {
224
- return this._cursor;
225
- }
226
- async *[Symbol.asyncIterator]() {
227
- const cursorState = this._cursor ? decodeCursor(this._cursor) : void 0;
228
- let exclusiveStartKey = cursorState?.lastEvaluatedKey;
229
- do {
230
- debug("%s page %s key=%o", this.mode, this.tableName, exclusiveStartKey ?? "(start)");
231
- const response = await this.fetchPage(exclusiveStartKey);
232
- for (const item of response.Items ?? []) {
233
- yield item;
234
- }
235
- exclusiveStartKey = response.LastEvaluatedKey;
236
- if (exclusiveStartKey) {
237
- this._cursor = encodeCursor(exclusiveStartKey);
238
- } else {
239
- this._cursor = void 0;
240
- }
241
- } while (exclusiveStartKey);
242
- }
243
- async fetchPage(exclusiveStartKey) {
244
- const command = this.mode === "query" ? this.buildQueryCommand(exclusiveStartKey) : this.buildScanCommand(exclusiveStartKey);
245
- const doFetch = /* @__PURE__ */ __name(async () => {
246
- try {
247
- return await this.client.send(command);
248
- } catch (error) {
249
- throw new DatastoreError(`Failed to ${this.mode} table "${this.tableName}"`, this.tableName, {
250
- cause: error
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
251
249
  });
252
250
  }
253
- }, "doFetch");
254
- if (!this.tracer) return doFetch();
255
- return this.tracer.withSpan(`celerity.datastore.${this.mode}_page`, () => doFetch(), {
256
- "datastore.table": this.tableName
257
- });
258
- }
259
- buildQueryCommand(exclusiveStartKey) {
260
- const opts = this.options;
261
- const keyExpr = buildKeyConditionExpression(opts.key, opts.range);
262
- const filterExpr = opts.filter ? buildFilterExpression(opts.filter) : void 0;
263
- return new import_lib_dynamodb.QueryCommand({
264
- TableName: this.tableName,
265
- IndexName: opts.indexName,
266
- KeyConditionExpression: keyExpr.expression,
267
- FilterExpression: filterExpr?.expression,
268
- ExpressionAttributeNames: {
269
- ...keyExpr.names,
270
- ...filterExpr?.names
271
- },
272
- ExpressionAttributeValues: {
273
- ...keyExpr.values,
274
- ...filterExpr?.values
275
- },
276
- ScanIndexForward: opts.sortAscending,
277
- Limit: opts.maxResults,
278
- ExclusiveStartKey: exclusiveStartKey,
279
- ConsistentRead: opts.consistentRead
280
- });
281
- }
282
- buildScanCommand(exclusiveStartKey) {
283
- const opts = this.options;
284
- const filterExpr = opts.filter ? buildFilterExpression(opts.filter) : void 0;
285
- return new import_lib_dynamodb.ScanCommand({
286
- TableName: this.tableName,
287
- IndexName: opts.indexName,
288
- FilterExpression: filterExpr?.expression,
289
- ExpressionAttributeNames: filterExpr?.names && Object.keys(filterExpr.names).length > 0 ? filterExpr.names : void 0,
290
- ExpressionAttributeValues: filterExpr?.values && Object.keys(filterExpr.values).length > 0 ? filterExpr.values : void 0,
291
- Limit: opts.maxResults,
292
- ExclusiveStartKey: exclusiveStartKey,
293
- ConsistentRead: opts.consistentRead
294
- });
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
+ };
295
289
  }
296
- };
290
+ });
297
291
 
298
292
  // src/providers/dynamodb/errors.ts
299
293
  function isConditionalCheckFailedError(error) {
300
294
  if (!(error instanceof Error)) return false;
301
295
  return error.name === "ConditionalCheckFailedException";
302
296
  }
303
- __name(isConditionalCheckFailedError, "isConditionalCheckFailedError");
297
+ var init_errors2 = __esm({
298
+ "src/providers/dynamodb/errors.ts"() {
299
+ "use strict";
300
+ __name(isConditionalCheckFailedError, "isConditionalCheckFailedError");
301
+ }
302
+ });
304
303
 
305
304
  // src/providers/dynamodb/dynamodb-datastore.ts
306
- var debug2 = (0, import_debug2.default)("celerity:datastore:dynamodb");
307
- var DynamoDBDatastore = class {
308
- static {
309
- __name(this, "DynamoDBDatastore");
310
- }
311
- tableName;
312
- client;
313
- tracer;
314
- constructor(tableName, client, tracer) {
315
- this.tableName = tableName;
316
- this.client = client;
317
- this.tracer = tracer;
318
- }
319
- async getItem(key, options) {
320
- debug2("getItem %s %o", this.tableName, key);
321
- return this.traced("celerity.datastore.get_item", {
322
- "datastore.table": this.tableName
323
- }, async () => {
324
- try {
325
- const response = await this.client.send(new import_lib_dynamodb2.GetCommand({
326
- TableName: this.tableName,
327
- Key: key,
328
- ConsistentRead: options?.consistentRead
329
- }));
330
- return response.Item ?? null;
331
- } catch (error) {
332
- throw new DatastoreError(`Failed to get item from table "${this.tableName}"`, this.tableName, {
333
- cause: error
334
- });
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");
335
319
  }
336
- });
337
- }
338
- async putItem(item, options) {
339
- debug2("putItem %s", this.tableName);
340
- return this.traced("celerity.datastore.put_item", {
341
- "datastore.table": this.tableName
342
- }, async () => {
343
- try {
344
- const conditionParams = options?.condition ? buildFilterExpression(options.condition) : void 0;
345
- await this.client.send(new import_lib_dynamodb2.PutCommand({
346
- TableName: this.tableName,
347
- Item: item,
348
- ConditionExpression: conditionParams?.expression,
349
- ExpressionAttributeNames: conditionParams?.names,
350
- ExpressionAttributeValues: conditionParams?.values
351
- }));
352
- } catch (error) {
353
- if (isConditionalCheckFailedError(error)) {
354
- throw new ConditionalCheckFailedError(this.tableName, {
355
- cause: error
356
- });
357
- }
358
- throw new DatastoreError(`Failed to put item in table "${this.tableName}"`, this.tableName, {
359
- cause: error
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
+ }
360
345
  });
361
346
  }
362
- });
363
- }
364
- async deleteItem(key, options) {
365
- debug2("deleteItem %s %o", this.tableName, key);
366
- return this.traced("celerity.datastore.delete_item", {
367
- "datastore.table": this.tableName
368
- }, async () => {
369
- try {
370
- const conditionParams = options?.condition ? buildFilterExpression(options.condition) : void 0;
371
- await this.client.send(new import_lib_dynamodb2.DeleteCommand({
372
- TableName: this.tableName,
373
- Key: key,
374
- ConditionExpression: conditionParams?.expression,
375
- ExpressionAttributeNames: conditionParams?.names,
376
- ExpressionAttributeValues: conditionParams?.values
377
- }));
378
- } catch (error) {
379
- if (isConditionalCheckFailedError(error)) {
380
- throw new ConditionalCheckFailedError(this.tableName, {
381
- cause: error
382
- });
383
- }
384
- throw new DatastoreError(`Failed to delete item from table "${this.tableName}"`, this.tableName, {
385
- cause: error
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
+ }
386
371
  });
387
372
  }
388
- });
389
- }
390
- query(options) {
391
- debug2("query %s pk=%s", this.tableName, options.key.name);
392
- return new DynamoDBItemListing(this.client, this.tableName, "query", options, this.tracer);
393
- }
394
- scan(options) {
395
- debug2("scan %s", this.tableName);
396
- return new DynamoDBItemListing(this.client, this.tableName, "scan", options ?? {}, this.tracer);
397
- }
398
- async batchGetItems(keys, options) {
399
- debug2("batchGetItems %s count=%d", this.tableName, keys.length);
400
- return this.traced("celerity.datastore.batch_get_items", {
401
- "datastore.table": this.tableName,
402
- "datastore.batch_size": keys.length
403
- }, async () => {
404
- try {
405
- const response = await this.client.send(new import_lib_dynamodb2.BatchGetCommand({
406
- RequestItems: {
407
- [this.tableName]: {
408
- Keys: keys,
409
- ConsistentRead: options?.consistentRead
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
+ });
410
392
  }
393
+ throw new DatastoreError(`Failed to delete item from table "${this.tableName}"`, this.tableName, {
394
+ cause: error
395
+ });
411
396
  }
412
- }));
413
- const items = response.Responses?.[this.tableName] ?? [];
414
- const unprocessedKeys = response.UnprocessedKeys?.[this.tableName]?.Keys ?? [];
415
- return {
416
- items,
417
- unprocessedKeys
418
- };
419
- } catch (error) {
420
- throw new DatastoreError(`Failed to batch get items from table "${this.tableName}"`, this.tableName, {
421
- cause: error
422
397
  });
423
398
  }
424
- });
425
- }
426
- async batchWriteItems(operations) {
427
- debug2("batchWriteItems %s count=%d", this.tableName, operations.length);
428
- return this.traced("celerity.datastore.batch_write_items", {
429
- "datastore.table": this.tableName,
430
- "datastore.batch_size": operations.length
431
- }, async () => {
432
- try {
433
- const writeRequests = operations.map((op) => {
434
- if (op.type === "put") {
435
- return {
436
- PutRequest: {
437
- Item: op.item
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
+ }
438
420
  }
421
+ }));
422
+ const items = response.Responses?.[this.tableName] ?? [];
423
+ const unprocessedKeys = response.UnprocessedKeys?.[this.tableName]?.Keys ?? [];
424
+ return {
425
+ items,
426
+ unprocessedKeys
439
427
  };
428
+ } catch (error) {
429
+ throw new DatastoreError(`Failed to batch get items from table "${this.tableName}"`, this.tableName, {
430
+ cause: error
431
+ });
440
432
  }
441
- return {
442
- DeleteRequest: {
443
- Key: op.key
444
- }
445
- };
446
433
  });
447
- const response = await this.client.send(new import_lib_dynamodb2.BatchWriteCommand({
448
- RequestItems: {
449
- [this.tableName]: writeRequests
450
- }
451
- }));
452
- const unprocessedRequests = response.UnprocessedItems?.[this.tableName] ?? [];
453
- const unprocessedOperations = unprocessedRequests.map((req) => {
454
- if (req.PutRequest) {
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
+ });
455
474
  return {
456
- type: "put",
457
- item: req.PutRequest.Item
475
+ unprocessedOperations
458
476
  };
477
+ } catch (error) {
478
+ throw new DatastoreError(`Failed to batch write items to table "${this.tableName}"`, this.tableName, {
479
+ cause: error
480
+ });
459
481
  }
460
- return {
461
- type: "delete",
462
- key: req.DeleteRequest.Key
463
- };
464
- });
465
- return {
466
- unprocessedOperations
467
- };
468
- } catch (error) {
469
- throw new DatastoreError(`Failed to batch write items to table "${this.tableName}"`, this.tableName, {
470
- cause: error
471
482
  });
472
483
  }
473
- });
474
- }
475
- traced(name, attributes, fn) {
476
- if (!this.tracer) return fn();
477
- return this.tracer.withSpan(name, (span) => fn(span), attributes);
484
+ traced(name, attributes, fn) {
485
+ if (!this.tracer) return fn();
486
+ return this.tracer.withSpan(name, (span) => fn(span), attributes);
487
+ }
488
+ };
478
489
  }
479
- };
490
+ });
480
491
 
481
492
  // src/providers/dynamodb/config.ts
493
+ var config_exports = {};
494
+ __export(config_exports, {
495
+ captureDynamoDBConfig: () => captureDynamoDBConfig
496
+ });
482
497
  function captureDynamoDBConfig() {
483
498
  return {
484
499
  region: process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION,
485
- endpoint: process.env.AWS_ENDPOINT_URL,
500
+ endpoint: process.env.CELERITY_AWS_DYNAMODB_ENDPOINT ?? process.env.AWS_ENDPOINT_URL,
486
501
  credentials: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY ? {
487
502
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
488
503
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
489
504
  } : void 0
490
505
  };
491
506
  }
492
- __name(captureDynamoDBConfig, "captureDynamoDBConfig");
507
+ var init_config = __esm({
508
+ "src/providers/dynamodb/config.ts"() {
509
+ "use strict";
510
+ __name(captureDynamoDBConfig, "captureDynamoDBConfig");
511
+ }
512
+ });
493
513
 
494
514
  // src/providers/dynamodb/dynamodb-datastore-client.ts
495
- var DynamoDBDatastoreClient = class {
496
- static {
497
- __name(this, "DynamoDBDatastoreClient");
498
- }
499
- tracer;
500
- client = null;
501
- docClient = null;
502
- config;
503
- constructor(config, tracer) {
504
- this.tracer = tracer;
505
- this.config = config ?? captureDynamoDBConfig();
506
- }
507
- datastore(name) {
508
- return new DynamoDBDatastore(name, this.getDocClient(), this.tracer);
509
- }
510
- close() {
511
- this.docClient?.destroy();
512
- this.client?.destroy();
513
- this.docClient = null;
514
- this.client = null;
515
- }
516
- getDocClient() {
517
- if (!this.docClient) {
518
- this.client = new import_client_dynamodb.DynamoDBClient({
519
- region: this.config.region,
520
- endpoint: this.config.endpoint,
521
- credentials: this.config.credentials
522
- });
523
- this.docClient = import_lib_dynamodb3.DynamoDBDocumentClient.from(this.client, {
524
- marshallOptions: {
525
- removeUndefinedValues: true
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
+ });
526
560
  }
527
- });
528
- }
529
- return this.docClient;
561
+ return this.docClient;
562
+ }
563
+ };
530
564
  }
531
- };
565
+ });
566
+
567
+ // src/index.ts
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
+ });
580
+ module.exports = __toCommonJS(index_exports);
581
+
582
+ // src/types.ts
583
+ var DatastoreClient = /* @__PURE__ */ Symbol.for("DatastoreClient");
532
584
 
533
585
  // src/factory.ts
534
586
  var import_config2 = require("@celerity-sdk/config");
535
- function createDatastoreClient(options) {
587
+ async function createDatastoreClient(options) {
536
588
  const resolved = (0, import_config2.resolveConfig)("datastore");
537
589
  const provider = options?.provider ?? resolved.provider;
538
590
  switch (provider) {
539
- case "aws":
540
- return new DynamoDBDatastoreClient(options?.aws, options?.tracer);
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
+ }
541
595
  case "local":
542
596
  return createLocalClient(options);
543
597
  default:
@@ -545,17 +599,19 @@ function createDatastoreClient(options) {
545
599
  }
546
600
  }
547
601
  __name(createDatastoreClient, "createDatastoreClient");
548
- function createLocalClient(options) {
602
+ async function createLocalClient(options) {
549
603
  const deployTarget = options?.deployTarget?.toLowerCase();
550
604
  switch (deployTarget) {
551
605
  case "aws":
552
606
  case "aws-serverless":
553
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));
554
610
  const localConfig = {
555
- ...captureDynamoDBConfig(),
611
+ ...captureDynamoDBConfig2(),
556
612
  ...options?.aws
557
613
  };
558
- return new DynamoDBDatastoreClient(localConfig, options?.tracer);
614
+ return new DynamoDBDatastoreClient2(localConfig, options?.tracer);
559
615
  }
560
616
  // case "gcloud":
561
617
  // case "gcloud-serverless":
@@ -606,7 +662,7 @@ __name(getDatastore, "getDatastore");
606
662
  // src/layer.ts
607
663
  var import_debug3 = __toESM(require("debug"), 1);
608
664
  var import_common2 = require("@celerity-sdk/common");
609
- var import_config4 = require("@celerity-sdk/config");
665
+ var import_config3 = require("@celerity-sdk/config");
610
666
  var debug3 = (0, import_debug3.default)("celerity:datastore");
611
667
  function captureDatastoreLayerConfig() {
612
668
  return {
@@ -624,7 +680,7 @@ var DatastoreLayer = class {
624
680
  if (!this.initialized) {
625
681
  this.config = captureDatastoreLayerConfig();
626
682
  const tracer = context.container.has(import_common2.TRACER_TOKEN) ? await context.container.resolve(import_common2.TRACER_TOKEN) : void 0;
627
- const client = createDatastoreClient({
683
+ const client = await createDatastoreClient({
628
684
  tracer,
629
685
  deployTarget: this.config.deployTarget
630
686
  });
@@ -632,11 +688,11 @@ var DatastoreLayer = class {
632
688
  context.container.register("DatastoreClient", {
633
689
  useValue: client
634
690
  });
635
- const links = (0, import_config4.captureResourceLinks)();
636
- const datastoreLinks = (0, import_config4.getLinksOfType)(links, "datastore");
691
+ const links = (0, import_config3.captureResourceLinks)();
692
+ const datastoreLinks = (0, import_config3.getLinksOfType)(links, "datastore");
637
693
  if (datastoreLinks.size > 0) {
638
694
  const configService = await context.container.resolve(import_common2.CONFIG_SERVICE_TOKEN);
639
- const resourceConfig = configService.namespace(import_config4.RESOURCE_CONFIG_NAMESPACE);
695
+ const resourceConfig = configService.namespace(import_config3.RESOURCE_CONFIG_NAMESPACE);
640
696
  for (const [resourceName, configKey] of datastoreLinks) {
641
697
  const actualName = await resourceConfig.getOrThrow(configKey);
642
698
  debug3("registered datastore resource %s \u2192 %s", resourceName, actualName);
@@ -660,6 +716,9 @@ var DatastoreLayer = class {
660
716
  return next();
661
717
  }
662
718
  };
719
+
720
+ // src/index.ts
721
+ init_errors();
663
722
  // Annotate the CommonJS export names for ESM import in node:
664
723
  0 && (module.exports = {
665
724
  ConditionalCheckFailedError,
@@ -668,7 +727,6 @@ var DatastoreLayer = class {
668
727
  DatastoreClient,
669
728
  DatastoreError,
670
729
  DatastoreLayer,
671
- DynamoDBDatastoreClient,
672
730
  createDatastoreClient,
673
731
  datastoreToken,
674
732
  getDatastore