@celerity-sdk/datastore 0.3.1 → 0.4.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 CHANGED
@@ -4,9 +4,9 @@ NoSQL data store abstraction for the Celerity Node SDK.
4
4
 
5
5
  Provides a unified `DatastoreClient` interface for working with NoSQL databases across cloud providers:
6
6
 
7
- - **AWS**:Amazon DynamoDB
8
- - **GCP**:Google Cloud Datastore
9
- - **Azure**:Azure Cosmos DB
7
+ - **AWS**: Amazon DynamoDB
8
+ - **Google Cloud**: Google Cloud Firestore
9
+ - **Azure**: Azure Cosmos DB
10
10
 
11
11
  ## Installation
12
12
 
@@ -21,7 +21,7 @@ Install the cloud SDK for your target platform as a peer dependency:
21
21
  pnpm add @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
22
22
 
23
23
  # GCP
24
- pnpm add @google-cloud/datastore
24
+ pnpm add @google-cloud/firestore
25
25
 
26
26
  # Azure
27
27
  pnpm add @azure/cosmos
@@ -29,7 +29,7 @@ pnpm add @azure/cosmos
29
29
 
30
30
  ## Status
31
31
 
32
- This package is a stub:the interface and provider implementations are planned but not yet implemented.
32
+ This package implements the `DatastoreClient` interface and provides a `DynamoDBProvider` for AWS DynamoDB. Support for Google Cloud Firestore and Azure Cosmos DB will be added in future releases.
33
33
 
34
34
  ## Part of the Celerity Framework
35
35
 
package/dist/index.cjs CHANGED
@@ -1,8 +1,15 @@
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 __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
6
13
  var __copyProps = (to, from, except, desc) => {
7
14
  if (from && typeof from === "object" || typeof from === "function") {
8
15
  for (let key of __getOwnPropNames(from))
@@ -11,9 +18,659 @@ var __copyProps = (to, from, except, desc) => {
11
18
  }
12
19
  return to;
13
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
14
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
30
 
16
31
  // src/index.ts
17
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
+ });
18
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
+ // 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";
76
+ }
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");
82
+
83
+ // src/providers/dynamodb/expressions.ts
84
+ var COMPARISON_OPERATORS = {
85
+ eq: "=",
86
+ ne: "<>",
87
+ lt: "<",
88
+ le: "<=",
89
+ gt: ">",
90
+ ge: ">="
91
+ };
92
+ function buildKeyConditionExpression(key, range) {
93
+ const names = {};
94
+ const values = {};
95
+ let counter = 0;
96
+ const pkName = `#k${counter}`;
97
+ const pkValue = `:k${counter}`;
98
+ names[pkName] = key.name;
99
+ values[pkValue] = key.value;
100
+ counter++;
101
+ let expression = `${pkName} = ${pkValue}`;
102
+ if (range) {
103
+ const skName = `#k${counter}`;
104
+ names[skName] = range.name;
105
+ switch (range.operator) {
106
+ case "eq":
107
+ case "lt":
108
+ case "le":
109
+ case "gt":
110
+ case "ge": {
111
+ const skValue = `:k${counter}`;
112
+ values[skValue] = range.value;
113
+ expression += ` AND ${skName} ${COMPARISON_OPERATORS[range.operator]} ${skValue}`;
114
+ break;
115
+ }
116
+ case "between": {
117
+ const lowVal = `:k${counter}a`;
118
+ const highVal = `:k${counter}b`;
119
+ values[lowVal] = range.low;
120
+ values[highVal] = range.high;
121
+ expression += ` AND ${skName} BETWEEN ${lowVal} AND ${highVal}`;
122
+ break;
123
+ }
124
+ case "startsWith": {
125
+ const skValue = `:k${counter}`;
126
+ values[skValue] = range.value;
127
+ expression += ` AND begins_with(${skName}, ${skValue})`;
128
+ break;
129
+ }
130
+ }
131
+ }
132
+ return {
133
+ expression,
134
+ names,
135
+ values
136
+ };
137
+ }
138
+ __name(buildKeyConditionExpression, "buildKeyConditionExpression");
139
+ function buildFilterExpression(conditions) {
140
+ const condArray = Array.isArray(conditions) ? conditions : [
141
+ conditions
142
+ ];
143
+ const names = {};
144
+ const values = {};
145
+ const parts = [];
146
+ let counter = 0;
147
+ for (const cond of condArray) {
148
+ const attrName = `#f${counter}`;
149
+ names[attrName] = cond.name;
150
+ switch (cond.operator) {
151
+ case "eq":
152
+ case "ne":
153
+ case "lt":
154
+ case "le":
155
+ case "gt":
156
+ case "ge": {
157
+ const valKey = `:f${counter}`;
158
+ values[valKey] = cond.value;
159
+ parts.push(`${attrName} ${COMPARISON_OPERATORS[cond.operator]} ${valKey}`);
160
+ break;
161
+ }
162
+ case "between": {
163
+ const lowVal = `:f${counter}a`;
164
+ const highVal = `:f${counter}b`;
165
+ values[lowVal] = cond.low;
166
+ values[highVal] = cond.high;
167
+ parts.push(`${attrName} BETWEEN ${lowVal} AND ${highVal}`);
168
+ break;
169
+ }
170
+ case "startsWith":
171
+ case "contains": {
172
+ const valKey = `:f${counter}`;
173
+ values[valKey] = cond.value;
174
+ const fnName = cond.operator === "startsWith" ? "begins_with" : "contains";
175
+ parts.push(`${fnName}(${attrName}, ${valKey})`);
176
+ break;
177
+ }
178
+ case "exists":
179
+ parts.push(`attribute_exists(${attrName})`);
180
+ break;
181
+ }
182
+ counter++;
183
+ }
184
+ return {
185
+ expression: parts.join(" AND "),
186
+ names,
187
+ values
188
+ };
189
+ }
190
+ __name(buildFilterExpression, "buildFilterExpression");
191
+
192
+ // src/providers/dynamodb/dynamodb-item-listing.ts
193
+ var debug = (0, import_debug.default)("celerity:datastore:dynamodb");
194
+ function encodeCursor(lastEvaluatedKey) {
195
+ const state = {
196
+ lastEvaluatedKey
197
+ };
198
+ return Buffer.from(JSON.stringify(state)).toString("base64url");
199
+ }
200
+ __name(encodeCursor, "encodeCursor");
201
+ function decodeCursor(cursor) {
202
+ return JSON.parse(Buffer.from(cursor, "base64url").toString("utf-8"));
203
+ }
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
251
+ });
252
+ }
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
+ });
295
+ }
296
+ };
297
+
298
+ // src/providers/dynamodb/errors.ts
299
+ function isConditionalCheckFailedError(error) {
300
+ if (!(error instanceof Error)) return false;
301
+ return error.name === "ConditionalCheckFailedException";
302
+ }
303
+ __name(isConditionalCheckFailedError, "isConditionalCheckFailedError");
304
+
305
+ // 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
+ });
335
+ }
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
360
+ });
361
+ }
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
386
+ });
387
+ }
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
410
+ }
411
+ }
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
+ });
423
+ }
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
438
+ }
439
+ };
440
+ }
441
+ return {
442
+ DeleteRequest: {
443
+ Key: op.key
444
+ }
445
+ };
446
+ });
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) {
455
+ return {
456
+ type: "put",
457
+ item: req.PutRequest.Item
458
+ };
459
+ }
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
+ });
472
+ }
473
+ });
474
+ }
475
+ traced(name, attributes, fn) {
476
+ if (!this.tracer) return fn();
477
+ return this.tracer.withSpan(name, (span) => fn(span), attributes);
478
+ }
479
+ };
480
+
481
+ // src/providers/dynamodb/config.ts
482
+ function captureDynamoDBConfig() {
483
+ return {
484
+ region: process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION,
485
+ endpoint: process.env.AWS_ENDPOINT_URL,
486
+ credentials: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY ? {
487
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID,
488
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
489
+ } : void 0
490
+ };
491
+ }
492
+ __name(captureDynamoDBConfig, "captureDynamoDBConfig");
493
+
494
+ // 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
526
+ }
527
+ });
528
+ }
529
+ return this.docClient;
530
+ }
531
+ };
532
+
533
+ // src/factory.ts
534
+ var import_config2 = require("@celerity-sdk/config");
535
+ function createDatastoreClient(options) {
536
+ const resolved = (0, import_config2.resolveConfig)("datastore");
537
+ const provider = options?.provider ?? resolved.provider;
538
+ switch (provider) {
539
+ case "aws":
540
+ return new DynamoDBDatastoreClient(options?.aws, options?.tracer);
541
+ case "local":
542
+ return createLocalClient(options);
543
+ default:
544
+ throw new Error(`Unsupported datastore provider: "${provider}"`);
545
+ }
546
+ }
547
+ __name(createDatastoreClient, "createDatastoreClient");
548
+ function createLocalClient(options) {
549
+ const deployTarget = options?.deployTarget?.toLowerCase();
550
+ switch (deployTarget) {
551
+ case "aws":
552
+ case "aws-serverless":
553
+ case void 0: {
554
+ const localConfig = {
555
+ ...captureDynamoDBConfig(),
556
+ ...options?.aws
557
+ };
558
+ return new DynamoDBDatastoreClient(localConfig, options?.tracer);
559
+ }
560
+ // case "gcloud":
561
+ // case "gcloud-serverless":
562
+ // v1: Firestore emulator
563
+ // case "azure":
564
+ // case "azure-serverless":
565
+ // v1: Cosmos DB emulator
566
+ default:
567
+ throw new Error(`Unsupported local datastore deploy target: "${deployTarget}". Only AWS is supported in v0.`);
568
+ }
569
+ }
570
+ __name(createLocalClient, "createLocalClient");
571
+
572
+ // src/decorators.ts
573
+ var import_reflect_metadata = require("reflect-metadata");
574
+ var import_common = require("@celerity-sdk/common");
575
+ function datastoreToken(resourceName) {
576
+ return /* @__PURE__ */ Symbol.for(`celerity:datastore:${resourceName}`);
577
+ }
578
+ __name(datastoreToken, "datastoreToken");
579
+ var DEFAULT_DATASTORE_TOKEN = /* @__PURE__ */ Symbol.for("celerity:datastore:default");
580
+ function Datastore(resourceName) {
581
+ return (target, _propertyKey, parameterIndex) => {
582
+ const token = resourceName ? datastoreToken(resourceName) : DEFAULT_DATASTORE_TOKEN;
583
+ const existing = Reflect.getOwnMetadata(import_common.INJECT_METADATA, target) ?? /* @__PURE__ */ new Map();
584
+ existing.set(parameterIndex, token);
585
+ Reflect.defineMetadata(import_common.INJECT_METADATA, existing, target);
586
+ if (resourceName) {
587
+ const resources = Reflect.getOwnMetadata(import_common.USE_RESOURCE_METADATA, target) ?? [];
588
+ if (!resources.includes(resourceName)) {
589
+ Reflect.defineMetadata(import_common.USE_RESOURCE_METADATA, [
590
+ ...resources,
591
+ resourceName
592
+ ], target);
593
+ }
594
+ }
595
+ };
596
+ }
597
+ __name(Datastore, "Datastore");
598
+
599
+ // src/helpers.ts
600
+ function getDatastore(container, resourceName) {
601
+ const token = resourceName ? datastoreToken(resourceName) : DEFAULT_DATASTORE_TOKEN;
602
+ return container.resolve(token);
603
+ }
604
+ __name(getDatastore, "getDatastore");
605
+
606
+ // src/layer.ts
607
+ var import_debug3 = __toESM(require("debug"), 1);
608
+ var import_common2 = require("@celerity-sdk/common");
609
+ var import_config4 = require("@celerity-sdk/config");
610
+ var debug3 = (0, import_debug3.default)("celerity:datastore");
611
+ function captureDatastoreLayerConfig() {
612
+ return {
613
+ deployTarget: process.env.CELERITY_DEPLOY_TARGET
614
+ };
615
+ }
616
+ __name(captureDatastoreLayerConfig, "captureDatastoreLayerConfig");
617
+ var DatastoreLayer = class {
618
+ static {
619
+ __name(this, "DatastoreLayer");
620
+ }
621
+ initialized = false;
622
+ config = null;
623
+ async handle(context, next) {
624
+ if (!this.initialized) {
625
+ this.config = captureDatastoreLayerConfig();
626
+ const tracer = context.container.has(import_common2.TRACER_TOKEN) ? await context.container.resolve(import_common2.TRACER_TOKEN) : void 0;
627
+ const client = createDatastoreClient({
628
+ tracer,
629
+ deployTarget: this.config.deployTarget
630
+ });
631
+ debug3("registering DatastoreClient");
632
+ context.container.register("DatastoreClient", {
633
+ useValue: client
634
+ });
635
+ const links = (0, import_config4.captureResourceLinks)();
636
+ const datastoreLinks = (0, import_config4.getLinksOfType)(links, "datastore");
637
+ if (datastoreLinks.size > 0) {
638
+ const configService = await context.container.resolve(import_common2.CONFIG_SERVICE_TOKEN);
639
+ const resourceConfig = configService.namespace(import_config4.RESOURCE_CONFIG_NAMESPACE);
640
+ for (const [resourceName, configKey] of datastoreLinks) {
641
+ const actualName = await resourceConfig.getOrThrow(configKey);
642
+ debug3("registered datastore resource %s \u2192 %s", resourceName, actualName);
643
+ context.container.register(datastoreToken(resourceName), {
644
+ useValue: client.datastore(actualName)
645
+ });
646
+ }
647
+ if (datastoreLinks.size === 1) {
648
+ const [, configKey] = [
649
+ ...datastoreLinks.entries()
650
+ ][0];
651
+ const actualName = await resourceConfig.getOrThrow(configKey);
652
+ debug3("registered default datastore \u2192 %s", actualName);
653
+ context.container.register(DEFAULT_DATASTORE_TOKEN, {
654
+ useValue: client.datastore(actualName)
655
+ });
656
+ }
657
+ }
658
+ this.initialized = true;
659
+ }
660
+ return next();
661
+ }
662
+ };
663
+ // Annotate the CommonJS export names for ESM import in node:
664
+ 0 && (module.exports = {
665
+ ConditionalCheckFailedError,
666
+ DEFAULT_DATASTORE_TOKEN,
667
+ Datastore,
668
+ DatastoreClient,
669
+ DatastoreError,
670
+ DatastoreLayer,
671
+ DynamoDBDatastoreClient,
672
+ createDatastoreClient,
673
+ datastoreToken,
674
+ getDatastore
675
+ });
19
676
  //# sourceMappingURL=index.cjs.map