dyno-table 1.0.0-alpha.1 → 1.0.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 (96) hide show
  1. package/README.md +751 -172
  2. package/dist/builder-types-C_PDZhnP.d.ts +118 -0
  3. package/dist/builder-types-DtwbqMeF.d.cts +118 -0
  4. package/dist/builders/condition-check-builder.cjs +394 -0
  5. package/dist/builders/condition-check-builder.cjs.map +1 -0
  6. package/dist/builders/condition-check-builder.d.cts +157 -0
  7. package/dist/builders/condition-check-builder.d.ts +157 -0
  8. package/dist/builders/condition-check-builder.js +392 -0
  9. package/dist/builders/condition-check-builder.js.map +1 -0
  10. package/dist/builders/delete-builder.cjs +405 -0
  11. package/dist/builders/delete-builder.cjs.map +1 -0
  12. package/dist/builders/delete-builder.d.cts +166 -0
  13. package/dist/builders/delete-builder.d.ts +166 -0
  14. package/dist/builders/delete-builder.js +403 -0
  15. package/dist/builders/delete-builder.js.map +1 -0
  16. package/dist/builders/paginator.cjs +199 -0
  17. package/dist/builders/paginator.cjs.map +1 -0
  18. package/dist/builders/paginator.d.cts +179 -0
  19. package/dist/builders/paginator.d.ts +179 -0
  20. package/dist/builders/paginator.js +197 -0
  21. package/dist/builders/paginator.js.map +1 -0
  22. package/dist/builders/put-builder.cjs +476 -0
  23. package/dist/builders/put-builder.cjs.map +1 -0
  24. package/dist/builders/put-builder.d.cts +274 -0
  25. package/dist/builders/put-builder.d.ts +274 -0
  26. package/dist/builders/put-builder.js +474 -0
  27. package/dist/builders/put-builder.js.map +1 -0
  28. package/dist/builders/query-builder.cjs +674 -0
  29. package/dist/builders/query-builder.cjs.map +1 -0
  30. package/dist/builders/query-builder.d.cts +6 -0
  31. package/dist/builders/query-builder.d.ts +6 -0
  32. package/dist/builders/query-builder.js +672 -0
  33. package/dist/builders/query-builder.js.map +1 -0
  34. package/dist/builders/transaction-builder.cjs +894 -0
  35. package/dist/builders/transaction-builder.cjs.map +1 -0
  36. package/dist/builders/transaction-builder.d.cts +511 -0
  37. package/dist/builders/transaction-builder.d.ts +511 -0
  38. package/dist/builders/transaction-builder.js +892 -0
  39. package/dist/builders/transaction-builder.js.map +1 -0
  40. package/dist/builders/update-builder.cjs +627 -0
  41. package/dist/builders/update-builder.cjs.map +1 -0
  42. package/dist/builders/update-builder.d.cts +365 -0
  43. package/dist/builders/update-builder.d.ts +365 -0
  44. package/dist/builders/update-builder.js +625 -0
  45. package/dist/builders/update-builder.js.map +1 -0
  46. package/dist/conditions--ld9a78i.d.ts +331 -0
  47. package/dist/conditions-ChhQWd6z.d.cts +331 -0
  48. package/dist/conditions.cjs +59 -0
  49. package/dist/conditions.cjs.map +1 -0
  50. package/dist/conditions.d.cts +3 -0
  51. package/dist/conditions.d.ts +3 -0
  52. package/dist/conditions.js +43 -0
  53. package/dist/conditions.js.map +1 -0
  54. package/dist/entity.cjs +228 -0
  55. package/dist/entity.cjs.map +1 -0
  56. package/dist/entity.d.cts +149 -0
  57. package/dist/entity.d.ts +149 -0
  58. package/dist/entity.js +224 -0
  59. package/dist/entity.js.map +1 -0
  60. package/dist/query-builder-Csror9Iu.d.ts +507 -0
  61. package/dist/query-builder-D2FM9rsu.d.cts +507 -0
  62. package/dist/standard-schema.cjs +4 -0
  63. package/dist/standard-schema.cjs.map +1 -0
  64. package/dist/standard-schema.d.cts +57 -0
  65. package/dist/standard-schema.d.ts +57 -0
  66. package/dist/standard-schema.js +3 -0
  67. package/dist/standard-schema.js.map +1 -0
  68. package/dist/table-BEhBPy2G.d.cts +364 -0
  69. package/dist/table-BW3cmUqr.d.ts +364 -0
  70. package/dist/{index.js → table.cjs} +88 -127
  71. package/dist/table.cjs.map +1 -0
  72. package/dist/table.d.cts +12 -0
  73. package/dist/table.d.ts +12 -0
  74. package/dist/{index.cjs → table.js} +86 -176
  75. package/dist/table.js.map +1 -0
  76. package/dist/types.cjs +4 -0
  77. package/dist/types.cjs.map +1 -0
  78. package/dist/types.d.cts +22 -0
  79. package/dist/types.d.ts +22 -0
  80. package/dist/types.js +3 -0
  81. package/dist/types.js.map +1 -0
  82. package/dist/utils/partition-key-template.cjs +19 -0
  83. package/dist/utils/partition-key-template.cjs.map +1 -0
  84. package/dist/utils/partition-key-template.d.cts +32 -0
  85. package/dist/utils/partition-key-template.d.ts +32 -0
  86. package/dist/utils/partition-key-template.js +17 -0
  87. package/dist/utils/partition-key-template.js.map +1 -0
  88. package/dist/utils/sort-key-template.cjs +19 -0
  89. package/dist/utils/sort-key-template.cjs.map +1 -0
  90. package/dist/utils/sort-key-template.d.cts +35 -0
  91. package/dist/utils/sort-key-template.d.ts +35 -0
  92. package/dist/utils/sort-key-template.js +17 -0
  93. package/dist/utils/sort-key-template.js.map +1 -0
  94. package/package.json +77 -7
  95. package/dist/index.d.cts +0 -2971
  96. package/dist/index.d.ts +0 -2971
@@ -0,0 +1,118 @@
1
+ import { DynamoItem, TableConfig } from './types.js';
2
+
3
+ /**
4
+ * Interface for DynamoDB command objects that can contain expressions
5
+ */
6
+ interface DynamoCommandWithExpressions {
7
+ conditionExpression?: string;
8
+ updateExpression?: string;
9
+ filterExpression?: string;
10
+ keyConditionExpression?: string;
11
+ projectionExpression?: string;
12
+ expressionAttributeNames?: Record<string, string>;
13
+ expressionAttributeValues?: Record<string, unknown>;
14
+ [key: string]: unknown;
15
+ }
16
+
17
+ interface DeleteCommandParams extends DynamoCommandWithExpressions {
18
+ tableName: string;
19
+ key: Record<string, unknown>;
20
+ conditionExpression?: string;
21
+ expressionAttributeNames?: Record<string, string>;
22
+ expressionAttributeValues?: DynamoItem;
23
+ returnValues?: "ALL_OLD";
24
+ }
25
+ /**
26
+ * Parameters for the DynamoDB put command.
27
+ *
28
+ * These parameters are used when executing the operation against DynamoDB.
29
+ *
30
+ * The `returnValues` property can be:
31
+ * - `"ALL_OLD"`: Return the attributes of the item as they were before the operation
32
+ * - `"NONE"`: Return nothing
33
+ * - `"CONSISTENT"`: Triggers a GET operation after the put to retrieve the updated item state
34
+ */
35
+ interface PutCommandParams extends DynamoCommandWithExpressions {
36
+ tableName: string;
37
+ item: DynamoItem;
38
+ conditionExpression?: string;
39
+ expressionAttributeNames?: Record<string, string>;
40
+ expressionAttributeValues?: Record<string, unknown>;
41
+ returnValues?: "ALL_OLD" | "NONE" | "CONSISTENT";
42
+ }
43
+ /**
44
+ * Parameters for the DynamoDB update command.
45
+ * These parameters are used when executing the operation against DynamoDB.
46
+ */
47
+ interface UpdateCommandParams extends DynamoCommandWithExpressions {
48
+ /** The name of the DynamoDB table */
49
+ tableName: string;
50
+ /** The primary key of the item to update */
51
+ key: Record<string, unknown>;
52
+ /** The update expression (SET, REMOVE, ADD, DELETE clauses) */
53
+ updateExpression: string;
54
+ /** Optional condition expression that must be satisfied */
55
+ conditionExpression?: string;
56
+ /** Map of expression attribute name placeholders to actual names */
57
+ expressionAttributeNames?: Record<string, string>;
58
+ /** Map of expression attribute value placeholders to actual values */
59
+ expressionAttributeValues?: DynamoItem;
60
+ /** Which item attributes to include in the response */
61
+ returnValues?: "ALL_NEW" | "UPDATED_NEW" | "ALL_OLD" | "UPDATED_OLD" | "NONE";
62
+ }
63
+ interface ConditionCheckCommandParams extends DynamoCommandWithExpressions {
64
+ tableName: string;
65
+ key: Record<string, unknown>;
66
+ conditionExpression: string;
67
+ expressionAttributeNames?: Record<string, string>;
68
+ expressionAttributeValues?: DynamoItem;
69
+ }
70
+ /**
71
+ * Base interface for all builder classes that support pagination
72
+ * to be used by Paginator without creating circular dependencies.
73
+ */
74
+ interface BaseBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig, B = unknown> {
75
+ clone(): B;
76
+ limit(limit: number): B;
77
+ getLimit(): number | undefined;
78
+ startFrom(lastEvaluatedKey: DynamoItem): B;
79
+ execute(): Promise<{
80
+ items: T[];
81
+ lastEvaluatedKey?: DynamoItem;
82
+ }>;
83
+ }
84
+ /**
85
+ * Interface for the QueryBuilder class to be used by Paginator
86
+ * without creating a circular dependency.
87
+ */
88
+ interface QueryBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, QueryBuilderInterface<T, TConfig>> {
89
+ }
90
+ /**
91
+ * Interface for the ScanBuilder class to be used by Paginator
92
+ * without creating a circular dependency.
93
+ */
94
+ interface ScanBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, ScanBuilderInterface<T, TConfig>> {
95
+ }
96
+ /**
97
+ * Interface for the FilterBuilder class to be used by Paginator
98
+ * without creating a circular dependency.
99
+ */
100
+ interface FilterBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, FilterBuilderInterface<T, TConfig>> {
101
+ }
102
+ /**
103
+ * Represents the result of a single page query operation.
104
+ * This interface provides all necessary information about the current page
105
+ * and the availability of subsequent pages.
106
+ */
107
+ interface PaginationResult<T> {
108
+ /** The items (dinosaurs, habitats, etc.) retrieved for the current page */
109
+ items: T[];
110
+ /** DynamoDB's last evaluated key, used internally for pagination */
111
+ lastEvaluatedKey?: DynamoItem;
112
+ /** Indicates whether there are more pages available */
113
+ hasNextPage: boolean;
114
+ /** The current page number (1-indexed) */
115
+ page: number;
116
+ }
117
+
118
+ export type { ConditionCheckCommandParams as C, DeleteCommandParams as D, FilterBuilderInterface as F, PaginationResult as P, QueryBuilderInterface as Q, ScanBuilderInterface as S, UpdateCommandParams as U, PutCommandParams as a };
@@ -0,0 +1,118 @@
1
+ import { DynamoItem, TableConfig } from './types.cjs';
2
+
3
+ /**
4
+ * Interface for DynamoDB command objects that can contain expressions
5
+ */
6
+ interface DynamoCommandWithExpressions {
7
+ conditionExpression?: string;
8
+ updateExpression?: string;
9
+ filterExpression?: string;
10
+ keyConditionExpression?: string;
11
+ projectionExpression?: string;
12
+ expressionAttributeNames?: Record<string, string>;
13
+ expressionAttributeValues?: Record<string, unknown>;
14
+ [key: string]: unknown;
15
+ }
16
+
17
+ interface DeleteCommandParams extends DynamoCommandWithExpressions {
18
+ tableName: string;
19
+ key: Record<string, unknown>;
20
+ conditionExpression?: string;
21
+ expressionAttributeNames?: Record<string, string>;
22
+ expressionAttributeValues?: DynamoItem;
23
+ returnValues?: "ALL_OLD";
24
+ }
25
+ /**
26
+ * Parameters for the DynamoDB put command.
27
+ *
28
+ * These parameters are used when executing the operation against DynamoDB.
29
+ *
30
+ * The `returnValues` property can be:
31
+ * - `"ALL_OLD"`: Return the attributes of the item as they were before the operation
32
+ * - `"NONE"`: Return nothing
33
+ * - `"CONSISTENT"`: Triggers a GET operation after the put to retrieve the updated item state
34
+ */
35
+ interface PutCommandParams extends DynamoCommandWithExpressions {
36
+ tableName: string;
37
+ item: DynamoItem;
38
+ conditionExpression?: string;
39
+ expressionAttributeNames?: Record<string, string>;
40
+ expressionAttributeValues?: Record<string, unknown>;
41
+ returnValues?: "ALL_OLD" | "NONE" | "CONSISTENT";
42
+ }
43
+ /**
44
+ * Parameters for the DynamoDB update command.
45
+ * These parameters are used when executing the operation against DynamoDB.
46
+ */
47
+ interface UpdateCommandParams extends DynamoCommandWithExpressions {
48
+ /** The name of the DynamoDB table */
49
+ tableName: string;
50
+ /** The primary key of the item to update */
51
+ key: Record<string, unknown>;
52
+ /** The update expression (SET, REMOVE, ADD, DELETE clauses) */
53
+ updateExpression: string;
54
+ /** Optional condition expression that must be satisfied */
55
+ conditionExpression?: string;
56
+ /** Map of expression attribute name placeholders to actual names */
57
+ expressionAttributeNames?: Record<string, string>;
58
+ /** Map of expression attribute value placeholders to actual values */
59
+ expressionAttributeValues?: DynamoItem;
60
+ /** Which item attributes to include in the response */
61
+ returnValues?: "ALL_NEW" | "UPDATED_NEW" | "ALL_OLD" | "UPDATED_OLD" | "NONE";
62
+ }
63
+ interface ConditionCheckCommandParams extends DynamoCommandWithExpressions {
64
+ tableName: string;
65
+ key: Record<string, unknown>;
66
+ conditionExpression: string;
67
+ expressionAttributeNames?: Record<string, string>;
68
+ expressionAttributeValues?: DynamoItem;
69
+ }
70
+ /**
71
+ * Base interface for all builder classes that support pagination
72
+ * to be used by Paginator without creating circular dependencies.
73
+ */
74
+ interface BaseBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig, B = unknown> {
75
+ clone(): B;
76
+ limit(limit: number): B;
77
+ getLimit(): number | undefined;
78
+ startFrom(lastEvaluatedKey: DynamoItem): B;
79
+ execute(): Promise<{
80
+ items: T[];
81
+ lastEvaluatedKey?: DynamoItem;
82
+ }>;
83
+ }
84
+ /**
85
+ * Interface for the QueryBuilder class to be used by Paginator
86
+ * without creating a circular dependency.
87
+ */
88
+ interface QueryBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, QueryBuilderInterface<T, TConfig>> {
89
+ }
90
+ /**
91
+ * Interface for the ScanBuilder class to be used by Paginator
92
+ * without creating a circular dependency.
93
+ */
94
+ interface ScanBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, ScanBuilderInterface<T, TConfig>> {
95
+ }
96
+ /**
97
+ * Interface for the FilterBuilder class to be used by Paginator
98
+ * without creating a circular dependency.
99
+ */
100
+ interface FilterBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, FilterBuilderInterface<T, TConfig>> {
101
+ }
102
+ /**
103
+ * Represents the result of a single page query operation.
104
+ * This interface provides all necessary information about the current page
105
+ * and the availability of subsequent pages.
106
+ */
107
+ interface PaginationResult<T> {
108
+ /** The items (dinosaurs, habitats, etc.) retrieved for the current page */
109
+ items: T[];
110
+ /** DynamoDB's last evaluated key, used internally for pagination */
111
+ lastEvaluatedKey?: DynamoItem;
112
+ /** Indicates whether there are more pages available */
113
+ hasNextPage: boolean;
114
+ /** The current page number (1-indexed) */
115
+ page: number;
116
+ }
117
+
118
+ export type { ConditionCheckCommandParams as C, DeleteCommandParams as D, FilterBuilderInterface as F, PaginationResult as P, QueryBuilderInterface as Q, ScanBuilderInterface as S, UpdateCommandParams as U, PutCommandParams as a };
@@ -0,0 +1,394 @@
1
+ 'use strict';
2
+
3
+ // src/conditions.ts
4
+ var createComparisonCondition = (type) => (attr, value) => ({
5
+ type,
6
+ attr,
7
+ value
8
+ });
9
+ var eq = createComparisonCondition("eq");
10
+ var ne = createComparisonCondition("ne");
11
+ var lt = createComparisonCondition("lt");
12
+ var lte = createComparisonCondition("lte");
13
+ var gt = createComparisonCondition("gt");
14
+ var gte = createComparisonCondition("gte");
15
+ var between = (attr, lower, upper) => ({
16
+ type: "between",
17
+ attr,
18
+ value: [lower, upper]
19
+ });
20
+ var beginsWith = createComparisonCondition("beginsWith");
21
+ var contains = createComparisonCondition("contains");
22
+ var attributeExists = (attr) => ({
23
+ type: "attributeExists",
24
+ attr
25
+ });
26
+ var attributeNotExists = (attr) => ({
27
+ type: "attributeNotExists",
28
+ attr
29
+ });
30
+ var and = (...conditions) => ({
31
+ type: "and",
32
+ conditions
33
+ });
34
+ var or = (...conditions) => ({
35
+ type: "or",
36
+ conditions
37
+ });
38
+ var not = (condition) => ({
39
+ type: "not",
40
+ condition
41
+ });
42
+
43
+ // src/expression.ts
44
+ var generateAttributeName = (params, attr) => {
45
+ for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
46
+ if (existingAttr === attr) {
47
+ return existingName;
48
+ }
49
+ }
50
+ const attrName = `#${Object.keys(params.expressionAttributeNames).length}`;
51
+ params.expressionAttributeNames[attrName] = attr;
52
+ return attrName;
53
+ };
54
+ var generateValueName = (params, value) => {
55
+ const valueName = `:${params.valueCounter.count++}`;
56
+ params.expressionAttributeValues[valueName] = value;
57
+ return valueName;
58
+ };
59
+ var validateCondition = (condition, requiresAttr = true, requiresValue = true) => {
60
+ if (requiresAttr && !condition.attr) {
61
+ throw new Error(`Attribute is required for ${condition.type} condition`);
62
+ }
63
+ if (requiresValue && condition.value === void 0) {
64
+ throw new Error(`Value is required for ${condition.type} condition`);
65
+ }
66
+ };
67
+ var buildComparisonExpression = (condition, operator, params) => {
68
+ validateCondition(condition);
69
+ if (!condition.attr) {
70
+ throw new Error(`Attribute is required for ${condition.type} condition`);
71
+ }
72
+ const attrName = generateAttributeName(params, condition.attr);
73
+ const valueName = generateValueName(params, condition.value);
74
+ return `${attrName} ${operator} ${valueName}`;
75
+ };
76
+ var buildBetweenExpression = (condition, params) => {
77
+ validateCondition(condition);
78
+ if (!condition.attr) {
79
+ throw new Error(`Attribute is required for ${condition.type} condition`);
80
+ }
81
+ if (!Array.isArray(condition.value) || condition.value.length !== 2) {
82
+ throw new Error("Between condition requires an array of two values");
83
+ }
84
+ const attrName = generateAttributeName(params, condition.attr);
85
+ const lowerName = generateValueName(params, condition.value[0]);
86
+ const upperName = generateValueName(params, condition.value[1]);
87
+ return `${attrName} BETWEEN ${lowerName} AND ${upperName}`;
88
+ };
89
+ var buildFunctionExpression = (functionName, condition, params) => {
90
+ validateCondition(condition);
91
+ if (!condition.attr) {
92
+ throw new Error(`Attribute is required for ${condition.type} condition`);
93
+ }
94
+ const attrName = generateAttributeName(params, condition.attr);
95
+ const valueName = generateValueName(params, condition.value);
96
+ return `${functionName}(${attrName}, ${valueName})`;
97
+ };
98
+ var buildAttributeFunction = (functionName, condition, params) => {
99
+ validateCondition(condition, true, false);
100
+ if (!condition.attr) {
101
+ throw new Error(`Attribute is required for ${condition.type} condition`);
102
+ }
103
+ const attrName = generateAttributeName(params, condition.attr);
104
+ return `${functionName}(${attrName})`;
105
+ };
106
+ var buildLogicalExpression = (operator, conditions, params) => {
107
+ if (!conditions || conditions.length === 0) {
108
+ throw new Error(`At least one condition is required for ${operator} expression`);
109
+ }
110
+ const expressions = conditions.map((c) => buildExpression(c, params));
111
+ return `(${expressions.join(` ${operator} `)})`;
112
+ };
113
+ var buildExpression = (condition, params) => {
114
+ if (!condition) return "";
115
+ try {
116
+ const expressionBuilders = {
117
+ eq: () => buildComparisonExpression(condition, "=", params),
118
+ ne: () => buildComparisonExpression(condition, "<>", params),
119
+ lt: () => buildComparisonExpression(condition, "<", params),
120
+ lte: () => buildComparisonExpression(condition, "<=", params),
121
+ gt: () => buildComparisonExpression(condition, ">", params),
122
+ gte: () => buildComparisonExpression(condition, ">=", params),
123
+ between: () => buildBetweenExpression(condition, params),
124
+ beginsWith: () => buildFunctionExpression("begins_with", condition, params),
125
+ contains: () => buildFunctionExpression("contains", condition, params),
126
+ attributeExists: () => buildAttributeFunction("attribute_exists", condition, params),
127
+ attributeNotExists: () => buildAttributeFunction("attribute_not_exists", condition, params),
128
+ and: () => {
129
+ if (!condition.conditions) {
130
+ throw new Error("Conditions array is required for AND operator");
131
+ }
132
+ return buildLogicalExpression("AND", condition.conditions, params);
133
+ },
134
+ or: () => {
135
+ if (!condition.conditions) {
136
+ throw new Error("Conditions array is required for OR operator");
137
+ }
138
+ return buildLogicalExpression("OR", condition.conditions, params);
139
+ },
140
+ not: () => {
141
+ if (!condition.condition) {
142
+ throw new Error("Condition is required for NOT operator");
143
+ }
144
+ return `NOT (${buildExpression(condition.condition, params)})`;
145
+ }
146
+ };
147
+ const builder = expressionBuilders[condition.type];
148
+ if (!builder) {
149
+ throw new Error(`Unknown condition type: ${condition.type}`);
150
+ }
151
+ return builder();
152
+ } catch (error) {
153
+ if (error instanceof Error) {
154
+ console.error(`Error building expression for condition type ${condition.type}:`, error.message);
155
+ } else {
156
+ console.error(`Error building expression for condition type ${condition.type}:`, error);
157
+ }
158
+ throw error;
159
+ }
160
+ };
161
+ var prepareExpressionParams = (condition) => {
162
+ if (!condition) return {};
163
+ const params = {
164
+ expressionAttributeNames: {},
165
+ expressionAttributeValues: {},
166
+ valueCounter: { count: 0 }
167
+ };
168
+ const expression = buildExpression(condition, params);
169
+ return {
170
+ expression,
171
+ names: Object.keys(params.expressionAttributeNames).length > 0 ? params.expressionAttributeNames : void 0,
172
+ values: Object.keys(params.expressionAttributeValues).length > 0 ? params.expressionAttributeValues : void 0
173
+ };
174
+ };
175
+
176
+ // src/utils/debug-expression.ts
177
+ function debugCommand(command) {
178
+ const result = {};
179
+ function replaceAliases(expressionString) {
180
+ if (!expressionString) {
181
+ return expressionString;
182
+ }
183
+ let replacedString = expressionString;
184
+ for (const alias in command.expressionAttributeNames) {
185
+ const attributeName = command.expressionAttributeNames[alias];
186
+ const regex = new RegExp(alias, "g");
187
+ replacedString = replacedString.replace(regex, attributeName);
188
+ }
189
+ for (const alias in command.expressionAttributeValues) {
190
+ let attributeValue = command.expressionAttributeValues[alias];
191
+ if (attributeValue instanceof Set) {
192
+ const array = Array.from(attributeValue);
193
+ attributeValue = `Set(${array.length}){${array.map((v) => JSON.stringify(v)).join(", ")}}`;
194
+ } else {
195
+ attributeValue = JSON.stringify(attributeValue);
196
+ }
197
+ const regex = new RegExp(alias, "g");
198
+ replacedString = replacedString.replace(regex, attributeValue);
199
+ }
200
+ return replacedString;
201
+ }
202
+ if (command.updateExpression) {
203
+ result.updateExpression = replaceAliases(command.updateExpression);
204
+ }
205
+ if (command.conditionExpression) {
206
+ result.conditionExpression = replaceAliases(command.conditionExpression);
207
+ }
208
+ if (command.filterExpression) {
209
+ result.filterExpression = replaceAliases(command.filterExpression);
210
+ }
211
+ if (command.keyConditionExpression) {
212
+ result.keyConditionExpression = replaceAliases(command.keyConditionExpression);
213
+ }
214
+ if (command.projectionExpression) {
215
+ result.projectionExpression = replaceAliases(command.projectionExpression);
216
+ }
217
+ return {
218
+ raw: command,
219
+ readable: result
220
+ };
221
+ }
222
+
223
+ // src/builders/condition-check-builder.ts
224
+ var ConditionCheckBuilder = class {
225
+ key;
226
+ tableName;
227
+ conditionExpression;
228
+ constructor(tableName, key) {
229
+ this.tableName = tableName;
230
+ this.key = key;
231
+ }
232
+ /**
233
+ * Adds a condition that must be satisfied for the check to succeed.
234
+ * Use this method when you need to:
235
+ * - Validate complex item states
236
+ * - Check multiple attributes together
237
+ * - Ensure safety conditions are met
238
+ *
239
+ * @example
240
+ * ```typescript
241
+ * // Check dinosaur health and behavior
242
+ * builder.condition(op =>
243
+ * op.and([
244
+ * op.gt('stats.health', 50),
245
+ * op.not(op.eq('status', 'SEDATED')),
246
+ * op.lt('aggressionLevel', 8)
247
+ * ])
248
+ * );
249
+ *
250
+ * // Verify habitat conditions
251
+ * builder.condition(op =>
252
+ * op.and([
253
+ * op.eq('powerStatus', 'ONLINE'),
254
+ * op.between('temperature', 20, 30),
255
+ * op.attributeExists('lastMaintenance')
256
+ * ])
257
+ * );
258
+ *
259
+ * // Check breeding conditions
260
+ * builder.condition(op =>
261
+ * op.and([
262
+ * op.eq('species', 'VELOCIRAPTOR'),
263
+ * op.gte('age', 3),
264
+ * op.eq('geneticPurity', 100)
265
+ * ])
266
+ * );
267
+ * ```
268
+ *
269
+ * @param condition - Either a Condition DynamoItem or a callback function that builds the condition
270
+ * @returns The builder instance for method chaining
271
+ */
272
+ condition(condition) {
273
+ if (typeof condition === "function") {
274
+ const conditionOperator = {
275
+ eq,
276
+ ne,
277
+ lt,
278
+ lte,
279
+ gt,
280
+ gte,
281
+ between,
282
+ beginsWith,
283
+ contains,
284
+ attributeExists,
285
+ attributeNotExists,
286
+ and,
287
+ or,
288
+ not
289
+ };
290
+ this.conditionExpression = condition(conditionOperator);
291
+ } else {
292
+ this.conditionExpression = condition;
293
+ }
294
+ return this;
295
+ }
296
+ /**
297
+ * Generates the DynamoDB command parameters for direct execution.
298
+ * Use this method when you want to:
299
+ * - Execute the condition check as a standalone operation
300
+ * - Get the raw DynamoDB command for custom execution
301
+ * - Inspect the generated command parameters
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * const command = new ConditionCheckBuilder('myTable', { id: '123' })
306
+ * .condition(op => op.attributeExists('status'))
307
+ * .toDynamoCommand();
308
+ * // Use command with DynamoDB client
309
+ * ```
310
+ *
311
+ * @throws {Error} If no condition has been set
312
+ * @returns The DynamoDB command parameters
313
+ */
314
+ toDynamoCommand() {
315
+ if (!this.conditionExpression) {
316
+ throw new Error("Condition is required for condition check operations");
317
+ }
318
+ const { expression, names, values } = prepareExpressionParams(this.conditionExpression);
319
+ if (!expression) {
320
+ throw new Error("Failed to generate condition expression");
321
+ }
322
+ return {
323
+ tableName: this.tableName,
324
+ key: this.key,
325
+ conditionExpression: expression,
326
+ expressionAttributeNames: names,
327
+ expressionAttributeValues: values
328
+ };
329
+ }
330
+ /**
331
+ * Adds this condition check operation to a transaction.
332
+ * Use this method when you need to:
333
+ * - Verify habitat safety before transfers
334
+ * - Ensure proper feeding conditions
335
+ * - Validate security protocols
336
+ *
337
+ * @example
338
+ * ```ts
339
+ * const transaction = new TransactionBuilder();
340
+ * new ConditionCheckBuilder('habitats', { id: 'PADDOCK-B' })
341
+ * .condition(op => op.and([
342
+ * op.eq('securityStatus', 'ACTIVE'),
343
+ * op.lt('currentOccupants', 3),
344
+ * op.eq('habitatType', 'CARNIVORE')
345
+ * ]))
346
+ * .withTransaction(transaction);
347
+ * // Add dinosaur transfer operations
348
+ * ```
349
+ *
350
+ * @param transaction - The transaction builder to add this operation to
351
+ * @throws {Error} If no condition has been set
352
+ * @returns The builder instance for method chaining
353
+ */
354
+ withTransaction(transaction) {
355
+ if (!this.conditionExpression) {
356
+ throw new Error("Condition is required for condition check operations");
357
+ }
358
+ const command = this.toDynamoCommand();
359
+ transaction.conditionCheckWithCommand(command);
360
+ return this;
361
+ }
362
+ /**
363
+ * Gets a human-readable representation of the condition check command
364
+ * with all expression placeholders replaced by their actual values.
365
+ * Use this method when you need to:
366
+ * - Debug complex condition expressions
367
+ * - Verify condition parameters
368
+ * - Log safety checks
369
+ * - Troubleshoot condition failures
370
+ *
371
+ * @example
372
+ * ```ts
373
+ * const debugInfo = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })
374
+ * .condition(op => op.and([
375
+ * op.between('stats.health', 50, 100),
376
+ * op.not(op.eq('status', 'SEDATED')),
377
+ * op.attributeExists('lastFeedingTime')
378
+ * op.eq('version', 1)
379
+ * ]))
380
+ * .debug();
381
+ * console.log(debugInfo);
382
+ * ```
383
+ *
384
+ * @returns A readable representation of the condition check command with resolved expressions
385
+ */
386
+ debug() {
387
+ const command = this.toDynamoCommand();
388
+ return debugCommand(command);
389
+ }
390
+ };
391
+
392
+ exports.ConditionCheckBuilder = ConditionCheckBuilder;
393
+ //# sourceMappingURL=condition-check-builder.cjs.map
394
+ //# sourceMappingURL=condition-check-builder.cjs.map