dyno-table 1.0.0-alpha.1 → 1.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/README.md +752 -172
- package/dist/builder-types-C_PDZhnP.d.ts +118 -0
- package/dist/builder-types-DtwbqMeF.d.cts +118 -0
- package/dist/builders/condition-check-builder.cjs +394 -0
- package/dist/builders/condition-check-builder.cjs.map +1 -0
- package/dist/builders/condition-check-builder.d.cts +157 -0
- package/dist/builders/condition-check-builder.d.ts +157 -0
- package/dist/builders/condition-check-builder.js +392 -0
- package/dist/builders/condition-check-builder.js.map +1 -0
- package/dist/builders/delete-builder.cjs +405 -0
- package/dist/builders/delete-builder.cjs.map +1 -0
- package/dist/builders/delete-builder.d.cts +166 -0
- package/dist/builders/delete-builder.d.ts +166 -0
- package/dist/builders/delete-builder.js +403 -0
- package/dist/builders/delete-builder.js.map +1 -0
- package/dist/builders/paginator.cjs +199 -0
- package/dist/builders/paginator.cjs.map +1 -0
- package/dist/builders/paginator.d.cts +179 -0
- package/dist/builders/paginator.d.ts +179 -0
- package/dist/builders/paginator.js +197 -0
- package/dist/builders/paginator.js.map +1 -0
- package/dist/builders/put-builder.cjs +476 -0
- package/dist/builders/put-builder.cjs.map +1 -0
- package/dist/builders/put-builder.d.cts +274 -0
- package/dist/builders/put-builder.d.ts +274 -0
- package/dist/builders/put-builder.js +474 -0
- package/dist/builders/put-builder.js.map +1 -0
- package/dist/builders/query-builder.cjs +674 -0
- package/dist/builders/query-builder.cjs.map +1 -0
- package/dist/builders/query-builder.d.cts +6 -0
- package/dist/builders/query-builder.d.ts +6 -0
- package/dist/builders/query-builder.js +672 -0
- package/dist/builders/query-builder.js.map +1 -0
- package/dist/builders/transaction-builder.cjs +918 -0
- package/dist/builders/transaction-builder.cjs.map +1 -0
- package/dist/builders/transaction-builder.d.cts +511 -0
- package/dist/builders/transaction-builder.d.ts +511 -0
- package/dist/builders/transaction-builder.js +916 -0
- package/dist/builders/transaction-builder.js.map +1 -0
- package/dist/builders/update-builder.cjs +627 -0
- package/dist/builders/update-builder.cjs.map +1 -0
- package/dist/builders/update-builder.d.cts +365 -0
- package/dist/builders/update-builder.d.ts +365 -0
- package/dist/builders/update-builder.js +625 -0
- package/dist/builders/update-builder.js.map +1 -0
- package/dist/conditions--ld9a78i.d.ts +331 -0
- package/dist/conditions-ChhQWd6z.d.cts +331 -0
- package/dist/conditions.cjs +59 -0
- package/dist/conditions.cjs.map +1 -0
- package/dist/conditions.d.cts +3 -0
- package/dist/conditions.d.ts +3 -0
- package/dist/conditions.js +43 -0
- package/dist/conditions.js.map +1 -0
- package/dist/entity.cjs +211 -0
- package/dist/entity.cjs.map +1 -0
- package/dist/entity.d.cts +149 -0
- package/dist/entity.d.ts +149 -0
- package/dist/entity.js +207 -0
- package/dist/entity.js.map +1 -0
- package/dist/query-builder-Csror9Iu.d.ts +507 -0
- package/dist/query-builder-D2FM9rsu.d.cts +507 -0
- package/dist/standard-schema.cjs +4 -0
- package/dist/standard-schema.cjs.map +1 -0
- package/dist/standard-schema.d.cts +57 -0
- package/dist/standard-schema.d.ts +57 -0
- package/dist/standard-schema.js +3 -0
- package/dist/standard-schema.js.map +1 -0
- package/dist/table-BEhBPy2G.d.cts +364 -0
- package/dist/table-BW3cmUqr.d.ts +364 -0
- package/dist/{index.cjs → table.cjs} +108 -175
- package/dist/table.cjs.map +1 -0
- package/dist/table.d.cts +12 -0
- package/dist/table.d.ts +12 -0
- package/dist/{index.js → table.js} +107 -127
- package/dist/table.js.map +1 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +22 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/partition-key-template.cjs +19 -0
- package/dist/utils/partition-key-template.cjs.map +1 -0
- package/dist/utils/partition-key-template.d.cts +32 -0
- package/dist/utils/partition-key-template.d.ts +32 -0
- package/dist/utils/partition-key-template.js +17 -0
- package/dist/utils/partition-key-template.js.map +1 -0
- package/dist/utils/sort-key-template.cjs +19 -0
- package/dist/utils/sort-key-template.cjs.map +1 -0
- package/dist/utils/sort-key-template.d.cts +35 -0
- package/dist/utils/sort-key-template.d.ts +35 -0
- package/dist/utils/sort-key-template.js +17 -0
- package/dist/utils/sort-key-template.js.map +1 -0
- package/package.json +77 -7
- package/dist/index.d.cts +0 -2971
- package/dist/index.d.ts +0 -2971
|
@@ -0,0 +1,474 @@
|
|
|
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 beginsWith = createComparisonCondition("beginsWith");
|
|
19
|
+
var contains = createComparisonCondition("contains");
|
|
20
|
+
var attributeExists = (attr) => ({
|
|
21
|
+
type: "attributeExists",
|
|
22
|
+
attr
|
|
23
|
+
});
|
|
24
|
+
var attributeNotExists = (attr) => ({
|
|
25
|
+
type: "attributeNotExists",
|
|
26
|
+
attr
|
|
27
|
+
});
|
|
28
|
+
var and = (...conditions) => ({
|
|
29
|
+
type: "and",
|
|
30
|
+
conditions
|
|
31
|
+
});
|
|
32
|
+
var or = (...conditions) => ({
|
|
33
|
+
type: "or",
|
|
34
|
+
conditions
|
|
35
|
+
});
|
|
36
|
+
var not = (condition) => ({
|
|
37
|
+
type: "not",
|
|
38
|
+
condition
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// src/expression.ts
|
|
42
|
+
var generateAttributeName = (params, attr) => {
|
|
43
|
+
for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
|
|
44
|
+
if (existingAttr === attr) {
|
|
45
|
+
return existingName;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const attrName = `#${Object.keys(params.expressionAttributeNames).length}`;
|
|
49
|
+
params.expressionAttributeNames[attrName] = attr;
|
|
50
|
+
return attrName;
|
|
51
|
+
};
|
|
52
|
+
var generateValueName = (params, value) => {
|
|
53
|
+
const valueName = `:${params.valueCounter.count++}`;
|
|
54
|
+
params.expressionAttributeValues[valueName] = value;
|
|
55
|
+
return valueName;
|
|
56
|
+
};
|
|
57
|
+
var validateCondition = (condition, requiresAttr = true, requiresValue = true) => {
|
|
58
|
+
if (requiresAttr && !condition.attr) {
|
|
59
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
60
|
+
}
|
|
61
|
+
if (requiresValue && condition.value === void 0) {
|
|
62
|
+
throw new Error(`Value is required for ${condition.type} condition`);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var buildComparisonExpression = (condition, operator, params) => {
|
|
66
|
+
validateCondition(condition);
|
|
67
|
+
if (!condition.attr) {
|
|
68
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
69
|
+
}
|
|
70
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
71
|
+
const valueName = generateValueName(params, condition.value);
|
|
72
|
+
return `${attrName} ${operator} ${valueName}`;
|
|
73
|
+
};
|
|
74
|
+
var buildBetweenExpression = (condition, params) => {
|
|
75
|
+
validateCondition(condition);
|
|
76
|
+
if (!condition.attr) {
|
|
77
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
78
|
+
}
|
|
79
|
+
if (!Array.isArray(condition.value) || condition.value.length !== 2) {
|
|
80
|
+
throw new Error("Between condition requires an array of two values");
|
|
81
|
+
}
|
|
82
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
83
|
+
const lowerName = generateValueName(params, condition.value[0]);
|
|
84
|
+
const upperName = generateValueName(params, condition.value[1]);
|
|
85
|
+
return `${attrName} BETWEEN ${lowerName} AND ${upperName}`;
|
|
86
|
+
};
|
|
87
|
+
var buildFunctionExpression = (functionName, condition, params) => {
|
|
88
|
+
validateCondition(condition);
|
|
89
|
+
if (!condition.attr) {
|
|
90
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
91
|
+
}
|
|
92
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
93
|
+
const valueName = generateValueName(params, condition.value);
|
|
94
|
+
return `${functionName}(${attrName}, ${valueName})`;
|
|
95
|
+
};
|
|
96
|
+
var buildAttributeFunction = (functionName, condition, params) => {
|
|
97
|
+
validateCondition(condition, true, false);
|
|
98
|
+
if (!condition.attr) {
|
|
99
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
100
|
+
}
|
|
101
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
102
|
+
return `${functionName}(${attrName})`;
|
|
103
|
+
};
|
|
104
|
+
var buildLogicalExpression = (operator, conditions, params) => {
|
|
105
|
+
if (!conditions || conditions.length === 0) {
|
|
106
|
+
throw new Error(`At least one condition is required for ${operator} expression`);
|
|
107
|
+
}
|
|
108
|
+
const expressions = conditions.map((c) => buildExpression(c, params));
|
|
109
|
+
return `(${expressions.join(` ${operator} `)})`;
|
|
110
|
+
};
|
|
111
|
+
var buildExpression = (condition, params) => {
|
|
112
|
+
if (!condition) return "";
|
|
113
|
+
try {
|
|
114
|
+
const expressionBuilders = {
|
|
115
|
+
eq: () => buildComparisonExpression(condition, "=", params),
|
|
116
|
+
ne: () => buildComparisonExpression(condition, "<>", params),
|
|
117
|
+
lt: () => buildComparisonExpression(condition, "<", params),
|
|
118
|
+
lte: () => buildComparisonExpression(condition, "<=", params),
|
|
119
|
+
gt: () => buildComparisonExpression(condition, ">", params),
|
|
120
|
+
gte: () => buildComparisonExpression(condition, ">=", params),
|
|
121
|
+
between: () => buildBetweenExpression(condition, params),
|
|
122
|
+
beginsWith: () => buildFunctionExpression("begins_with", condition, params),
|
|
123
|
+
contains: () => buildFunctionExpression("contains", condition, params),
|
|
124
|
+
attributeExists: () => buildAttributeFunction("attribute_exists", condition, params),
|
|
125
|
+
attributeNotExists: () => buildAttributeFunction("attribute_not_exists", condition, params),
|
|
126
|
+
and: () => {
|
|
127
|
+
if (!condition.conditions) {
|
|
128
|
+
throw new Error("Conditions array is required for AND operator");
|
|
129
|
+
}
|
|
130
|
+
return buildLogicalExpression("AND", condition.conditions, params);
|
|
131
|
+
},
|
|
132
|
+
or: () => {
|
|
133
|
+
if (!condition.conditions) {
|
|
134
|
+
throw new Error("Conditions array is required for OR operator");
|
|
135
|
+
}
|
|
136
|
+
return buildLogicalExpression("OR", condition.conditions, params);
|
|
137
|
+
},
|
|
138
|
+
not: () => {
|
|
139
|
+
if (!condition.condition) {
|
|
140
|
+
throw new Error("Condition is required for NOT operator");
|
|
141
|
+
}
|
|
142
|
+
return `NOT (${buildExpression(condition.condition, params)})`;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const builder = expressionBuilders[condition.type];
|
|
146
|
+
if (!builder) {
|
|
147
|
+
throw new Error(`Unknown condition type: ${condition.type}`);
|
|
148
|
+
}
|
|
149
|
+
return builder();
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (error instanceof Error) {
|
|
152
|
+
console.error(`Error building expression for condition type ${condition.type}:`, error.message);
|
|
153
|
+
} else {
|
|
154
|
+
console.error(`Error building expression for condition type ${condition.type}:`, error);
|
|
155
|
+
}
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
var prepareExpressionParams = (condition) => {
|
|
160
|
+
if (!condition) return {};
|
|
161
|
+
const params = {
|
|
162
|
+
expressionAttributeNames: {},
|
|
163
|
+
expressionAttributeValues: {},
|
|
164
|
+
valueCounter: { count: 0 }
|
|
165
|
+
};
|
|
166
|
+
const expression = buildExpression(condition, params);
|
|
167
|
+
return {
|
|
168
|
+
expression,
|
|
169
|
+
names: Object.keys(params.expressionAttributeNames).length > 0 ? params.expressionAttributeNames : void 0,
|
|
170
|
+
values: Object.keys(params.expressionAttributeValues).length > 0 ? params.expressionAttributeValues : void 0
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// src/utils/debug-expression.ts
|
|
175
|
+
function debugCommand(command) {
|
|
176
|
+
const result = {};
|
|
177
|
+
function replaceAliases(expressionString) {
|
|
178
|
+
if (!expressionString) {
|
|
179
|
+
return expressionString;
|
|
180
|
+
}
|
|
181
|
+
let replacedString = expressionString;
|
|
182
|
+
for (const alias in command.expressionAttributeNames) {
|
|
183
|
+
const attributeName = command.expressionAttributeNames[alias];
|
|
184
|
+
const regex = new RegExp(alias, "g");
|
|
185
|
+
replacedString = replacedString.replace(regex, attributeName);
|
|
186
|
+
}
|
|
187
|
+
for (const alias in command.expressionAttributeValues) {
|
|
188
|
+
let attributeValue = command.expressionAttributeValues[alias];
|
|
189
|
+
if (attributeValue instanceof Set) {
|
|
190
|
+
const array = Array.from(attributeValue);
|
|
191
|
+
attributeValue = `Set(${array.length}){${array.map((v) => JSON.stringify(v)).join(", ")}}`;
|
|
192
|
+
} else {
|
|
193
|
+
attributeValue = JSON.stringify(attributeValue);
|
|
194
|
+
}
|
|
195
|
+
const regex = new RegExp(alias, "g");
|
|
196
|
+
replacedString = replacedString.replace(regex, attributeValue);
|
|
197
|
+
}
|
|
198
|
+
return replacedString;
|
|
199
|
+
}
|
|
200
|
+
if (command.updateExpression) {
|
|
201
|
+
result.updateExpression = replaceAliases(command.updateExpression);
|
|
202
|
+
}
|
|
203
|
+
if (command.conditionExpression) {
|
|
204
|
+
result.conditionExpression = replaceAliases(command.conditionExpression);
|
|
205
|
+
}
|
|
206
|
+
if (command.filterExpression) {
|
|
207
|
+
result.filterExpression = replaceAliases(command.filterExpression);
|
|
208
|
+
}
|
|
209
|
+
if (command.keyConditionExpression) {
|
|
210
|
+
result.keyConditionExpression = replaceAliases(command.keyConditionExpression);
|
|
211
|
+
}
|
|
212
|
+
if (command.projectionExpression) {
|
|
213
|
+
result.projectionExpression = replaceAliases(command.projectionExpression);
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
raw: command,
|
|
217
|
+
readable: result
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/builders/put-builder.ts
|
|
222
|
+
var PutBuilder = class {
|
|
223
|
+
item;
|
|
224
|
+
options;
|
|
225
|
+
executor;
|
|
226
|
+
tableName;
|
|
227
|
+
constructor(executor, item, tableName) {
|
|
228
|
+
this.executor = executor;
|
|
229
|
+
this.item = item;
|
|
230
|
+
this.tableName = tableName;
|
|
231
|
+
this.options = {
|
|
232
|
+
returnValues: "NONE"
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
set(valuesOrPath, value) {
|
|
236
|
+
if (typeof valuesOrPath === "object") {
|
|
237
|
+
Object.assign(this.item, valuesOrPath);
|
|
238
|
+
} else {
|
|
239
|
+
this.item[valuesOrPath] = value;
|
|
240
|
+
}
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Adds a condition that must be satisfied for the put operation to succeed.
|
|
245
|
+
* Use this method when you need to:
|
|
246
|
+
* - Prevent overwriting existing items (optimistic locking)
|
|
247
|
+
* - Ensure items meet certain criteria before replacement
|
|
248
|
+
* - Implement complex business rules for item updates
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* // Ensure item doesn't exist (insert only)
|
|
253
|
+
* builder.condition(op => op.attributeNotExists('id'))
|
|
254
|
+
*
|
|
255
|
+
* // Complex condition with version check
|
|
256
|
+
* builder.condition(op =>
|
|
257
|
+
* op.and([
|
|
258
|
+
* op.attributeExists('id'),
|
|
259
|
+
* op.eq('version', currentVersion),
|
|
260
|
+
* op.eq('status', 'ACTIVE')
|
|
261
|
+
* ])
|
|
262
|
+
* )
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
266
|
+
* @returns The builder instance for method chaining
|
|
267
|
+
*/
|
|
268
|
+
/**
|
|
269
|
+
* Adds a condition that must be satisfied for the put operation to succeed.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* // Ensure unique dinosaur ID
|
|
274
|
+
* builder.condition(op =>
|
|
275
|
+
* op.attributeNotExists('id')
|
|
276
|
+
* );
|
|
277
|
+
*
|
|
278
|
+
* // Verify habitat requirements
|
|
279
|
+
* builder.condition(op =>
|
|
280
|
+
* op.and([
|
|
281
|
+
* op.eq('securityStatus', 'READY'),
|
|
282
|
+
* op.attributeExists('lastInspection'),
|
|
283
|
+
* op.gt('securityLevel', 5)
|
|
284
|
+
* ])
|
|
285
|
+
* );
|
|
286
|
+
*
|
|
287
|
+
* // Check breeding facility conditions
|
|
288
|
+
* builder.condition(op =>
|
|
289
|
+
* op.and([
|
|
290
|
+
* op.between('temperature', 25, 30),
|
|
291
|
+
* op.between('humidity', 60, 80),
|
|
292
|
+
* op.eq('quarantineStatus', 'CLEAR')
|
|
293
|
+
* ])
|
|
294
|
+
* );
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
298
|
+
* @returns The builder instance for method chaining
|
|
299
|
+
*/
|
|
300
|
+
condition(condition) {
|
|
301
|
+
if (typeof condition === "function") {
|
|
302
|
+
const conditionOperator = {
|
|
303
|
+
eq,
|
|
304
|
+
ne,
|
|
305
|
+
lt,
|
|
306
|
+
lte,
|
|
307
|
+
gt,
|
|
308
|
+
gte,
|
|
309
|
+
between,
|
|
310
|
+
beginsWith,
|
|
311
|
+
contains,
|
|
312
|
+
attributeExists,
|
|
313
|
+
attributeNotExists,
|
|
314
|
+
and,
|
|
315
|
+
or,
|
|
316
|
+
not
|
|
317
|
+
};
|
|
318
|
+
this.options.condition = condition(conditionOperator);
|
|
319
|
+
} else {
|
|
320
|
+
this.options.condition = condition;
|
|
321
|
+
}
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Sets whether to return the item's previous values (if it existed).
|
|
326
|
+
*
|
|
327
|
+
* @options
|
|
328
|
+
* - NONE: No return value
|
|
329
|
+
* - ALL_OLD: Returns the item's previous state if it existed, no read capacity units are consumed
|
|
330
|
+
* - CONSISTENT: (default) Performs a GET operation after the put to retrieve the item's new state
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* ```ts
|
|
334
|
+
* // Get previous dinosaur state
|
|
335
|
+
* const result = await builder
|
|
336
|
+
* .returnValues('ALL_OLD')
|
|
337
|
+
* .execute();
|
|
338
|
+
*
|
|
339
|
+
* if (result) {
|
|
340
|
+
* console.log('Previous profile:', {
|
|
341
|
+
* species: result.species,
|
|
342
|
+
* status: result.status,
|
|
343
|
+
* stats: {
|
|
344
|
+
* health: result.stats.health,
|
|
345
|
+
* threatLevel: result.stats.threatLevel
|
|
346
|
+
* }
|
|
347
|
+
* });
|
|
348
|
+
* }
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @param returnValues - Use 'ALL_OLD' to return previous values if the item was overwritten, or 'NONE' (default).
|
|
352
|
+
* @returns The builder instance for method chaining
|
|
353
|
+
*/
|
|
354
|
+
returnValues(returnValues) {
|
|
355
|
+
this.options.returnValues = returnValues;
|
|
356
|
+
return this;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Generate the DynamoDB command parameters
|
|
360
|
+
*/
|
|
361
|
+
toDynamoCommand() {
|
|
362
|
+
const { expression, names, values } = prepareExpressionParams(this.options.condition);
|
|
363
|
+
return {
|
|
364
|
+
tableName: this.tableName,
|
|
365
|
+
item: this.item,
|
|
366
|
+
conditionExpression: expression,
|
|
367
|
+
expressionAttributeNames: names,
|
|
368
|
+
expressionAttributeValues: values,
|
|
369
|
+
returnValues: this.options.returnValues
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Adds this put operation to a transaction.
|
|
374
|
+
* Use this method when you need to:
|
|
375
|
+
* - Transfer dinosaurs between habitats
|
|
376
|
+
* - Initialize new breeding programs
|
|
377
|
+
* - Update multiple facility records
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```ts
|
|
381
|
+
* const transaction = new TransactionBuilder();
|
|
382
|
+
*
|
|
383
|
+
* // Add dinosaur to new habitat
|
|
384
|
+
* new PutBuilder(executor, {
|
|
385
|
+
* id: 'TREX-002',
|
|
386
|
+
* location: 'PADDOCK-B',
|
|
387
|
+
* status: 'ACTIVE',
|
|
388
|
+
* transferDate: new Date().toISOString()
|
|
389
|
+
* }, 'dinosaurs')
|
|
390
|
+
* .withTransaction(transaction);
|
|
391
|
+
*
|
|
392
|
+
* // Update habitat records
|
|
393
|
+
* new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-B' })
|
|
394
|
+
* .add('occupants', 1)
|
|
395
|
+
* .set('lastTransfer', new Date().toISOString())
|
|
396
|
+
* .withTransaction(transaction);
|
|
397
|
+
*
|
|
398
|
+
* // Execute transfer atomically
|
|
399
|
+
* await transaction.execute();
|
|
400
|
+
* ```
|
|
401
|
+
*
|
|
402
|
+
* @param transaction - The transaction builder to add this operation to
|
|
403
|
+
* @returns The builder instance for method chaining
|
|
404
|
+
*/
|
|
405
|
+
withTransaction(transaction) {
|
|
406
|
+
const command = this.toDynamoCommand();
|
|
407
|
+
transaction.putWithCommand(command);
|
|
408
|
+
return this;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Executes the put operation against DynamoDB.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```ts
|
|
415
|
+
* try {
|
|
416
|
+
* // Put with condition and return old values
|
|
417
|
+
* const result = await new PutBuilder(executor, newItem, 'myTable')
|
|
418
|
+
* .condition(op => op.eq('version', 1))
|
|
419
|
+
* .returnValues('ALL_OLD')
|
|
420
|
+
* .execute();
|
|
421
|
+
*
|
|
422
|
+
* console.log('Put successful, old item:', result);
|
|
423
|
+
* } catch (error) {
|
|
424
|
+
* // Handle condition check failure or other errors
|
|
425
|
+
* console.error('Put failed:', error);
|
|
426
|
+
* }
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* @returns A promise that resolves to the operation result (type depends on returnValues setting)
|
|
430
|
+
* @throws Will throw an error if the condition check fails or other DynamoDB errors occur
|
|
431
|
+
*/
|
|
432
|
+
async execute() {
|
|
433
|
+
const params = this.toDynamoCommand();
|
|
434
|
+
return this.executor(params);
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Gets a human-readable representation of the put command
|
|
438
|
+
* with all expression placeholders replaced by their actual values.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* const debugInfo = new PutBuilder(executor, {
|
|
443
|
+
* id: 'RAPTOR-003',
|
|
444
|
+
* species: 'Velociraptor',
|
|
445
|
+
* status: 'QUARANTINE',
|
|
446
|
+
* stats: {
|
|
447
|
+
* health: 100,
|
|
448
|
+
* aggressionLevel: 7,
|
|
449
|
+
* age: 2
|
|
450
|
+
* }
|
|
451
|
+
* }, 'dinosaurs')
|
|
452
|
+
* .condition(op =>
|
|
453
|
+
* op.and([
|
|
454
|
+
* op.attributeNotExists('id'),
|
|
455
|
+
* op.eq('quarantineStatus', 'READY'),
|
|
456
|
+
* op.gt('securityLevel', 8)
|
|
457
|
+
* ])
|
|
458
|
+
* )
|
|
459
|
+
* .debug();
|
|
460
|
+
*
|
|
461
|
+
* console.log('Dinosaur transfer command:', debugInfo);
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @returns A readable representation of the put command with resolved expressions
|
|
465
|
+
*/
|
|
466
|
+
debug() {
|
|
467
|
+
const command = this.toDynamoCommand();
|
|
468
|
+
return debugCommand(command);
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
export { PutBuilder };
|
|
473
|
+
//# sourceMappingURL=put-builder.js.map
|
|
474
|
+
//# sourceMappingURL=put-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/conditions.ts","../../src/expression.ts","../../src/utils/debug-expression.ts","../../src/builders/put-builder.ts"],"names":[],"mappings":";AA+FO,IAAM,yBACX,GAAA,CAAC,IACD,KAAA,CAAC,MAAc,KAA+B,MAAA;AAAA,EAC5C,IAAA;AAAA,EACA,IAAA;AAAA,EACA;AACF,CAAA,CAAA;AAQK,IAAM,EAAA,GAAK,0BAA0B,IAAI,CAAA;AAQzC,IAAM,EAAA,GAAK,0BAA0B,IAAI,CAAA;AAQzC,IAAM,EAAA,GAAK,0BAA0B,IAAI,CAAA;AAQzC,IAAM,GAAA,GAAM,0BAA0B,KAAK,CAAA;AAQ3C,IAAM,EAAA,GAAK,0BAA0B,IAAI,CAAA;AAQzC,IAAM,GAAA,GAAM,0BAA0B,KAAK,CAAA;AAQ3C,IAAM,OAAU,GAAA,CAAC,IAAc,EAAA,KAAA,EAAgB,KAA+B,MAAA;AAAA,EACnF,IAAM,EAAA,SAAA;AAAA,EACN,IAAA;AAAA,EACA,KAAA,EAAO,CAAC,KAAA,EAAO,KAAK;AACtB,CAAA,CAAA;AAQO,IAAM,UAAA,GAAa,0BAA0B,YAAY,CAAA;AAQzD,IAAM,QAAA,GAAW,0BAA0B,UAAU,CAAA;AAQrD,IAAM,eAAA,GAAkB,CAAC,IAA6B,MAAA;AAAA,EAC3D,IAAM,EAAA,iBAAA;AAAA,EACN;AACF,CAAA,CAAA;AAQO,IAAM,kBAAA,GAAqB,CAAC,IAA6B,MAAA;AAAA,EAC9D,IAAM,EAAA,oBAAA;AAAA,EACN;AACF,CAAA,CAAA;AAaO,IAAM,GAAA,GAAM,IAAI,UAAwC,MAAA;AAAA,EAC7D,IAAM,EAAA,KAAA;AAAA,EACN;AACF,CAAA,C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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmCO,gBAAgB,WAAuC,EAAA;AAC5D,IAAM,MAAA,OAAA,GAAU,KAAK,eAAgB,EAAA;AACrC,IAAA,WAAA,CAAY,eAAe,OAAO,CAAA;AAElC,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAwBA,MAAa,OAAkC,GAAA;AAC7C,IAAM,MAAA,MAAA,GAAS,KAAK,eAAgB,EAAA;AACpC,IAAO,OAAA,IAAA,CAAK,SAAS,MAAM,CAAA;AAAA;AAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgCO,KAAoB,GAAA;AACzB,IAAM,MAAA,OAAA,GAAU,KAAK,eAAgB,EAAA;AACrC,IAAA,OAAO,aAAa,OAAO,CAAA;AAAA;AAE/B","file":"put-builder.js","sourcesContent":["import type { Path, PathType } from \"./builders/types\";\nimport type { DynamoItem } from \"./types\";\n\n/**\n * Supported comparison operators for DynamoDB conditions.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html AWS DynamoDB - Comparison Operator Reference}\n *\n * - eq: Equals (=)\n * - ne: Not equals (≠ / <>)\n * - lt: Less than (<)\n * - lte: Less than or equal to (≤)\n * - gt: Greater than (>)\n * - gte: Greater than or equal to (≥)\n * - between: Between two values (inclusive)\n * - beginsWith: Checks if string attribute begins with specified substring\n * - contains: Checks if string/set attribute contains specified value\n * - attributeExists: Checks if attribute exists\n * - attributeNotExists: Checks if attribute does not exist\n */\nexport type ComparisonOperator =\n | \"eq\"\n | \"ne\"\n | \"lt\"\n | \"lte\"\n | \"gt\"\n | \"gte\"\n | \"between\"\n | \"beginsWith\"\n | \"contains\"\n | \"attributeExists\"\n | \"attributeNotExists\";\n\n/**\n * Logical operators for combining multiple conditions.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Logical AWS DynamoDB - Logical Operator Reference}\n *\n * - and: Evaluates to true if all conditions are true\n * - or: Evaluates to true if any condition is true\n * - not: Negate the result of a condition\n */\nexport type LogicalOperator = \"and\" | \"or\" | \"not\";\n\n/**\n * Represents a DynamoDB condition expression.\n * Can be either a comparison condition or a logical combination of conditions.\n *\n * @example\n * // Simple comparison condition\n * const condition: Condition = {\n * type: \"eq\",\n * attr: \"status\",\n * value: \"ACTIVE\"\n * };\n *\n * @example\n * // Logical combination of conditions\n * const condition: Condition = {\n * type: \"and\",\n * conditions: [\n * { type: \"eq\", attr: \"status\", value: \"ACTIVE\" },\n * { type: \"gt\", attr: \"age\", value: 5 }\n * ]\n * };\n */\nexport interface Condition {\n /** The type of condition (comparison or logical operator) */\n type: ComparisonOperator | LogicalOperator;\n /** The attribute name for comparison conditions */\n attr?: string;\n /** The value to compare against for comparison conditions */\n value?: unknown;\n /** Array of conditions for logical operators (and/or) */\n conditions?: Condition[];\n /** Single condition for the 'not' operator */\n condition?: Condition;\n}\n\n/**\n * Parameters used to build DynamoDB expression strings.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html Expression Attribute Names}\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeValues.html Expression Attribute Values}\n */\nexport interface ExpressionParams {\n /** Map of attribute name placeholders to actual attribute names */\n expressionAttributeNames: Record<string, string>;\n /** Map of value placeholders to actual values */\n expressionAttributeValues: DynamoItem;\n /** Counter for generating unique value placeholders */\n valueCounter: { count: number };\n}\n\n/**\n * Creates a comparison condition builder function for the specified operator.\n * @internal\n */\nexport const createComparisonCondition =\n (type: ComparisonOperator) =>\n (attr: string, value: unknown): Condition => ({\n type,\n attr,\n value,\n });\n\n/**\n * Creates an equals (=) condition\n * @example\n * eq(\"status\", \"ACTIVE\") // status = \"ACTIVE\"\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const eq = createComparisonCondition(\"eq\");\n\n/**\n * Creates a not equals (≠) condition\n * @example\n * ne(\"status\", \"DELETED\") // status <> \"DELETED\"\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const ne = createComparisonCondition(\"ne\");\n\n/**\n * Creates a less than (<) condition\n * @example\n * lt(\"age\", 18) // age < 18\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const lt = createComparisonCondition(\"lt\");\n\n/**\n * Creates a less than or equal to (≤) condition\n * @example\n * lte(\"age\", 18) // age <= 18\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const lte = createComparisonCondition(\"lte\");\n\n/**\n * Creates a greater than (>) condition\n * @example\n * gt(\"price\", 100) // price > 100\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const gt = createComparisonCondition(\"gt\");\n\n/**\n * Creates a greater than or equal to (≥) condition\n * @example\n * gte(\"price\", 100) // price >= 100\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html}\n */\nexport const gte = createComparisonCondition(\"gte\");\n\n/**\n * Creates a between condition that checks if a value is within a range (inclusive)\n * @example\n * between(\"age\", 18, 65) // age BETWEEN 18 AND 65\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Comparators AWS DynamoDB - BETWEEN}\n */\nexport const between = (attr: string, lower: unknown, upper: unknown): Condition => ({\n type: \"between\",\n attr,\n value: [lower, upper],\n});\n\n/**\n * Creates a begins_with condition that checks if a string attribute starts with a substring\n * @example\n * beginsWith(\"email\", \"@example.com\") // begins_with(email, \"@example.com\")\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions AWS DynamoDB - begins_with}\n */\nexport const beginsWith = createComparisonCondition(\"beginsWith\");\n\n/**\n * Creates a contains condition that checks if a string contains a substring or if a set contains an element\n * @example\n * contains(\"tags\", \"important\") // contains(tags, \"important\")\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions AWS DynamoDB - contains}\n */\nexport const contains = createComparisonCondition(\"contains\");\n\n/**\n * Creates a condition that checks if an attribute exists\n * @example\n * attributeExists(\"email\") // attribute_exists(email)\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions AWS DynamoDB - attribute_exists}\n */\nexport const attributeExists = (attr: string): Condition => ({\n type: \"attributeExists\",\n attr,\n});\n\n/**\n * Creates a condition that checks if an attribute does not exist\n * @example\n * attributeNotExists(\"deletedAt\") // attribute_not_exists(deletedAt)\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions AWS DynamoDB - attribute_not_exists}\n */\nexport const attributeNotExists = (attr: string): Condition => ({\n type: \"attributeNotExists\",\n attr,\n});\n\n// --- Logical Operators ---\n\n/**\n * Combines multiple conditions with AND operator\n * @example\n * and(\n * eq(\"status\", \"ACTIVE\"),\n * gt(\"age\", 18)\n * ) // status = \"ACTIVE\" AND age > 18\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Logical AWS DynamoDB - AND}\n */\nexport const and = (...conditions: Condition[]): Condition => ({\n type: \"and\",\n conditions,\n});\n\n/**\n * Combines multiple conditions with OR operator\n * @example\n * or(\n * eq(\"status\", \"PENDING\"),\n * eq(\"status\", \"PROCESSING\")\n * ) // status = \"PENDING\" OR status = \"PROCESSING\"\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Logical AWS DynamoDB - OR}\n */\nexport const or = (...conditions: Condition[]): Condition => ({\n type: \"or\",\n conditions,\n});\n\n/**\n * Negates a condition\n * @example\n * not(eq(\"status\", \"DELETED\")) // NOT status = \"DELETED\"\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Logical AWS DynamoDB - NOT}\n */\nexport const not = (condition: Condition): Condition => ({\n type: \"not\",\n condition,\n});\n\n/**\n * Type-safe operators for building key conditions in DynamoDB queries.\n * Only includes operators that are valid for key conditions.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions AWS DynamoDB - Key Condition Expressions}\n *\n * @example\n * // Using with sort key conditions\n * table.query({\n * pk: \"USER#123\",\n * sk: op => op.beginsWith(\"ORDER#\")\n * })\n */\nexport type KeyConditionOperator = {\n /** Equals comparison for key attributes */\n eq: (value: unknown) => Condition;\n /** Less than comparison for key attributes */\n lt: (value: unknown) => Condition;\n /** Less than or equal comparison for key attributes */\n lte: (value: unknown) => Condition;\n /** Greater than comparison for key attributes */\n gt: (value: unknown) => Condition;\n /** Greater than or equal comparison for key attributes */\n gte: (value: unknown) => Condition;\n /** Between range comparison for key attributes */\n between: (lower: unknown, upper: unknown) => Condition;\n /** Begins with comparison for key attributes */\n beginsWith: (value: unknown) => Condition;\n /** Combines multiple key conditions with AND */\n and: (...conditions: Condition[]) => Condition;\n};\n\n/**\n * Type-safe operators for building conditions in DynamoDB operations.\n * Includes all available condition operators with proper type inference.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html AWS DynamoDB - Condition Expressions}\n *\n * @example\n * // Using with type-safe conditions\n * interface User {\n * status: string;\n * age: number;\n * email?: string;\n * }\n *\n * table.scan<User>()\n * .where(op => op.and(\n * op.eq(\"status\", \"ACTIVE\"),\n * op.gt(\"age\", 18),\n * op.attributeExists(\"email\")\n * ))\n *\n * @template T The type of the item being operated on\n */\nexport type ConditionOperator<T extends DynamoItem> = {\n eq: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n ne: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n lt: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n lte: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n gt: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n gte: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n between: <K extends Path<T>>(attr: K, lower: PathType<T, K>, upper: PathType<T, K>) => Condition;\n beginsWith: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n contains: <K extends Path<T>>(attr: K, value: PathType<T, K>) => Condition;\n attributeExists: <K extends Path<T>>(attr: K) => Condition;\n attributeNotExists: <K extends Path<T>>(attr: K) => Condition;\n and: (...conditions: Condition[]) => Condition;\n or: (...conditions: Condition[]) => Condition;\n not: (condition: Condition) => Condition;\n};\n\n/**\n * Primary key type for QUERY operations.\n * Allows building complex key conditions for the sort key.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html AWS DynamoDB - Query Operations}\n *\n * @example\n * // Query items with a specific partition key and sort key prefix\n * table.query({\n * pk: \"USER#123\",\n * sk: op => op.beginsWith(\"ORDER#2023\")\n * })\n *\n * @example\n * // Query items within a specific sort key range\n * table.query({\n * pk: \"USER#123\",\n * sk: op => op.between(\"ORDER#2023-01\", \"ORDER#2023-12\")\n * })\n */\nexport type PrimaryKey = {\n /** Partition key value */\n pk: string;\n /** Optional sort key condition builder */\n sk?: (op: KeyConditionOperator) => Condition;\n};\n\n/**\n * Primary key type for GET and DELETE operations.\n * Used when you need to specify exact key values without conditions.\n * @see {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html AWS DynamoDB - Working with Items}\n *\n * @example\n * // Get a specific item by its complete primary key\n * table.get({\n * pk: \"USER#123\",\n * sk: \"PROFILE#123\"\n * })\n *\n * @example\n * // Delete a specific item by its complete primary key\n * table.delete({\n * pk: \"USER#123\",\n * sk: \"ORDER#456\"\n * })\n */\nexport type PrimaryKeyWithoutExpression = {\n /** Partition key value */\n pk: string;\n /** Optional sort key value */\n sk?: string;\n};\n","import type { ComparisonOperator, Condition, ExpressionParams, LogicalOperator } from \"./conditions\";\n\nexport const generateAttributeName = (params: ExpressionParams, attr: string): string => {\n // Check if the attribute already exists in the expressionAttributeNames\n for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {\n if (existingAttr === attr) {\n return existingName;\n }\n }\n\n // If not found, create a new attribute name\n const attrName = `#${Object.keys(params.expressionAttributeNames).length}`;\n params.expressionAttributeNames[attrName] = attr;\n return attrName;\n};\n\nexport const generateValueName = (params: ExpressionParams, value: unknown): string => {\n const valueName = `:${params.valueCounter.count++}`;\n params.expressionAttributeValues[valueName] = value;\n return valueName;\n};\n\nconst validateCondition = (condition: Condition, requiresAttr = true, requiresValue = true): void => {\n if (requiresAttr && !condition.attr) {\n throw new Error(`Attribute is required for ${condition.type} condition`);\n }\n\n if (requiresValue && condition.value === undefined) {\n throw new Error(`Value is required for ${condition.type} condition`);\n }\n};\n\nconst buildComparisonExpression = (condition: Condition, operator: string, params: ExpressionParams): string => {\n validateCondition(condition);\n\n if (!condition.attr) {\n throw new Error(`Attribute is required for ${condition.type} condition`);\n }\n\n const attrName = generateAttributeName(params, condition.attr);\n const valueName = generateValueName(params, condition.value);\n\n return `${attrName} ${operator} ${valueName}`;\n};\n\nconst buildBetweenExpression = (condition: Condition, params: ExpressionParams): string => {\n validateCondition(condition);\n\n if (!condition.attr) {\n throw new Error(`Attribute is required for ${condition.type} condition`);\n }\n\n if (!Array.isArray(condition.value) || condition.value.length !== 2) {\n throw new Error(\"Between condition requires an array of two values\");\n }\n\n const attrName = generateAttributeName(params, condition.attr);\n const lowerName = generateValueName(params, condition.value[0]);\n const upperName = generateValueName(params, condition.value[1]);\n\n return `${attrName} BETWEEN ${lowerName} AND ${upperName}`;\n};\n\nconst buildFunctionExpression = (functionName: string, condition: Condition, params: ExpressionParams): string => {\n validateCondition(condition);\n\n if (!condition.attr) {\n throw new Error(`Attribute is required for ${condition.type} condition`);\n }\n\n const attrName = generateAttributeName(params, condition.attr);\n const valueName = generateValueName(params, condition.value);\n\n return `${functionName}(${attrName}, ${valueName})`;\n};\n\nconst buildAttributeFunction = (functionName: string, condition: Condition, params: ExpressionParams): string => {\n validateCondition(condition, true, false);\n\n if (!condition.attr) {\n throw new Error(`Attribute is required for ${condition.type} condition`);\n }\n\n const attrName = generateAttributeName(params, condition.attr);\n return `${functionName}(${attrName})`;\n};\n\nconst buildLogicalExpression = (operator: string, conditions: Condition[], params: ExpressionParams): string => {\n if (!conditions || conditions.length === 0) {\n throw new Error(`At least one condition is required for ${operator} expression`);\n }\n\n const expressions = conditions.map((c) => buildExpression(c, params));\n return `(${expressions.join(` ${operator} `)})`;\n};\n\nexport const buildExpression = (condition: Condition, params: ExpressionParams): string => {\n if (!condition) return \"\";\n\n try {\n // Map of condition types to their expression builders\n const expressionBuilders: Record<ComparisonOperator | LogicalOperator, () => string> = {\n eq: () => buildComparisonExpression(condition, \"=\", params),\n ne: () => buildComparisonExpression(condition, \"<>\", params),\n lt: () => buildComparisonExpression(condition, \"<\", params),\n lte: () => buildComparisonExpression(condition, \"<=\", params),\n gt: () => buildComparisonExpression(condition, \">\", params),\n gte: () => buildComparisonExpression(condition, \">=\", params),\n between: () => buildBetweenExpression(condition, params),\n beginsWith: () => buildFunctionExpression(\"begins_with\", condition, params),\n contains: () => buildFunctionExpression(\"contains\", condition, params),\n attributeExists: () => buildAttributeFunction(\"attribute_exists\", condition, params),\n attributeNotExists: () => buildAttributeFunction(\"attribute_not_exists\", condition, params),\n and: () => {\n if (!condition.conditions) {\n throw new Error(\"Conditions array is required for AND operator\");\n }\n return buildLogicalExpression(\"AND\", condition.conditions, params);\n },\n or: () => {\n if (!condition.conditions) {\n throw new Error(\"Conditions array is required for OR operator\");\n }\n return buildLogicalExpression(\"OR\", condition.conditions, params);\n },\n not: () => {\n if (!condition.condition) {\n throw new Error(\"Condition is required for NOT operator\");\n }\n return `NOT (${buildExpression(condition.condition, params)})`;\n },\n };\n\n const builder = expressionBuilders[condition.type];\n if (!builder) {\n throw new Error(`Unknown condition type: ${condition.type}`);\n }\n\n return builder();\n } catch (error: unknown) {\n if (error instanceof Error) {\n console.error(`Error building expression for condition type ${condition.type}:`, error.message);\n } else {\n console.error(`Error building expression for condition type ${condition.type}:`, error);\n }\n throw error;\n }\n};\n\nexport const prepareExpressionParams = (\n condition?: Condition,\n): {\n expression?: string;\n names?: Record<string, string>;\n values?: Record<string, unknown>;\n} => {\n if (!condition) return {};\n\n const params: ExpressionParams = {\n expressionAttributeNames: {},\n expressionAttributeValues: {},\n valueCounter: { count: 0 },\n };\n\n const expression = buildExpression(condition, params);\n\n return {\n expression,\n names: Object.keys(params.expressionAttributeNames).length > 0 ? params.expressionAttributeNames : undefined,\n values: Object.keys(params.expressionAttributeValues).length > 0 ? params.expressionAttributeValues : undefined,\n };\n};\n","/**\n * Interface for DynamoDB command objects that can contain expressions\n */\nexport interface DynamoCommandWithExpressions {\n conditionExpression?: string;\n updateExpression?: string;\n filterExpression?: string;\n keyConditionExpression?: string;\n projectionExpression?: string;\n expressionAttributeNames?: Record<string, string>;\n expressionAttributeValues?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\ntype ReadableDynamoCommand = {\n conditionExpression?: string;\n updateExpression?: string;\n filterExpression?: string;\n keyConditionExpression?: string;\n projectionExpression?: string;\n};\n\n/**\n * Utility function to debug a DynamoDB command by replacing all placeholders\n * in expressions with their actual values.\n *\n * @param command Any DynamoDB command with expressions and attribute maps\n * @returns An object with the same structure but with readable expressions\n */\nexport function debugCommand<T extends DynamoCommandWithExpressions>(\n command: T,\n): {\n raw: T;\n readable: ReadableDynamoCommand;\n} {\n // Create a copy of the command\n const result: ReadableDynamoCommand = {};\n\n function replaceAliases(expressionString: string) {\n if (!expressionString) {\n return expressionString;\n }\n\n let replacedString = expressionString;\n for (const alias in command.expressionAttributeNames) {\n const attributeName = command.expressionAttributeNames[alias];\n const regex = new RegExp(alias, \"g\");\n\n replacedString = replacedString.replace(regex, attributeName as string);\n }\n\n for (const alias in command.expressionAttributeValues) {\n let attributeValue = command.expressionAttributeValues[alias];\n\n // Handle Set objects for better readability\n if (attributeValue instanceof Set) {\n const array = Array.from(attributeValue);\n attributeValue = `Set(${array.length}){${array.map((v) => JSON.stringify(v)).join(\", \")}}`;\n } else {\n // Stringify other values for display\n attributeValue = JSON.stringify(attributeValue);\n }\n\n const regex = new RegExp(alias, \"g\");\n replacedString = replacedString.replace(regex, attributeValue as string);\n }\n\n return replacedString;\n }\n\n if (command.updateExpression) {\n result.updateExpression = replaceAliases(command.updateExpression);\n }\n if (command.conditionExpression) {\n result.conditionExpression = replaceAliases(command.conditionExpression);\n }\n if (command.filterExpression) {\n result.filterExpression = replaceAliases(command.filterExpression);\n }\n if (command.keyConditionExpression) {\n result.keyConditionExpression = replaceAliases(command.keyConditionExpression);\n }\n if (command.projectionExpression) {\n result.projectionExpression = replaceAliases(command.projectionExpression);\n }\n\n return {\n raw: command,\n readable: result,\n };\n}\n","import type { Condition, ConditionOperator } from \"../conditions\";\nimport {\n eq,\n ne,\n lt,\n lte,\n gt,\n gte,\n between,\n beginsWith,\n contains,\n attributeExists,\n attributeNotExists,\n and,\n or,\n not,\n} from \"../conditions\";\nimport type { TransactionBuilder } from \"./transaction-builder\";\nimport { prepareExpressionParams } from \"../expression\";\nimport { debugCommand } from \"../utils/debug-expression\";\nimport type { PutCommandParams } from \"./builder-types\";\nimport type { Path, PathType } from \"./types\";\nimport type { DynamoItem } from \"../types\";\n\n/**\n * Configuration options for DynamoDB put operations.\n */\nexport interface PutOptions {\n /** Optional condition that must be satisfied for the put operation to succeed */\n condition?: Condition;\n /** Determines how to handle the return value of the put operation\n * @options\n * - NONE: No return value\n * - ALL_OLD: Returns the item's previous state if it existed\n * - CONSISTENT: (default) Performs a GET operation after the put to retrieve the item's new state\n */\n returnValues?: \"ALL_OLD\" | \"NONE\" | \"CONSISTENT\";\n}\n\ntype PutExecutor<T extends DynamoItem> = (params: PutCommandParams) => Promise<T>;\n\n/**\n * Builder for creating DynamoDB put operations.\n *\n * @example\n * ```typescript\n * // Add new dinosaur\n * const result = await new PutBuilder(executor, {\n * id: 'RAPTOR-001',\n * species: 'Velociraptor',\n * status: 'ACTIVE',\n * stats: {\n * health: 100,\n * age: 5,\n * threatLevel: 8\n * }\n * }, 'dinosaurs').execute();\n *\n * // Create new habitat with conditions\n * const result = await new PutBuilder(executor, {\n * id: 'PADDOCK-C',\n * type: 'CARNIVORE',\n * securityLevel: 'MAXIMUM',\n * capacity: 3,\n * environmentType: 'TROPICAL'\n * }, 'habitats')\n * .condition(op => op.attributeNotExists('id'))\n * .execute();\n * ```\n *\n * @typeParam T - The type of item being put into the table\n */\nexport class PutBuilder<T extends DynamoItem> {\n private readonly item: T;\n private options: PutOptions;\n private readonly executor: PutExecutor<T>;\n private readonly tableName: string;\n\n constructor(executor: PutExecutor<T>, item: T, tableName: string) {\n this.executor = executor;\n this.item = item;\n this.tableName = tableName;\n this.options = {\n returnValues: \"NONE\",\n };\n }\n\n /**\n * Sets multiple attributes of an item using an DynamoItem.\n *\n * @example\n * ```typescript\n * // Update multiple attributes\n * builder.set({\n * species: 'Tyrannosaurus Rex',\n * height: 20,\n * diet: 'CARNIVORE',\n * 'stats.threatLevel': 10\n * });\n * ```\n */\n set(values: Partial<T>): this;\n\n /**\n * Sets a single attribute to a specific value.\n *\n * @example\n * ```typescript\n * // Set simple attributes\n * builder\n * .set('status', 'SLEEPING')\n * .set('lastFeeding', new Date().toISOString());\n *\n * // Set nested attributes\n * builder\n * .set('location.zone', 'RESTRICTED')\n * .set('stats.health', 100);\n * ```\n */\n set<K extends Path<T>>(path: K, value: PathType<T, K>): this;\n set<K extends Path<T>>(valuesOrPath: K | Partial<T>, value?: PathType<T, K>): this {\n if (typeof valuesOrPath === \"object\") {\n Object.assign(this.item, valuesOrPath);\n } else {\n // @ts-ignore\n this.item[valuesOrPath] = value;\n }\n return this;\n }\n\n /**\n * Adds a condition that must be satisfied for the put operation to succeed.\n * Use this method when you need to:\n * - Prevent overwriting existing items (optimistic locking)\n * - Ensure items meet certain criteria before replacement\n * - Implement complex business rules for item updates\n *\n * @example\n * ```ts\n * // Ensure item doesn't exist (insert only)\n * builder.condition(op => op.attributeNotExists('id'))\n *\n * // Complex condition with version check\n * builder.condition(op =>\n * op.and([\n * op.attributeExists('id'),\n * op.eq('version', currentVersion),\n * op.eq('status', 'ACTIVE')\n * ])\n * )\n * ```\n *\n * @param condition - Either a Condition object or a callback function that builds the condition\n * @returns The builder instance for method chaining\n */\n /**\n * Adds a condition that must be satisfied for the put operation to succeed.\n *\n * @example\n * ```typescript\n * // Ensure unique dinosaur ID\n * builder.condition(op =>\n * op.attributeNotExists('id')\n * );\n *\n * // Verify habitat requirements\n * builder.condition(op =>\n * op.and([\n * op.eq('securityStatus', 'READY'),\n * op.attributeExists('lastInspection'),\n * op.gt('securityLevel', 5)\n * ])\n * );\n *\n * // Check breeding facility conditions\n * builder.condition(op =>\n * op.and([\n * op.between('temperature', 25, 30),\n * op.between('humidity', 60, 80),\n * op.eq('quarantineStatus', 'CLEAR')\n * ])\n * );\n * ```\n *\n * @param condition - Either a Condition object or a callback function that builds the condition\n * @returns The builder instance for method chaining\n */\n public condition(condition: Condition | ((op: ConditionOperator<T>) => Condition)): this {\n if (typeof condition === \"function\") {\n const conditionOperator: ConditionOperator<T> = {\n eq,\n ne,\n lt,\n lte,\n gt,\n gte,\n between,\n beginsWith,\n contains,\n attributeExists,\n attributeNotExists,\n and,\n or,\n not,\n };\n this.options.condition = condition(conditionOperator);\n } else {\n this.options.condition = condition;\n }\n return this;\n }\n\n /**\n * Sets whether to return the item's previous values (if it existed).\n *\n * @options\n * - NONE: No return value\n * - ALL_OLD: Returns the item's previous state if it existed, no read capacity units are consumed\n * - CONSISTENT: (default) Performs a GET operation after the put to retrieve the item's new state\n *\n * @example\n * ```ts\n * // Get previous dinosaur state\n * const result = await builder\n * .returnValues('ALL_OLD')\n * .execute();\n *\n * if (result) {\n * console.log('Previous profile:', {\n * species: result.species,\n * status: result.status,\n * stats: {\n * health: result.stats.health,\n * threatLevel: result.stats.threatLevel\n * }\n * });\n * }\n * ```\n *\n * @param returnValues - Use 'ALL_OLD' to return previous values if the item was overwritten, or 'NONE' (default).\n * @returns The builder instance for method chaining\n */\n public returnValues(returnValues: \"ALL_OLD\" | \"NONE\" | \"CONSISTENT\"): this {\n this.options.returnValues = returnValues;\n return this;\n }\n\n /**\n * Generate the DynamoDB command parameters\n */\n private toDynamoCommand(): PutCommandParams {\n const { expression, names, values } = prepareExpressionParams(this.options.condition);\n\n return {\n tableName: this.tableName,\n item: this.item,\n conditionExpression: expression,\n expressionAttributeNames: names,\n expressionAttributeValues: values,\n returnValues: this.options.returnValues,\n };\n }\n\n /**\n * Adds this put operation to a transaction.\n * Use this method when you need to:\n * - Transfer dinosaurs between habitats\n * - Initialize new breeding programs\n * - Update multiple facility records\n *\n * @example\n * ```ts\n * const transaction = new TransactionBuilder();\n *\n * // Add dinosaur to new habitat\n * new PutBuilder(executor, {\n * id: 'TREX-002',\n * location: 'PADDOCK-B',\n * status: 'ACTIVE',\n * transferDate: new Date().toISOString()\n * }, 'dinosaurs')\n * .withTransaction(transaction);\n *\n * // Update habitat records\n * new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-B' })\n * .add('occupants', 1)\n * .set('lastTransfer', new Date().toISOString())\n * .withTransaction(transaction);\n *\n * // Execute transfer atomically\n * await transaction.execute();\n * ```\n *\n * @param transaction - The transaction builder to add this operation to\n * @returns The builder instance for method chaining\n */\n public withTransaction(transaction: TransactionBuilder): this {\n const command = this.toDynamoCommand();\n transaction.putWithCommand(command);\n\n return this;\n }\n\n /**\n * Executes the put operation against DynamoDB.\n *\n * @example\n * ```ts\n * try {\n * // Put with condition and return old values\n * const result = await new PutBuilder(executor, newItem, 'myTable')\n * .condition(op => op.eq('version', 1))\n * .returnValues('ALL_OLD')\n * .execute();\n *\n * console.log('Put successful, old item:', result);\n * } catch (error) {\n * // Handle condition check failure or other errors\n * console.error('Put failed:', error);\n * }\n * ```\n *\n * @returns A promise that resolves to the operation result (type depends on returnValues setting)\n * @throws Will throw an error if the condition check fails or other DynamoDB errors occur\n */\n public async execute(): Promise<T | undefined> {\n const params = this.toDynamoCommand();\n return this.executor(params);\n }\n\n /**\n * Gets a human-readable representation of the put command\n * with all expression placeholders replaced by their actual values.\n *\n * @example\n * ```ts\n * const debugInfo = new PutBuilder(executor, {\n * id: 'RAPTOR-003',\n * species: 'Velociraptor',\n * status: 'QUARANTINE',\n * stats: {\n * health: 100,\n * aggressionLevel: 7,\n * age: 2\n * }\n * }, 'dinosaurs')\n * .condition(op =>\n * op.and([\n * op.attributeNotExists('id'),\n * op.eq('quarantineStatus', 'READY'),\n * op.gt('securityLevel', 8)\n * ])\n * )\n * .debug();\n *\n * console.log('Dinosaur transfer command:', debugInfo);\n * ```\n *\n * @returns A readable representation of the put command with resolved expressions\n */\n public debug(): DynamoItem {\n const command = this.toDynamoCommand();\n return debugCommand(command);\n }\n}\n"]}
|