dynamo-command-builder 0.1.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.js ADDED
@@ -0,0 +1,992 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+
5
+ // src/utils/query-request/comparator.ts
6
+ function createKeyConditionExpression(queryRequest) {
7
+ const { sKey, skValue2, skComparator } = queryRequest;
8
+ let keyConditionExpression = "#pk = :pk";
9
+ if (!sKey && !skValue2) return keyConditionExpression;
10
+ switch (getOperatorSymbolByKey(skComparator ?? "=")) {
11
+ case "<":
12
+ keyConditionExpression += " AND #sk < :sk";
13
+ break;
14
+ case ">":
15
+ keyConditionExpression += " AND #sk > :sk";
16
+ break;
17
+ case "<=":
18
+ keyConditionExpression += " AND #sk <= :sk";
19
+ break;
20
+ case ">=":
21
+ keyConditionExpression += " AND #sk >= :sk";
22
+ break;
23
+ case "BEGINS_WITH":
24
+ if (!skValue2) throw new Error("BEGINS_WITH operation requires skValue2.");
25
+ keyConditionExpression += ` AND begins_with(#sk, :skValue2)`;
26
+ break;
27
+ case "BETWEEN":
28
+ if (!sKey || !skValue2) throw new Error("BETWEEN operation requires both sk and skValue2.");
29
+ keyConditionExpression += " AND #sk BETWEEN :sk AND :skValue2";
30
+ break;
31
+ default:
32
+ keyConditionExpression += " AND #sk = :sk";
33
+ break;
34
+ }
35
+ return keyConditionExpression;
36
+ }
37
+ function getOperatorSymbolByKey(operation) {
38
+ switch (operation.toUpperCase()) {
39
+ case "BETWEEN":
40
+ return "BETWEEN";
41
+ case "BEGINS_WITH":
42
+ return "BEGINS_WITH";
43
+ case "GREATER_THAN":
44
+ case ">":
45
+ return ">";
46
+ case "LESS_THAN":
47
+ case "<":
48
+ return "<";
49
+ case "GREATER_THAN_OR_EQUAL":
50
+ case ">=":
51
+ return ">=";
52
+ case "LESS_THAN_OR_EQUAL":
53
+ case "<=":
54
+ return "<=";
55
+ case "EQUAL":
56
+ case "=":
57
+ return "=";
58
+ default:
59
+ throw new Error(`Invalid operation key: ${operation}`);
60
+ }
61
+ }
62
+
63
+ // src/utils/dynamo-value-parse.ts
64
+ function parseDynamoKeyValue(key, keyType) {
65
+ switch (keyType) {
66
+ case "S":
67
+ return key;
68
+ case "N":
69
+ return Number(key);
70
+ case "BOOL":
71
+ return key.toLowerCase() === "true";
72
+ case "NULL":
73
+ return null;
74
+ case "M":
75
+ try {
76
+ return JSON.parse(key);
77
+ } catch {
78
+ throw new Error("Invalid JSON format for partitionKeyType M (Map)");
79
+ }
80
+ case "L":
81
+ try {
82
+ return JSON.parse(key);
83
+ } catch {
84
+ throw new Error("Invalid JSON format for partitionKeyType L (List)");
85
+ }
86
+ case "SS":
87
+ return key.split(",").map((item) => item.trim());
88
+ case "NS":
89
+ return key.split(",").map((item) => Number(item.trim())).filter((num) => !isNaN(num));
90
+ case "BS":
91
+ return key.split(",").map((item) => Buffer.from(item.trim(), "base64"));
92
+ default:
93
+ throw new Error(`Unsupported partitionKeyType: ${keyType}`);
94
+ }
95
+ }
96
+
97
+ // src/utils/expression-attribute-names.ts
98
+ function extractExpAttributeNamesFromExpression(expression) {
99
+ const result = {};
100
+ for (const raw of expression.split(",")) {
101
+ const attr = raw.trim();
102
+ if (attr.startsWith("#")) {
103
+ result[attr] = attr.slice(1);
104
+ }
105
+ }
106
+ return result;
107
+ }
108
+ function extractExpAttributeNamesFromUpdateExp(expression) {
109
+ const result = {};
110
+ const body = expression.replace(/^\s*SET\s+/i, "").trim();
111
+ if (!body) return result;
112
+ const assignments = body.split(",");
113
+ for (const assignment of assignments) {
114
+ const leftSide = assignment.split("=")[0]?.trim();
115
+ if (!leftSide || !leftSide.startsWith("#")) continue;
116
+ result[leftSide] = leftSide.slice(1);
117
+ }
118
+ return result;
119
+ }
120
+
121
+ // src/utils/reserved-keywords.ts
122
+ var RESERVED_KEYWORDS_SET = /* @__PURE__ */ new Set([
123
+ "ABORT",
124
+ "ABSOLUTE",
125
+ "ACTION",
126
+ "ADD",
127
+ "AFTER",
128
+ "AGENT",
129
+ "AGGREGATE",
130
+ "ALL",
131
+ "ALLOCATE",
132
+ "ALTER",
133
+ "ANALYZE",
134
+ "AND",
135
+ "ANY",
136
+ "ARCHIVE",
137
+ "ARE",
138
+ "ARRAY",
139
+ "AS",
140
+ "ASC",
141
+ "ASCII",
142
+ "ASENSITIVE",
143
+ "ASSERTION",
144
+ "ASYMMETRIC",
145
+ "AT",
146
+ "ATOMIC",
147
+ "ATTACH",
148
+ "ATTRIBUTE",
149
+ "AUTH",
150
+ "AUTHORIZATION",
151
+ "AUTHORIZE",
152
+ "AUTO",
153
+ "AVG",
154
+ "BACK",
155
+ "BACKUP",
156
+ "BASE",
157
+ "BATCH",
158
+ "BEFORE",
159
+ "BEGIN",
160
+ "BETWEEN",
161
+ "BIGINT",
162
+ "BINARY",
163
+ "BIT",
164
+ "BLOB",
165
+ "BLOCK",
166
+ "BOOLEAN",
167
+ "BOTH",
168
+ "BREADTH",
169
+ "BUCKET",
170
+ "BULK",
171
+ "BY",
172
+ "BYTE",
173
+ "CALL",
174
+ "CALLED",
175
+ "CALLING",
176
+ "CAPACITY",
177
+ "CASCADE",
178
+ "CASCADED",
179
+ "CASE",
180
+ "CAST",
181
+ "CATALOG",
182
+ "CHAR",
183
+ "CHARACTER",
184
+ "CHECK",
185
+ "CLASS",
186
+ "CLOB",
187
+ "CLOSE",
188
+ "CLUSTER",
189
+ "CLUSTERED",
190
+ "CLUSTERING",
191
+ "CLUSTERS",
192
+ "COALESCE",
193
+ "COLLATE",
194
+ "COLLATION",
195
+ "COLLECTION",
196
+ "COLUMN",
197
+ "COLUMNS",
198
+ "COMBINE",
199
+ "COMMENT",
200
+ "COMMIT",
201
+ "COMPACT",
202
+ "COMPILE",
203
+ "COMPRESS",
204
+ "CONDITION",
205
+ "CONFLICT",
206
+ "CONNECT",
207
+ "CONNECTION",
208
+ "CONSISTENCY",
209
+ "CONSISTENT",
210
+ "CONSTRAINT",
211
+ "CONSTRUCTOR",
212
+ "CONSUMED",
213
+ "CONTINUE",
214
+ "CONVERT",
215
+ "COPY",
216
+ "CORRESPONDING",
217
+ "COUNT",
218
+ "COUNTER",
219
+ "CREATE",
220
+ "CROSS",
221
+ "CSV",
222
+ "CUBE",
223
+ "CURRENT",
224
+ "CURSOR",
225
+ "CYCLE",
226
+ "DATA",
227
+ "DATABASE",
228
+ "DATE",
229
+ "DATETIME",
230
+ "DAY",
231
+ "DEALLOCATE",
232
+ "DEC",
233
+ "DECIMAL",
234
+ "DECLARE",
235
+ "DEFAULT",
236
+ "DEFERRABLE",
237
+ "DEFERRED",
238
+ "DEFINE",
239
+ "DEFINED",
240
+ "DEFINITION",
241
+ "DELETE",
242
+ "DELIMITED",
243
+ "DEPTH",
244
+ "DEREF",
245
+ "DESC",
246
+ "DESCRIBE",
247
+ "DESCRIPTOR",
248
+ "DETACH",
249
+ "DETERMINISTIC",
250
+ "DIAGNOSTICS",
251
+ "DIRECTORIES",
252
+ "DISABLE",
253
+ "DISCONNECT",
254
+ "DISTINCT",
255
+ "DISTRIBUTE",
256
+ "DO",
257
+ "DOMAIN",
258
+ "DOUBLE",
259
+ "DROP",
260
+ "DUMP",
261
+ "DURATION",
262
+ "DYNAMIC",
263
+ "EACH",
264
+ "ELEMENT",
265
+ "ELSE",
266
+ "ELSEIF",
267
+ "EMPTY",
268
+ "ENABLE",
269
+ "END",
270
+ "EQUAL",
271
+ "EQUALS",
272
+ "ERROR",
273
+ "ESCAPE",
274
+ "ESCAPED",
275
+ "EVAL",
276
+ "EVALUATE",
277
+ "EXCEEDED",
278
+ "EXCEPT",
279
+ "EXCEPTIONS",
280
+ "EXCLUSIVE",
281
+ "EXEC",
282
+ "EXECUTE",
283
+ "EXISTS",
284
+ "EXIT",
285
+ "EXPLAIN",
286
+ "EXPLODE",
287
+ "EXPORT",
288
+ "EXPRESSION",
289
+ "EXTENDED",
290
+ "EXTERNAL",
291
+ "EXTRACT",
292
+ "FAIL",
293
+ "FALSE",
294
+ "FAMILY",
295
+ "FETCH",
296
+ "FIELDS",
297
+ "FILE",
298
+ "FILTER",
299
+ "FILTERING",
300
+ "FINAL",
301
+ "FINISH",
302
+ "FIRST",
303
+ "FIXED",
304
+ "FLATTERN",
305
+ "FLOAT",
306
+ "FOR",
307
+ "FORCE",
308
+ "FOREIGN",
309
+ "FORMAT",
310
+ "FORWARD",
311
+ "FOUND",
312
+ "FREE",
313
+ "FROM",
314
+ "FULL",
315
+ "FUNCTION",
316
+ "FUNCTIONS",
317
+ "GENERAL",
318
+ "GENERATE",
319
+ "GET",
320
+ "GLOB",
321
+ "GLOBAL",
322
+ "GO",
323
+ "GOTO",
324
+ "GRANT",
325
+ "GROUP",
326
+ "GROUPING",
327
+ "HANDLER",
328
+ "HASH",
329
+ "HAVE",
330
+ "HAVING",
331
+ "HEAP",
332
+ "HIDDEN",
333
+ "HOLD",
334
+ "HOUR",
335
+ "IDENTIFIED",
336
+ "IDENTITY",
337
+ "IF",
338
+ "IGNORE",
339
+ "IMMEDIATE",
340
+ "IMPORT",
341
+ "IN",
342
+ "INCLUDING",
343
+ "INCLUSIVE",
344
+ "INCREMENT",
345
+ "INDEX",
346
+ "INDEXED",
347
+ "INDEXES",
348
+ "INET",
349
+ "INF",
350
+ "INFINITE",
351
+ "INITIALLY",
352
+ "INLINE",
353
+ "INNER",
354
+ "INNTER",
355
+ "INPUT",
356
+ "INSENSITIVE",
357
+ "INSERT",
358
+ "INSTEAD",
359
+ "INT",
360
+ "INTEGER",
361
+ "INTERSECT",
362
+ "INTERVAL",
363
+ "INTO",
364
+ "INVALIDATE",
365
+ "IS",
366
+ "ISOLATION",
367
+ "ITEM",
368
+ "ITEMS",
369
+ "ITERATE",
370
+ "JOIN",
371
+ "KEY",
372
+ "KEYS",
373
+ "LAG",
374
+ "LANGUAGE",
375
+ "LARGE",
376
+ "LAST",
377
+ "LATERAL",
378
+ "LEAD",
379
+ "LEADING",
380
+ "LEAVE",
381
+ "LEFT",
382
+ "LEVEL",
383
+ "LIKE",
384
+ "LIMIT",
385
+ "LIMITED",
386
+ "LINES",
387
+ "LIST",
388
+ "LOAD",
389
+ "LOCAL",
390
+ "LOCALTIME",
391
+ "LOCALTIMESTAMP",
392
+ "LOCATION",
393
+ "LOCATOR",
394
+ "LOCK",
395
+ "LOCKS",
396
+ "LOG",
397
+ "LOGED",
398
+ "LONG",
399
+ "LOOP",
400
+ "LOW",
401
+ "MAP",
402
+ "MATCH",
403
+ "MATERIALIZED",
404
+ "MAX",
405
+ "MAXLEN",
406
+ "MEMBER",
407
+ "MERGE",
408
+ "METHOD",
409
+ "MIN",
410
+ "MINUS",
411
+ "MINUTE",
412
+ "MISSING",
413
+ "MOD",
414
+ "MODE",
415
+ "MODIFIES",
416
+ "MODIFY",
417
+ "MODULE",
418
+ "MONTH",
419
+ "MULTI",
420
+ "MULTISET",
421
+ "NAME",
422
+ "NAMES",
423
+ "NATIONAL",
424
+ "NATURAL",
425
+ "NCHAR",
426
+ "NCLOB",
427
+ "NEW",
428
+ "NEXT",
429
+ "NO",
430
+ "NONE",
431
+ "NOT",
432
+ "NULL",
433
+ "NULLIF",
434
+ "NUMBER",
435
+ "NUMERIC",
436
+ "OBJECT",
437
+ "OF",
438
+ "OFFLINE",
439
+ "OFFSET",
440
+ "OLD",
441
+ "ON",
442
+ "ONLINE",
443
+ "ONLY",
444
+ "OPAQUE",
445
+ "OPEN",
446
+ "OPERATOR",
447
+ "OPTION",
448
+ "OR",
449
+ "ORDER",
450
+ "ORDINALITY",
451
+ "OUT",
452
+ "OUTER",
453
+ "OUTPUT",
454
+ "OVER",
455
+ "OVERLAPS",
456
+ "OVERRIDE",
457
+ "PARTITION",
458
+ "PARTITIONED",
459
+ "PARTITIONS",
460
+ "PATH",
461
+ "PERCENT",
462
+ "PERCENTILE",
463
+ "PERMISSION",
464
+ "PERMISSIONS",
465
+ "PIPE",
466
+ "PIPELINED",
467
+ "PLAN",
468
+ "POOL",
469
+ "POSITION",
470
+ "PRECISION",
471
+ "PREPARE",
472
+ "PRESERVE",
473
+ "PRIMARY",
474
+ "PRIOR",
475
+ "PRIVATE",
476
+ "PRIVILEGES",
477
+ "PROCEDURE",
478
+ "PROCESSED",
479
+ "PROJECT",
480
+ "PROJECTION",
481
+ "PROPERTY",
482
+ "PROVISIONING",
483
+ "PUBLIC",
484
+ "PUT",
485
+ "QUERY",
486
+ "QUIT",
487
+ "QUORUM",
488
+ "RAISE",
489
+ "RANGE",
490
+ "RANK",
491
+ "RAW",
492
+ "READ",
493
+ "READS",
494
+ "REAL",
495
+ "REBUILD",
496
+ "RECORD",
497
+ "RECURSIVE",
498
+ "REDUCE",
499
+ "REF",
500
+ "REFERENCE",
501
+ "REFERENCES",
502
+ "REFERENCING",
503
+ "REGEXP",
504
+ "REGION",
505
+ "REINDEX",
506
+ "RELATIVE",
507
+ "RELEASE",
508
+ "REMA",
509
+ "REMAINDER",
510
+ "RENAME",
511
+ "REPEAT",
512
+ "REPLACE",
513
+ "REQUEST",
514
+ "RESET",
515
+ "RESIGNAL",
516
+ "RESOURCE",
517
+ "RESPONSE",
518
+ "RESTORE",
519
+ "RESTRICT",
520
+ "RESULT",
521
+ "RETURN",
522
+ "RETURNING",
523
+ "REVEAL",
524
+ "REVERSE",
525
+ "REVOKE",
526
+ "RIGHT",
527
+ "ROLE",
528
+ "ROLES",
529
+ "ROLLBACK",
530
+ "ROLLUP",
531
+ "ROUTINE",
532
+ "ROW",
533
+ "ROWS",
534
+ "RULE",
535
+ "RULES",
536
+ "SAMPLE",
537
+ "SATISFIES",
538
+ "SAVE",
539
+ "SAVEPOINT",
540
+ "SCAN",
541
+ "SCHEMA",
542
+ "SCOPE",
543
+ "SCROLL",
544
+ "SEARCH",
545
+ "SECOND",
546
+ "SECTION",
547
+ "SEGMENT",
548
+ "SEGMENTS",
549
+ "SELECT",
550
+ "SELF",
551
+ "SEMI",
552
+ "SENSITIVE",
553
+ "SEPARATE",
554
+ "SEQUENCE",
555
+ "SERIALIZABLE",
556
+ "SESSION",
557
+ "SET",
558
+ "SETS",
559
+ "SHARD",
560
+ "SHARE",
561
+ "SHARED",
562
+ "SHORT",
563
+ "SHOW",
564
+ "SIGNAL",
565
+ "SIMILAR",
566
+ "SIZE",
567
+ "SKEWED",
568
+ "SMALLINT",
569
+ "SNAPSHOT",
570
+ "SOME",
571
+ "SOURCE",
572
+ "SPACE",
573
+ "SPLIT",
574
+ "SQL",
575
+ "SQLCODE",
576
+ "SQLERROR",
577
+ "SQLEXCEPTION",
578
+ "SQLSTATE",
579
+ "SQLWARNING",
580
+ "START",
581
+ "STATE",
582
+ "STATIC",
583
+ "STATUS",
584
+ "STORAGE",
585
+ "STORE",
586
+ "STORED",
587
+ "STREAM",
588
+ "STRING",
589
+ "STRUCT",
590
+ "STYLE",
591
+ "SUB",
592
+ "SUBMULTISET",
593
+ "SUBPARTITION",
594
+ "SUBSTRING",
595
+ "SUBTYPE",
596
+ "SUM",
597
+ "SUPER",
598
+ "SYMMETRIC",
599
+ "SYNONYM",
600
+ "SYSTEM",
601
+ "TABLE",
602
+ "TABLESAMPLE",
603
+ "TEMP",
604
+ "TEMPORARY",
605
+ "TERMINATED",
606
+ "TEXT",
607
+ "THAN",
608
+ "THEN",
609
+ "THROUGHPUT",
610
+ "TIME",
611
+ "TIMESTAMP",
612
+ "TIMEZONE",
613
+ "TINYINT",
614
+ "TO",
615
+ "TOKEN",
616
+ "TOTAL",
617
+ "TOUCH",
618
+ "TRAILING",
619
+ "TRAN",
620
+ "TRANSACTION",
621
+ "TRANSFORM",
622
+ "TRANSLATE",
623
+ "TRANSLATION",
624
+ "TREAT",
625
+ "TRIGGER",
626
+ "TRIM",
627
+ "TRUE",
628
+ "TRUNCATE",
629
+ "TTL",
630
+ "TUPLE",
631
+ "TYPE",
632
+ "UNDER",
633
+ "UNDO",
634
+ "UNION",
635
+ "UNIQUE",
636
+ "UNIT",
637
+ "UNKNOWN",
638
+ "UNLOGGED",
639
+ "UNNEST",
640
+ "UNPROCESSED",
641
+ "UNSIGNED",
642
+ "UNTIL",
643
+ "UPDATE",
644
+ "UPPER",
645
+ "URL",
646
+ "USAGE",
647
+ "USE",
648
+ "USING",
649
+ "UUID",
650
+ "VACUUM",
651
+ "VALUE",
652
+ "VALUED",
653
+ "VALUES",
654
+ "VARCHAR",
655
+ "VARIABLE",
656
+ "VARIANCE",
657
+ "VARINT",
658
+ "VARYING",
659
+ "VIEW",
660
+ "VIEWS",
661
+ "VIRTUAL",
662
+ "VOID",
663
+ "WAIT",
664
+ "WHEN",
665
+ "WHENEVER",
666
+ "WHERE",
667
+ "WHILE",
668
+ "WINDOW",
669
+ "WITH",
670
+ "WITHIN",
671
+ "WITHOUT",
672
+ "WORK",
673
+ "WRAPPED",
674
+ "WRITE",
675
+ "YEAR",
676
+ "ZONE"
677
+ ]);
678
+ var UPDATE_RESERVED_REGEX = new RegExp(`\\b(?:${[...RESERVED_KEYWORDS_SET].join("|")})\\b(?=\\s*=)`, "gi");
679
+ function replaceReservedKeywordsFromUpdateExp(updateExpression) {
680
+ return updateExpression.replace(UPDATE_RESERVED_REGEX, (match) => `#${match}`);
681
+ }
682
+ function replaceReservedKeywordsFromProjection(projection) {
683
+ return projection.split(",").map((item) => {
684
+ const trimmed = item.trim();
685
+ if (!trimmed) return trimmed;
686
+ return RESERVED_KEYWORDS_SET.has(trimmed.toUpperCase()) ? `#${trimmed}` : trimmed;
687
+ }).join(", ");
688
+ }
689
+
690
+ // src/command/build-get-command.ts
691
+ function buildGetCommandInput(input) {
692
+ const {
693
+ tableName: TableName,
694
+ // The name of the DynamoDB table
695
+ key: Key,
696
+ // The key of the item to retrieve
697
+ projectionExpression,
698
+ // Optional: Specifies attributes to retrieve
699
+ expressionAttributeNames: extraExpressionAttributesNames,
700
+ // Additional expression attribute names
701
+ consistentRead: ConsistentRead,
702
+ // Optional: Specifies whether to use strongly consistent reads
703
+ returnConsumedCapacity: ReturnConsumedCapacity
704
+ // Optional: Determines whether to return consumed capacity
705
+ } = input;
706
+ const ProjectionExpression = projectionExpression ? replaceReservedKeywordsFromProjection(projectionExpression) : void 0;
707
+ const commandInput = {
708
+ TableName,
709
+ Key,
710
+ ProjectionExpression,
711
+ ConsistentRead,
712
+ ReturnConsumedCapacity
713
+ };
714
+ const expressionAttributeNames = {
715
+ ...ProjectionExpression ? extractExpAttributeNamesFromExpression(ProjectionExpression) : {},
716
+ ...extraExpressionAttributesNames ?? {}
717
+ };
718
+ if (Object.keys(expressionAttributeNames).length > 0) {
719
+ commandInput.ExpressionAttributeNames = expressionAttributeNames;
720
+ }
721
+ return commandInput;
722
+ }
723
+
724
+ // src/command/build-put-command.ts
725
+ function buildPutCommandInput(input) {
726
+ const {
727
+ tableName,
728
+ item,
729
+ conditionExpression,
730
+ expressionAttributeNames,
731
+ expressionAttributeValues,
732
+ returnValues = "NONE",
733
+ returnConsumedCapacity,
734
+ returnItemCollectionMetrics
735
+ } = input;
736
+ const commandInput = {
737
+ TableName: tableName,
738
+ Item: item,
739
+ ConditionExpression: conditionExpression,
740
+ ReturnValues: returnValues,
741
+ ReturnConsumedCapacity: returnConsumedCapacity,
742
+ ReturnItemCollectionMetrics: returnItemCollectionMetrics
743
+ };
744
+ const hasCondition = !!conditionExpression;
745
+ const hasProvidedNames = !!expressionAttributeNames && Object.keys(expressionAttributeNames).length > 0;
746
+ if (hasCondition || hasProvidedNames) {
747
+ const generatedNames = hasCondition ? extractExpAttributeNamesFromExpression(conditionExpression) : {};
748
+ const mergedNames = hasProvidedNames ? { ...generatedNames, ...expressionAttributeNames } : generatedNames;
749
+ if (Object.keys(mergedNames).length > 0) {
750
+ commandInput.ExpressionAttributeNames = mergedNames;
751
+ }
752
+ }
753
+ if (expressionAttributeValues && Object.keys(expressionAttributeValues).length > 0) {
754
+ commandInput.ExpressionAttributeValues = expressionAttributeValues;
755
+ }
756
+ return commandInput;
757
+ }
758
+
759
+ // src/command/build-query-command.ts
760
+ function buildQueryCommandInput(input) {
761
+ const { queryRequest } = input;
762
+ const KeyConditionExpression = createKeyConditionExpression(queryRequest);
763
+ const ProjectionExpression = input.projectionExpression ? replaceReservedKeywordsFromProjection(input.projectionExpression) : void 0;
764
+ const ExpressionAttributeNames = {
765
+ "#pk": queryRequest.pKeyProp,
766
+ ...queryRequest.sKeyProp && { "#sk": queryRequest.sKeyProp },
767
+ ...ProjectionExpression && extractExpAttributeNamesFromExpression(ProjectionExpression),
768
+ ...input.extraExpAttributeNames || {}
769
+ };
770
+ const ExpressionAttributeValues = {
771
+ ":pk": parseDynamoKeyValue(queryRequest.pKey, queryRequest.pKeyType),
772
+ ...queryRequest.sKey && {
773
+ ":sk": parseDynamoKeyValue(queryRequest.sKey, queryRequest.sKeyType ?? `S`)
774
+ },
775
+ ...queryRequest.skValue2 && {
776
+ ":skValue2": parseDynamoKeyValue(queryRequest.skValue2, queryRequest.sKeyType ?? `S`)
777
+ },
778
+ ...input.extraExpAttributeValues || {}
779
+ };
780
+ const commandInput = {
781
+ TableName: input.tableName,
782
+ IndexName: queryRequest.indexName,
783
+ KeyConditionExpression,
784
+ FilterExpression: input.filterExpression,
785
+ ExpressionAttributeNames,
786
+ ExpressionAttributeValues,
787
+ ProjectionExpression,
788
+ ScanIndexForward: queryRequest.sorting !== void 0 ? queryRequest.sorting.toUpperCase() === "ASC" : input.scanIndexForward,
789
+ ReturnConsumedCapacity: input.returnConsumedCapacity,
790
+ ExclusiveStartKey: queryRequest.lastEvaluatedKey,
791
+ Limit: queryRequest.limit
792
+ };
793
+ return commandInput;
794
+ }
795
+
796
+ // src/command/build-update-command.ts
797
+ function buildUpdateCommandInput(input) {
798
+ const commandInput = {
799
+ TableName: input.tableName,
800
+ Key: input.key,
801
+ ConditionExpression: input.conditionExpression,
802
+ ReturnValues: input.returnValues ?? "NONE",
803
+ ReturnConsumedCapacity: input.returnConsumedCapacity,
804
+ ReturnItemCollectionMetrics: input.returnItemCollectionMetrics
805
+ };
806
+ const names = {};
807
+ const values = {};
808
+ if (input.expressionAttributeNames) {
809
+ for (const key in input.expressionAttributeNames) {
810
+ names[key] = input.expressionAttributeNames[key];
811
+ }
812
+ }
813
+ if (input.extraExpAttributeNames) {
814
+ for (const key in input.extraExpAttributeNames) {
815
+ names[key] = input.extraExpAttributeNames[key];
816
+ }
817
+ }
818
+ if (input.expressionAttributeValues) {
819
+ for (const key in input.expressionAttributeValues) {
820
+ values[key] = input.expressionAttributeValues[key];
821
+ }
822
+ }
823
+ if (input.extraExpAttributeValues) {
824
+ for (const key in input.extraExpAttributeValues) {
825
+ values[key] = input.extraExpAttributeValues[key];
826
+ }
827
+ }
828
+ let updateExpression = input.updateExpression;
829
+ if (updateExpression) {
830
+ if (Object.keys(names).length > 0) {
831
+ commandInput.ExpressionAttributeNames = names;
832
+ }
833
+ if (Object.keys(values).length > 0) {
834
+ commandInput.ExpressionAttributeValues = values;
835
+ }
836
+ commandInput.UpdateExpression = updateExpression;
837
+ return commandInput;
838
+ }
839
+ const item = input.item;
840
+ if (!item || Object.keys(item).length === 0) {
841
+ throw new Error("Either updateExpression or item with at least one field must be provided.");
842
+ }
843
+ const updateParts = [];
844
+ const itemRecord = item;
845
+ for (const field in itemRecord) {
846
+ const value = itemRecord[field];
847
+ let valueKey = `:${field}`;
848
+ if (values[valueKey] !== void 0) {
849
+ let counter = 1;
850
+ let candidate;
851
+ do {
852
+ candidate = `:${field}_update_${counter++}`;
853
+ } while (values[candidate] !== void 0);
854
+ valueKey = candidate;
855
+ }
856
+ updateParts.push(`${field} = ${valueKey}`);
857
+ values[valueKey] = value;
858
+ }
859
+ const rawExpression = `SET ${updateParts.join(", ")}`;
860
+ updateExpression = replaceReservedKeywordsFromUpdateExp(rawExpression);
861
+ const autoNames = extractExpAttributeNamesFromUpdateExp(updateExpression);
862
+ for (const key in autoNames) {
863
+ if (!(key in names)) {
864
+ names[key] = autoNames[key];
865
+ }
866
+ }
867
+ if (Object.keys(names).length > 0) {
868
+ commandInput.ExpressionAttributeNames = names;
869
+ }
870
+ if (Object.keys(values).length > 0) {
871
+ commandInput.ExpressionAttributeValues = values;
872
+ }
873
+ commandInput.UpdateExpression = updateExpression;
874
+ return commandInput;
875
+ }
876
+ var dynamoQueryRequestSch = zod.z.object({
877
+ pKey: zod.z.string(),
878
+ pKeyType: zod.z.string(),
879
+ pKeyProp: zod.z.string(),
880
+ sKey: zod.z.string().optional(),
881
+ sKeyType: zod.z.string().optional(),
882
+ sKeyProp: zod.z.string().optional(),
883
+ skValue2: zod.z.string().optional(),
884
+ skValue2Type: zod.z.string().optional(),
885
+ skComparator: zod.z.string().optional(),
886
+ indexName: zod.z.string().optional(),
887
+ limit: zod.z.number().optional(),
888
+ lastEvaluatedKey: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
889
+ sorting: zod.z.string().optional()
890
+ }).superRefine((data, ctx) => {
891
+ if (data.skComparator) {
892
+ if (!data.sKey) {
893
+ ctx.addIssue({
894
+ path: ["sKey"],
895
+ message: "sKey is required when skComparator is present",
896
+ code: "invalid_type",
897
+ expected: "string",
898
+ received: typeof data.sKey
899
+ });
900
+ }
901
+ if (!data.sKeyProp) {
902
+ ctx.addIssue({
903
+ path: ["sKeyProp"],
904
+ message: "sKeyProp is required when skComparator is present",
905
+ code: "invalid_type",
906
+ expected: "string",
907
+ received: typeof data.sKeyProp
908
+ });
909
+ }
910
+ if (!data.sKeyType) {
911
+ ctx.addIssue({
912
+ path: ["sKeyType"],
913
+ message: "sKeyType is required when skComparator is present",
914
+ code: "invalid_type",
915
+ expected: "string",
916
+ received: typeof data.sKeyType
917
+ });
918
+ }
919
+ }
920
+ });
921
+
922
+ // src/types/command.types.ts
923
+ var returnConsumedCapacityOptionsSchema = zod.z.enum(["INDEXES", "TOTAL", "NONE"]);
924
+ var returnItemCollectionMetricsOptionsSchema = zod.z.enum(["SIZE", "NONE"]);
925
+ var customGetCommandInputSchema = zod.z.object({
926
+ tableName: zod.z.string(),
927
+ key: zod.z.record(zod.z.string(), zod.z.unknown()),
928
+ projectionExpression: zod.z.string().optional(),
929
+ expressionAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
930
+ consistentRead: zod.z.boolean().optional(),
931
+ returnConsumedCapacity: returnConsumedCapacityOptionsSchema.optional()
932
+ });
933
+ var customQueryCommandInputSchema = zod.z.object({
934
+ tableName: zod.z.string(),
935
+ queryRequest: dynamoQueryRequestSch,
936
+ keyConditionExpression: zod.z.string().optional(),
937
+ filterExpression: zod.z.string().optional(),
938
+ expressionAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
939
+ expressionAttributeValues: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
940
+ extraExpAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
941
+ extraExpAttributeValues: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
942
+ projectionExpression: zod.z.string().optional(),
943
+ scanIndexForward: zod.z.boolean().optional(),
944
+ returnConsumedCapacity: returnConsumedCapacityOptionsSchema.optional()
945
+ });
946
+ var customPutCommandInputSchema = zod.z.object({
947
+ tableName: zod.z.string(),
948
+ item: zod.z.record(zod.z.string(), zod.z.unknown()),
949
+ conditionExpression: zod.z.string().optional(),
950
+ expressionAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
951
+ expressionAttributeValues: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
952
+ returnValues: zod.z.enum(["NONE", "ALL_OLD", "UPDATED_OLD", "ALL_NEW", "UPDATED_NEW"]).optional(),
953
+ returnConsumedCapacity: returnConsumedCapacityOptionsSchema.optional(),
954
+ returnItemCollectionMetrics: returnItemCollectionMetricsOptionsSchema.optional()
955
+ });
956
+ var customUpdateCommandInputSchema = zod.z.object({
957
+ tableName: zod.z.string(),
958
+ key: zod.z.record(zod.z.string(), zod.z.unknown()),
959
+ item: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
960
+ updateExpression: zod.z.string().optional(),
961
+ conditionExpression: zod.z.string().optional(),
962
+ expressionAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
963
+ // Overwrite ExpressionAttributeNames
964
+ expressionAttributeValues: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
965
+ // Overwrite ExpressionAttributeValues
966
+ extraExpAttributeNames: zod.z.record(zod.z.string(), zod.z.string()).optional(),
967
+ // Add ExpressionAttributeNames without overwriting existing ones
968
+ extraExpAttributeValues: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
969
+ // Add ExpressionAttributeValues without overwriting existing ones
970
+ returnValues: zod.z.enum(["NONE", "ALL_OLD", "UPDATED_OLD", "ALL_NEW", "UPDATED_NEW"]).optional(),
971
+ returnConsumedCapacity: returnConsumedCapacityOptionsSchema.optional(),
972
+ returnItemCollectionMetrics: returnItemCollectionMetricsOptionsSchema.optional()
973
+ });
974
+
975
+ exports.RESERVED_KEYWORDS_SET = RESERVED_KEYWORDS_SET;
976
+ exports.buildGetCommandInput = buildGetCommandInput;
977
+ exports.buildPutCommandInput = buildPutCommandInput;
978
+ exports.buildQueryCommandInput = buildQueryCommandInput;
979
+ exports.buildUpdateCommandInput = buildUpdateCommandInput;
980
+ exports.createKeyConditionExpression = createKeyConditionExpression;
981
+ exports.customGetCommandInputSchema = customGetCommandInputSchema;
982
+ exports.customPutCommandInputSchema = customPutCommandInputSchema;
983
+ exports.customQueryCommandInputSchema = customQueryCommandInputSchema;
984
+ exports.customUpdateCommandInputSchema = customUpdateCommandInputSchema;
985
+ exports.dynamoQueryRequestSch = dynamoQueryRequestSch;
986
+ exports.extractExpAttributeNamesFromExpression = extractExpAttributeNamesFromExpression;
987
+ exports.extractExpAttributeNamesFromUpdateExp = extractExpAttributeNamesFromUpdateExp;
988
+ exports.parseDynamoKeyValue = parseDynamoKeyValue;
989
+ exports.replaceReservedKeywordsFromProjection = replaceReservedKeywordsFromProjection;
990
+ exports.replaceReservedKeywordsFromUpdateExp = replaceReservedKeywordsFromUpdateExp;
991
+ //# sourceMappingURL=index.js.map
992
+ //# sourceMappingURL=index.js.map