dyno-table 2.3.3 → 2.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.
@@ -1,10 +1,11 @@
1
- import { GetBuilder, PutBuilder, QueryBuilder, ScanBuilder, DeleteBuilder, UpdateBuilder, TransactionBuilder, BatchBuilder, ConditionCheckBuilder, buildExpression, generateAttributeName, debugCommand } from './chunk-DTFJJASK.js';
1
+ import { GetBuilder, PutBuilder, QueryBuilder, ScanBuilder, DeleteBuilder, UpdateBuilder, TransactionBuilder, BatchBuilder, ConditionCheckBuilder, buildExpression, generateAttributeName } from './chunk-NYJGW3XH.js';
2
+ import { ConfigurationErrors, OperationErrors } from './chunk-FF7FYGDH.js';
2
3
  import { eq, and, beginsWith, between, gte, gt, lte, lt } from './chunk-2WIBY7PZ.js';
3
4
 
4
5
  // src/utils/chunk-array.ts
5
6
  function* chunkArray(array, size) {
6
7
  if (size <= 0) {
7
- throw new Error("Chunk size must be greater than 0");
8
+ throw ConfigurationErrors.invalidChunkSize(size);
8
9
  }
9
10
  for (let i = 0; i < array.length; i += size) {
10
11
  yield array.slice(i, i + size);
@@ -36,11 +37,21 @@ var Table = class {
36
37
  this.sortKey = config.indexes.sortKey;
37
38
  this.gsis = config.indexes.gsis || {};
38
39
  }
40
+ getIndexAttributeNames() {
41
+ const names = /* @__PURE__ */ new Set();
42
+ for (const gsi of Object.values(this.gsis)) {
43
+ names.add(gsi.partitionKey);
44
+ if (gsi.sortKey) {
45
+ names.add(gsi.sortKey);
46
+ }
47
+ }
48
+ return Array.from(names);
49
+ }
39
50
  createKeyForPrimaryIndex(keyCondition) {
40
51
  const primaryCondition = { [this.partitionKey]: keyCondition.pk };
41
52
  if (this.sortKey) {
42
53
  if (!keyCondition.sk) {
43
- throw new Error("Sort key has not been provided but the Table has a sort key");
54
+ throw ConfigurationErrors.sortKeyRequired(this.tableName, this.partitionKey, this.sortKey);
44
55
  }
45
56
  primaryCondition[this.sortKey] = keyCondition.sk;
46
57
  }
@@ -81,6 +92,7 @@ var Table = class {
81
92
  return this.put(item).condition((op) => op.attributeNotExists(this.partitionKey)).returnValues("INPUT");
82
93
  }
83
94
  get(keyCondition) {
95
+ const indexAttributeNames = this.getIndexAttributeNames();
84
96
  const executor = async (params) => {
85
97
  try {
86
98
  const result = await this.dynamoClient.get({
@@ -94,11 +106,10 @@ var Table = class {
94
106
  item: result.Item ? result.Item : void 0
95
107
  };
96
108
  } catch (error) {
97
- console.error("Error getting item:", error);
98
- throw error;
109
+ throw OperationErrors.getFailed(params.tableName, keyCondition, error instanceof Error ? error : void 0);
99
110
  }
100
111
  };
101
- return new GetBuilder(executor, keyCondition, this.tableName);
112
+ return new GetBuilder(executor, keyCondition, this.tableName, indexAttributeNames);
102
113
  }
103
114
  /**
104
115
  * Updates an item in the table
@@ -135,8 +146,7 @@ var Table = class {
135
146
  }
136
147
  return result.Attributes;
137
148
  } catch (error) {
138
- console.error("Error creating item:", error);
139
- throw error;
149
+ throw OperationErrors.putFailed(params.tableName, params.item, error instanceof Error ? error : void 0);
140
150
  }
141
151
  };
142
152
  return new PutBuilder(executor, item, this.tableName);
@@ -146,12 +156,13 @@ var Table = class {
146
156
  * If useIndex is called on the returned QueryBuilder, it will use the GSI configuration
147
157
  */
148
158
  query(keyCondition) {
159
+ const indexAttributeNames = this.getIndexAttributeNames();
149
160
  const pkAttributeName = this.partitionKey;
150
161
  const skAttributeName = this.sortKey;
151
162
  let keyConditionExpression = eq(pkAttributeName, keyCondition.pk);
152
163
  if (keyCondition.sk) {
153
164
  if (!skAttributeName) {
154
- throw new Error("Sort key is not defined for Index");
165
+ throw ConfigurationErrors.sortKeyNotDefined(this.tableName, pkAttributeName);
155
166
  }
156
167
  const keyConditionOperator = {
157
168
  eq: (value) => eq(skAttributeName, value),
@@ -172,7 +183,7 @@ var Table = class {
172
183
  const gsiName = String(options.indexName);
173
184
  const gsi = this.gsis[gsiName];
174
185
  if (!gsi) {
175
- throw new Error(`GSI with name "${gsiName}" does not exist on table "${this.tableName}"`);
186
+ throw ConfigurationErrors.gsiNotFound(gsiName, this.tableName, Object.keys(this.gsis));
176
187
  }
177
188
  const gsiPkAttributeName = gsi.partitionKey;
178
189
  const gsiSkAttributeName = gsi.sortKey;
@@ -201,7 +212,7 @@ var Table = class {
201
212
  }
202
213
  }
203
214
  if (!pkValue) {
204
- throw new Error("Could not extract partition key value from key condition");
215
+ throw ConfigurationErrors.pkExtractionFailed(this.tableName, options.indexName, originalKeyCondition);
205
216
  }
206
217
  let gsiKeyCondition = eq(gsiPkAttributeName, pkValue);
207
218
  if (skValue && gsiSkAttributeName) {
@@ -252,12 +263,14 @@ var Table = class {
252
263
  lastEvaluatedKey: result.LastEvaluatedKey
253
264
  };
254
265
  } catch (error) {
255
- console.log(debugCommand(params));
256
- console.error("Error querying items:", error);
257
- throw error;
266
+ throw OperationErrors.queryFailed(
267
+ this.tableName,
268
+ { indexName, keyConditionExpression: keyConditionExpression2, filterExpression },
269
+ error instanceof Error ? error : void 0
270
+ );
258
271
  }
259
272
  };
260
- return new QueryBuilder(executor, keyConditionExpression);
273
+ return new QueryBuilder(executor, keyConditionExpression, indexAttributeNames);
261
274
  }
262
275
  /**
263
276
  * Creates a scan builder for scanning the entire table
@@ -300,9 +313,11 @@ var Table = class {
300
313
  lastEvaluatedKey: result.LastEvaluatedKey
301
314
  };
302
315
  } catch (error) {
303
- console.log(debugCommand(params));
304
- console.error("Error scanning items:", error);
305
- throw error;
316
+ throw OperationErrors.scanFailed(
317
+ this.tableName,
318
+ { indexName: options.indexName, filterExpression },
319
+ error instanceof Error ? error : void 0
320
+ );
306
321
  }
307
322
  };
308
323
  return new ScanBuilder(executor);
@@ -322,8 +337,7 @@ var Table = class {
322
337
  item: result.Attributes
323
338
  };
324
339
  } catch (error) {
325
- console.error("Error deleting item:", error);
326
- throw error;
340
+ throw OperationErrors.deleteFailed(params.tableName, keyCondition, error instanceof Error ? error : void 0);
327
341
  }
328
342
  };
329
343
  return new DeleteBuilder(executor, this.tableName, keyCondition);
@@ -350,8 +364,7 @@ var Table = class {
350
364
  item: result.Attributes
351
365
  };
352
366
  } catch (error) {
353
- console.error("Error updating item:", error);
354
- throw error;
367
+ throw OperationErrors.updateFailed(params.tableName, keyCondition, error instanceof Error ? error : void 0);
355
368
  }
356
369
  };
357
370
  return new UpdateBuilder(executor, this.tableName, keyCondition);
@@ -483,8 +496,11 @@ var Table = class {
483
496
  allUnprocessedKeys.push(...unprocessedKeys);
484
497
  }
485
498
  } catch (error) {
486
- console.error("Error in batch get operation:", error);
487
- throw error;
499
+ throw OperationErrors.batchGetFailed(
500
+ this.tableName,
501
+ { requestedKeys: keys.length },
502
+ error instanceof Error ? error : void 0
503
+ );
488
504
  }
489
505
  }
490
506
  return {
@@ -545,8 +561,11 @@ var Table = class {
545
561
  allUnprocessedItems.push(...unprocessedItems);
546
562
  }
547
563
  } catch (error) {
548
- console.error("Error in batch write operation:", error);
549
- throw error;
564
+ throw OperationErrors.batchWriteFailed(
565
+ this.tableName,
566
+ { requestedOperations: operations.length },
567
+ error instanceof Error ? error : void 0
568
+ );
550
569
  }
551
570
  }
552
571
  return {