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