dyno-table 2.2.0 → 2.3.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.
Files changed (106) hide show
  1. package/README.md +200 -1860
  2. package/dist/builders.cjs +55 -0
  3. package/dist/builders.d.cts +4 -0
  4. package/dist/builders.d.ts +4 -0
  5. package/dist/builders.js +2 -0
  6. package/dist/chunk-2EWNZOUK.js +618 -0
  7. package/dist/chunk-2WIBY7PZ.js +46 -0
  8. package/dist/chunk-7UJJ7JXM.cjs +63 -0
  9. package/dist/chunk-DTFJJASK.js +3200 -0
  10. package/dist/chunk-EODPMYPE.js +558 -0
  11. package/dist/chunk-KA3VPIPS.cjs +560 -0
  12. package/dist/chunk-NTA6GDPP.cjs +622 -0
  13. package/dist/chunk-PB7BBCZO.cjs +32 -0
  14. package/dist/chunk-QVRMYGC4.js +29 -0
  15. package/dist/chunk-XYL43FDX.cjs +3217 -0
  16. package/dist/{conditions-CC3NDfUU.d.cts → conditions-CcZL0sR2.d.cts} +1 -1
  17. package/dist/{conditions-DD0bvyHm.d.ts → conditions-D_w7vVYG.d.ts} +1 -1
  18. package/dist/conditions.cjs +67 -62
  19. package/dist/conditions.d.cts +1 -1
  20. package/dist/conditions.d.ts +1 -1
  21. package/dist/conditions.js +1 -48
  22. package/dist/entity.cjs +14 -625
  23. package/dist/entity.d.cts +5 -13
  24. package/dist/entity.d.ts +5 -13
  25. package/dist/entity.js +2 -626
  26. package/dist/index-2cbm07Bi.d.ts +2797 -0
  27. package/dist/index-DlN8G9hd.d.cts +2797 -0
  28. package/dist/index.cjs +111 -4446
  29. package/dist/index.d.cts +3 -11
  30. package/dist/index.d.ts +3 -11
  31. package/dist/index.js +5 -4428
  32. package/dist/standard-schema.cjs +0 -2
  33. package/dist/standard-schema.js +0 -2
  34. package/dist/table.cjs +7 -3782
  35. package/dist/table.d.cts +163 -12
  36. package/dist/table.d.ts +163 -12
  37. package/dist/table.js +3 -3785
  38. package/dist/types.cjs +0 -2
  39. package/dist/types.js +0 -2
  40. package/dist/utils.cjs +10 -30
  41. package/dist/utils.js +1 -31
  42. package/package.json +12 -67
  43. package/dist/batch-builder-BPoHyN_Q.d.cts +0 -398
  44. package/dist/batch-builder-Cdo49C2r.d.ts +0 -398
  45. package/dist/builder-types-BTVhQSHI.d.cts +0 -169
  46. package/dist/builder-types-CzuLR4Th.d.ts +0 -169
  47. package/dist/builders/condition-check-builder.cjs +0 -422
  48. package/dist/builders/condition-check-builder.cjs.map +0 -1
  49. package/dist/builders/condition-check-builder.d.cts +0 -153
  50. package/dist/builders/condition-check-builder.d.ts +0 -153
  51. package/dist/builders/condition-check-builder.js +0 -420
  52. package/dist/builders/condition-check-builder.js.map +0 -1
  53. package/dist/builders/delete-builder.cjs +0 -484
  54. package/dist/builders/delete-builder.cjs.map +0 -1
  55. package/dist/builders/delete-builder.d.cts +0 -211
  56. package/dist/builders/delete-builder.d.ts +0 -211
  57. package/dist/builders/delete-builder.js +0 -482
  58. package/dist/builders/delete-builder.js.map +0 -1
  59. package/dist/builders/paginator.cjs +0 -193
  60. package/dist/builders/paginator.cjs.map +0 -1
  61. package/dist/builders/paginator.d.cts +0 -155
  62. package/dist/builders/paginator.d.ts +0 -155
  63. package/dist/builders/paginator.js +0 -191
  64. package/dist/builders/paginator.js.map +0 -1
  65. package/dist/builders/put-builder.cjs +0 -554
  66. package/dist/builders/put-builder.cjs.map +0 -1
  67. package/dist/builders/put-builder.d.cts +0 -319
  68. package/dist/builders/put-builder.d.ts +0 -319
  69. package/dist/builders/put-builder.js +0 -552
  70. package/dist/builders/put-builder.js.map +0 -1
  71. package/dist/builders/query-builder.cjs +0 -743
  72. package/dist/builders/query-builder.cjs.map +0 -1
  73. package/dist/builders/query-builder.d.cts +0 -6
  74. package/dist/builders/query-builder.d.ts +0 -6
  75. package/dist/builders/query-builder.js +0 -741
  76. package/dist/builders/query-builder.js.map +0 -1
  77. package/dist/builders/transaction-builder.cjs +0 -906
  78. package/dist/builders/transaction-builder.cjs.map +0 -1
  79. package/dist/builders/transaction-builder.d.cts +0 -464
  80. package/dist/builders/transaction-builder.d.ts +0 -464
  81. package/dist/builders/transaction-builder.js +0 -904
  82. package/dist/builders/transaction-builder.js.map +0 -1
  83. package/dist/builders/update-builder.cjs +0 -668
  84. package/dist/builders/update-builder.cjs.map +0 -1
  85. package/dist/builders/update-builder.d.cts +0 -374
  86. package/dist/builders/update-builder.d.ts +0 -374
  87. package/dist/builders/update-builder.js +0 -666
  88. package/dist/builders/update-builder.js.map +0 -1
  89. package/dist/conditions.cjs.map +0 -1
  90. package/dist/conditions.js.map +0 -1
  91. package/dist/entity.cjs.map +0 -1
  92. package/dist/entity.js.map +0 -1
  93. package/dist/index.cjs.map +0 -1
  94. package/dist/index.js.map +0 -1
  95. package/dist/query-builder-CUWdavZw.d.ts +0 -477
  96. package/dist/query-builder-DoZzZz_c.d.cts +0 -477
  97. package/dist/standard-schema.cjs.map +0 -1
  98. package/dist/standard-schema.js.map +0 -1
  99. package/dist/table-CZBMkW2Z.d.ts +0 -276
  100. package/dist/table-f-3wsT7K.d.cts +0 -276
  101. package/dist/table.cjs.map +0 -1
  102. package/dist/table.js.map +0 -1
  103. package/dist/types.cjs.map +0 -1
  104. package/dist/types.js.map +0 -1
  105. package/dist/utils.cjs.map +0 -1
  106. package/dist/utils.js.map +0 -1
@@ -1,741 +0,0 @@
1
- // src/conditions.ts
2
- var createComparisonCondition = (type) => (attr, value) => ({
3
- type,
4
- attr,
5
- value
6
- });
7
- var eq = createComparisonCondition("eq");
8
- var ne = createComparisonCondition("ne");
9
- var lt = createComparisonCondition("lt");
10
- var lte = createComparisonCondition("lte");
11
- var gt = createComparisonCondition("gt");
12
- var gte = createComparisonCondition("gte");
13
- var between = (attr, lower, upper) => ({
14
- type: "between",
15
- attr,
16
- value: [lower, upper]
17
- });
18
- var inArray = (attr, values) => ({
19
- type: "in",
20
- attr,
21
- value: values
22
- });
23
- var beginsWith = createComparisonCondition("beginsWith");
24
- var contains = createComparisonCondition("contains");
25
- var attributeExists = (attr) => ({
26
- type: "attributeExists",
27
- attr
28
- });
29
- var attributeNotExists = (attr) => ({
30
- type: "attributeNotExists",
31
- attr
32
- });
33
- var and = (...conditions) => ({
34
- type: "and",
35
- conditions
36
- });
37
- var or = (...conditions) => ({
38
- type: "or",
39
- conditions
40
- });
41
- var not = (condition) => ({
42
- type: "not",
43
- condition
44
- });
45
-
46
- // src/builders/paginator.ts
47
- var Paginator = class {
48
- queryBuilder;
49
- pageSize;
50
- currentPage = 0;
51
- lastEvaluatedKey;
52
- hasMorePages = true;
53
- totalItemsRetrieved = 0;
54
- overallLimit;
55
- constructor(queryBuilder, pageSize) {
56
- this.queryBuilder = queryBuilder;
57
- this.pageSize = pageSize;
58
- this.overallLimit = queryBuilder.getLimit();
59
- }
60
- /**
61
- * Gets the current page number (1-indexed).
62
- *
63
- * @example
64
- * ```ts
65
- * const paginator = new QueryBuilder(executor, eq('species', 'Tyrannosaurus'))
66
- * .paginate(5);
67
- *
68
- * await paginator.getNextPage();
69
- * console.log(`Reviewing T-Rex group ${paginator.getCurrentPage()}`);
70
- * ```
71
- *
72
- * @returns The current page number, starting from 1
73
- */
74
- getCurrentPage() {
75
- return this.currentPage;
76
- }
77
- /**
78
- * Checks if there are more pages of dinosaurs or habitats to process.
79
- *
80
- * This method takes into account both:
81
- * - DynamoDB's lastEvaluatedKey mechanism
82
- * - Any overall limit set on the query
83
- *
84
- * @example
85
- * ```ts
86
- * // Process all security incidents
87
- * const paginator = new QueryBuilder(executor, eq('type', 'SECURITY_BREACH'))
88
- * .sortDescending()
89
- * .paginate(10);
90
- *
91
- * while (paginator.hasNextPage()) {
92
- * const page = await paginator.getNextPage();
93
- * for (const incident of page.items) {
94
- * await processSecurityBreach(incident);
95
- * }
96
- * console.log(`Processed incidents page ${page.page}`);
97
- * }
98
- * ```
99
- *
100
- * @returns true if there are more pages available, false otherwise
101
- */
102
- hasNextPage() {
103
- if (this.overallLimit !== void 0 && this.totalItemsRetrieved >= this.overallLimit) {
104
- return false;
105
- }
106
- return this.hasMorePages;
107
- }
108
- /**
109
- * Retrieves the next page of dinosaurs or habitats from DynamoDB.
110
- *
111
- * This method handles:
112
- * - Automatic continuation between groups
113
- * - Respect for park capacity limits
114
- * - Group size adjustments for safety
115
- *
116
- * @example
117
- * ```ts
118
- * const paginator = new QueryBuilder(executor, eq('species', 'Velociraptor'))
119
- * .filter(op => op.eq('status', 'ACTIVE'))
120
- * .paginate(5);
121
- *
122
- * // Check first raptor group
123
- * const page1 = await paginator.getNextPage();
124
- * console.log(`Found ${page1.items.length} active raptors`);
125
- *
126
- * // Continue inspection if more groups exist
127
- * if (page1.hasNextPage) {
128
- * const page2 = await paginator.getNextPage();
129
- * console.log(`Inspecting raptor group ${page2.page}`);
130
- *
131
- * for (const raptor of page2.items) {
132
- * await performHealthCheck(raptor);
133
- * }
134
- * }
135
- * ```
136
- *
137
- * @returns A promise that resolves to a PaginationResult containing:
138
- * - items: The dinosaurs/habitats for this page
139
- * - hasNextPage: Whether more groups exist
140
- * - page: The current group number
141
- * - lastEvaluatedKey: DynamoDB's continuation token
142
- */
143
- async getNextPage() {
144
- if (!this.hasNextPage()) {
145
- return {
146
- items: [],
147
- hasNextPage: false,
148
- page: this.currentPage
149
- };
150
- }
151
- let effectivePageSize = this.pageSize;
152
- if (this.overallLimit !== void 0) {
153
- const remainingItems = this.overallLimit - this.totalItemsRetrieved;
154
- if (remainingItems <= 0) {
155
- return {
156
- items: [],
157
- hasNextPage: false,
158
- page: this.currentPage
159
- };
160
- }
161
- if (effectivePageSize !== void 0) {
162
- effectivePageSize = Math.min(effectivePageSize, remainingItems);
163
- } else {
164
- effectivePageSize = remainingItems;
165
- }
166
- }
167
- const query = this.queryBuilder.clone();
168
- if (effectivePageSize !== void 0) {
169
- query.limit(effectivePageSize);
170
- }
171
- if (this.lastEvaluatedKey) {
172
- query.startFrom(this.lastEvaluatedKey);
173
- }
174
- const generator = await query.execute();
175
- const items = [];
176
- let itemCount = 0;
177
- for await (const item of generator) {
178
- if (effectivePageSize !== void 0 && itemCount >= effectivePageSize) {
179
- break;
180
- }
181
- items.push(item);
182
- itemCount++;
183
- }
184
- const lastEvaluatedKey = generator.getLastEvaluatedKey();
185
- const result = { items, lastEvaluatedKey };
186
- this.currentPage += 1;
187
- this.lastEvaluatedKey = result.lastEvaluatedKey;
188
- this.totalItemsRetrieved += result.items.length;
189
- this.hasMorePages = !!result.lastEvaluatedKey && (this.overallLimit === void 0 || this.totalItemsRetrieved < this.overallLimit);
190
- return {
191
- items: result.items,
192
- lastEvaluatedKey: result.lastEvaluatedKey,
193
- hasNextPage: this.hasNextPage(),
194
- page: this.currentPage
195
- };
196
- }
197
- /**
198
- * Gets all remaining dinosaurs or habitats and combines them into a single array.
199
- *
200
- * @example
201
- * ```ts
202
- * // Get complete carnivore inventory
203
- * const paginator = new QueryBuilder(executor, eq('diet', 'CARNIVORE'))
204
- * .filter(op => op.eq('status', 'ACTIVE'))
205
- * .paginate(10);
206
- *
207
- * try {
208
- * const allCarnivores = await paginator.getAllPages();
209
- * console.log(`Park contains ${allCarnivores.length} active carnivores`);
210
- *
211
- * // Calculate total threat level
212
- * const totalThreat = allCarnivores.reduce(
213
- * (sum, dino) => sum + dino.stats.threatLevel,
214
- * 0
215
- * );
216
- * console.log(`Total threat level: ${totalThreat}`);
217
- * } catch (error) {
218
- * console.error('Failed to complete carnivore census:', error);
219
- * }
220
- * ```
221
- *
222
- * @returns A promise that resolves to an array containing all remaining items
223
- */
224
- async getAllPages() {
225
- const allItems = [];
226
- while (this.hasNextPage()) {
227
- const result = await this.getNextPage();
228
- allItems.push(...result.items);
229
- }
230
- return allItems;
231
- }
232
- };
233
-
234
- // src/builders/filter-builder.ts
235
- var FilterBuilder = class {
236
- options = {};
237
- selectedFields = /* @__PURE__ */ new Set();
238
- /**
239
- * Sets the maximum number of items to return.
240
- *
241
- * Note: This limit applies to the items that match the key condition
242
- * before any filter expressions are applied.
243
- *
244
- * @example
245
- * ```typescript
246
- * // Get first 10 dinosaurs
247
- * const result = await builder
248
- * .limit(10)
249
- * .execute();
250
- * ```
251
- *
252
- * @param limit - Maximum number of items to return
253
- * @returns The builder instance for method chaining
254
- */
255
- limit(limit) {
256
- this.options.limit = limit;
257
- return this;
258
- }
259
- /**
260
- * Gets the current limit set on the operation.
261
- * This is used internally by the paginator to manage result sets.
262
- *
263
- * @returns The current limit or undefined if no limit is set
264
- */
265
- getLimit() {
266
- return this.options.limit;
267
- }
268
- /**
269
- * Specifies a Global Secondary Index (GSI) to use for the operation.
270
- *
271
- * @example
272
- * ```typescript
273
- * // Find all dinosaurs of a specific species
274
- * builder
275
- * .useIndex('species-status-index')
276
- * .filter(op => op.eq('status', 'ACTIVE'));
277
- *
278
- * // Search high-security habitats
279
- * builder
280
- * .useIndex('security-level-index')
281
- * .filter(op =>
282
- * op.and([
283
- * op.gt('securityLevel', 8),
284
- * op.eq('status', 'OPERATIONAL')
285
- * ])
286
- * );
287
- * ```
288
- *
289
- * @param indexName - The name of the GSI to use (type-safe based on table configuration)
290
- * @returns The builder instance for method chaining
291
- */
292
- useIndex(indexName) {
293
- this.options.indexName = indexName;
294
- return this;
295
- }
296
- /**
297
- * Sets whether to use strongly consistent reads for the operation.
298
- *
299
- * Note:
300
- * - Consistent reads are not available on GSIs
301
- * - Consistent reads consume twice the throughput
302
- * - Default is eventually consistent reads
303
- *
304
- * @example
305
- * ```typescript
306
- * // Check immediate dinosaur status
307
- * const result = await builder
308
- * .filter(op => op.eq('status', 'ACTIVE'))
309
- * .consistentRead()
310
- * .execute();
311
- *
312
- * // Monitor security breaches
313
- * const result = await builder
314
- * .useIndex('primary-index')
315
- * .consistentRead(isEmergencyMode)
316
- * .execute();
317
- * ```
318
- *
319
- * @param consistentRead - Whether to use consistent reads (defaults to true)
320
- * @returns The builder instance for method chaining
321
- */
322
- consistentRead(consistentRead = true) {
323
- this.options.consistentRead = consistentRead;
324
- return this;
325
- }
326
- /**
327
- * Adds a filter expression to refine the operation results.
328
- *
329
- * @example
330
- * ```typescript
331
- * // Find aggressive carnivores
332
- * builder.filter(op =>
333
- * op.and([
334
- * op.eq('diet', 'CARNIVORE'),
335
- * op.gt('aggressionLevel', 7),
336
- * op.eq('status', 'ACTIVE')
337
- * ])
338
- * );
339
- *
340
- * // Search suitable breeding habitats
341
- * builder.filter(op =>
342
- * op.and([
343
- * op.between('temperature', 25, 30),
344
- * op.lt('currentOccupants', 3),
345
- * op.eq('quarantineStatus', 'CLEAR')
346
- * ])
347
- * );
348
- * ```
349
- *
350
- * @param condition - Either a Condition object or a callback function that builds the condition
351
- * @returns The builder instance for method chaining
352
- */
353
- filter(condition) {
354
- const newCondition = typeof condition === "function" ? condition(this.getConditionOperator()) : condition;
355
- if (this.options.filter) {
356
- if (this.options.filter.type === "and" && this.options.filter.conditions) {
357
- this.options.filter = {
358
- type: "and",
359
- conditions: [...this.options.filter.conditions, newCondition]
360
- };
361
- } else {
362
- this.options.filter = and(this.options.filter, newCondition);
363
- }
364
- } else {
365
- this.options.filter = newCondition;
366
- }
367
- return this;
368
- }
369
- getConditionOperator() {
370
- return {
371
- eq,
372
- ne,
373
- lt,
374
- lte,
375
- gt,
376
- gte,
377
- between,
378
- inArray,
379
- beginsWith,
380
- contains,
381
- attributeExists,
382
- attributeNotExists,
383
- and,
384
- or,
385
- not
386
- };
387
- }
388
- /**
389
- * Specifies which attributes to return in the results.
390
- *
391
- * @example
392
- * ```typescript
393
- * // Get basic dinosaur info
394
- * builder.select([
395
- * 'species',
396
- * 'status',
397
- * 'stats.health',
398
- * 'stats.aggressionLevel'
399
- * ]);
400
- *
401
- * // Monitor habitat conditions
402
- * builder
403
- * .select('securityStatus')
404
- * .select([
405
- * 'currentOccupants',
406
- * 'temperature',
407
- * 'lastInspectionDate'
408
- * ]);
409
- * ```
410
- *
411
- * @param fields - A single field name or an array of field names to return
412
- * @returns The builder instance for method chaining
413
- */
414
- select(fields) {
415
- if (typeof fields === "string") {
416
- this.selectedFields.add(fields);
417
- } else if (Array.isArray(fields)) {
418
- for (const field of fields) {
419
- this.selectedFields.add(field);
420
- }
421
- }
422
- this.options.projection = Array.from(this.selectedFields);
423
- return this;
424
- }
425
- /**
426
- * Creates a paginator that handles DynamoDB pagination automatically.
427
- * The paginator handles:
428
- * - Tracking the last evaluated key
429
- * - Managing page boundaries
430
- * - Respecting overall query limits
431
- *
432
- * @example
433
- * ```typescript
434
- * // Create a paginator for dinosaur records with specific page size
435
- * const paginator = builder
436
- * .filter(op => op.eq('status', 'ACTIVE'))
437
- * .paginate(10);
438
- *
439
- * // Create a paginator with automatic DynamoDB paging (no page size limit)
440
- * const autoPaginator = builder
441
- * .filter(op => op.eq('status', 'ACTIVE'))
442
- * .paginate();
443
- *
444
- * // Process pages of dinosaur results
445
- * while (paginator.hasNextPage()) {
446
- * const page = await paginator.getNextPage();
447
- * console.log(`Processing page ${page.page}, count: ${page.items.length}`);
448
- * // Process dinosaur data
449
- * }
450
- * ```
451
- *
452
- * @param pageSize - The number of items to return per page. If not provided, DynamoDB will automatically determine page sizes.
453
- * @returns A Paginator instance that manages the pagination state
454
- * @see Paginator for more pagination control options
455
- */
456
- paginate(pageSize) {
457
- return new Paginator(this, pageSize);
458
- }
459
- /**
460
- * Sets the starting point using a previous lastEvaluatedKey.
461
- *
462
- * Note: This method is typically used for manual pagination.
463
- * For automatic pagination, use the paginate() method instead.
464
- *
465
- * @example
466
- * ```typescript
467
- * // First batch of dinosaurs
468
- * const result1 = await builder
469
- * .filter(op => op.eq('status', 'ACTIVE'))
470
- * .limit(5)
471
- * .execute();
472
- *
473
- * const lastKey = result1.getLastEvaluatedKey();
474
- * if (lastKey) {
475
- * // Continue listing dinosaurs
476
- * const result2 = await builder
477
- * .filter(op => op.eq('status', 'ACTIVE'))
478
- * .startFrom(lastKey)
479
- * .limit(5)
480
- * .execute();
481
- *
482
- * const items = await result2.toArray();
483
- * console.log('Additional dinosaurs:', items);
484
- * }
485
- * ```
486
- *
487
- * @param lastEvaluatedKey - The exclusive start key from a previous result
488
- * @returns The builder instance for method chaining
489
- */
490
- startFrom(lastEvaluatedKey) {
491
- this.options.lastEvaluatedKey = lastEvaluatedKey;
492
- return this;
493
- }
494
- };
495
-
496
- // src/builders/result-iterator.ts
497
- var ResultIterator = class {
498
- constructor(queryBuilder, directExecutor) {
499
- this.queryBuilder = queryBuilder;
500
- this.directExecutor = directExecutor;
501
- this.overallLimit = queryBuilder.getLimit();
502
- }
503
- lastEvaluatedKey;
504
- itemsYielded = 0;
505
- overallLimit;
506
- /**
507
- * Async iterator with automatic pagination
508
- */
509
- async *[Symbol.asyncIterator]() {
510
- let hasMorePages = true;
511
- while (hasMorePages) {
512
- const result = await this.directExecutor();
513
- for (const item of result.items) {
514
- if (this.overallLimit !== void 0 && this.itemsYielded >= this.overallLimit) {
515
- return;
516
- }
517
- yield item;
518
- this.itemsYielded++;
519
- }
520
- if (result.lastEvaluatedKey !== null && result.lastEvaluatedKey !== void 0) {
521
- this.lastEvaluatedKey = result.lastEvaluatedKey;
522
- this.queryBuilder.startFrom(result.lastEvaluatedKey);
523
- } else if (result.lastEvaluatedKey === null) {
524
- if (this.lastEvaluatedKey === void 0) {
525
- this.lastEvaluatedKey = null;
526
- }
527
- }
528
- hasMorePages = !!result.lastEvaluatedKey && (this.overallLimit === void 0 || this.itemsYielded < this.overallLimit);
529
- }
530
- }
531
- /**
532
- * Convert to array (loads all pages).
533
- *
534
- * ```ts
535
- * const result = await table.query({ pk: "foo" }).execute();
536
- * const allItemsFromDynamo = await result.toArray();
537
- * ```
538
- *
539
- * Note: This will load all pages into memory. For large datasets, consider using async iteration instead.
540
- *```ts
541
- * const result = await table.query({ pk: "foo" }).execute();
542
- * for await (const item of result) {
543
- * // Process each item
544
- * }
545
- * ```
546
- */
547
- async toArray() {
548
- const items = [];
549
- for await (const item of this) {
550
- items.push(item);
551
- }
552
- return items;
553
- }
554
- /**
555
- * Get the last evaluated key
556
- */
557
- getLastEvaluatedKey() {
558
- return this.lastEvaluatedKey === null ? void 0 : this.lastEvaluatedKey;
559
- }
560
- };
561
-
562
- // src/builders/query-builder.ts
563
- var QueryBuilder = class _QueryBuilder extends FilterBuilder {
564
- keyCondition;
565
- options = {};
566
- executor;
567
- constructor(executor, keyCondition) {
568
- super();
569
- this.executor = executor;
570
- this.keyCondition = keyCondition;
571
- }
572
- /**
573
- * Sets the maximum number of items to return from the query.
574
- *
575
- * Note: This is the default behavior if no sort order is specified.
576
- *
577
- * @example
578
- * ```typescript
579
- * // Get orders in chronological order
580
- * const result = await new QueryBuilder(executor, eq('userId', '123'))
581
- * .sortAscending()
582
- * .execute();
583
- *
584
- * // Get events from oldest to newest
585
- * const result = await new QueryBuilder(executor, eq('entityId', 'order-123'))
586
- * .useIndex('entity-timestamp-index')
587
- * .sortAscending()
588
- * .execute();
589
- * ```
590
- *
591
- * @returns The builder instance for method chaining
592
- */
593
- /**
594
- * Sets the query to return items in ascending order by sort key.
595
- *
596
- * @example
597
- * ```typescript
598
- * // List dinosaurs by age
599
- * const result = await new QueryBuilder(executor, eq('species', 'Velociraptor'))
600
- * .useIndex('age-index')
601
- * .sortAscending()
602
- * .execute();
603
- *
604
- * // View incidents chronologically
605
- * const result = await new QueryBuilder(executor, eq('type', 'SECURITY_BREACH'))
606
- * .useIndex('date-index')
607
- * .sortAscending()
608
- * .execute();
609
- * ```
610
- *
611
- * @returns The builder instance for method chaining
612
- */
613
- sortAscending() {
614
- this.options.scanIndexForward = true;
615
- return this;
616
- }
617
- /**
618
- * Sets the query to return items in descending order by sort key.
619
- *
620
- * @example
621
- * ```typescript
622
- * // Get most recent security incidents
623
- * const result = await new QueryBuilder(executor, eq('type', 'SECURITY_BREACH'))
624
- * .useIndex('date-index')
625
- * .sortDescending()
626
- * .limit(10)
627
- * .execute();
628
- *
629
- * // Check latest dinosaur activities
630
- * const result = await new QueryBuilder(executor, eq('species', 'Velociraptor'))
631
- * .useIndex('activity-time-index')
632
- * .filter(op => op.eq('status', 'ACTIVE'))
633
- * .sortDescending()
634
- * .execute();
635
- * ```
636
- *
637
- * @returns The builder instance for method chaining
638
- */
639
- sortDescending() {
640
- this.options.scanIndexForward = false;
641
- return this;
642
- }
643
- /**
644
- * Creates a deep clone of this QueryBuilder instance.
645
- *
646
- * This is particularly useful when:
647
- * - Implementing pagination (used internally by paginate())
648
- * - Creating query templates
649
- * - Running multiple variations of a query
650
- *
651
- * @example
652
- * ```typescript
653
- * // Create base dinosaur query
654
- * const baseQuery = new QueryBuilder(executor, eq('species', 'Velociraptor'))
655
- * .useIndex('status-index')
656
- * .select(['id', 'status', 'location']);
657
- *
658
- * // Check active dinosaurs
659
- * const activeRaptors = baseQuery.clone()
660
- * .filter(op => op.eq('status', 'HUNTING'))
661
- * .execute();
662
- *
663
- * // Check contained dinosaurs
664
- * const containedRaptors = baseQuery.clone()
665
- * .filter(op => op.eq('status', 'CONTAINED'))
666
- * .execute();
667
- *
668
- * // Check sedated dinosaurs
669
- * const sedatedRaptors = baseQuery.clone()
670
- * .filter(op => op.eq('status', 'SEDATED'))
671
- * .execute();
672
- * ```
673
- *
674
- * @returns A new QueryBuilder instance with the same configuration
675
- */
676
- clone() {
677
- const clone = new _QueryBuilder(this.executor, this.keyCondition);
678
- clone.options = {
679
- ...this.options,
680
- filter: this.deepCloneFilter(this.options.filter)
681
- };
682
- clone.selectedFields = new Set(this.selectedFields);
683
- return clone;
684
- }
685
- deepCloneFilter(filter) {
686
- if (!filter) return filter;
687
- if (filter.type === "and" || filter.type === "or") {
688
- return {
689
- ...filter,
690
- conditions: filter.conditions?.map((condition) => this.deepCloneFilter(condition)).filter((c) => c !== void 0)
691
- };
692
- }
693
- return { ...filter };
694
- }
695
- /**
696
- * Executes the query against DynamoDB and returns a generator that behaves like an array.
697
- *
698
- * The generator automatically handles pagination and provides array-like methods
699
- * for processing results efficiently without loading everything into memory at once.
700
- *
701
- * @example
702
- * ```typescript
703
- * try {
704
- * // Find active carnivores with automatic pagination
705
- * const results = await new QueryBuilder(executor, eq('habitatId', 'PADDOCK-A'))
706
- * .useIndex('species-status-index')
707
- * .filter(op =>
708
- * op.and([
709
- * op.eq('diet', 'CARNIVORE'),
710
- * op.eq('status', 'ACTIVE'),
711
- * op.gt('aggressionLevel', 7)
712
- * ])
713
- * )
714
- * .sortDescending()
715
- * .execute();
716
- *
717
- * // Use like an array with automatic pagination
718
- * for await (const dinosaur of results) {
719
- * console.log(`Processing ${dinosaur.name}`);
720
- * }
721
- *
722
- * // Or convert to array and use array methods
723
- * const allItems = await results.toArray();
724
- * const dangerousOnes = allItems.filter(dino => dino.aggressionLevel > 9);
725
- * const totalCount = allItems.length;
726
- * } catch (error) {
727
- * console.error('Security scan failed:', error);
728
- * }
729
- * ```
730
- *
731
- * @returns A promise that resolves to a ResultGenerator that behaves like an array
732
- */
733
- async execute() {
734
- const directExecutor = () => this.executor(this.keyCondition, this.options);
735
- return new ResultIterator(this, directExecutor);
736
- }
737
- };
738
-
739
- export { QueryBuilder };
740
- //# sourceMappingURL=query-builder.js.map
741
- //# sourceMappingURL=query-builder.js.map