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.
- package/README.md +751 -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 +894 -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 +892 -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 +228 -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 +224 -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.js → table.cjs} +88 -127
- package/dist/table.cjs.map +1 -0
- package/dist/table.d.cts +12 -0
- package/dist/table.d.ts +12 -0
- package/dist/{index.cjs → table.js} +86 -176
- 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,166 @@
|
|
|
1
|
+
import { C as Condition, P as PrimaryKeyWithoutExpression, b as ConditionOperator } from '../conditions--ld9a78i.js';
|
|
2
|
+
import { TransactionBuilder } from './transaction-builder.js';
|
|
3
|
+
import { D as DeleteCommandParams } from '../builder-types-C_PDZhnP.js';
|
|
4
|
+
import { DynamoItem } from '../types.js';
|
|
5
|
+
import '@aws-sdk/lib-dynamodb';
|
|
6
|
+
|
|
7
|
+
interface DeleteOptions {
|
|
8
|
+
condition?: Condition;
|
|
9
|
+
returnValues?: "ALL_OLD";
|
|
10
|
+
}
|
|
11
|
+
type DeleteExecutor = (params: DeleteCommandParams) => Promise<{
|
|
12
|
+
item?: DynamoItem;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Builder for creating DynamoDB delete operations.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Simple delete
|
|
20
|
+
* const result = await new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
|
|
21
|
+
* .execute();
|
|
22
|
+
*
|
|
23
|
+
* // Conditional delete with old value retrieval
|
|
24
|
+
* const result = await new DeleteBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
|
|
25
|
+
* .condition(op =>
|
|
26
|
+
* op.and([
|
|
27
|
+
* op.eq('status', 'DECOMMISSIONED'),
|
|
28
|
+
* op.eq('occupants', 0),
|
|
29
|
+
* op.lt('securityIncidents', 1)
|
|
30
|
+
* ])
|
|
31
|
+
* )
|
|
32
|
+
* .returnValues('ALL_OLD')
|
|
33
|
+
* .execute();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare class DeleteBuilder {
|
|
37
|
+
private options;
|
|
38
|
+
private readonly executor;
|
|
39
|
+
private readonly tableName;
|
|
40
|
+
private readonly key;
|
|
41
|
+
constructor(executor: DeleteExecutor, tableName: string, key: PrimaryKeyWithoutExpression);
|
|
42
|
+
/**
|
|
43
|
+
* Adds a condition that must be satisfied for the delete operation to succeed.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // Ensure dinosaur can be safely removed
|
|
48
|
+
* builder.condition(op =>
|
|
49
|
+
* op.and([
|
|
50
|
+
* op.eq('status', 'SEDATED'),
|
|
51
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
52
|
+
* op.attributeExists('lastCheckup')
|
|
53
|
+
* ])
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* // Verify habitat is empty
|
|
57
|
+
* builder.condition(op =>
|
|
58
|
+
* op.and([
|
|
59
|
+
* op.eq('occupants', 0),
|
|
60
|
+
* op.eq('maintenanceStatus', 'COMPLETE'),
|
|
61
|
+
* op.not(op.attributeExists('activeAlerts'))
|
|
62
|
+
* ])
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
67
|
+
* @returns The builder instance for method chaining
|
|
68
|
+
*/
|
|
69
|
+
condition<T extends DynamoItem>(condition: Condition | ((op: ConditionOperator<T>) => Condition)): DeleteBuilder;
|
|
70
|
+
/**
|
|
71
|
+
* Sets whether to return the item's attribute values before deletion.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* // Archive dinosaur data before removal
|
|
76
|
+
* const result = await builder
|
|
77
|
+
* .returnValues('ALL_OLD')
|
|
78
|
+
* .execute();
|
|
79
|
+
*
|
|
80
|
+
* if (result.item) {
|
|
81
|
+
* console.log('Removed dinosaur data:', {
|
|
82
|
+
* species: result.item.species,
|
|
83
|
+
* age: result.item.age,
|
|
84
|
+
* lastLocation: result.item.location
|
|
85
|
+
* });
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param returnValues - Use 'ALL_OLD' to return all attributes of the deleted item
|
|
90
|
+
* @returns The builder instance for method chaining
|
|
91
|
+
*/
|
|
92
|
+
returnValues(returnValues: "ALL_OLD"): DeleteBuilder;
|
|
93
|
+
/**
|
|
94
|
+
* Generate the DynamoDB command parameters
|
|
95
|
+
*/
|
|
96
|
+
private toDynamoCommand;
|
|
97
|
+
/**
|
|
98
|
+
* Adds this delete operation to a transaction.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const transaction = new TransactionBuilder();
|
|
103
|
+
*
|
|
104
|
+
* // Remove dinosaur from old habitat
|
|
105
|
+
* new DeleteBuilder(executor, 'dinosaurs', { id: 'RAPTOR-001' })
|
|
106
|
+
* .condition(op => op.eq('status', 'SEDATED'))
|
|
107
|
+
* .withTransaction(transaction);
|
|
108
|
+
*
|
|
109
|
+
* // Update old habitat occupancy
|
|
110
|
+
* new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
|
|
111
|
+
* .add('occupants', -1)
|
|
112
|
+
* .withTransaction(transaction);
|
|
113
|
+
*
|
|
114
|
+
* // Execute transfer atomically
|
|
115
|
+
* await transaction.execute();
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @param transaction - The transaction builder to add this operation to
|
|
119
|
+
*/
|
|
120
|
+
withTransaction(transaction: TransactionBuilder): void;
|
|
121
|
+
/**
|
|
122
|
+
* Executes the delete operation against DynamoDB.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* // Delete with condition and retrieve old values
|
|
127
|
+
* const result = await new DeleteBuilder(executor, 'myTable', { id: '123' })
|
|
128
|
+
* .condition(op => op.eq('status', 'INACTIVE'))
|
|
129
|
+
* .returnValues('ALL_OLD')
|
|
130
|
+
* .execute();
|
|
131
|
+
*
|
|
132
|
+
* if (result.item) {
|
|
133
|
+
* console.log('Deleted item:', result.item);
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @returns A promise that resolves to an object containing the deleted item's attributes (if returnValues is 'ALL_OLD')
|
|
138
|
+
*/
|
|
139
|
+
execute(): Promise<{
|
|
140
|
+
item?: DynamoItem;
|
|
141
|
+
}>;
|
|
142
|
+
/**
|
|
143
|
+
* Gets a human-readable representation of the delete command
|
|
144
|
+
* with all expression placeholders replaced by their actual values.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const debugInfo = new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
|
|
149
|
+
* .condition(op => op.and([
|
|
150
|
+
* op.eq('status', 'SEDATED'),
|
|
151
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
152
|
+
* op.gt('sedationLevel', 8)
|
|
153
|
+
* op.eq('version', 1),
|
|
154
|
+
* op.attributeExists('status')
|
|
155
|
+
* ]))
|
|
156
|
+
* .debug();
|
|
157
|
+
*
|
|
158
|
+
* console.log('Delete command:', debugInfo);
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @returns A readable representation of the delete command with resolved expressions
|
|
162
|
+
*/
|
|
163
|
+
debug(): DynamoItem;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { DeleteBuilder, type DeleteOptions };
|
|
@@ -0,0 +1,403 @@
|
|
|
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/delete-builder.ts
|
|
222
|
+
var DeleteBuilder = class {
|
|
223
|
+
options = {
|
|
224
|
+
returnValues: "ALL_OLD"
|
|
225
|
+
};
|
|
226
|
+
executor;
|
|
227
|
+
tableName;
|
|
228
|
+
key;
|
|
229
|
+
constructor(executor, tableName, key) {
|
|
230
|
+
this.executor = executor;
|
|
231
|
+
this.tableName = tableName;
|
|
232
|
+
this.key = key;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Adds a condition that must be satisfied for the delete operation to succeed.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* // Ensure dinosaur can be safely removed
|
|
240
|
+
* builder.condition(op =>
|
|
241
|
+
* op.and([
|
|
242
|
+
* op.eq('status', 'SEDATED'),
|
|
243
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
244
|
+
* op.attributeExists('lastCheckup')
|
|
245
|
+
* ])
|
|
246
|
+
* );
|
|
247
|
+
*
|
|
248
|
+
* // Verify habitat is empty
|
|
249
|
+
* builder.condition(op =>
|
|
250
|
+
* op.and([
|
|
251
|
+
* op.eq('occupants', 0),
|
|
252
|
+
* op.eq('maintenanceStatus', 'COMPLETE'),
|
|
253
|
+
* op.not(op.attributeExists('activeAlerts'))
|
|
254
|
+
* ])
|
|
255
|
+
* );
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
259
|
+
* @returns The builder instance for method chaining
|
|
260
|
+
*/
|
|
261
|
+
condition(condition) {
|
|
262
|
+
if (typeof condition === "function") {
|
|
263
|
+
const conditionOperator = {
|
|
264
|
+
eq,
|
|
265
|
+
ne,
|
|
266
|
+
lt,
|
|
267
|
+
lte,
|
|
268
|
+
gt,
|
|
269
|
+
gte,
|
|
270
|
+
between,
|
|
271
|
+
beginsWith,
|
|
272
|
+
contains,
|
|
273
|
+
attributeExists,
|
|
274
|
+
attributeNotExists,
|
|
275
|
+
and,
|
|
276
|
+
or,
|
|
277
|
+
not
|
|
278
|
+
};
|
|
279
|
+
this.options.condition = condition(conditionOperator);
|
|
280
|
+
} else {
|
|
281
|
+
this.options.condition = condition;
|
|
282
|
+
}
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Sets whether to return the item's attribute values before deletion.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* // Archive dinosaur data before removal
|
|
291
|
+
* const result = await builder
|
|
292
|
+
* .returnValues('ALL_OLD')
|
|
293
|
+
* .execute();
|
|
294
|
+
*
|
|
295
|
+
* if (result.item) {
|
|
296
|
+
* console.log('Removed dinosaur data:', {
|
|
297
|
+
* species: result.item.species,
|
|
298
|
+
* age: result.item.age,
|
|
299
|
+
* lastLocation: result.item.location
|
|
300
|
+
* });
|
|
301
|
+
* }
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @param returnValues - Use 'ALL_OLD' to return all attributes of the deleted item
|
|
305
|
+
* @returns The builder instance for method chaining
|
|
306
|
+
*/
|
|
307
|
+
returnValues(returnValues) {
|
|
308
|
+
this.options.returnValues = returnValues;
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Generate the DynamoDB command parameters
|
|
313
|
+
*/
|
|
314
|
+
toDynamoCommand() {
|
|
315
|
+
const { expression, names, values } = prepareExpressionParams(this.options.condition);
|
|
316
|
+
return {
|
|
317
|
+
tableName: this.tableName,
|
|
318
|
+
key: this.key,
|
|
319
|
+
conditionExpression: expression,
|
|
320
|
+
expressionAttributeNames: names,
|
|
321
|
+
expressionAttributeValues: values,
|
|
322
|
+
returnValues: this.options.returnValues
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Adds this delete operation to a transaction.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* const transaction = new TransactionBuilder();
|
|
331
|
+
*
|
|
332
|
+
* // Remove dinosaur from old habitat
|
|
333
|
+
* new DeleteBuilder(executor, 'dinosaurs', { id: 'RAPTOR-001' })
|
|
334
|
+
* .condition(op => op.eq('status', 'SEDATED'))
|
|
335
|
+
* .withTransaction(transaction);
|
|
336
|
+
*
|
|
337
|
+
* // Update old habitat occupancy
|
|
338
|
+
* new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
|
|
339
|
+
* .add('occupants', -1)
|
|
340
|
+
* .withTransaction(transaction);
|
|
341
|
+
*
|
|
342
|
+
* // Execute transfer atomically
|
|
343
|
+
* await transaction.execute();
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @param transaction - The transaction builder to add this operation to
|
|
347
|
+
*/
|
|
348
|
+
withTransaction(transaction) {
|
|
349
|
+
const command = this.toDynamoCommand();
|
|
350
|
+
transaction.deleteWithCommand(command);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Executes the delete operation against DynamoDB.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* // Delete with condition and retrieve old values
|
|
358
|
+
* const result = await new DeleteBuilder(executor, 'myTable', { id: '123' })
|
|
359
|
+
* .condition(op => op.eq('status', 'INACTIVE'))
|
|
360
|
+
* .returnValues('ALL_OLD')
|
|
361
|
+
* .execute();
|
|
362
|
+
*
|
|
363
|
+
* if (result.item) {
|
|
364
|
+
* console.log('Deleted item:', result.item);
|
|
365
|
+
* }
|
|
366
|
+
* ```
|
|
367
|
+
*
|
|
368
|
+
* @returns A promise that resolves to an object containing the deleted item's attributes (if returnValues is 'ALL_OLD')
|
|
369
|
+
*/
|
|
370
|
+
async execute() {
|
|
371
|
+
const params = this.toDynamoCommand();
|
|
372
|
+
return this.executor(params);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Gets a human-readable representation of the delete command
|
|
376
|
+
* with all expression placeholders replaced by their actual values.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```ts
|
|
380
|
+
* const debugInfo = new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
|
|
381
|
+
* .condition(op => op.and([
|
|
382
|
+
* op.eq('status', 'SEDATED'),
|
|
383
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
384
|
+
* op.gt('sedationLevel', 8)
|
|
385
|
+
* op.eq('version', 1),
|
|
386
|
+
* op.attributeExists('status')
|
|
387
|
+
* ]))
|
|
388
|
+
* .debug();
|
|
389
|
+
*
|
|
390
|
+
* console.log('Delete command:', debugInfo);
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
* @returns A readable representation of the delete command with resolved expressions
|
|
394
|
+
*/
|
|
395
|
+
debug() {
|
|
396
|
+
const command = this.toDynamoCommand();
|
|
397
|
+
return debugCommand(command);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
export { DeleteBuilder };
|
|
402
|
+
//# sourceMappingURL=delete-builder.js.map
|
|
403
|
+
//# sourceMappingURL=delete-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/conditions.ts","../../src/expression.ts","../../src/utils/debug-expression.ts","../../src/builders/delete-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,G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AAkB,OAAO,CAAA;AAAA;AACvC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAa,OAA0C,GAAA;AACrD,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,EAuBA,KAAoB,GAAA;AAClB,IAAM,MAAA,OAAA,GAAU,KAAK,eAAgB,EAAA;AACrC,IAAA,OAAO,aAAa,OAAO,CAAA;AAAA;AAE/B","file":"delete-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, PrimaryKeyWithoutExpression } 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 { DeleteCommandParams } from \"./builder-types\";\nimport type { DynamoItem } from \"../types\";\n\nexport interface DeleteOptions {\n condition?: Condition;\n returnValues?: \"ALL_OLD\";\n}\n\ntype DeleteExecutor = (params: DeleteCommandParams) => Promise<{ item?: DynamoItem }>;\n\n/**\n * Builder for creating DynamoDB delete operations.\n *\n * @example\n * ```typescript\n * // Simple delete\n * const result = await new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })\n * .execute();\n *\n * // Conditional delete with old value retrieval\n * const result = await new DeleteBuilder(executor, 'habitats', { id: 'PADDOCK-A' })\n * .condition(op =>\n * op.and([\n * op.eq('status', 'DECOMMISSIONED'),\n * op.eq('occupants', 0),\n * op.lt('securityIncidents', 1)\n * ])\n * )\n * .returnValues('ALL_OLD')\n * .execute();\n * ```\n */\nexport class DeleteBuilder {\n private options: DeleteOptions = {\n returnValues: \"ALL_OLD\",\n };\n private readonly executor: DeleteExecutor;\n private readonly tableName: string;\n private readonly key: PrimaryKeyWithoutExpression;\n\n constructor(executor: DeleteExecutor, tableName: string, key: PrimaryKeyWithoutExpression) {\n this.executor = executor;\n this.tableName = tableName;\n this.key = key;\n }\n\n /**\n * Adds a condition that must be satisfied for the delete operation to succeed.\n *\n * @example\n * ```typescript\n * // Ensure dinosaur can be safely removed\n * builder.condition(op =>\n * op.and([\n * op.eq('status', 'SEDATED'),\n * op.eq('location', 'MEDICAL_BAY'),\n * op.attributeExists('lastCheckup')\n * ])\n * );\n *\n * // Verify habitat is empty\n * builder.condition(op =>\n * op.and([\n * op.eq('occupants', 0),\n * op.eq('maintenanceStatus', 'COMPLETE'),\n * op.not(op.attributeExists('activeAlerts'))\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<T extends DynamoItem>(\n condition: Condition | ((op: ConditionOperator<T>) => Condition),\n ): DeleteBuilder {\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 attribute values before deletion.\n *\n * @example\n * ```ts\n * // Archive dinosaur data before removal\n * const result = await builder\n * .returnValues('ALL_OLD')\n * .execute();\n *\n * if (result.item) {\n * console.log('Removed dinosaur data:', {\n * species: result.item.species,\n * age: result.item.age,\n * lastLocation: result.item.location\n * });\n * }\n * ```\n *\n * @param returnValues - Use 'ALL_OLD' to return all attributes of the deleted item\n * @returns The builder instance for method chaining\n */\n public returnValues(returnValues: \"ALL_OLD\"): DeleteBuilder {\n this.options.returnValues = returnValues;\n return this;\n }\n\n /**\n * Generate the DynamoDB command parameters\n */\n private toDynamoCommand(): DeleteCommandParams {\n const { expression, names, values } = prepareExpressionParams(this.options.condition);\n\n return {\n tableName: this.tableName,\n key: this.key,\n conditionExpression: expression,\n expressionAttributeNames: names,\n expressionAttributeValues: values,\n returnValues: this.options.returnValues,\n };\n }\n\n /**\n * Adds this delete operation to a transaction.\n *\n * @example\n * ```ts\n * const transaction = new TransactionBuilder();\n *\n * // Remove dinosaur from old habitat\n * new DeleteBuilder(executor, 'dinosaurs', { id: 'RAPTOR-001' })\n * .condition(op => op.eq('status', 'SEDATED'))\n * .withTransaction(transaction);\n *\n * // Update old habitat occupancy\n * new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-A' })\n * .add('occupants', -1)\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 */\n public withTransaction(transaction: TransactionBuilder) {\n const command = this.toDynamoCommand();\n\n transaction.deleteWithCommand(command);\n }\n\n /**\n * Executes the delete operation against DynamoDB.\n *\n * @example\n * ```ts\n * // Delete with condition and retrieve old values\n * const result = await new DeleteBuilder(executor, 'myTable', { id: '123' })\n * .condition(op => op.eq('status', 'INACTIVE'))\n * .returnValues('ALL_OLD')\n * .execute();\n *\n * if (result.item) {\n * console.log('Deleted item:', result.item);\n * }\n * ```\n *\n * @returns A promise that resolves to an object containing the deleted item's attributes (if returnValues is 'ALL_OLD')\n */\n public async execute(): Promise<{ item?: DynamoItem }> {\n const params = this.toDynamoCommand();\n return this.executor(params);\n }\n\n /**\n * Gets a human-readable representation of the delete command\n * with all expression placeholders replaced by their actual values.\n *\n * @example\n * ```ts\n * const debugInfo = new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })\n * .condition(op => op.and([\n * op.eq('status', 'SEDATED'),\n * op.eq('location', 'MEDICAL_BAY'),\n * op.gt('sedationLevel', 8)\n * op.eq('version', 1),\n * op.attributeExists('status')\n * ]))\n * .debug();\n *\n * console.log('Delete command:', debugInfo);\n * ```\n *\n * @returns A readable representation of the delete command with resolved expressions\n */\n debug(): DynamoItem {\n const command = this.toDynamoCommand();\n return debugCommand(command);\n }\n}\n"]}
|