dyno-table 2.5.2 → 2.6.1
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/dist/builders/batch-builder.d.ts +249 -0
- package/dist/builders/builder-types.d.ts +123 -0
- package/dist/builders/condition-check-builder.d.ts +149 -0
- package/dist/builders/delete-builder.d.ts +208 -0
- package/dist/builders/entity-aware-builders.d.ts +127 -0
- package/dist/builders/filter-builder.d.ts +294 -0
- package/dist/builders/get-builder.d.ts +191 -0
- package/dist/builders/index.d.ts +14 -0
- package/dist/builders/paginator.d.ts +151 -0
- package/dist/builders/put-builder.d.ts +317 -0
- package/dist/builders/query-builder.d.ts +218 -0
- package/dist/builders/result-iterator.d.ts +55 -0
- package/dist/builders/scan-builder.d.ts +109 -0
- package/dist/builders/transaction-builder.d.ts +462 -0
- package/dist/builders/types.d.ts +25 -0
- package/dist/builders/update-builder.d.ts +372 -0
- package/dist/builders.cjs +3648 -43
- package/dist/builders.d.ts +1 -4
- package/dist/builders.js +3648 -3
- package/dist/conditions.cjs +60 -67
- package/dist/conditions.d.ts +705 -3
- package/dist/conditions.js +46 -1
- package/dist/entity/ddb-indexing.d.ts +45 -0
- package/dist/entity/entity.d.ts +188 -0
- package/dist/entity/index-utils.d.ts +24 -0
- package/dist/entity.cjs +1126 -15
- package/dist/entity.d.ts +1 -261
- package/dist/entity.js +1127 -3
- package/dist/errors.d.ts +212 -0
- package/dist/expression.d.ts +9 -0
- package/dist/index-definition.d.ts +10 -0
- package/dist/index.cjs +5388 -270
- package/dist/index.d.ts +16 -273
- package/dist/index.js +5332 -6
- package/dist/operation-types.d.ts +8 -0
- package/dist/standard-schema.d.ts +2 -4
- package/dist/table.cjs +4311 -7
- package/dist/table.d.ts +13 -8
- package/dist/table.js +4315 -4
- package/dist/types.d.ts +6 -9
- package/dist/utils/chunk-array.d.ts +9 -0
- package/dist/utils/debug-expression.d.ts +32 -0
- package/dist/utils/debug-transaction.d.ts +17 -0
- package/dist/utils/error-factory.d.ts +162 -0
- package/dist/utils/error-utils.d.ts +170 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/partition-key-template.d.ts +30 -0
- package/dist/utils/sort-key-template.d.ts +33 -0
- package/dist/utils.cjs +28 -10
- package/dist/utils.d.ts +1 -66
- package/dist/utils.js +29 -1
- package/package.json +53 -66
- package/dist/builders.d.cts +0 -4
- package/dist/chunk-2WIBY7PZ.js +0 -46
- package/dist/chunk-3DR6VOFW.cjs +0 -3349
- package/dist/chunk-42LH2UEM.js +0 -577
- package/dist/chunk-7UJJ7JXM.cjs +0 -63
- package/dist/chunk-ELULXDSB.cjs +0 -564
- package/dist/chunk-FF7FYGDH.js +0 -543
- package/dist/chunk-G5ERTQFX.cjs +0 -843
- package/dist/chunk-NYJGW3XH.js +0 -3334
- package/dist/chunk-PB7BBCZO.cjs +0 -32
- package/dist/chunk-QVRMYGC4.js +0 -29
- package/dist/chunk-RNX2DAHA.js +0 -818
- package/dist/chunk-ZUBCW3LA.cjs +0 -579
- package/dist/conditions-BSAcZswY.d.ts +0 -731
- package/dist/conditions-C8bM__Pn.d.cts +0 -731
- package/dist/conditions.d.cts +0 -3
- package/dist/entity.d.cts +0 -261
- package/dist/index-Bc-ra0im.d.ts +0 -3042
- package/dist/index-CPCmWsEv.d.cts +0 -3042
- package/dist/index.d.cts +0 -273
- package/dist/standard-schema.d.cts +0 -57
- package/dist/table.d.cts +0 -165
- package/dist/types.d.cts +0 -29
- package/dist/utils.d.cts +0 -66
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { PrimaryKeyWithoutExpression } from "../conditions";
|
|
2
|
+
import { BatchError } from "../errors";
|
|
3
|
+
import type { BatchWriteOperation } from "../operation-types";
|
|
4
|
+
import type { DynamoItem } from "../types";
|
|
5
|
+
import type { DeleteCommandParams, PutCommandParams } from "./builder-types";
|
|
6
|
+
import type { GetCommandParams } from "./get-builder";
|
|
7
|
+
/**
|
|
8
|
+
* Represents a single operation within a DynamoDB batch.
|
|
9
|
+
* Each operation can be one of:
|
|
10
|
+
* - Put: Insert or replace an item
|
|
11
|
+
* - Delete: Remove an item
|
|
12
|
+
* - Get: Retrieve an item
|
|
13
|
+
*/
|
|
14
|
+
export type BatchItem = {
|
|
15
|
+
type: "Put";
|
|
16
|
+
params: PutCommandParams;
|
|
17
|
+
} | {
|
|
18
|
+
type: "Delete";
|
|
19
|
+
params: DeleteCommandParams;
|
|
20
|
+
} | {
|
|
21
|
+
type: "Get";
|
|
22
|
+
params: GetCommandParams;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Typed batch item that preserves entity type information
|
|
26
|
+
*/
|
|
27
|
+
export type TypedBatchItem<T extends DynamoItem = DynamoItem> = {
|
|
28
|
+
type: "Put";
|
|
29
|
+
params: PutCommandParams;
|
|
30
|
+
entityType?: string;
|
|
31
|
+
resultType?: T;
|
|
32
|
+
} | {
|
|
33
|
+
type: "Delete";
|
|
34
|
+
params: DeleteCommandParams;
|
|
35
|
+
entityType?: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "Get";
|
|
38
|
+
params: GetCommandParams;
|
|
39
|
+
entityType?: string;
|
|
40
|
+
resultType?: T;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for batch operations
|
|
44
|
+
*/
|
|
45
|
+
export interface BatchConfig {
|
|
46
|
+
partitionKey: string;
|
|
47
|
+
sortKey?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Executor function for batch write operations
|
|
51
|
+
*/
|
|
52
|
+
type BatchWriteExecutor = (operations: Array<BatchWriteOperation<DynamoItem>>) => Promise<{
|
|
53
|
+
unprocessedItems: Array<BatchWriteOperation<DynamoItem>>;
|
|
54
|
+
}>;
|
|
55
|
+
/**
|
|
56
|
+
* Executor function for batch get operations
|
|
57
|
+
*/
|
|
58
|
+
type BatchGetExecutor = (keys: Array<PrimaryKeyWithoutExpression>) => Promise<{
|
|
59
|
+
items: DynamoItem[];
|
|
60
|
+
unprocessedKeys: PrimaryKeyWithoutExpression[];
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Result structure for batch operations
|
|
64
|
+
*/
|
|
65
|
+
export interface BatchResult {
|
|
66
|
+
/** Whether the batch operation completed successfully */
|
|
67
|
+
success: boolean;
|
|
68
|
+
/** Write operation results */
|
|
69
|
+
writes: {
|
|
70
|
+
/** Number of write operations processed successfully */
|
|
71
|
+
processed: number;
|
|
72
|
+
/** Write operations that were not processed and may need retry */
|
|
73
|
+
unprocessed: Array<BatchWriteOperation<DynamoItem>>;
|
|
74
|
+
};
|
|
75
|
+
/** Read operation results */
|
|
76
|
+
reads: {
|
|
77
|
+
/** Items retrieved from the batch get operations */
|
|
78
|
+
items: DynamoItem[];
|
|
79
|
+
/** Number of items found and returned */
|
|
80
|
+
found: number;
|
|
81
|
+
/** Keys that were not processed and may need retry */
|
|
82
|
+
unprocessed: PrimaryKeyWithoutExpression[];
|
|
83
|
+
};
|
|
84
|
+
/** Total number of operations in the batch */
|
|
85
|
+
totalOperations: number;
|
|
86
|
+
/** Any errors that occurred during batch processing */
|
|
87
|
+
errors?: BatchError[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Typed result structure for batch operations with entity type information
|
|
91
|
+
*/
|
|
92
|
+
export interface TypedBatchResult<TEntities extends Record<string, DynamoItem> = Record<string, DynamoItem>> {
|
|
93
|
+
/** Whether the batch operation completed successfully */
|
|
94
|
+
success: boolean;
|
|
95
|
+
/** Write operation results */
|
|
96
|
+
writes: {
|
|
97
|
+
/** Number of write operations processed successfully */
|
|
98
|
+
processed: number;
|
|
99
|
+
/** Write operations that were not processed and may need retry */
|
|
100
|
+
unprocessed: Array<BatchWriteOperation<DynamoItem>>;
|
|
101
|
+
};
|
|
102
|
+
/** Read operation results with typed items */
|
|
103
|
+
reads: {
|
|
104
|
+
/** Items retrieved from the batch get operations, grouped by entity type */
|
|
105
|
+
itemsByType: {
|
|
106
|
+
[K in keyof TEntities]: TEntities[K][];
|
|
107
|
+
};
|
|
108
|
+
/** All items retrieved (typed as union of all entity types) */
|
|
109
|
+
items: TEntities[keyof TEntities][];
|
|
110
|
+
/** Number of items found and returned */
|
|
111
|
+
found: number;
|
|
112
|
+
/** Keys that were not processed and may need retry */
|
|
113
|
+
unprocessed: PrimaryKeyWithoutExpression[];
|
|
114
|
+
};
|
|
115
|
+
/** Total number of operations in the batch */
|
|
116
|
+
totalOperations: number;
|
|
117
|
+
/** Any errors that occurred during batch execution */
|
|
118
|
+
errors?: BatchError[];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Builder for creating and executing DynamoDB batch operations with full entity support and type inference.
|
|
122
|
+
*
|
|
123
|
+
* Use BatchBuilder when you need to:
|
|
124
|
+
* - Perform multiple operations efficiently (up to 25 writes, 100 reads per batch)
|
|
125
|
+
* - Maintain entity validation, key generation, and type safety
|
|
126
|
+
* - Mix read and write operations in a single batch
|
|
127
|
+
* - Get typed results grouped by entity type
|
|
128
|
+
*
|
|
129
|
+
* @example Basic Usage
|
|
130
|
+
* ```typescript
|
|
131
|
+
* // Define entity types for the batch
|
|
132
|
+
* const batch = table.batchBuilder<{
|
|
133
|
+
* User: UserEntity;
|
|
134
|
+
* Order: OrderEntity;
|
|
135
|
+
* }>();
|
|
136
|
+
*
|
|
137
|
+
* // Add operations using entity repositories
|
|
138
|
+
* userRepo.create(newUser).withBatch(batch, 'User')
|
|
139
|
+
* userRepo.delete({ id: 'old-user' }).withBatch(batch, 'User')
|
|
140
|
+
* orderRepo.get({ id: 'existing-order' }).withBatch(batch, 'Order')
|
|
141
|
+
*
|
|
142
|
+
* // Execute all operations and get typed results
|
|
143
|
+
* const result = await batch.execute()
|
|
144
|
+
* const users: UserEntity[] = result.reads.itemsByType.User
|
|
145
|
+
* const orders: OrderEntity[] = result.reads.itemsByType.Order
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @example Error Handling
|
|
149
|
+
* ```typescript
|
|
150
|
+
* try {
|
|
151
|
+
* const result = await batch.execute()
|
|
152
|
+
*
|
|
153
|
+
* if (result.writes.unprocessed.length > 0) {
|
|
154
|
+
* console.warn('Some writes were not processed:', result.writes.unprocessed)
|
|
155
|
+
* }
|
|
156
|
+
* } catch (error) {
|
|
157
|
+
* if (error instanceof BatchError) {
|
|
158
|
+
* console.error('Batch operation failed:', error.message)
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare class BatchBuilder<TEntities extends Record<string, DynamoItem> = Record<string, DynamoItem>> {
|
|
164
|
+
private batchWriteExecutor;
|
|
165
|
+
private batchGetExecutor;
|
|
166
|
+
private config;
|
|
167
|
+
private writeItems;
|
|
168
|
+
private getItems;
|
|
169
|
+
constructor(batchWriteExecutor: BatchWriteExecutor, batchGetExecutor: BatchGetExecutor, config: BatchConfig);
|
|
170
|
+
/**
|
|
171
|
+
* Checks if the batch is empty (contains no operations)
|
|
172
|
+
*
|
|
173
|
+
* @returns true if the batch contains no operations
|
|
174
|
+
*/
|
|
175
|
+
isEmpty(): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Gets the count of operations in the batch
|
|
178
|
+
*
|
|
179
|
+
* @returns Object containing the count of write and read operations
|
|
180
|
+
*/
|
|
181
|
+
getOperationCount(): {
|
|
182
|
+
writes: number;
|
|
183
|
+
reads: number;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Validates that the batch is not empty before execution
|
|
187
|
+
*
|
|
188
|
+
* @throws {BatchError} If the batch is empty
|
|
189
|
+
*/
|
|
190
|
+
private validateNotEmpty;
|
|
191
|
+
/**
|
|
192
|
+
* Adds a put operation to the batch with entity type information.
|
|
193
|
+
* This method is used internally by entity builders.
|
|
194
|
+
*
|
|
195
|
+
* @param command - The complete put command configuration
|
|
196
|
+
* @param entityType - The entity type name for type tracking
|
|
197
|
+
* @returns The batch builder for method chaining
|
|
198
|
+
* @internal
|
|
199
|
+
*/
|
|
200
|
+
putWithCommand<K extends keyof TEntities>(command: PutCommandParams, entityType?: K): this;
|
|
201
|
+
/**
|
|
202
|
+
* Adds a delete operation to the batch with entity type information.
|
|
203
|
+
* This method is used internally by entity builders.
|
|
204
|
+
*
|
|
205
|
+
* @param command - The complete delete command configuration
|
|
206
|
+
* @param entityType - The entity type name for type tracking
|
|
207
|
+
* @returns The batch builder for method chaining
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
deleteWithCommand<K extends keyof TEntities>(command: DeleteCommandParams, entityType?: K): this;
|
|
211
|
+
/**
|
|
212
|
+
* Adds a get operation to the batch with entity type information.
|
|
213
|
+
* This method is used internally by entity builders.
|
|
214
|
+
*
|
|
215
|
+
* @param command - The complete get command configuration
|
|
216
|
+
* @param entityType - The entity type name for type tracking
|
|
217
|
+
* @returns The batch builder for method chaining
|
|
218
|
+
* @internal
|
|
219
|
+
*/
|
|
220
|
+
getWithCommand<K extends keyof TEntities>(command: GetCommandParams, entityType?: K): this;
|
|
221
|
+
/**
|
|
222
|
+
* Executes all write operations in the batch.
|
|
223
|
+
*
|
|
224
|
+
* @returns A promise that resolves to any unprocessed operations
|
|
225
|
+
* @private
|
|
226
|
+
*/
|
|
227
|
+
private executeWrites;
|
|
228
|
+
/**
|
|
229
|
+
* Executes all get operations in the batch.
|
|
230
|
+
*
|
|
231
|
+
* @returns A promise that resolves to the retrieved items
|
|
232
|
+
* @private
|
|
233
|
+
*/
|
|
234
|
+
private executeGets;
|
|
235
|
+
/**
|
|
236
|
+
* Groups retrieved items by their entity type.
|
|
237
|
+
* @private
|
|
238
|
+
*/
|
|
239
|
+
private groupItemsByType;
|
|
240
|
+
/**
|
|
241
|
+
* Executes all operations in the batch with typed results.
|
|
242
|
+
* Performs write operations first, then get operations.
|
|
243
|
+
*
|
|
244
|
+
* @returns A promise that resolves to a TypedBatchResult with entity type information
|
|
245
|
+
* @throws {BatchError} If the batch is empty or if operations fail
|
|
246
|
+
*/
|
|
247
|
+
execute(): Promise<TypedBatchResult<TEntities>>;
|
|
248
|
+
}
|
|
249
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { DynamoItem, TableConfig } from "../types";
|
|
2
|
+
import type { DynamoCommandWithExpressions } from "../utils/debug-expression";
|
|
3
|
+
import type { ResultIterator } from "./result-iterator";
|
|
4
|
+
export interface DeleteCommandParams extends DynamoCommandWithExpressions {
|
|
5
|
+
tableName: string;
|
|
6
|
+
key: Record<string, unknown>;
|
|
7
|
+
conditionExpression?: string;
|
|
8
|
+
expressionAttributeNames?: Record<string, string>;
|
|
9
|
+
expressionAttributeValues?: DynamoItem;
|
|
10
|
+
returnValues?: "ALL_OLD";
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parameters for the DynamoDB put command.
|
|
14
|
+
*
|
|
15
|
+
* These parameters are used when executing the operation against DynamoDB.
|
|
16
|
+
*
|
|
17
|
+
* The `returnValues` property can be:
|
|
18
|
+
* - `"ALL_OLD"`: Return the attributes of the item as they were before the operation
|
|
19
|
+
* - `"NONE"`: Return nothing
|
|
20
|
+
* - `"CONSISTENT"`: Triggers a GET operation after the put to retrieve the updated item state
|
|
21
|
+
* - `"INPUT"`: Return the input values that were passed to the operation (useful for create operations)
|
|
22
|
+
*/
|
|
23
|
+
export interface PutCommandParams extends DynamoCommandWithExpressions {
|
|
24
|
+
tableName: string;
|
|
25
|
+
item: DynamoItem;
|
|
26
|
+
conditionExpression?: string;
|
|
27
|
+
expressionAttributeNames?: Record<string, string>;
|
|
28
|
+
expressionAttributeValues?: Record<string, unknown>;
|
|
29
|
+
returnValues?: "ALL_OLD" | "NONE" | "CONSISTENT" | "INPUT";
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parameters for the DynamoDB update command.
|
|
33
|
+
* These parameters are used when executing the operation against DynamoDB.
|
|
34
|
+
*/
|
|
35
|
+
export interface UpdateCommandParams extends DynamoCommandWithExpressions {
|
|
36
|
+
/** The name of the DynamoDB table */
|
|
37
|
+
tableName: string;
|
|
38
|
+
/** The primary key of the item to update */
|
|
39
|
+
key: Record<string, unknown>;
|
|
40
|
+
/** The update expression (SET, REMOVE, ADD, DELETE clauses) */
|
|
41
|
+
updateExpression: string;
|
|
42
|
+
/** Optional condition expression that must be satisfied */
|
|
43
|
+
conditionExpression?: string;
|
|
44
|
+
/** Map of expression attribute name placeholders to actual names */
|
|
45
|
+
expressionAttributeNames?: Record<string, string>;
|
|
46
|
+
/** Map of expression attribute value placeholders to actual values */
|
|
47
|
+
expressionAttributeValues?: DynamoItem;
|
|
48
|
+
/** Which item attributes to include in the response */
|
|
49
|
+
returnValues?: "ALL_NEW" | "UPDATED_NEW" | "ALL_OLD" | "UPDATED_OLD" | "NONE";
|
|
50
|
+
}
|
|
51
|
+
export interface ConditionCheckCommandParams extends DynamoCommandWithExpressions {
|
|
52
|
+
tableName: string;
|
|
53
|
+
key: Record<string, unknown>;
|
|
54
|
+
conditionExpression: string;
|
|
55
|
+
expressionAttributeNames?: Record<string, string>;
|
|
56
|
+
expressionAttributeValues?: DynamoItem;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Base interface for all builder classes that support pagination
|
|
60
|
+
* to be used by Paginator without creating circular dependencies.
|
|
61
|
+
*/
|
|
62
|
+
export interface BaseBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig, B = unknown> {
|
|
63
|
+
clone(): B;
|
|
64
|
+
limit(limit: number): B;
|
|
65
|
+
getLimit(): number | undefined;
|
|
66
|
+
startFrom(lastEvaluatedKey: DynamoItem): B;
|
|
67
|
+
execute(): Promise<ResultIterator<T, TConfig>>;
|
|
68
|
+
findOne(): Promise<T | undefined>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Interface for the QueryBuilder class to be used by Paginator
|
|
72
|
+
* without creating a circular dependency.
|
|
73
|
+
*/
|
|
74
|
+
export interface QueryBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, QueryBuilderInterface<T, TConfig>> {
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Interface for the ScanBuilder class to be used by Paginator
|
|
78
|
+
* without creating a circular dependency.
|
|
79
|
+
*/
|
|
80
|
+
export interface ScanBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, ScanBuilderInterface<T, TConfig>> {
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Interface for the FilterBuilder class to be used by Paginator
|
|
84
|
+
* without creating a circular dependency.
|
|
85
|
+
*/
|
|
86
|
+
export interface FilterBuilderInterface<T extends DynamoItem, TConfig extends TableConfig = TableConfig> extends BaseBuilderInterface<T, TConfig, FilterBuilderInterface<T, TConfig>> {
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Represents the result of a single page query operation.
|
|
90
|
+
* This interface provides all necessary information about the current page
|
|
91
|
+
* and the availability of subsequent pages.
|
|
92
|
+
*/
|
|
93
|
+
export interface PaginationResult<T> {
|
|
94
|
+
/** The items (dinosaurs, habitats, etc.) retrieved for the current page */
|
|
95
|
+
items: T[];
|
|
96
|
+
/** DynamoDB's last evaluated key, used internally for pagination */
|
|
97
|
+
lastEvaluatedKey?: DynamoItem;
|
|
98
|
+
/** Indicates whether there are more pages available */
|
|
99
|
+
hasNextPage: boolean;
|
|
100
|
+
/** The current page number (1-indexed) */
|
|
101
|
+
page: number;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Represents a single operation within a DynamoDB transaction.
|
|
105
|
+
* Each operation can be one of:
|
|
106
|
+
* - Put: Insert or replace an item
|
|
107
|
+
* - Update: Modify an existing item
|
|
108
|
+
* - Delete: Remove an item
|
|
109
|
+
* - ConditionCheck: Verify item state without modification
|
|
110
|
+
*/
|
|
111
|
+
export type TransactionItem = {
|
|
112
|
+
type: "Put";
|
|
113
|
+
params: PutCommandParams;
|
|
114
|
+
} | {
|
|
115
|
+
type: "Update";
|
|
116
|
+
params: UpdateCommandParams;
|
|
117
|
+
} | {
|
|
118
|
+
type: "Delete";
|
|
119
|
+
params: DeleteCommandParams;
|
|
120
|
+
} | {
|
|
121
|
+
type: "ConditionCheck";
|
|
122
|
+
params: ConditionCheckCommandParams;
|
|
123
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Condition, ConditionOperator, PrimaryKeyWithoutExpression } from "../conditions";
|
|
2
|
+
import type { DynamoItem } from "../types";
|
|
3
|
+
import type { ConditionCheckCommandParams } from "./builder-types";
|
|
4
|
+
import type { TransactionBuilder } from "./transaction-builder";
|
|
5
|
+
/**
|
|
6
|
+
* Builder for creating DynamoDB condition check operations.
|
|
7
|
+
* Use this builder when you need to:
|
|
8
|
+
* - Verify item state without modifying it
|
|
9
|
+
* - Ensure preconditions in transactions
|
|
10
|
+
* - Implement optimistic locking patterns
|
|
11
|
+
* - Validate business rules
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Check if dinosaur is ready for feeding
|
|
16
|
+
* const check = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })
|
|
17
|
+
* .condition(op =>
|
|
18
|
+
* op.and([
|
|
19
|
+
* op.eq('status', 'HUNTING'),
|
|
20
|
+
* op.gt('stats.hunger', 80),
|
|
21
|
+
* op.lt('stats.health', 100)
|
|
22
|
+
* ])
|
|
23
|
+
* )
|
|
24
|
+
* .toDynamoCommand();
|
|
25
|
+
*
|
|
26
|
+
* // Check habitat security status
|
|
27
|
+
* const securityCheck = new ConditionCheckBuilder('habitats', { id: 'PADDOCK-A' })
|
|
28
|
+
* .condition(op =>
|
|
29
|
+
* op.and([
|
|
30
|
+
* op.eq('securityStatus', 'ACTIVE'),
|
|
31
|
+
* op.attributeExists('lastInspection'),
|
|
32
|
+
* op.lt('threatLevel', 5)
|
|
33
|
+
* ])
|
|
34
|
+
* )
|
|
35
|
+
* .toDynamoCommand();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class ConditionCheckBuilder {
|
|
39
|
+
private readonly key;
|
|
40
|
+
private readonly tableName;
|
|
41
|
+
private conditionExpression?;
|
|
42
|
+
constructor(tableName: string, key: PrimaryKeyWithoutExpression);
|
|
43
|
+
/**
|
|
44
|
+
* Adds a condition that must be satisfied for the check to succeed.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Check dinosaur health and behavior
|
|
49
|
+
* builder.condition(op =>
|
|
50
|
+
* op.and([
|
|
51
|
+
* op.gt('stats.health', 50),
|
|
52
|
+
* op.not(op.eq('status', 'SEDATED')),
|
|
53
|
+
* op.lt('aggressionLevel', 8)
|
|
54
|
+
* ])
|
|
55
|
+
* );
|
|
56
|
+
*
|
|
57
|
+
* // Verify habitat conditions
|
|
58
|
+
* builder.condition(op =>
|
|
59
|
+
* op.and([
|
|
60
|
+
* op.eq('powerStatus', 'ONLINE'),
|
|
61
|
+
* op.between('temperature', 20, 30),
|
|
62
|
+
* op.attributeExists('lastMaintenance')
|
|
63
|
+
* ])
|
|
64
|
+
* );
|
|
65
|
+
*
|
|
66
|
+
* // Check breeding conditions
|
|
67
|
+
* builder.condition(op =>
|
|
68
|
+
* op.and([
|
|
69
|
+
* op.eq('species', 'VELOCIRAPTOR'),
|
|
70
|
+
* op.gte('age', 3),
|
|
71
|
+
* op.eq('geneticPurity', 100)
|
|
72
|
+
* ])
|
|
73
|
+
* );
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @param condition - Either a Condition DynamoItem or a callback function that builds the condition
|
|
77
|
+
* @returns The builder instance for method chaining
|
|
78
|
+
*/
|
|
79
|
+
condition<T extends DynamoItem>(condition: Condition | ((op: ConditionOperator<T>) => Condition)): this;
|
|
80
|
+
/**
|
|
81
|
+
* Generates the DynamoDB command parameters for direct execution.
|
|
82
|
+
* Use this method when you want to:
|
|
83
|
+
* - Execute the condition check as a standalone operation
|
|
84
|
+
* - Get the raw DynamoDB command for custom execution
|
|
85
|
+
* - Inspect the generated command parameters
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const command = new ConditionCheckBuilder('myTable', { id: '123' })
|
|
90
|
+
* .condition(op => op.attributeExists('status'))
|
|
91
|
+
* .toDynamoCommand();
|
|
92
|
+
* // Use command with DynamoDB client
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @throws {Error} If no condition has been set
|
|
96
|
+
* @returns The DynamoDB command parameters
|
|
97
|
+
*/
|
|
98
|
+
private toDynamoCommand;
|
|
99
|
+
/**
|
|
100
|
+
* Adds this condition check operation to a transaction.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const transaction = new TransactionBuilder();
|
|
105
|
+
* new ConditionCheckBuilder('habitats', { id: 'PADDOCK-B' })
|
|
106
|
+
* .condition(op => op.and([
|
|
107
|
+
* op.eq('securityStatus', 'ACTIVE'),
|
|
108
|
+
* op.lt('currentOccupants', 3),
|
|
109
|
+
* op.eq('habitatType', 'CARNIVORE')
|
|
110
|
+
* ]))
|
|
111
|
+
* .withTransaction(transaction);
|
|
112
|
+
* // Add dinosaur transfer operations
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @param transaction - The transaction builder to add this operation to
|
|
116
|
+
* @throws {Error} If no condition has been set
|
|
117
|
+
* @returns The builder instance for method chaining
|
|
118
|
+
*/
|
|
119
|
+
withTransaction(transaction: TransactionBuilder): this;
|
|
120
|
+
/**
|
|
121
|
+
* Gets a human-readable representation of the condition check command
|
|
122
|
+
* with all expression placeholders replaced by their actual values.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const debugInfo = new ConditionCheckBuilder('dinosaurs', { id: 'TREX-001' })
|
|
127
|
+
* .condition(op => op.and([
|
|
128
|
+
* op.between('stats.health', 50, 100),
|
|
129
|
+
* op.not(op.eq('status', 'SEDATED')),
|
|
130
|
+
* op.attributeExists('lastFeedingTime')
|
|
131
|
+
* op.eq('version', 1)
|
|
132
|
+
* ]))
|
|
133
|
+
* .debug();
|
|
134
|
+
* console.log(debugInfo);
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @returns A readable representation of the condition check command with resolved expressions
|
|
138
|
+
*/
|
|
139
|
+
debug(): {
|
|
140
|
+
raw: ConditionCheckCommandParams;
|
|
141
|
+
readable: {
|
|
142
|
+
conditionExpression?: string;
|
|
143
|
+
updateExpression?: string;
|
|
144
|
+
filterExpression?: string;
|
|
145
|
+
keyConditionExpression?: string;
|
|
146
|
+
projectionExpression?: string;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
}
|