dyno-table 0.1.3 → 0.1.5

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/index.d.mts DELETED
@@ -1,323 +0,0 @@
1
- import * as _aws_sdk_lib_dynamodb from '@aws-sdk/lib-dynamodb';
2
- import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
3
- import { z } from 'zod';
4
-
5
- interface ExpressionAttributes {
6
- names?: Record<string, string>;
7
- values?: Record<string, unknown>;
8
- }
9
- interface ExpressionResult {
10
- expression?: string;
11
- attributes: ExpressionAttributes;
12
- }
13
- type FunctionOperator = "attribute_exists" | "attribute_not_exists" | "begins_with" | "contains" | "not_contains" | "attribute_type";
14
- type ComparisonOperator = "=" | "<" | "<=" | ">" | ">=" | "<>";
15
- type SpecialOperator = "BETWEEN" | "IN" | "size";
16
- type ConditionOperator = FunctionOperator | ComparisonOperator | SpecialOperator;
17
- type FilterOperator = "=" | "<" | "<=" | ">" | ">=" | "<>" | "BETWEEN" | "IN" | "contains" | "begins_with";
18
- interface FilterCondition {
19
- field: string;
20
- operator: FilterOperator;
21
- value: unknown;
22
- }
23
- interface Condition {
24
- field: string;
25
- operator: ConditionOperator;
26
- value?: unknown;
27
- }
28
- type SKCondition = {
29
- operator: FilterOperator;
30
- value: string;
31
- };
32
- type PrimaryKey = {
33
- pk: string;
34
- sk?: SKCondition | string;
35
- };
36
- interface TableIndexConfig {
37
- pkName: string;
38
- skName?: string;
39
- }
40
-
41
- interface IExpressionBuilder {
42
- buildKeyCondition(key: PrimaryKey, indexConfig: TableIndexConfig): ExpressionResult;
43
- createExpression(filters: Condition[]): ExpressionResult;
44
- buildUpdateExpression(updates: Record<string, unknown>): ExpressionResult;
45
- }
46
- declare class ExpressionBuilder implements IExpressionBuilder {
47
- private nameCount;
48
- private valueCount;
49
- private generateAlias;
50
- private reset;
51
- private createAttributePath;
52
- private addValue;
53
- private buildComparison;
54
- createExpression(conditions: Array<{
55
- field: string;
56
- operator: ConditionOperator;
57
- value?: unknown;
58
- }>): ExpressionResult;
59
- private formatAttributes;
60
- buildKeyCondition(key: PrimaryKey, indexConfig: TableIndexConfig): ExpressionResult;
61
- buildUpdateExpression(updates: Record<string, unknown>): ExpressionResult;
62
- }
63
-
64
- type DynamoKey = Record<string, unknown>;
65
- interface DynamoExpression {
66
- expression?: string;
67
- names?: Record<string, string>;
68
- values?: Record<string, unknown>;
69
- }
70
- interface DynamoBatchWriteItem {
71
- put?: Record<string, unknown>;
72
- delete?: DynamoKey;
73
- }
74
- interface DynamoPutOperation {
75
- type: "put";
76
- item: Record<string, unknown>;
77
- condition?: DynamoExpression;
78
- }
79
- interface DynamoUpdateOperation {
80
- type: "update";
81
- key: PrimaryKeyWithoutExpression;
82
- update: DynamoExpression;
83
- condition?: DynamoExpression;
84
- }
85
- interface DynamoQueryOperation {
86
- type: "query";
87
- keyCondition?: DynamoExpression;
88
- filter?: DynamoExpression;
89
- limit?: number;
90
- indexName?: string;
91
- }
92
- interface DynamoDeleteOperation {
93
- type: "delete";
94
- key: PrimaryKeyWithoutExpression;
95
- condition?: DynamoExpression;
96
- }
97
- interface DynamoBatchWriteOperation {
98
- type: "batchWrite";
99
- operations: DynamoBatchWriteItem[];
100
- }
101
- interface DynamoTransactOperation {
102
- type: "transactWrite";
103
- operations: Array<{
104
- put?: {
105
- item: Record<string, unknown>;
106
- condition?: DynamoExpression;
107
- };
108
- delete?: {
109
- key: PrimaryKeyWithoutExpression;
110
- condition?: DynamoExpression;
111
- };
112
- update?: {
113
- key: PrimaryKeyWithoutExpression;
114
- update: DynamoExpression;
115
- condition?: DynamoExpression;
116
- };
117
- }>;
118
- }
119
- type DynamoOperation = DynamoPutOperation | DynamoUpdateOperation | DynamoQueryOperation | DynamoDeleteOperation | DynamoBatchWriteOperation | DynamoTransactOperation;
120
- type PrimaryKeyWithoutExpression = {
121
- pk: string;
122
- sk?: string;
123
- };
124
- type BatchWriteOperation = {
125
- type: "put";
126
- item: Record<string, unknown>;
127
- } | {
128
- type: "delete";
129
- key: PrimaryKeyWithoutExpression;
130
- };
131
-
132
- declare abstract class OperationBuilder<T extends DynamoOperation> {
133
- protected expressionBuilder: IExpressionBuilder;
134
- protected conditions: Array<{
135
- field: string;
136
- operator: ConditionOperator;
137
- value?: unknown;
138
- }>;
139
- constructor(expressionBuilder: IExpressionBuilder);
140
- where(field: string, operator: FilterOperator, value: unknown): this;
141
- whereExists(field: string): this;
142
- whereNotExists(field: string): this;
143
- whereEquals(field: string, value: unknown): this;
144
- whereBetween(field: string, start: unknown, end: unknown): this;
145
- whereIn(field: string, values: unknown[]): this;
146
- protected buildConditionExpression(): ExpressionResult;
147
- abstract build(): T;
148
- }
149
-
150
- declare class PutBuilder extends OperationBuilder<DynamoPutOperation> {
151
- private readonly item;
152
- private readonly onBuild;
153
- constructor(item: Record<string, unknown>, expressionBuilder: IExpressionBuilder, onBuild: (operation: DynamoPutOperation) => Promise<void>);
154
- build(): DynamoPutOperation;
155
- execute(): Promise<void>;
156
- }
157
-
158
- declare class QueryBuilder extends OperationBuilder<DynamoQueryOperation> {
159
- private readonly key;
160
- private readonly indexConfig;
161
- private readonly onBuild;
162
- private limitValue?;
163
- private indexNameValue?;
164
- constructor(key: PrimaryKey, indexConfig: TableIndexConfig, expressionBuilder: IExpressionBuilder, onBuild: (operation: DynamoQueryOperation) => Promise<{
165
- Items?: Record<string, unknown>[];
166
- Count?: number;
167
- ScannedCount?: number;
168
- LastEvaluatedKey?: Record<string, unknown>;
169
- }>);
170
- limit(value: number): this;
171
- useIndex(indexName: string): this;
172
- build(): DynamoQueryOperation;
173
- execute(): Promise<{
174
- Items?: Record<string, unknown>[];
175
- Count?: number;
176
- ScannedCount?: number;
177
- LastEvaluatedKey?: Record<string, unknown>;
178
- }>;
179
- }
180
-
181
- declare class UpdateBuilder extends OperationBuilder<DynamoUpdateOperation> {
182
- private readonly key;
183
- private readonly onBuild;
184
- private updates;
185
- constructor(key: PrimaryKeyWithoutExpression, expressionBuilder: IExpressionBuilder, onBuild: (operation: DynamoUpdateOperation) => Promise<{
186
- Attributes?: Record<string, unknown>;
187
- }>);
188
- set(field: string, value: unknown): this;
189
- setMany(attribtues: Record<string, unknown>): this;
190
- remove(...fields: string[]): this;
191
- increment(field: string, by?: number): this;
192
- build(): DynamoUpdateOperation;
193
- execute(): Promise<{
194
- Attributes?: Record<string, unknown>;
195
- }>;
196
- }
197
-
198
- declare class Table {
199
- private readonly dynamoService;
200
- private readonly expressionBuilder;
201
- private readonly indexes;
202
- constructor({ client, tableName, tableIndexes, expressionBuilder, }: {
203
- client: DynamoDBDocument;
204
- tableName: string;
205
- tableIndexes: Record<string, TableIndexConfig>;
206
- expressionBuilder?: ExpressionBuilder;
207
- });
208
- getIndexConfig(indexName?: string): TableIndexConfig;
209
- put(item: Record<string, unknown>): PutBuilder;
210
- update(key: PrimaryKeyWithoutExpression, data?: Record<string, unknown>): UpdateBuilder;
211
- query(key: PrimaryKey): QueryBuilder;
212
- get(key: PrimaryKeyWithoutExpression, options?: {
213
- indexName?: string;
214
- }): Promise<Record<string, any> | undefined>;
215
- delete(key: PrimaryKeyWithoutExpression): Promise<unknown>;
216
- scan(filters?: FilterCondition[], options?: {
217
- limit?: number;
218
- pageKey?: Record<string, unknown>;
219
- indexName?: string;
220
- }): Promise<_aws_sdk_lib_dynamodb.ScanCommandOutput>;
221
- batchWrite(operations: BatchWriteOperation[]): Promise<unknown>;
222
- transactWrite(operations: Array<{
223
- put?: {
224
- item: Record<string, unknown>;
225
- condition?: {
226
- expression: string;
227
- names?: Record<string, string>;
228
- values?: Record<string, unknown>;
229
- };
230
- };
231
- delete?: {
232
- key: PrimaryKeyWithoutExpression;
233
- condition?: {
234
- expression: string;
235
- names?: Record<string, string>;
236
- values?: Record<string, unknown>;
237
- };
238
- };
239
- update?: {
240
- key: PrimaryKeyWithoutExpression;
241
- update: {
242
- expression: string;
243
- names?: Record<string, string>;
244
- values?: Record<string, unknown>;
245
- };
246
- condition?: {
247
- expression: string;
248
- names?: Record<string, string>;
249
- values?: Record<string, unknown>;
250
- };
251
- };
252
- }>): Promise<unknown>;
253
- private executeOperation;
254
- private buildKeyFromIndex;
255
- private validateKey;
256
- }
257
-
258
- type InferZodSchema<T extends z.ZodType> = z.infer<T>;
259
- declare abstract class BaseRepository<TSchema extends z.ZodType> {
260
- protected readonly table: Table;
261
- protected readonly schema: TSchema;
262
- constructor(table: Table, schema: TSchema);
263
- protected abstract createPrimaryKey(data: InferZodSchema<TSchema>): PrimaryKeyWithoutExpression;
264
- protected abstract getIndexKeys(): {
265
- pk: string;
266
- sk?: string;
267
- };
268
- /**
269
- * Default attribute applied to ALL records that get stored in DDB
270
- */
271
- protected abstract getType(): string;
272
- protected abstract getTypeAttributeName(): string;
273
- protected beforeInsert(data: InferZodSchema<TSchema>): InferZodSchema<TSchema>;
274
- protected beforeUpdate(data: InferZodSchema<TSchema>): InferZodSchema<TSchema>;
275
- create(data: InferZodSchema<TSchema>): Promise<InferZodSchema<TSchema>>;
276
- update(key: PrimaryKeyWithoutExpression, updates: Partial<InferZodSchema<TSchema>>): Promise<InferZodSchema<TSchema>>;
277
- delete(key: PrimaryKeyWithoutExpression): Promise<void>;
278
- findOne(key: PrimaryKeyWithoutExpression): Promise<InferZodSchema<TSchema> | null>;
279
- findOrFail(key: PrimaryKeyWithoutExpression): Promise<InferZodSchema<TSchema>>;
280
- protected query(key: PrimaryKeyWithoutExpression): QueryBuilder;
281
- }
282
-
283
- interface RetryStrategy {
284
- maxAttempts: number;
285
- baseDelay: number;
286
- /**
287
- * Check if the error should be retried
288
- * @param error The error that was thrown
289
- * @param attempt The amount of attempts this action has made to run the action
290
- * @returns Whether the action should be retried
291
- */
292
- shouldRetry: (error: unknown, attempt: number) => boolean;
293
- /**
294
- * Get the delay in milliseconds for the next retry attempt
295
- * @param attempt The amount of attempts this action has made to run the action
296
- * @returns The delay in milliseconds
297
- */
298
- getDelay: (attempt: number) => number;
299
- }
300
-
301
- declare class ExponentialBackoffStrategy implements RetryStrategy {
302
- maxAttempts: number;
303
- baseDelay: number;
304
- private maxDelay;
305
- private jitter;
306
- constructor(maxAttempts?: number, baseDelay?: number, maxDelay?: number, jitter?: boolean);
307
- shouldRetry(error: unknown, attempt: number): boolean;
308
- getDelay(attempt: number): number;
309
- }
310
-
311
- declare class DynamoError extends Error {
312
- readonly originalError: Error;
313
- readonly context?: Record<string, unknown> | undefined;
314
- constructor(message: string, originalError: Error, context?: Record<string, unknown> | undefined);
315
- }
316
- declare class ConditionalCheckFailedError extends DynamoError {
317
- constructor(message: string, originalError: Error, context?: Record<string, unknown>);
318
- }
319
- declare class ResourceNotFoundError extends DynamoError {
320
- constructor(message: string, originalError: Error, context?: Record<string, unknown>);
321
- }
322
-
323
- export { BaseRepository, ConditionalCheckFailedError, DynamoError, ExponentialBackoffStrategy, type FilterCondition, type FilterOperator, type PrimaryKey, ResourceNotFoundError, type RetryStrategy, Table, type TableIndexConfig };