dyno-table 2.5.1 → 2.6.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/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.d.ts +1 -4
- package/dist/{chunk-U6MQGB6Y.js → chunk-JZB6TYST.js} +133 -136
- package/dist/{chunk-ZXM6LPRV.cjs → chunk-Z334X72N.cjs} +133 -136
- package/dist/conditions.d.ts +705 -3
- 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 +4 -4
- package/dist/entity.d.ts +1 -261
- package/dist/entity.js +1 -1
- 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 +25 -25
- package/dist/index.d.ts +16 -273
- package/dist/index.js +1 -1
- package/dist/operation-types.d.ts +8 -0
- package/dist/standard-schema.d.ts +2 -4
- package/dist/table.d.ts +13 -8
- 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.d.ts +1 -66
- package/package.json +12 -10
- package/dist/builders.d.cts +0 -4
- 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,208 @@
|
|
|
1
|
+
import type { Condition, ConditionOperator, PrimaryKeyWithoutExpression } from "../conditions";
|
|
2
|
+
import type { DynamoItem } from "../types";
|
|
3
|
+
import type { BatchBuilder } from "./batch-builder";
|
|
4
|
+
import type { DeleteCommandParams } from "./builder-types";
|
|
5
|
+
import type { TransactionBuilder } from "./transaction-builder";
|
|
6
|
+
export interface DeleteOptions {
|
|
7
|
+
condition?: Condition;
|
|
8
|
+
returnValues?: "ALL_OLD";
|
|
9
|
+
}
|
|
10
|
+
type DeleteExecutor = (params: DeleteCommandParams) => Promise<{
|
|
11
|
+
item?: DynamoItem;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Builder for creating DynamoDB delete operations.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Simple delete
|
|
19
|
+
* const result = await new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
|
|
20
|
+
* .execute();
|
|
21
|
+
*
|
|
22
|
+
* // Conditional delete with old value retrieval
|
|
23
|
+
* const result = await new DeleteBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
|
|
24
|
+
* .condition(op =>
|
|
25
|
+
* op.and([
|
|
26
|
+
* op.eq('status', 'DECOMMISSIONED'),
|
|
27
|
+
* op.eq('occupants', 0),
|
|
28
|
+
* op.lt('securityIncidents', 1)
|
|
29
|
+
* ])
|
|
30
|
+
* )
|
|
31
|
+
* .returnValues('ALL_OLD')
|
|
32
|
+
* .execute();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class DeleteBuilder {
|
|
36
|
+
private options;
|
|
37
|
+
private readonly executor;
|
|
38
|
+
private readonly tableName;
|
|
39
|
+
private readonly key;
|
|
40
|
+
constructor(executor: DeleteExecutor, tableName: string, key: PrimaryKeyWithoutExpression);
|
|
41
|
+
/**
|
|
42
|
+
* Adds a condition that must be satisfied for the delete operation to succeed.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Ensure dinosaur can be safely removed
|
|
47
|
+
* builder.condition(op =>
|
|
48
|
+
* op.and([
|
|
49
|
+
* op.eq('status', 'SEDATED'),
|
|
50
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
51
|
+
* op.attributeExists('lastCheckup')
|
|
52
|
+
* ])
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* // Verify habitat is empty
|
|
56
|
+
* builder.condition(op =>
|
|
57
|
+
* op.and([
|
|
58
|
+
* op.eq('occupants', 0),
|
|
59
|
+
* op.eq('maintenanceStatus', 'COMPLETE'),
|
|
60
|
+
* op.not(op.attributeExists('activeAlerts'))
|
|
61
|
+
* ])
|
|
62
|
+
* );
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
66
|
+
* @returns The builder instance for method chaining
|
|
67
|
+
*/
|
|
68
|
+
condition<T extends DynamoItem>(condition: Condition | ((op: ConditionOperator<T>) => Condition)): DeleteBuilder;
|
|
69
|
+
/**
|
|
70
|
+
* Sets whether to return the item's attribute values before deletion.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* // Archive dinosaur data before removal
|
|
75
|
+
* const result = await builder
|
|
76
|
+
* .returnValues('ALL_OLD')
|
|
77
|
+
* .execute();
|
|
78
|
+
*
|
|
79
|
+
* if (result.item) {
|
|
80
|
+
* console.log('Removed dinosaur data:', {
|
|
81
|
+
* species: result.item.species,
|
|
82
|
+
* age: result.item.age,
|
|
83
|
+
* lastLocation: result.item.location
|
|
84
|
+
* });
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param returnValues - Use 'ALL_OLD' to return all attributes of the deleted item
|
|
89
|
+
* @returns The builder instance for method chaining
|
|
90
|
+
*/
|
|
91
|
+
returnValues(returnValues: "ALL_OLD"): DeleteBuilder;
|
|
92
|
+
/**
|
|
93
|
+
* Generate the DynamoDB command parameters
|
|
94
|
+
*/
|
|
95
|
+
private toDynamoCommand;
|
|
96
|
+
/**
|
|
97
|
+
* Adds this delete operation to a transaction.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const transaction = new TransactionBuilder();
|
|
102
|
+
*
|
|
103
|
+
* // Remove dinosaur from old habitat
|
|
104
|
+
* new DeleteBuilder(executor, 'dinosaurs', { id: 'RAPTOR-001' })
|
|
105
|
+
* .condition(op => op.eq('status', 'SEDATED'))
|
|
106
|
+
* .withTransaction(transaction);
|
|
107
|
+
*
|
|
108
|
+
* // Update old habitat occupancy
|
|
109
|
+
* new UpdateBuilder(executor, 'habitats', { id: 'PADDOCK-A' })
|
|
110
|
+
* .add('occupants', -1)
|
|
111
|
+
* .withTransaction(transaction);
|
|
112
|
+
*
|
|
113
|
+
* // Execute transfer atomically
|
|
114
|
+
* await transaction.execute();
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @param transaction - The transaction builder to add this operation to
|
|
118
|
+
*/
|
|
119
|
+
withTransaction(transaction: TransactionBuilder): void;
|
|
120
|
+
/**
|
|
121
|
+
* Adds this delete operation to a batch with optional entity type information.
|
|
122
|
+
*
|
|
123
|
+
* @example Basic Usage
|
|
124
|
+
* ```ts
|
|
125
|
+
* const batch = table.batchBuilder();
|
|
126
|
+
*
|
|
127
|
+
* // Remove multiple dinosaurs in batch
|
|
128
|
+
* dinosaurRepo.delete({ id: 'old-dino-1' }).withBatch(batch);
|
|
129
|
+
* dinosaurRepo.delete({ id: 'old-dino-2' }).withBatch(batch);
|
|
130
|
+
* dinosaurRepo.delete({ id: 'old-dino-3' }).withBatch(batch);
|
|
131
|
+
*
|
|
132
|
+
* // Execute all deletions efficiently
|
|
133
|
+
* await batch.execute();
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example Typed Usage
|
|
137
|
+
* ```ts
|
|
138
|
+
* const batch = table.batchBuilder<{
|
|
139
|
+
* User: UserEntity;
|
|
140
|
+
* Order: OrderEntity;
|
|
141
|
+
* }>();
|
|
142
|
+
*
|
|
143
|
+
* // Add operations with type information
|
|
144
|
+
* userRepo.delete({ id: 'user-1' }).withBatch(batch, 'User');
|
|
145
|
+
* orderRepo.delete({ id: 'order-1' }).withBatch(batch, 'Order');
|
|
146
|
+
*
|
|
147
|
+
* // Execute batch operations
|
|
148
|
+
* await batch.execute();
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @param batch - The batch builder to add this operation to
|
|
152
|
+
* @param entityType - Optional entity type key for type tracking
|
|
153
|
+
*/
|
|
154
|
+
withBatch<TEntities extends Record<string, DynamoItem> = Record<string, DynamoItem>, K extends keyof TEntities = keyof TEntities>(batch: BatchBuilder<TEntities>, entityType?: K): void;
|
|
155
|
+
/**
|
|
156
|
+
* Executes the delete operation against DynamoDB.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* // Delete with condition and retrieve old values
|
|
161
|
+
* const result = await new DeleteBuilder(executor, 'myTable', { id: '123' })
|
|
162
|
+
* .condition(op => op.eq('status', 'INACTIVE'))
|
|
163
|
+
* .returnValues('ALL_OLD')
|
|
164
|
+
* .execute();
|
|
165
|
+
*
|
|
166
|
+
* if (result.item) {
|
|
167
|
+
* console.log('Deleted item:', result.item);
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @returns A promise that resolves to an object containing the deleted item's attributes (if returnValues is 'ALL_OLD')
|
|
172
|
+
*/
|
|
173
|
+
execute(): Promise<{
|
|
174
|
+
item?: DynamoItem;
|
|
175
|
+
}>;
|
|
176
|
+
/**
|
|
177
|
+
* Gets a human-readable representation of the delete command
|
|
178
|
+
* with all expression placeholders replaced by their actual values.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* const debugInfo = new DeleteBuilder(executor, 'dinosaurs', { id: 'TREX-001' })
|
|
183
|
+
* .condition(op => op.and([
|
|
184
|
+
* op.eq('status', 'SEDATED'),
|
|
185
|
+
* op.eq('location', 'MEDICAL_BAY'),
|
|
186
|
+
* op.gt('sedationLevel', 8)
|
|
187
|
+
* op.eq('version', 1),
|
|
188
|
+
* op.attributeExists('status')
|
|
189
|
+
* ]))
|
|
190
|
+
* .debug();
|
|
191
|
+
*
|
|
192
|
+
* console.log('Delete command:', debugInfo);
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @returns A readable representation of the delete command with resolved expressions
|
|
196
|
+
*/
|
|
197
|
+
debug(): {
|
|
198
|
+
raw: DeleteCommandParams;
|
|
199
|
+
readable: {
|
|
200
|
+
conditionExpression?: string;
|
|
201
|
+
updateExpression?: string;
|
|
202
|
+
filterExpression?: string;
|
|
203
|
+
keyConditionExpression?: string;
|
|
204
|
+
projectionExpression?: string;
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { Condition, ConditionOperator } from "../conditions";
|
|
2
|
+
import type { IndexDefinition } from "../entity/entity";
|
|
3
|
+
import type { Table } from "../table";
|
|
4
|
+
import type { DynamoItem } from "../types";
|
|
5
|
+
import type { UpdateCommandParams } from "./builder-types";
|
|
6
|
+
import type { DeleteBuilder } from "./delete-builder";
|
|
7
|
+
import type { GetBuilder } from "./get-builder";
|
|
8
|
+
import type { PutBuilder } from "./put-builder";
|
|
9
|
+
import type { TransactionBuilder } from "./transaction-builder";
|
|
10
|
+
import type { Path, PathType } from "./types";
|
|
11
|
+
import type { UpdateBuilder } from "./update-builder";
|
|
12
|
+
type SetElementType<T> = T extends Set<infer U> ? U : T extends Array<infer U> ? U : never;
|
|
13
|
+
type PathSetElementType<T, K extends Path<T>> = SetElementType<PathType<T, K>>;
|
|
14
|
+
/**
|
|
15
|
+
* Entity-aware wrapper for PutBuilder that automatically provides entity name to batch operations
|
|
16
|
+
*/
|
|
17
|
+
export type EntityAwarePutBuilder<T extends DynamoItem> = PutBuilder<T> & {
|
|
18
|
+
readonly entityName: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Creates an entity-aware PutBuilder
|
|
22
|
+
*/
|
|
23
|
+
export declare function createEntityAwarePutBuilder<T extends DynamoItem>(builder: PutBuilder<T>, entityName: string): EntityAwarePutBuilder<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Entity-aware wrapper for GetBuilder that automatically provides entity name to batch operations
|
|
26
|
+
*/
|
|
27
|
+
export type EntityAwareGetBuilder<T extends DynamoItem> = GetBuilder<T> & {
|
|
28
|
+
readonly entityName: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Creates an entity-aware GetBuilder
|
|
32
|
+
*/
|
|
33
|
+
export declare function createEntityAwareGetBuilder<T extends DynamoItem>(builder: GetBuilder<T>, entityName: string): EntityAwareGetBuilder<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Entity-aware wrapper for DeleteBuilder that automatically provides entity name to batch operations
|
|
36
|
+
*/
|
|
37
|
+
export type EntityAwareDeleteBuilder = DeleteBuilder & {
|
|
38
|
+
readonly entityName: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Creates an entity-aware DeleteBuilder
|
|
42
|
+
*/
|
|
43
|
+
export declare function createEntityAwareDeleteBuilder(builder: DeleteBuilder, entityName: string): EntityAwareDeleteBuilder;
|
|
44
|
+
/**
|
|
45
|
+
* Entity-aware wrapper for UpdateBuilder that adds forceIndexRebuild functionality
|
|
46
|
+
* and automatically provides entity name to batch operations
|
|
47
|
+
*/
|
|
48
|
+
export declare class EntityAwareUpdateBuilder<T extends DynamoItem> {
|
|
49
|
+
private forceRebuildIndexes;
|
|
50
|
+
readonly entityName: string;
|
|
51
|
+
private builder;
|
|
52
|
+
private entityConfig?;
|
|
53
|
+
private updateDataApplied;
|
|
54
|
+
constructor(builder: UpdateBuilder<T>, entityName: string);
|
|
55
|
+
/**
|
|
56
|
+
* Configure entity-specific logic for automatic timestamp generation and index updates
|
|
57
|
+
*/
|
|
58
|
+
configureEntityLogic(config: {
|
|
59
|
+
data: Partial<T>;
|
|
60
|
+
key: T;
|
|
61
|
+
table: Table;
|
|
62
|
+
indexes: Record<string, IndexDefinition<T>> | undefined;
|
|
63
|
+
generateTimestamps: () => Record<string, string | number>;
|
|
64
|
+
buildIndexUpdates: (currentData: T, updates: Partial<T>, table: Table, indexes: Record<string, IndexDefinition<T>> | undefined, forceRebuildIndexes?: string[]) => Record<string, string>;
|
|
65
|
+
}): void;
|
|
66
|
+
/**
|
|
67
|
+
* Forces a rebuild of one or more readonly indexes during the update operation.
|
|
68
|
+
*
|
|
69
|
+
* By default, readonly indexes are not updated during entity updates to prevent
|
|
70
|
+
* errors when required index attributes are missing. This method allows you to
|
|
71
|
+
* override that behavior and force specific indexes to be rebuilt.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Force rebuild a single readonly index
|
|
76
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
77
|
+
* .forceIndexRebuild('gsi1')
|
|
78
|
+
* .execute();
|
|
79
|
+
*
|
|
80
|
+
* // Force rebuild multiple readonly indexes
|
|
81
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
82
|
+
* .forceIndexRebuild(['gsi1', 'gsi2'])
|
|
83
|
+
* .execute();
|
|
84
|
+
*
|
|
85
|
+
* // Chain with other update operations
|
|
86
|
+
* const result = await repo.update({ id: 'TREX-001' }, { status: 'ACTIVE' })
|
|
87
|
+
* .set('lastUpdated', new Date().toISOString())
|
|
88
|
+
* .forceIndexRebuild('gsi1')
|
|
89
|
+
* .condition(op => op.eq('status', 'INACTIVE'))
|
|
90
|
+
* .execute();
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @param indexes - A single index name or array of index names to force rebuild
|
|
94
|
+
* @returns The builder instance for method chaining
|
|
95
|
+
*/
|
|
96
|
+
forceIndexRebuild(indexes: string | string[]): this;
|
|
97
|
+
/**
|
|
98
|
+
* Gets the list of indexes that should be force rebuilt.
|
|
99
|
+
* This is used internally by entity update logic.
|
|
100
|
+
*
|
|
101
|
+
* @returns Array of index names to force rebuild
|
|
102
|
+
*/
|
|
103
|
+
getForceRebuildIndexes(): string[];
|
|
104
|
+
/**
|
|
105
|
+
* Apply entity-specific update data (timestamps and index updates)
|
|
106
|
+
* This is called automatically when needed
|
|
107
|
+
*/
|
|
108
|
+
private applyEntityUpdates;
|
|
109
|
+
set(values: Partial<T>): this;
|
|
110
|
+
set<K extends Path<T>>(path: K, value: PathType<T, K>): this;
|
|
111
|
+
remove<K extends Path<T>>(path: K): this;
|
|
112
|
+
add<K extends Path<T>>(path: K, value: PathType<T, K>): this;
|
|
113
|
+
deleteElementsFromSet<K extends Path<T>>(path: K, value: PathSetElementType<T, K>[] | Set<PathSetElementType<T, K>>): this;
|
|
114
|
+
condition(condition: Condition | ((op: ConditionOperator<T>) => Condition)): this;
|
|
115
|
+
returnValues(returnValues: "ALL_NEW" | "UPDATED_NEW" | "ALL_OLD" | "UPDATED_OLD" | "NONE"): this;
|
|
116
|
+
toDynamoCommand(): UpdateCommandParams;
|
|
117
|
+
withTransaction(transaction: TransactionBuilder): void;
|
|
118
|
+
debug(): ReturnType<UpdateBuilder<T>["debug"]>;
|
|
119
|
+
execute(): Promise<{
|
|
120
|
+
item?: T;
|
|
121
|
+
}>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Creates an entity-aware UpdateBuilder with force index rebuild functionality
|
|
125
|
+
*/
|
|
126
|
+
export declare function createEntityAwareUpdateBuilder<T extends DynamoItem>(builder: UpdateBuilder<T>, entityName: string): EntityAwareUpdateBuilder<T>;
|
|
127
|
+
export {};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { type Condition, type ConditionOperator } from "../conditions";
|
|
2
|
+
import type { DynamoItem, GSINames, TableConfig } from "../types";
|
|
3
|
+
import type { FilterBuilderInterface } from "./builder-types";
|
|
4
|
+
import { Paginator } from "./paginator";
|
|
5
|
+
import type { ResultIterator } from "./result-iterator";
|
|
6
|
+
import type { Path } from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for DynamoDB filter operations.
|
|
9
|
+
* These are common options shared between query and scan operations.
|
|
10
|
+
*/
|
|
11
|
+
export interface FilterOptions {
|
|
12
|
+
/** Filter conditions applied to results */
|
|
13
|
+
filter?: Condition;
|
|
14
|
+
/** Maximum number of items to return */
|
|
15
|
+
limit?: number;
|
|
16
|
+
/** Name of the Global Secondary Index to use */
|
|
17
|
+
indexName?: string;
|
|
18
|
+
/** Whether to use strongly consistent reads */
|
|
19
|
+
consistentRead?: boolean;
|
|
20
|
+
/** List of attributes to return in the result */
|
|
21
|
+
projection?: string[];
|
|
22
|
+
/** Token for starting the operation from a specific point */
|
|
23
|
+
lastEvaluatedKey?: DynamoItem;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Abstract base builder for creating DynamoDB filter operations.
|
|
27
|
+
* This class provides common functionality for both Query and Scan operations.
|
|
28
|
+
*
|
|
29
|
+
* The builder supports:
|
|
30
|
+
* - Type-safe GSI selection
|
|
31
|
+
* - Complex filter conditions
|
|
32
|
+
* - Pagination
|
|
33
|
+
* - Consistent reads
|
|
34
|
+
* - Attribute projection
|
|
35
|
+
*
|
|
36
|
+
* @typeParam T - The type of items being filtered
|
|
37
|
+
* @typeParam TConfig - The table configuration type for type-safe GSI selection
|
|
38
|
+
*/
|
|
39
|
+
export declare abstract class FilterBuilder<T extends DynamoItem, TConfig extends TableConfig = TableConfig> implements FilterBuilderInterface<T, TConfig> {
|
|
40
|
+
protected options: FilterOptions;
|
|
41
|
+
protected selectedFields: Set<string>;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the maximum number of items to return.
|
|
44
|
+
*
|
|
45
|
+
* Note: This limit applies to the items that match the key condition
|
|
46
|
+
* before any filter expressions are applied.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Get first 10 dinosaurs
|
|
51
|
+
* const result = await builder
|
|
52
|
+
* .limit(10)
|
|
53
|
+
* .execute();
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @param limit - Maximum number of items to return
|
|
57
|
+
* @returns The builder instance for method chaining
|
|
58
|
+
*/
|
|
59
|
+
limit(limit: number): this;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the current limit set on the operation.
|
|
62
|
+
* This is used internally by the paginator to manage result sets.
|
|
63
|
+
*
|
|
64
|
+
* @returns The current limit or undefined if no limit is set
|
|
65
|
+
*/
|
|
66
|
+
getLimit(): number | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Specifies a Global Secondary Index (GSI) to use for the operation.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* // Find all dinosaurs of a specific species
|
|
73
|
+
* builder
|
|
74
|
+
* .useIndex('species-status-index')
|
|
75
|
+
* .filter(op => op.eq('status', 'ACTIVE'));
|
|
76
|
+
*
|
|
77
|
+
* // Search high-security habitats
|
|
78
|
+
* builder
|
|
79
|
+
* .useIndex('security-level-index')
|
|
80
|
+
* .filter(op =>
|
|
81
|
+
* op.and([
|
|
82
|
+
* op.gt('securityLevel', 8),
|
|
83
|
+
* op.eq('status', 'OPERATIONAL')
|
|
84
|
+
* ])
|
|
85
|
+
* );
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param indexName - The name of the GSI to use (type-safe based on table configuration)
|
|
89
|
+
* @returns The builder instance for method chaining
|
|
90
|
+
*/
|
|
91
|
+
useIndex<I extends GSINames<TConfig>>(indexName: I): this;
|
|
92
|
+
/**
|
|
93
|
+
* Sets whether to use strongly consistent reads for the operation.
|
|
94
|
+
*
|
|
95
|
+
* Note:
|
|
96
|
+
* - Consistent reads are not available on GSIs
|
|
97
|
+
* - Consistent reads consume twice the throughput
|
|
98
|
+
* - Default is eventually consistent reads
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* // Check immediate dinosaur status
|
|
103
|
+
* const result = await builder
|
|
104
|
+
* .filter(op => op.eq('status', 'ACTIVE'))
|
|
105
|
+
* .consistentRead()
|
|
106
|
+
* .execute();
|
|
107
|
+
*
|
|
108
|
+
* // Monitor security breaches
|
|
109
|
+
* const result = await builder
|
|
110
|
+
* .useIndex('primary-index')
|
|
111
|
+
* .consistentRead(isEmergencyMode)
|
|
112
|
+
* .execute();
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @param consistentRead - Whether to use consistent reads (defaults to true)
|
|
116
|
+
* @returns The builder instance for method chaining
|
|
117
|
+
*/
|
|
118
|
+
consistentRead(consistentRead?: boolean): this;
|
|
119
|
+
/**
|
|
120
|
+
* Adds a filter expression to refine the operation results.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Find aggressive carnivores
|
|
125
|
+
* builder.filter(op =>
|
|
126
|
+
* op.and([
|
|
127
|
+
* op.eq('diet', 'CARNIVORE'),
|
|
128
|
+
* op.gt('aggressionLevel', 7),
|
|
129
|
+
* op.eq('status', 'ACTIVE')
|
|
130
|
+
* ])
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* // Search suitable breeding habitats
|
|
134
|
+
* builder.filter(op =>
|
|
135
|
+
* op.and([
|
|
136
|
+
* op.between('temperature', 25, 30),
|
|
137
|
+
* op.lt('currentOccupants', 3),
|
|
138
|
+
* op.eq('quarantineStatus', 'CLEAR')
|
|
139
|
+
* ])
|
|
140
|
+
* );
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @param condition - Either a Condition object or a callback function that builds the condition
|
|
144
|
+
* @returns The builder instance for method chaining
|
|
145
|
+
*/
|
|
146
|
+
filter(condition: Condition | ((op: ConditionOperator<T>) => Condition)): this;
|
|
147
|
+
private getConditionOperator;
|
|
148
|
+
/**
|
|
149
|
+
* Specifies which attributes to return in the results.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // Get basic dinosaur info
|
|
154
|
+
* builder.select([
|
|
155
|
+
* 'species',
|
|
156
|
+
* 'status',
|
|
157
|
+
* 'stats.health',
|
|
158
|
+
* 'stats.aggressionLevel'
|
|
159
|
+
* ]);
|
|
160
|
+
*
|
|
161
|
+
* // Monitor habitat conditions
|
|
162
|
+
* builder
|
|
163
|
+
* .select('securityStatus')
|
|
164
|
+
* .select([
|
|
165
|
+
* 'currentOccupants',
|
|
166
|
+
* 'temperature',
|
|
167
|
+
* 'lastInspectionDate'
|
|
168
|
+
* ]);
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @param fields - A single field name or an array of field names to return
|
|
172
|
+
* @returns The builder instance for method chaining
|
|
173
|
+
*/
|
|
174
|
+
select<K extends Path<T>>(fields: K | K[]): this;
|
|
175
|
+
/**
|
|
176
|
+
* Creates a paginator that handles DynamoDB pagination automatically.
|
|
177
|
+
* The paginator handles:
|
|
178
|
+
* - Tracking the last evaluated key
|
|
179
|
+
* - Managing page boundaries
|
|
180
|
+
* - Respecting overall query limits
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* // Create a paginator for dinosaur records with specific page size
|
|
185
|
+
* const paginator = builder
|
|
186
|
+
* .filter(op => op.eq('status', 'ACTIVE'))
|
|
187
|
+
* .paginate(10);
|
|
188
|
+
*
|
|
189
|
+
* // Create a paginator with automatic DynamoDB paging (no page size limit)
|
|
190
|
+
* const autoPaginator = builder
|
|
191
|
+
* .filter(op => op.eq('status', 'ACTIVE'))
|
|
192
|
+
* .paginate();
|
|
193
|
+
*
|
|
194
|
+
* // Process pages of dinosaur results
|
|
195
|
+
* while (paginator.hasNextPage()) {
|
|
196
|
+
* const page = await paginator.getNextPage();
|
|
197
|
+
* console.log(`Processing page ${page.page}, count: ${page.items.length}`);
|
|
198
|
+
* // Process dinosaur data
|
|
199
|
+
* }
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @param pageSize - The number of items to return per page. If not provided, DynamoDB will automatically determine page sizes.
|
|
203
|
+
* @returns A Paginator instance that manages the pagination state
|
|
204
|
+
* @see Paginator for more pagination control options
|
|
205
|
+
*/
|
|
206
|
+
paginate(pageSize?: number): Paginator<T, TConfig>;
|
|
207
|
+
/**
|
|
208
|
+
* Sets the starting point using a previous lastEvaluatedKey.
|
|
209
|
+
*
|
|
210
|
+
* Note: This method is typically used for manual pagination.
|
|
211
|
+
* For automatic pagination, use the paginate() method instead.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* // First batch of dinosaurs
|
|
216
|
+
* const result1 = await builder
|
|
217
|
+
* .filter(op => op.eq('status', 'ACTIVE'))
|
|
218
|
+
* .limit(5)
|
|
219
|
+
* .execute();
|
|
220
|
+
*
|
|
221
|
+
* const lastKey = result1.getLastEvaluatedKey();
|
|
222
|
+
* if (lastKey) {
|
|
223
|
+
* // Continue listing dinosaurs
|
|
224
|
+
* const result2 = await builder
|
|
225
|
+
* .filter(op => op.eq('status', 'ACTIVE'))
|
|
226
|
+
* .startFrom(lastKey)
|
|
227
|
+
* .limit(5)
|
|
228
|
+
* .execute();
|
|
229
|
+
*
|
|
230
|
+
* const items = await result2.toArray();
|
|
231
|
+
* console.log('Additional dinosaurs:', items);
|
|
232
|
+
* }
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @param lastEvaluatedKey - The exclusive start key from a previous result
|
|
236
|
+
* @returns The builder instance for method chaining
|
|
237
|
+
*/
|
|
238
|
+
startFrom(lastEvaluatedKey: DynamoItem): this;
|
|
239
|
+
/**
|
|
240
|
+
* Creates a deep clone of this builder instance.
|
|
241
|
+
*
|
|
242
|
+
* This is particularly useful when:
|
|
243
|
+
* - Implementing pagination (used internally by paginate())
|
|
244
|
+
* - Creating operation templates
|
|
245
|
+
* - Running multiple variations of an operation
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* // Create base dinosaur query
|
|
250
|
+
* const baseBuilder = builder
|
|
251
|
+
* .useIndex('status-index')
|
|
252
|
+
* .select(['id', 'status', 'location']);
|
|
253
|
+
*
|
|
254
|
+
* // Check active dinosaurs
|
|
255
|
+
* const activeRaptors = baseBuilder.clone()
|
|
256
|
+
* .filter(op => op.eq('status', 'HUNTING'))
|
|
257
|
+
* .execute();
|
|
258
|
+
*
|
|
259
|
+
* // Check contained dinosaurs
|
|
260
|
+
* const containedRaptors = baseBuilder.clone()
|
|
261
|
+
* .filter(op => op.eq('status', 'CONTAINED'))
|
|
262
|
+
* .execute();
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @returns A new builder instance with the same configuration
|
|
266
|
+
*/
|
|
267
|
+
abstract clone(): FilterBuilderInterface<T, TConfig>;
|
|
268
|
+
/**
|
|
269
|
+
* Executes the operation against DynamoDB and returns a generator that behaves like an array.
|
|
270
|
+
* This method must be implemented by subclasses to handle
|
|
271
|
+
* their specific execution logic.
|
|
272
|
+
*/
|
|
273
|
+
abstract execute(): Promise<ResultIterator<T, TConfig>>;
|
|
274
|
+
/**
|
|
275
|
+
* Executes the operation and returns the first matching item, if any.
|
|
276
|
+
*
|
|
277
|
+
* This helper:
|
|
278
|
+
* - Applies an internal limit of 1
|
|
279
|
+
* - Streams results until a match is found or there are no more pages
|
|
280
|
+
* - Avoids mutating the current builder by using a clone
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* const latest = await table
|
|
285
|
+
* .query(eq("moduleId", moduleId))
|
|
286
|
+
* .useIndex("module-version-index")
|
|
287
|
+
* .sortDescending()
|
|
288
|
+
* .findOne();
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* @returns The first matching item, or undefined if none found
|
|
292
|
+
*/
|
|
293
|
+
findOne(): Promise<T | undefined>;
|
|
294
|
+
}
|