dyno-table 0.1.7 → 0.1.8
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/LICENSE +21 -0
- package/README.md +115 -17
- 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.js +392 -0
- package/dist/builders/condition-check-builder.js.map +1 -0
- package/dist/builders/delete-builder.cjs +422 -0
- package/dist/builders/delete-builder.cjs.map +1 -0
- package/dist/builders/delete-builder.js +420 -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.js +197 -0
- package/dist/builders/paginator.js.map +1 -0
- package/dist/builders/put-builder.cjs +468 -0
- package/dist/builders/put-builder.cjs.map +1 -0
- package/dist/builders/put-builder.js +466 -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.js +672 -0
- package/dist/builders/query-builder.js.map +1 -0
- package/dist/builders/transaction-builder.cjs +876 -0
- package/dist/builders/transaction-builder.cjs.map +1 -0
- package/dist/builders/transaction-builder.js +874 -0
- package/dist/builders/transaction-builder.js.map +1 -0
- package/dist/builders/update-builder.cjs +662 -0
- package/dist/builders/update-builder.cjs.map +1 -0
- package/dist/builders/update-builder.js +660 -0
- package/dist/builders/update-builder.js.map +1 -0
- package/dist/conditions.cjs +59 -0
- package/dist/conditions.cjs.map +1 -0
- package/dist/conditions.js +43 -0
- package/dist/conditions.js.map +1 -0
- package/dist/entity.cjs +169 -0
- package/dist/entity.cjs.map +1 -0
- package/dist/entity.js +165 -0
- package/dist/entity.js.map +1 -0
- package/dist/index.cjs +3333 -0
- package/dist/index.d.cts +2971 -0
- package/dist/index.d.ts +386 -338
- package/dist/index.js +247 -232
- package/dist/standard-schema.cjs +4 -0
- package/dist/standard-schema.cjs.map +1 -0
- package/dist/standard-schema.js +3 -0
- package/dist/standard-schema.js.map +1 -0
- package/dist/table.cjs +3265 -0
- package/dist/table.cjs.map +1 -0
- package/dist/table.js +3263 -0
- package/dist/table.js.map +1 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/key-template.cjs +19 -0
- package/dist/utils/key-template.cjs.map +1 -0
- package/dist/utils/key-template.js +17 -0
- package/dist/utils/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.js +17 -0
- package/dist/utils/sort-key-template.js.map +1 -0
- package/package.json +24 -26
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Scott Maunder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -28,23 +28,45 @@ await table
|
|
|
28
28
|
|
|
29
29
|
## 📑 Table of Contents
|
|
30
30
|
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
- [
|
|
34
|
-
- [
|
|
35
|
-
- [
|
|
36
|
-
|
|
37
|
-
- [Type
|
|
38
|
-
- [
|
|
39
|
-
|
|
40
|
-
- [Batch
|
|
41
|
-
|
|
42
|
-
- [
|
|
43
|
-
- [
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
31
|
+
- [🦖 dyno-table ](#-dyno-table--)
|
|
32
|
+
- [🌟 Why dyno-table?](#-why-dyno-table)
|
|
33
|
+
- [📑 Table of Contents](#-table-of-contents)
|
|
34
|
+
- [📦 Installation](#-installation)
|
|
35
|
+
- [🚀 Quick Start](#-quick-start)
|
|
36
|
+
- [1. Configure Your Table](#1-configure-your-table)
|
|
37
|
+
- [2. Perform Type-Safe Operations](#2-perform-type-safe-operations)
|
|
38
|
+
- [🧩 Advanced Features](#-advanced-features)
|
|
39
|
+
- [Transactional Operations](#transactional-operations)
|
|
40
|
+
- [Batch Processing](#batch-processing)
|
|
41
|
+
- [Pagination Made Simple](#pagination-made-simple)
|
|
42
|
+
- [🛡️ Type-Safe Query Building](#️-type-safe-query-building)
|
|
43
|
+
- [Comparison Operators](#comparison-operators)
|
|
44
|
+
- [Logical Operators](#logical-operators)
|
|
45
|
+
- [Query Operations](#query-operations)
|
|
46
|
+
- [Put Operations](#put-operations)
|
|
47
|
+
- [Update Operations](#update-operations)
|
|
48
|
+
- [Condition Operators](#condition-operators)
|
|
49
|
+
- [Multiple Operations](#multiple-operations)
|
|
50
|
+
- [🔄 Type Safety Features](#-type-safety-features)
|
|
51
|
+
- [Nested Object Support](#nested-object-support)
|
|
52
|
+
- [Type-Safe Conditions](#type-safe-conditions)
|
|
53
|
+
- [🔄 Batch Operations](#-batch-operations)
|
|
54
|
+
- [Batch Get](#batch-get)
|
|
55
|
+
- [Batch Write](#batch-write)
|
|
56
|
+
- [🔒 Transaction Operations](#-transaction-operations)
|
|
57
|
+
- [Transaction Builder](#transaction-builder)
|
|
58
|
+
- [Transaction Options](#transaction-options)
|
|
59
|
+
- [🏗️ Entity Pattern Best Practices (Coming Soon TM)](#️-entity-pattern-best-practices-coming-soon-tm)
|
|
60
|
+
- [🚨 Error Handling](#-error-handling)
|
|
61
|
+
- [📚 API Reference](#-api-reference)
|
|
62
|
+
- [Condition Operators](#condition-operators-1)
|
|
63
|
+
- [Comparison Operators](#comparison-operators-1)
|
|
64
|
+
- [Attribute Operators](#attribute-operators)
|
|
65
|
+
- [Logical Operators](#logical-operators-1)
|
|
66
|
+
- [Key Condition Operators](#key-condition-operators)
|
|
67
|
+
- [🔮 Future Roadmap](#-future-roadmap)
|
|
68
|
+
- [🤝 Contributing](#-contributing)
|
|
69
|
+
- [🦔 Running Examples](#-running-examples)
|
|
48
70
|
|
|
49
71
|
## 📦 Installation
|
|
50
72
|
|
|
@@ -426,6 +448,51 @@ const limited = await table
|
|
|
426
448
|
.execute();
|
|
427
449
|
```
|
|
428
450
|
|
|
451
|
+
### Put Operations
|
|
452
|
+
|
|
453
|
+
| Operation | Method Example | Description |
|
|
454
|
+
|---------------------|---------------------------------------------------------------------|------------------------------------------------------------------------|
|
|
455
|
+
| **Create New Item** | `.create<Dinosaur>({ pk: "SPECIES#trex", sk: "PROFILE#001", ... })` | Creates a new item with a condition to ensure it doesn't already exist |
|
|
456
|
+
| **Put Item** | `.put<Dinosaur>({ pk: "SPECIES#trex", sk: "PROFILE#001", ... })` | Creates or replaces an item |
|
|
457
|
+
| **With Condition** | `.put(item).condition(op => op.attributeNotExists("pk"))` | Adds a condition that must be satisfied |
|
|
458
|
+
|
|
459
|
+
#### Return Values
|
|
460
|
+
|
|
461
|
+
Control what data is returned from put operations:
|
|
462
|
+
|
|
463
|
+
| Option | Description | Example |
|
|
464
|
+
|----------------|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
|
|
465
|
+
| **NONE** | Default. No return value. | `.put(item).returnValues("NONE").execute()` |
|
|
466
|
+
| **ALL_OLD** | Returns the item's previous state if it existed. (Does not consume any RCU and returns strongly consistent values) | `.put(item).returnValues("ALL_OLD").execute()` |
|
|
467
|
+
| **CONSISTENT** | Performs a consistent GET operation after the put to retrieve the item's new state. (Does consume RCU) | `.put(item).returnValues("CONSISTENT").execute()` |
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
// Create with no return value (default)
|
|
471
|
+
await table.put<Dinosaur>({
|
|
472
|
+
pk: "SPECIES#trex",
|
|
473
|
+
sk: "PROFILE#001",
|
|
474
|
+
name: "Tyrannosaurus Rex",
|
|
475
|
+
diet: "carnivore"
|
|
476
|
+
}).execute();
|
|
477
|
+
|
|
478
|
+
// Create and return the newly created item
|
|
479
|
+
const newDino = await table.put<Dinosaur>({
|
|
480
|
+
pk: "SPECIES#trex",
|
|
481
|
+
sk: "PROFILE#002",
|
|
482
|
+
name: "Tyrannosaurus Rex",
|
|
483
|
+
diet: "carnivore"
|
|
484
|
+
}).returnValues("CONSISTENT").execute();
|
|
485
|
+
|
|
486
|
+
// Update with condition and get previous values
|
|
487
|
+
const oldDino = await table.put<Dinosaur>({
|
|
488
|
+
pk: "SPECIES#trex",
|
|
489
|
+
sk: "PROFILE#001",
|
|
490
|
+
name: "Tyrannosaurus Rex",
|
|
491
|
+
diet: "omnivore", // Updated diet
|
|
492
|
+
discoveryYear: 1905
|
|
493
|
+
}).returnValues("ALL_OLD").execute();
|
|
494
|
+
```
|
|
495
|
+
|
|
429
496
|
### Update Operations
|
|
430
497
|
|
|
431
498
|
| Operation | Method Example | Generated Expression |
|
|
@@ -782,3 +849,34 @@ pnpm test
|
|
|
782
849
|
# Build the project
|
|
783
850
|
pnpm build
|
|
784
851
|
```
|
|
852
|
+
|
|
853
|
+
## 🦔 Running Examples
|
|
854
|
+
|
|
855
|
+
There's a few pre-configured example scripts in the `examples` directory.
|
|
856
|
+
|
|
857
|
+
First you'll need to install the dependencies:
|
|
858
|
+
|
|
859
|
+
```bash
|
|
860
|
+
pnpm install
|
|
861
|
+
```
|
|
862
|
+
Then setup the test table in local DynamoDB by running the following command:
|
|
863
|
+
|
|
864
|
+
```bash
|
|
865
|
+
pnpm run ddb:start
|
|
866
|
+
pnpm run local:setup
|
|
867
|
+
```
|
|
868
|
+
|
|
869
|
+
To run the examples, you can use the following command:
|
|
870
|
+
|
|
871
|
+
```bash
|
|
872
|
+
npx tsx examples/[EXAMPLE_NAME].ts
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
To view the test table GUI in action: [DynamoDB Admin](http://localhost:8001/)
|
|
876
|
+
|
|
877
|
+
<br />
|
|
878
|
+
To teardown the test table when you're done, run the following command:
|
|
879
|
+
|
|
880
|
+
```bash
|
|
881
|
+
pnpm run local:teardown
|
|
882
|
+
```
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/conditions.ts
|
|
4
|
+
var createComparisonCondition = (type) => (attr, value) => ({
|
|
5
|
+
type,
|
|
6
|
+
attr,
|
|
7
|
+
value
|
|
8
|
+
});
|
|
9
|
+
var eq = createComparisonCondition("eq");
|
|
10
|
+
var ne = createComparisonCondition("ne");
|
|
11
|
+
var lt = createComparisonCondition("lt");
|
|
12
|
+
var lte = createComparisonCondition("lte");
|
|
13
|
+
var gt = createComparisonCondition("gt");
|
|
14
|
+
var gte = createComparisonCondition("gte");
|
|
15
|
+
var between = (attr, lower, upper) => ({
|
|
16
|
+
type: "between",
|
|
17
|
+
attr,
|
|
18
|
+
value: [lower, upper]
|
|
19
|
+
});
|
|
20
|
+
var beginsWith = createComparisonCondition("beginsWith");
|
|
21
|
+
var contains = createComparisonCondition("contains");
|
|
22
|
+
var attributeExists = (attr) => ({
|
|
23
|
+
type: "attributeExists",
|
|
24
|
+
attr
|
|
25
|
+
});
|
|
26
|
+
var attributeNotExists = (attr) => ({
|
|
27
|
+
type: "attributeNotExists",
|
|
28
|
+
attr
|
|
29
|
+
});
|
|
30
|
+
var and = (...conditions) => ({
|
|
31
|
+
type: "and",
|
|
32
|
+
conditions
|
|
33
|
+
});
|
|
34
|
+
var or = (...conditions) => ({
|
|
35
|
+
type: "or",
|
|
36
|
+
conditions
|
|
37
|
+
});
|
|
38
|
+
var not = (condition) => ({
|
|
39
|
+
type: "not",
|
|
40
|
+
condition
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// src/expression.ts
|
|
44
|
+
var generateAttributeName = (params, attr) => {
|
|
45
|
+
for (const [existingName, existingAttr] of Object.entries(params.expressionAttributeNames)) {
|
|
46
|
+
if (existingAttr === attr) {
|
|
47
|
+
return existingName;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const attrName = `#${Object.keys(params.expressionAttributeNames).length}`;
|
|
51
|
+
params.expressionAttributeNames[attrName] = attr;
|
|
52
|
+
return attrName;
|
|
53
|
+
};
|
|
54
|
+
var generateValueName = (params, value) => {
|
|
55
|
+
const valueName = `:${params.valueCounter.count++}`;
|
|
56
|
+
params.expressionAttributeValues[valueName] = value;
|
|
57
|
+
return valueName;
|
|
58
|
+
};
|
|
59
|
+
var validateCondition = (condition, requiresAttr = true, requiresValue = true) => {
|
|
60
|
+
if (requiresAttr && !condition.attr) {
|
|
61
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
62
|
+
}
|
|
63
|
+
if (requiresValue && condition.value === void 0) {
|
|
64
|
+
throw new Error(`Value is required for ${condition.type} condition`);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var buildComparisonExpression = (condition, operator, params) => {
|
|
68
|
+
validateCondition(condition);
|
|
69
|
+
if (!condition.attr) {
|
|
70
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
71
|
+
}
|
|
72
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
73
|
+
const valueName = generateValueName(params, condition.value);
|
|
74
|
+
return `${attrName} ${operator} ${valueName}`;
|
|
75
|
+
};
|
|
76
|
+
var buildBetweenExpression = (condition, params) => {
|
|
77
|
+
validateCondition(condition);
|
|
78
|
+
if (!condition.attr) {
|
|
79
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(condition.value) || condition.value.length !== 2) {
|
|
82
|
+
throw new Error("Between condition requires an array of two values");
|
|
83
|
+
}
|
|
84
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
85
|
+
const lowerName = generateValueName(params, condition.value[0]);
|
|
86
|
+
const upperName = generateValueName(params, condition.value[1]);
|
|
87
|
+
return `${attrName} BETWEEN ${lowerName} AND ${upperName}`;
|
|
88
|
+
};
|
|
89
|
+
var buildFunctionExpression = (functionName, condition, params) => {
|
|
90
|
+
validateCondition(condition);
|
|
91
|
+
if (!condition.attr) {
|
|
92
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
93
|
+
}
|
|
94
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
95
|
+
const valueName = generateValueName(params, condition.value);
|
|
96
|
+
return `${functionName}(${attrName}, ${valueName})`;
|
|
97
|
+
};
|
|
98
|
+
var buildAttributeFunction = (functionName, condition, params) => {
|
|
99
|
+
validateCondition(condition, true, false);
|
|
100
|
+
if (!condition.attr) {
|
|
101
|
+
throw new Error(`Attribute is required for ${condition.type} condition`);
|
|
102
|
+
}
|
|
103
|
+
const attrName = generateAttributeName(params, condition.attr);
|
|
104
|
+
return `${functionName}(${attrName})`;
|
|
105
|
+
};
|
|
106
|
+
var buildLogicalExpression = (operator, conditions, params) => {
|
|
107
|
+
if (!conditions || conditions.length === 0) {
|
|
108
|
+
throw new Error(`At least one condition is required for ${operator} expression`);
|
|
109
|
+
}
|
|
110
|
+
const expressions = conditions.map((c) => buildExpression(c, params));
|
|
111
|
+
return `(${expressions.join(` ${operator} `)})`;
|
|
112
|
+
};
|
|
113
|
+
var buildExpression = (condition, params) => {
|
|
114
|
+
if (!condition) return "";
|
|
115
|
+
try {
|
|
116
|
+
const expressionBuilders = {
|
|
117
|
+
eq: () => buildComparisonExpression(condition, "=", params),
|
|
118
|
+
ne: () => buildComparisonExpression(condition, "<>", params),
|
|
119
|
+
lt: () => buildComparisonExpression(condition, "<", params),
|
|
120
|
+
lte: () => buildComparisonExpression(condition, "<=", params),
|
|
121
|
+
gt: () => buildComparisonExpression(condition, ">", params),
|
|
122
|
+
gte: () => buildComparisonExpression(condition, ">=", params),
|
|
123
|
+
between: () => buildBetweenExpression(condition, params),
|
|
124
|
+
beginsWith: () => buildFunctionExpression("begins_with", condition, params),
|
|
125
|
+
contains: () => buildFunctionExpression("contains", condition, params),
|
|
126
|
+
attributeExists: () => buildAttributeFunction("attribute_exists", condition, params),
|
|
127
|
+
attributeNotExists: () => buildAttributeFunction("attribute_not_exists", condition, params),
|
|
128
|
+
and: () => {
|
|
129
|
+
if (!condition.conditions) {
|
|
130
|
+
throw new Error("Conditions array is required for AND operator");
|
|
131
|
+
}
|
|
132
|
+
return buildLogicalExpression("AND", condition.conditions, params);
|
|
133
|
+
},
|
|
134
|
+
or: () => {
|
|
135
|
+
if (!condition.conditions) {
|
|
136
|
+
throw new Error("Conditions array is required for OR operator");
|
|
137
|
+
}
|
|
138
|
+
return buildLogicalExpression("OR", condition.conditions, params);
|
|
139
|
+
},
|
|
140
|
+
not: () => {
|
|
141
|
+
if (!condition.condition) {
|
|
142
|
+
throw new Error("Condition is required for NOT operator");
|
|
143
|
+
}
|
|
144
|
+
return `NOT (${buildExpression(condition.condition, params)})`;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const builder = expressionBuilders[condition.type];
|
|
148
|
+
if (!builder) {
|
|
149
|
+
throw new Error(`Unknown condition type: ${condition.type}`);
|
|
150
|
+
}
|
|
151
|
+
return builder();
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof Error) {
|
|
154
|
+
console.error(`Error building expression for condition type ${condition.type}:`, error.message);
|
|
155
|
+
} else {
|
|
156
|
+
console.error(`Error building expression for condition type ${condition.type}:`, error);
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var prepareExpressionParams = (condition) => {
|
|
162
|
+
if (!condition) return {};
|
|
163
|
+
const params = {
|
|
164
|
+
expressionAttributeNames: {},
|
|
165
|
+
expressionAttributeValues: {},
|
|
166
|
+
valueCounter: { count: 0 }
|
|
167
|
+
};
|
|
168
|
+
const expression = buildExpression(condition, params);
|
|
169
|
+
return {
|
|
170
|
+
expression,
|
|
171
|
+
names: Object.keys(params.expressionAttributeNames).length > 0 ? params.expressionAttributeNames : void 0,
|
|
172
|
+
values: Object.keys(params.expressionAttributeValues).length > 0 ? params.expressionAttributeValues : void 0
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/utils/debug-expression.ts
|
|
177
|
+
function debugCommand(command) {
|
|
178
|
+
const result = {};
|
|
179
|
+
function replaceAliases(expressionString) {
|
|
180
|
+
if (!expressionString) {
|
|
181
|
+
return expressionString;
|
|
182
|
+
}
|
|
183
|
+
let replacedString = expressionString;
|
|
184
|
+
for (const alias in command.expressionAttributeNames) {
|
|
185
|
+
const attributeName = command.expressionAttributeNames[alias];
|
|
186
|
+
const regex = new RegExp(alias, "g");
|
|
187
|
+
replacedString = replacedString.replace(regex, attributeName);
|
|
188
|
+
}
|
|
189
|
+
for (const alias in command.expressionAttributeValues) {
|
|
190
|
+
let attributeValue = command.expressionAttributeValues[alias];
|
|
191
|
+
if (attributeValue instanceof Set) {
|
|
192
|
+
const array = Array.from(attributeValue);
|
|
193
|
+
attributeValue = `Set(${array.length}){${array.map((v) => JSON.stringify(v)).join(", ")}}`;
|
|
194
|
+
} else {
|
|
195
|
+
attributeValue = JSON.stringify(attributeValue);
|
|
196
|
+
}
|
|
197
|
+
const regex = new RegExp(alias, "g");
|
|
198
|
+
replacedString = replacedString.replace(regex, attributeValue);
|
|
199
|
+
}
|
|
200
|
+
return replacedString;
|
|
201
|
+
}
|
|
202
|
+
if (command.updateExpression) {
|
|
203
|
+
result.updateExpression = replaceAliases(command.updateExpression);
|
|
204
|
+
}
|
|
205
|
+
if (command.conditionExpression) {
|
|
206
|
+
result.conditionExpression = replaceAliases(command.conditionExpression);
|
|
207
|
+
}
|
|
208
|
+
if (command.filterExpression) {
|
|
209
|
+
result.filterExpression = replaceAliases(command.filterExpression);
|
|
210
|
+
}
|
|
211
|
+
if (command.keyConditionExpression) {
|
|
212
|
+
result.keyConditionExpression = replaceAliases(command.keyConditionExpression);
|
|
213
|
+
}
|
|
214
|
+
if (command.projectionExpression) {
|
|
215
|
+
result.projectionExpression = replaceAliases(command.projectionExpression);
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
raw: command,
|
|
219
|
+
readable: result
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/builders/condition-check-builder.ts
|
|
224
|
+
var ConditionCheckBuilder = class {
|
|
225
|
+
key;
|
|
226
|
+
tableName;
|
|
227
|
+
conditionExpression;
|
|
228
|
+
constructor(tableName, key) {
|
|
229
|
+
this.tableName = tableName;
|
|
230
|
+
this.key = key;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Adds a condition that must be satisfied for the check to succeed.
|
|
234
|
+
* Use this method when you need to:
|
|
235
|
+
* - Validate complex item states
|
|
236
|
+
* - Check multiple attributes together
|
|
237
|
+
* - Ensure safety conditions are met
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* // Check dinosaur health and behavior
|
|
242
|
+
* builder.condition(op =>
|
|
243
|
+
* op.and([
|
|
244
|
+
* op.gt('stats.health', 50),
|
|
245
|
+
* op.not(op.eq('status', 'SEDATED')),
|
|
246
|
+
* op.lt('aggressionLevel', 8)
|
|
247
|
+
* ])
|
|
248
|
+
* );
|
|
249
|
+
*
|
|
250
|
+
* // Verify habitat conditions
|
|
251
|
+
* builder.condition(op =>
|
|
252
|
+
* op.and([
|
|
253
|
+
* op.eq('powerStatus', 'ONLINE'),
|
|
254
|
+
* op.between('temperature', 20, 30),
|
|
255
|
+
* op.attributeExists('lastMaintenance')
|
|
256
|
+
* ])
|
|
257
|
+
* );
|
|
258
|
+
*
|
|
259
|
+
* // Check breeding conditions
|
|
260
|
+
* builder.condition(op =>
|
|
261
|
+
* op.and([
|
|
262
|
+
* op.eq('species', 'VELOCIRAPTOR'),
|
|
263
|
+
* op.gte('age', 3),
|
|
264
|
+
* op.eq('geneticPurity', 100)
|
|
265
|
+
* ])
|
|
266
|
+
* );
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
270
|
+
* @returns The builder instance for method chaining
|
|
271
|
+
*/
|
|
272
|
+
condition(condition) {
|
|
273
|
+
if (typeof condition === "function") {
|
|
274
|
+
const conditionOperator = {
|
|
275
|
+
eq,
|
|
276
|
+
ne,
|
|
277
|
+
lt,
|
|
278
|
+
lte,
|
|
279
|
+
gt,
|
|
280
|
+
gte,
|
|
281
|
+
between,
|
|
282
|
+
beginsWith,
|
|
283
|
+
contains,
|
|
284
|
+
attributeExists,
|
|
285
|
+
attributeNotExists,
|
|
286
|
+
and,
|
|
287
|
+
or,
|
|
288
|
+
not
|
|
289
|
+
};
|
|
290
|
+
this.conditionExpression = condition(conditionOperator);
|
|
291
|
+
} else {
|
|
292
|
+
this.conditionExpression = condition;
|
|
293
|
+
}
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Generates the DynamoDB command parameters for direct execution.
|
|
298
|
+
* Use this method when you want to:
|
|
299
|
+
* - Execute the condition check as a standalone operation
|
|
300
|
+
* - Get the raw DynamoDB command for custom execution
|
|
301
|
+
* - Inspect the generated command parameters
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```ts
|
|
305
|
+
* const command = new ConditionCheckBuilder('myTable', { id: '123' })
|
|
306
|
+
* .condition(op => op.attributeExists('status'))
|
|
307
|
+
* .toDynamoCommand();
|
|
308
|
+
* // Use command with DynamoDB client
|
|
309
|
+
* ```
|
|
310
|
+
*
|
|
311
|
+
* @throws {Error} If no condition has been set
|
|
312
|
+
* @returns The DynamoDB command parameters
|
|
313
|
+
*/
|
|
314
|
+
toDynamoCommand() {
|
|
315
|
+
if (!this.conditionExpression) {
|
|
316
|
+
throw new Error("Condition is required for condition check operations");
|
|
317
|
+
}
|
|
318
|
+
const { expression, names, values } = prepareExpressionParams(this.conditionExpression);
|
|
319
|
+
if (!expression) {
|
|
320
|
+
throw new Error("Failed to generate condition expression");
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
tableName: this.tableName,
|
|
324
|
+
key: this.key,
|
|
325
|
+
conditionExpression: expression,
|
|
326
|
+
expressionAttributeNames: names,
|
|
327
|
+
expressionAttributeValues: values
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Adds this condition check operation to a transaction.
|
|
332
|
+
* Use this method when you need to:
|
|
333
|
+
* - Verify habitat safety before transfers
|
|
334
|
+
* - Ensure proper feeding conditions
|
|
335
|
+
* - Validate security protocols
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```ts
|
|
339
|
+
* const transaction = new TransactionBuilder();
|
|
340
|
+
* new ConditionCheckBuilder('habitats', { id: 'PADDOCK-B' })
|
|
341
|
+
* .condition(op => op.and([
|
|
342
|
+
* op.eq('securityStatus', 'ACTIVE'),
|
|
343
|
+
* op.lt('currentOccupants', 3),
|
|
344
|
+
* op.eq('habitatType', 'CARNIVORE')
|
|
345
|
+
* ]))
|
|
346
|
+
* .withTransaction(transaction);
|
|
347
|
+
* // Add dinosaur transfer operations
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @param transaction - The transaction builder to add this operation to
|
|
351
|
+
* @throws {Error} If no condition has been set
|
|
352
|
+
* @returns The builder instance for method chaining
|
|
353
|
+
*/
|
|
354
|
+
withTransaction(transaction) {
|
|
355
|
+
if (!this.conditionExpression) {
|
|
356
|
+
throw new Error("Condition is required for condition check operations");
|
|
357
|
+
}
|
|
358
|
+
const command = this.toDynamoCommand();
|
|
359
|
+
transaction.conditionCheckWithCommand(command);
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Gets a human-readable representation of the condition check command
|
|
364
|
+
* with all expression placeholders replaced by their actual values.
|
|
365
|
+
* Use this method when you need to:
|
|
366
|
+
* - Debug complex condition expressions
|
|
367
|
+
* - Verify condition parameters
|
|
368
|
+
* - Log safety checks
|
|
369
|
+
* - Troubleshoot condition failures
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```ts
|
|
373
|
+
* const debugInfo = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })
|
|
374
|
+
* .condition(op => op.and([
|
|
375
|
+
* op.between('stats.health', 50, 100),
|
|
376
|
+
* op.not(op.eq('status', 'SEDATED')),
|
|
377
|
+
* op.attributeExists('lastFeedingTime')
|
|
378
|
+
* op.eq('version', 1)
|
|
379
|
+
* ]))
|
|
380
|
+
* .debug();
|
|
381
|
+
* console.log(debugInfo);
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @returns A readable representation of the condition check command with resolved expressions
|
|
385
|
+
*/
|
|
386
|
+
debug() {
|
|
387
|
+
const command = this.toDynamoCommand();
|
|
388
|
+
return debugCommand(command);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
exports.ConditionCheckBuilder = ConditionCheckBuilder;
|
|
393
|
+
//# sourceMappingURL=condition-check-builder.cjs.map
|
|
394
|
+
//# sourceMappingURL=condition-check-builder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/conditions.ts","../../src/expression.ts","../../src/utils/debug-expression.ts","../../src/builders/condition-check-builder.ts"],"names":[],"mappings":";;;AA8FO,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,CAAA;AAWO,IAAM,EAAA,GAAK,IAAI,UAAwC,MAAA;AAAA,EAC5D,IAAM,EAAA,IAAA;AAAA,EACN;AACF,CAAA,CAAA;AAQO,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C,GAAA;AACrD,IAAI,IAAA,CAAC,KAAK,mBAAqB,EAAA;AAC7B,MAAM,MAAA,IAAI,MAAM,sDAAsD,CAAA;AAAA;AAGxE,IAAA,MAAM,EAAE,UAAY,EAAA,KAAA,EAAO,QAAW,GAAA,uBAAA,CAAwB,KAAK,mBAAmB,CAAA;AAEtF,IAAA,IAAI,CAAC,UAAY,EAAA;AACf,MAAM,MAAA,IAAI,MAAM,yCAAyC,CAAA;AAAA;AAG3D,IAAO,OAAA;AAAA,MACL,WAAW,IAAK,CAAA,SAAA;AAAA,MAChB,KAAK,IAAK,CAAA,GAAA;AAAA,MACV,mBAAqB,EAAA,UAAA;AAAA,MACrB,wBAA0B,EAAA,KAAA;AAAA,MAC1B,yBAA2B,EAAA;AAAA,KAC7B;AAAA;AACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0BA,gBAAgB,WAAwD,EAAA;AACtE,IAAI,IAAA,CAAC,KAAK,mBAAqB,EAAA;AAC7B,MAAM,MAAA,IAAI,MAAM,sDAAsD,CAAA;AAAA;AAGxE,IAAM,MAAA,OAAA,GAAU,KAAK,eAAgB,EAAA;AACrC,IAAA,WAAA,CAAY,0BAA0B,OAAO,CAAA;AAE7C,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;AAAA;AAAA,EA0BA,KAAiC,GAAA;AAC/B,IAAM,MAAA,OAAA,GAAU,KAAK,eAAgB,EAAA;AACrC,IAAA,OAAO,aAAa,OAAO,CAAA;AAAA;AAE/B","file":"condition-check-builder.cjs","sourcesContent":["import type { Path, PathType } from \"./builders/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: Record<string, unknown>;\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 Record<string, unknown>> = {\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 type { DynamoCommandWithExpressions } from \"../utils/debug-expression\";\nimport { debugCommand } from \"../utils/debug-expression\";\nimport type { ConditionCheckCommandParams } from \"./builder-types\";\n\n/**\n * Builder for creating DynamoDB condition check operations.\n * Use this builder when you need to:\n * - Verify item state without modifying it\n * - Ensure preconditions in transactions\n * - Implement optimistic locking patterns\n * - Validate business rules\n *\n * @example\n * ```typescript\n * // Check if dinosaur is ready for feeding\n * const check = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })\n * .condition(op =>\n * op.and([\n * op.eq('status', 'HUNTING'),\n * op.gt('stats.hunger', 80),\n * op.lt('stats.health', 100)\n * ])\n * )\n * .toDynamoCommand();\n *\n * // Check habitat security status\n * const securityCheck = new ConditionCheckBuilder('habitats', { id: 'PADDOCK-A' })\n * .condition(op =>\n * op.and([\n * op.eq('securityStatus', 'ACTIVE'),\n * op.attributeExists('lastInspection'),\n * op.lt('threatLevel', 5)\n * ])\n * )\n * .toDynamoCommand();\n * ```\n */\nexport class ConditionCheckBuilder {\n private readonly key: PrimaryKeyWithoutExpression;\n private readonly tableName: string;\n private conditionExpression?: Condition;\n\n constructor(tableName: string, key: PrimaryKeyWithoutExpression) {\n this.tableName = tableName;\n this.key = key;\n }\n\n /**\n * Adds a condition that must be satisfied for the check to succeed.\n * Use this method when you need to:\n * - Validate complex item states\n * - Check multiple attributes together\n * - Ensure safety conditions are met\n *\n * @example\n * ```typescript\n * // Check dinosaur health and behavior\n * builder.condition(op =>\n * op.and([\n * op.gt('stats.health', 50),\n * op.not(op.eq('status', 'SEDATED')),\n * op.lt('aggressionLevel', 8)\n * ])\n * );\n *\n * // Verify habitat conditions\n * builder.condition(op =>\n * op.and([\n * op.eq('powerStatus', 'ONLINE'),\n * op.between('temperature', 20, 30),\n * op.attributeExists('lastMaintenance')\n * ])\n * );\n *\n * // Check breeding conditions\n * builder.condition(op =>\n * op.and([\n * op.eq('species', 'VELOCIRAPTOR'),\n * op.gte('age', 3),\n * op.eq('geneticPurity', 100)\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 condition<T extends Record<string, unknown>>(\n condition: Condition | ((op: ConditionOperator<T>) => Condition),\n ): ConditionCheckBuilder {\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.conditionExpression = condition(conditionOperator);\n } else {\n this.conditionExpression = condition;\n }\n return this;\n }\n\n /**\n * Generates the DynamoDB command parameters for direct execution.\n * Use this method when you want to:\n * - Execute the condition check as a standalone operation\n * - Get the raw DynamoDB command for custom execution\n * - Inspect the generated command parameters\n *\n * @example\n * ```ts\n * const command = new ConditionCheckBuilder('myTable', { id: '123' })\n * .condition(op => op.attributeExists('status'))\n * .toDynamoCommand();\n * // Use command with DynamoDB client\n * ```\n *\n * @throws {Error} If no condition has been set\n * @returns The DynamoDB command parameters\n */\n private toDynamoCommand(): ConditionCheckCommandParams {\n if (!this.conditionExpression) {\n throw new Error(\"Condition is required for condition check operations\");\n }\n\n const { expression, names, values } = prepareExpressionParams(this.conditionExpression);\n\n if (!expression) {\n throw new Error(\"Failed to generate condition expression\");\n }\n\n return {\n tableName: this.tableName,\n key: this.key,\n conditionExpression: expression,\n expressionAttributeNames: names,\n expressionAttributeValues: values,\n };\n }\n\n /**\n * Adds this condition check operation to a transaction.\n * Use this method when you need to:\n * - Verify habitat safety before transfers\n * - Ensure proper feeding conditions\n * - Validate security protocols\n *\n * @example\n * ```ts\n * const transaction = new TransactionBuilder();\n * new ConditionCheckBuilder('habitats', { id: 'PADDOCK-B' })\n * .condition(op => op.and([\n * op.eq('securityStatus', 'ACTIVE'),\n * op.lt('currentOccupants', 3),\n * op.eq('habitatType', 'CARNIVORE')\n * ]))\n * .withTransaction(transaction);\n * // Add dinosaur transfer operations\n * ```\n *\n * @param transaction - The transaction builder to add this operation to\n * @throws {Error} If no condition has been set\n * @returns The builder instance for method chaining\n */\n withTransaction(transaction: TransactionBuilder): ConditionCheckBuilder {\n if (!this.conditionExpression) {\n throw new Error(\"Condition is required for condition check operations\");\n }\n\n const command = this.toDynamoCommand();\n transaction.conditionCheckWithCommand(command);\n\n return this;\n }\n\n /**\n * Gets a human-readable representation of the condition check command\n * with all expression placeholders replaced by their actual values.\n * Use this method when you need to:\n * - Debug complex condition expressions\n * - Verify condition parameters\n * - Log safety checks\n * - Troubleshoot condition failures\n *\n * @example\n * ```ts\n * const debugInfo = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })\n * .condition(op => op.and([\n * op.between('stats.health', 50, 100),\n * op.not(op.eq('status', 'SEDATED')),\n * op.attributeExists('lastFeedingTime')\n * op.eq('version', 1)\n * ]))\n * .debug();\n * console.log(debugInfo);\n * ```\n *\n * @returns A readable representation of the condition check command with resolved expressions\n */\n debug(): Record<string, unknown> {\n const command = this.toDynamoCommand();\n return debugCommand(command);\n }\n}\n"]}
|