@ftschopp/dynatable-core 1.0.0 → 1.1.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +442 -2
  3. package/dist/builders/delete/types.d.ts +6 -1
  4. package/dist/builders/delete/types.d.ts.map +1 -1
  5. package/dist/builders/get/types.d.ts +6 -1
  6. package/dist/builders/get/types.d.ts.map +1 -1
  7. package/dist/builders/put/types.d.ts +6 -1
  8. package/dist/builders/put/types.d.ts.map +1 -1
  9. package/dist/builders/query/create-query-builder.d.ts +1 -1
  10. package/dist/builders/query/create-query-builder.d.ts.map +1 -1
  11. package/dist/builders/query/types.d.ts +9 -10
  12. package/dist/builders/query/types.d.ts.map +1 -1
  13. package/dist/builders/transact-write/create-transact-write-builder.d.ts.map +1 -1
  14. package/dist/builders/transact-write/create-transact-write-builder.js +1 -1
  15. package/dist/builders/transact-write/types.d.ts +33 -6
  16. package/dist/builders/transact-write/types.d.ts.map +1 -1
  17. package/dist/builders/update/create-update-builder.d.ts.map +1 -1
  18. package/dist/builders/update/create-update-builder.js +30 -9
  19. package/dist/builders/update/types.d.ts +8 -2
  20. package/dist/builders/update/types.d.ts.map +1 -1
  21. package/dist/core/types.d.ts +1 -0
  22. package/dist/core/types.d.ts.map +1 -1
  23. package/dist/entity/create-entity-api.d.ts +15 -0
  24. package/dist/entity/create-entity-api.d.ts.map +1 -0
  25. package/dist/entity/create-entity-api.js +124 -0
  26. package/dist/entity/index.d.ts +12 -0
  27. package/dist/entity/index.d.ts.map +1 -0
  28. package/dist/entity/index.js +18 -0
  29. package/dist/entity/middleware/factories.d.ts +6 -0
  30. package/dist/entity/middleware/factories.d.ts.map +1 -0
  31. package/dist/entity/middleware/factories.js +15 -0
  32. package/dist/entity/middleware/types.d.ts +8 -0
  33. package/dist/entity/middleware/types.d.ts.map +1 -0
  34. package/dist/entity/middleware/types.js +2 -0
  35. package/dist/entity/middleware/with-middleware.d.ts +8 -0
  36. package/dist/entity/middleware/with-middleware.d.ts.map +1 -0
  37. package/dist/entity/middleware/with-middleware.js +29 -0
  38. package/dist/{entity.d.ts → entity/types.d.ts} +6 -17
  39. package/dist/entity/types.d.ts.map +1 -0
  40. package/dist/entity/types.js +2 -0
  41. package/dist/entity/validation/key-validation.d.ts +7 -0
  42. package/dist/entity/validation/key-validation.d.ts.map +1 -0
  43. package/dist/entity/validation/key-validation.js +25 -0
  44. package/dist/table.d.ts +8 -1
  45. package/dist/table.d.ts.map +1 -1
  46. package/dist/table.js +1 -0
  47. package/dist/utils/model-utils.d.ts +7 -1
  48. package/dist/utils/model-utils.d.ts.map +1 -1
  49. package/dist/utils/model-utils.js +29 -1
  50. package/dist/utils/zod-utils.d.ts +1 -1
  51. package/package.json +3 -2
  52. package/src/builders/README.md +68 -1
  53. package/src/builders/delete/types.ts +7 -1
  54. package/src/builders/get/types.ts +7 -1
  55. package/src/builders/put/types.ts +7 -1
  56. package/src/builders/query/create-query-builder.ts +4 -6
  57. package/src/builders/query/types.ts +9 -12
  58. package/src/builders/transact-write/README.md +37 -1
  59. package/src/builders/transact-write/create-transact-write-builder.ts +20 -14
  60. package/src/builders/transact-write/types.ts +44 -6
  61. package/src/builders/update/create-update-builder.test.ts +43 -0
  62. package/src/builders/update/create-update-builder.ts +47 -9
  63. package/src/builders/update/types.ts +9 -2
  64. package/src/core/types.ts +1 -0
  65. package/src/entity/create-entity-api.ts +212 -0
  66. package/src/entity/index.ts +19 -0
  67. package/src/entity/middleware/factories.ts +15 -0
  68. package/src/entity/middleware/types.ts +7 -0
  69. package/src/entity/middleware/with-middleware.ts +37 -0
  70. package/src/entity/types.ts +79 -0
  71. package/src/entity/validation/key-validation.ts +34 -0
  72. package/src/table.ts +10 -3
  73. package/src/utils/model-utils.test.ts +131 -1
  74. package/src/utils/model-utils.ts +32 -0
  75. package/dist/entity.d.ts.map +0 -1
  76. package/dist/entity.js +0 -161
  77. package/src/entity.ts +0 -337
@@ -1 +1 @@
1
- {"version":3,"file":"model-utils.d.ts","sourceRoot":"","sources":["../../src/utils/model-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAiB,eAAe,EAAE,MAAM,cAAc,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MAAM,EAG5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAG,MAoB7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,eAAe,EACnD,OAAO,CAAC,EACR,OAAO,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,OAAM,KAAK,GAAG,OAAO,GAAG,MAAc,KACrC,MAAM,CAAC,MAAM,EAAE,MAAM,CAWvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,EACzD,OAAO,CAAC,EACR,eAAe,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,UAAU;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,KACrD,UAAU,CAAC,CAAC,CAgCd,CAAC"}
1
+ {"version":3,"file":"model-utils.d.ts","sourceRoot":"","sources":["../../src/utils/model-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAiB,eAAe,EAAE,MAAM,cAAc,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MAAM,EAG5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAG,MAoB7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,eAAe,EACnD,OAAO,CAAC,EACR,OAAO,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,OAAM,KAAK,GAAG,OAAO,GAAG,MAAc,KACrC,MAAM,CAAC,MAAM,EAAE,MAAM,CAWvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,EACzD,OAAO,CAAC,EACR,eAAe,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,UAAU;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,KACrD,UAAU,CAAC,CAAC,CAgCd,CAAC;AAOF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,SAAS,KAAG,CAAC,GAAG,CAAC,EAAE,GAAG,SAoB1E,CAAC"}
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.applyPostDefaults = exports.resolveKeys = exports.resolveTemplate = exports.extractTemplateVars = void 0;
4
+ exports.stripInternalKeys = exports.applyPostDefaults = exports.resolveKeys = exports.resolveTemplate = exports.extractTemplateVars = void 0;
5
5
  const ulid_1 = require("ulid");
6
6
  /**
7
7
  * Extracts all variable names from a template string
@@ -86,3 +86,31 @@ const applyPostDefaults = (model, validatedItem, options) => {
86
86
  return result;
87
87
  };
88
88
  exports.applyPostDefaults = applyPostDefaults;
89
+ /**
90
+ * Internal DynamoDB keys that should be stripped when cleanInternalKeys is enabled
91
+ */
92
+ const INTERNAL_KEYS = ['PK', 'SK', '_type'];
93
+ /**
94
+ * Removes internal DynamoDB keys from an item or array of items
95
+ * @param data - Single item or array of items from DynamoDB
96
+ * @returns Data with internal keys removed
97
+ */
98
+ const stripInternalKeys = (data) => {
99
+ if (data === undefined || data === null) {
100
+ return data;
101
+ }
102
+ if (Array.isArray(data)) {
103
+ return data.map((item) => (0, exports.stripInternalKeys)(item));
104
+ }
105
+ if (typeof data === 'object') {
106
+ const cleaned = {};
107
+ for (const [key, value] of Object.entries(data)) {
108
+ if (!INTERNAL_KEYS.includes(key)) {
109
+ cleaned[key] = value;
110
+ }
111
+ }
112
+ return cleaned;
113
+ }
114
+ return data;
115
+ };
116
+ exports.stripInternalKeys = stripInternalKeys;
@@ -1,4 +1,4 @@
1
- import { AttributeDefinition, ModelDefinition } from '@/core/types';
1
+ import { AttributeDefinition, ModelDefinition } from '../core/types';
2
2
  import { ZodObject, ZodType } from 'zod';
3
3
  /**
4
4
  * Converts an attribute definition to a Zod schema type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftschopp/dynatable-core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Core library for DynamoDB single table design",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "access": "public"
9
9
  },
10
10
  "scripts": {
11
- "build": "tsc",
11
+ "build": "tsc && tsc-alias",
12
12
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
13
13
  "test": "jest"
14
14
  },
@@ -22,6 +22,7 @@
22
22
  "jest": "^30.2.0",
23
23
  "ts-jest": "^29.4.6",
24
24
  "ts-node": "^10.9.2",
25
+ "tsc-alias": "^1.8.16",
25
26
  "typescript": "5.9.3"
26
27
  },
27
28
  "dependencies": {
@@ -209,7 +209,33 @@ The `op` parameter in `.where()` provides these operators:
209
209
  - `op.gt(attr, value)` - Greater than (>)
210
210
  - `op.gte(attr, value)` - Greater than or equal (>=)
211
211
  - `op.between(attr, low, high)` - Between two values
212
- - `op.beginsWith(attr, prefix)` - Begins with a string (for string fields)
212
+
213
+ **String Operators:**
214
+
215
+ - `op.beginsWith(attr, prefix)` - Begins with a string prefix (for string/binary fields)
216
+ - `op.contains(attr, value)` - Contains a substring or value (works with strings, sets, and lists)
217
+
218
+ **Existence Operators:**
219
+
220
+ - `op.exists(attr)` - Attribute exists
221
+ - `op.notExists(attr)` - Attribute does not exist
222
+
223
+ **Type Checking:**
224
+
225
+ - `op.attributeType(attr, type)` - Check the attribute's type
226
+ - Valid types: `'S'` (String), `'N'` (Number), `'B'` (Binary), `'SS'` (String Set), `'NS'` (Number Set), `'BS'` (Binary Set), `'M'` (Map), `'L'` (List), `'NULL'`, `'BOOL'`
227
+
228
+ **Advanced Operators:**
229
+
230
+ - `op.in(attr, values[])` - Attribute value is in the provided array
231
+ - `op.size(attr)` - Get the size of an attribute (string length, number of elements in set/list, etc.)
232
+ - Returns a `SizeRef` object with comparison methods:
233
+ - `.eq(n)` - Size equals n
234
+ - `.ne(n)` - Size not equals n
235
+ - `.lt(n)` - Size less than n
236
+ - `.lte(n)` - Size less than or equal to n
237
+ - `.gt(n)` - Size greater than n
238
+ - `.gte(n)` - Size greater than or equal to n
213
239
 
214
240
  **Logical Operators:**
215
241
 
@@ -257,6 +283,47 @@ The `op` parameter in `.where()` provides these operators:
257
283
  // NOT operator
258
284
  .where((attr, op) => op.not(op.eq(attr.status, 'deleted')))
259
285
 
286
+ // Existence operators
287
+ .where((attr, op) => op.and(
288
+ op.eq(attr.username, 'alice'),
289
+ op.exists(attr.email) // Has email field
290
+ ))
291
+
292
+ .where((attr, op) => op.notExists(attr.deletedAt)) // Not deleted
293
+
294
+ // Contains operator (for strings, sets, lists)
295
+ .where((attr, op) => op.and(
296
+ op.eq(attr.username, 'alice'),
297
+ op.contains(attr.bio, 'developer') // Bio contains "developer"
298
+ ))
299
+
300
+ .where((attr, op) => op.contains(attr.tags, 'premium')) // Has "premium" in tags set/list
301
+
302
+ // IN operator
303
+ .where((attr, op) => op.in(attr.status, ['active', 'pending', 'verified']))
304
+
305
+ // Size operator
306
+ .where((attr, op) => op.size(attr.tags).gte(3)) // At least 3 tags
307
+ .where((attr, op) => op.size(attr.username).lt(20)) // Username shorter than 20 chars
308
+ .where((attr, op) => op.size(attr.comments).eq(0)) // No comments
309
+
310
+ // Attribute type checking
311
+ .where((attr, op) => op.attributeType(attr.metadata, 'M')) // Is a Map
312
+ .where((attr, op) => op.attributeType(attr.items, 'L')) // Is a List
313
+
314
+ // Complex example with new operators
315
+ await table.entities.User.query()
316
+ .where((attr, op) => op.and(
317
+ op.eq(attr.status, 'active'),
318
+ op.exists(attr.email),
319
+ op.size(attr.followers).gte(10),
320
+ op.or(
321
+ op.contains(attr.tags, 'premium'),
322
+ op.contains(attr.tags, 'verified')
323
+ )
324
+ ))
325
+ .execute();
326
+
260
327
  // Complete example with all features
261
328
  await table.entities.Photo.query()
262
329
  .where((attr, op) => op.and(
@@ -1,11 +1,17 @@
1
+ import type { DeleteCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { OperationBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder interface for DynamoDB DeleteItem operations
5
6
  */
6
- export interface DeleteBuilder<Model> extends OperationBuilder<Model> {
7
+ export interface DeleteBuilder<Model> extends Omit<OperationBuilder<Model>, 'dbParams'> {
7
8
  /**
8
9
  * Configures what values should be returned after the delete operation
9
10
  */
10
11
  returning(mode: 'NONE' | 'ALL_OLD'): DeleteBuilder<Model>;
12
+
13
+ /**
14
+ * Converts the builder state to DynamoDB DeleteItem parameters
15
+ */
16
+ dbParams(): DeleteCommandInput;
11
17
  }
@@ -1,10 +1,11 @@
1
+ import type { GetCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { ExecutableBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder for DynamoDB GetItem operations.
5
6
  * Supports projection, consistent read, and inspection of the final parameters.
6
7
  */
7
- export interface GetBuilder<KeyInput, Model> extends ExecutableBuilder<Model | undefined> {
8
+ export interface GetBuilder<KeyInput, Model> extends Omit<ExecutableBuilder<Model | undefined>, 'dbParams'> {
8
9
  /**
9
10
  * Adds a projection expression to only return specific attributes.
10
11
  * Returns a new immutable builder.
@@ -27,4 +28,9 @@ export interface GetBuilder<KeyInput, Model> extends ExecutableBuilder<Model | u
27
28
  * - NONE: No consumed capacity data returned (default)
28
29
  */
29
30
  returnConsumedCapacity(mode: 'INDEXES' | 'TOTAL' | 'NONE'): GetBuilder<KeyInput, Model>;
31
+
32
+ /**
33
+ * Converts the builder state to DynamoDB GetItem parameters
34
+ */
35
+ dbParams(): GetCommandInput;
30
36
  }
@@ -1,9 +1,10 @@
1
+ import type { PutCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { OperationBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder interface for DynamoDB PutItem operations
5
6
  */
6
- export interface PutBuilder<Model> extends OperationBuilder<Model> {
7
+ export interface PutBuilder<Model> extends Omit<OperationBuilder<Model>, 'dbParams'> {
7
8
  /**
8
9
  * Adds a condition that the item must not exist (checks pk and sk)
9
10
  */
@@ -15,4 +16,9 @@ export interface PutBuilder<Model> extends OperationBuilder<Model> {
15
16
  * - ALL_OLD: Returns the item as it was before being replaced (if it existed)
16
17
  */
17
18
  returning(mode: 'NONE' | 'ALL_OLD'): PutBuilder<Model>;
19
+
20
+ /**
21
+ * Converts the builder state to DynamoDB PutItem parameters
22
+ */
23
+ dbParams(): PutCommandInput;
18
24
  }
@@ -181,9 +181,7 @@ function buildSimpleAndExpression(conditions: Condition[]): {
181
181
  /**
182
182
  * Creates the query executor with all query methods
183
183
  */
184
- function createQueryExecutor<Model, M extends ModelDefinition = any>(
185
- state: QueryState<Model>
186
- ): QueryExecutor<Model, M> {
184
+ function createQueryExecutor<Model>(state: QueryState<Model>): QueryExecutor<Model> {
187
185
  return {
188
186
  limit(count) {
189
187
  return createQueryExecutor({
@@ -317,12 +315,12 @@ function createQueryExecutor<Model, M extends ModelDefinition = any>(
317
315
  /**
318
316
  * Creates a QueryBuilder for a table
319
317
  */
320
- export function createQueryBuilder<Model, M extends ModelDefinition = any>(
318
+ export function createQueryBuilder<Model>(
321
319
  tableName: string,
322
320
  client: DynamoDBClient,
323
- model?: M,
321
+ model?: ModelDefinition,
324
322
  logger?: DynamoDBLogger
325
- ): QueryBuilder<Model, M> {
323
+ ): QueryBuilder<Model> {
326
324
  return {
327
325
  where(fn) {
328
326
  // Create attribute builder proxy
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { ExecutableBuilder, AttrBuilder, OpBuilder, Condition } from '../shared';
3
- import { ModelDefinition } from '../../core/types';
4
3
 
5
4
  /**
6
5
  * Query result with pagination support
@@ -30,38 +29,36 @@ export interface QueryResult<Model> {
30
29
  /**
31
30
  * Query executor with additional query-specific options
32
31
  */
33
- export interface QueryExecutor<Model, M extends ModelDefinition = any> extends ExecutableBuilder<
34
- Model[]
35
- > {
32
+ export interface QueryExecutor<Model> extends ExecutableBuilder<Model[]> {
36
33
  /**
37
34
  * Limit the number of items to return
38
35
  */
39
- limit(count: number): QueryExecutor<Model, M>;
36
+ limit(count: number): QueryExecutor<Model>;
40
37
 
41
38
  /**
42
39
  * Scan index forward (ascending) or backward (descending)
43
40
  */
44
- scanIndexForward(forward: boolean): QueryExecutor<Model, M>;
41
+ scanIndexForward(forward: boolean): QueryExecutor<Model>;
45
42
 
46
43
  /**
47
44
  * Use a secondary index
48
45
  */
49
- useIndex(indexName: string): QueryExecutor<Model, M>;
46
+ useIndex(indexName: string): QueryExecutor<Model>;
50
47
 
51
48
  /**
52
49
  * Select specific attributes to return
53
50
  */
54
- select(attrs: (keyof Model)[]): QueryExecutor<Model, M>;
51
+ select(attrs: (keyof Model)[]): QueryExecutor<Model>;
55
52
 
56
53
  /**
57
54
  * Use consistent read
58
55
  */
59
- consistentRead(): QueryExecutor<Model, M>;
56
+ consistentRead(): QueryExecutor<Model>;
60
57
 
61
58
  /**
62
59
  * Start query from a specific key (for pagination)
63
60
  */
64
- startFrom(key: Record<string, any>): QueryExecutor<Model, M>;
61
+ startFrom(key: Record<string, any>): QueryExecutor<Model>;
65
62
 
66
63
  /**
67
64
  * Returns the raw DynamoDB query parameters
@@ -82,7 +79,7 @@ export interface QueryExecutor<Model, M extends ModelDefinition = any> extends E
82
79
  /**
83
80
  * Main Query Builder interface with type-safe where clause
84
81
  */
85
- export interface QueryBuilder<Model, M extends ModelDefinition = any> {
82
+ export interface QueryBuilder<Model> {
86
83
  /**
87
84
  * Build a condition expression using attributes and operators
88
85
  * Usage: .where((attr, op) => op.eq(attr.username, 'juanca'))
@@ -91,5 +88,5 @@ export interface QueryBuilder<Model, M extends ModelDefinition = any> {
91
88
  * op.gt(attr.age, 18)
92
89
  * ))
93
90
  */
94
- where(fn: (attr: AttrBuilder<Model>, op: OpBuilder) => Condition): QueryExecutor<Model, M>;
91
+ where(fn: (attr: AttrBuilder<Model>, op: OpBuilder) => Condition): QueryExecutor<Model>;
95
92
  }
@@ -5,9 +5,10 @@ TransactWrite provides atomic multi-item write operations in DynamoDB. All opera
5
5
  ## Features
6
6
 
7
7
  - **Functional API**: Immutable builders using pure functions
8
- - **Type-safe**: Full TypeScript support
8
+ - **Type-safe**: Full TypeScript support with AWS SDK types
9
9
  - **Composable**: Chain operations fluently
10
10
  - **Idempotent**: Support for client request tokens
11
+ - **Flexible**: Accepts both entity builder outputs and raw DynamoDB parameters
11
12
 
12
13
  ## Usage
13
14
 
@@ -144,6 +145,41 @@ Use client request tokens for idempotent operations:
144
145
  await table.transactWrite().addPut(params).withClientRequestToken('unique-id-12345').execute();
145
146
  ```
146
147
 
148
+ ## Type Safety
149
+
150
+ All operations are fully typed using AWS SDK types:
151
+
152
+ ```typescript
153
+ import type {
154
+ TransactPutParams,
155
+ TransactUpdateParams,
156
+ TransactDeleteParams,
157
+ TransactConditionCheckParams,
158
+ } from '@ftschopp/dynatable-core';
159
+
160
+ // addPut accepts PutCommandInput from @aws-sdk/lib-dynamodb
161
+ const putParams: TransactPutParams = {
162
+ TableName: 'MyTable',
163
+ Item: { pk: 'USER#123', sk: 'USER#123', name: 'John' },
164
+ };
165
+
166
+ // TypeScript validates all parameters at compile time
167
+ table.transactWrite().addPut(putParams).execute();
168
+ ```
169
+
170
+ ### Using Entity Builders
171
+
172
+ Entity builders automatically provide correctly typed parameters via `.dbParams()`:
173
+
174
+ ```typescript
175
+ // The entity builder's dbParams() returns PutCommandInput
176
+ const params = table.entities.User.put({ username: 'john' }).ifNotExists().dbParams();
177
+ // params is typed as PutCommandInput
178
+
179
+ // Pass it to the transaction builder
180
+ table.transactWrite().addPut(params).execute();
181
+ ```
182
+
147
183
  ## Functional Design
148
184
 
149
185
  The builder is immutable - each method returns a new instance:
@@ -1,7 +1,13 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
1
  import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
3
2
  import { TransactWriteCommand } from '@aws-sdk/lib-dynamodb';
4
- import { TransactWriteBuilder, TransactWriteState } from './types';
3
+ import {
4
+ TransactWriteBuilder,
5
+ TransactWriteState,
6
+ TransactPutParams,
7
+ TransactUpdateParams,
8
+ TransactDeleteParams,
9
+ TransactConditionCheckParams,
10
+ } from './types';
5
11
 
6
12
  /**
7
13
  * Creates the initial state for a TransactWrite builder
@@ -16,7 +22,7 @@ const createInitialState = (client: DynamoDBClient): TransactWriteState => ({
16
22
  */
17
23
  const addPutItem =
18
24
  (state: TransactWriteState) =>
19
- (params: any): TransactWriteState => ({
25
+ (params: TransactPutParams): TransactWriteState => ({
20
26
  ...state,
21
27
  items: [...state.items, { Put: params }],
22
28
  });
@@ -26,7 +32,7 @@ const addPutItem =
26
32
  */
27
33
  const addUpdateItem =
28
34
  (state: TransactWriteState) =>
29
- (params: any): TransactWriteState => ({
35
+ (params: TransactUpdateParams): TransactWriteState => ({
30
36
  ...state,
31
37
  items: [...state.items, { Update: params }],
32
38
  });
@@ -36,7 +42,7 @@ const addUpdateItem =
36
42
  */
37
43
  const addDeleteItem =
38
44
  (state: TransactWriteState) =>
39
- (params: any): TransactWriteState => ({
45
+ (params: TransactDeleteParams): TransactWriteState => ({
40
46
  ...state,
41
47
  items: [...state.items, { Delete: params }],
42
48
  });
@@ -46,7 +52,7 @@ const addDeleteItem =
46
52
  */
47
53
  const addConditionCheckItem =
48
54
  (state: TransactWriteState) =>
49
- (params: any): TransactWriteState => ({
55
+ (params: TransactConditionCheckParams): TransactWriteState => ({
50
56
  ...state,
51
57
  items: [...state.items, { ConditionCheck: params }],
52
58
  });
@@ -64,9 +70,9 @@ const setClientRequestToken =
64
70
  /**
65
71
  * Converts the builder state to DynamoDB parameters
66
72
  */
67
- const toDbParams = (state: TransactWriteState) => {
68
- const params: any = {
69
- TransactItems: state.items,
73
+ const toDbParams = (state: TransactWriteState): ReturnType<TransactWriteBuilder['dbParams']> => {
74
+ const params: ReturnType<TransactWriteBuilder['dbParams']> = {
75
+ TransactItems: [...state.items],
70
76
  };
71
77
 
72
78
  if (state.clientRequestToken) {
@@ -89,11 +95,11 @@ const execute = async (state: TransactWriteState) => {
89
95
  * Creates a builder from the current state
90
96
  */
91
97
  const createBuilder = (state: TransactWriteState): TransactWriteBuilder => ({
92
- addPut: (params: any) => createBuilder(addPutItem(state)(params)),
93
- addUpdate: (params: any) => createBuilder(addUpdateItem(state)(params)),
94
- addDelete: (params: any) => createBuilder(addDeleteItem(state)(params)),
95
- addConditionCheck: (params: any) => createBuilder(addConditionCheckItem(state)(params)),
96
- withClientRequestToken: (token: string) => createBuilder(setClientRequestToken(state)(token)),
98
+ addPut: (params) => createBuilder(addPutItem(state)(params)),
99
+ addUpdate: (params) => createBuilder(addUpdateItem(state)(params)),
100
+ addDelete: (params) => createBuilder(addDeleteItem(state)(params)),
101
+ addConditionCheck: (params) => createBuilder(addConditionCheckItem(state)(params)),
102
+ withClientRequestToken: (token) => createBuilder(setClientRequestToken(state)(token)),
97
103
  dbParams: () => toDbParams(state),
98
104
  execute: () => execute(state),
99
105
  });
@@ -1,8 +1,46 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
3
+ import type {
4
+ TransactWriteCommandInput,
5
+ TransactWriteCommandOutput,
6
+ PutCommandInput,
7
+ UpdateCommandInput,
8
+ DeleteCommandInput,
9
+ } from '@aws-sdk/lib-dynamodb';
10
+
11
+ // Extract types from AWS SDK's TransactWriteCommandInput
12
+ type TransactWriteItems = NonNullable<TransactWriteCommandInput['TransactItems']>;
13
+ type StrictTransactPut = NonNullable<TransactWriteItems[number]['Put']>;
14
+ type StrictTransactUpdate = NonNullable<TransactWriteItems[number]['Update']>;
15
+ type StrictTransactDelete = NonNullable<TransactWriteItems[number]['Delete']>;
16
+ type StrictTransactConditionCheck = NonNullable<TransactWriteItems[number]['ConditionCheck']>;
17
+
18
+ /**
19
+ * Parameters for a Put operation within a transaction
20
+ * Accepts both regular PutCommandInput and strict transaction Put params
21
+ */
22
+ export type TransactPutParams = PutCommandInput | StrictTransactPut;
23
+
24
+ /**
25
+ * Parameters for an Update operation within a transaction
26
+ * Accepts both regular UpdateCommandInput and strict transaction Update params
27
+ */
28
+ export type TransactUpdateParams = UpdateCommandInput | StrictTransactUpdate;
29
+
30
+ /**
31
+ * Parameters for a Delete operation within a transaction
32
+ * Accepts both regular DeleteCommandInput and strict transaction Delete params
33
+ */
34
+ export type TransactDeleteParams = DeleteCommandInput | StrictTransactDelete;
35
+
36
+ /**
37
+ * Parameters for a ConditionCheck operation within a transaction
38
+ */
39
+ export type TransactConditionCheckParams = StrictTransactConditionCheck;
3
40
 
4
41
  /**
5
42
  * Represents a single item in a TransactWrite operation
43
+ * Using any internally to allow flexibility between CommandInput and strict transaction types
6
44
  */
7
45
  export type TransactWriteItem =
8
46
  | { Put: any }
@@ -23,11 +61,11 @@ export type TransactWriteState = {
23
61
  * TransactWrite builder interface
24
62
  */
25
63
  export type TransactWriteBuilder = {
26
- readonly addPut: (params: any) => TransactWriteBuilder;
27
- readonly addUpdate: (params: any) => TransactWriteBuilder;
28
- readonly addDelete: (params: any) => TransactWriteBuilder;
29
- readonly addConditionCheck: (params: any) => TransactWriteBuilder;
64
+ readonly addPut: (params: TransactPutParams) => TransactWriteBuilder;
65
+ readonly addUpdate: (params: TransactUpdateParams) => TransactWriteBuilder;
66
+ readonly addDelete: (params: TransactDeleteParams) => TransactWriteBuilder;
67
+ readonly addConditionCheck: (params: TransactConditionCheckParams) => TransactWriteBuilder;
30
68
  readonly withClientRequestToken: (token: string) => TransactWriteBuilder;
31
- readonly dbParams: () => any;
32
- readonly execute: () => Promise<any>;
69
+ readonly dbParams: () => TransactWriteCommandInput;
70
+ readonly execute: () => Promise<TransactWriteCommandOutput>;
33
71
  };
@@ -52,6 +52,49 @@ describe('UpdateBuilder', () => {
52
52
  ':score_2': 100,
53
53
  });
54
54
  });
55
+
56
+ test('should build params with multiple SET operations using object', () => {
57
+ const key: Partial<TestModel> = { pk: 'USER#1', sk: 'USER#1' };
58
+ const params = createUpdateBuilder<TestModel>(tableName, key, client)
59
+ .set({
60
+ name: 'John Doe',
61
+ age: 30,
62
+ score: 100,
63
+ })
64
+ .dbParams();
65
+
66
+ expect(params.UpdateExpression).toBe('SET #name = :name_0, #age = :age_1, #score = :score_2');
67
+ expect(params.ExpressionAttributeNames).toEqual({
68
+ '#name': 'name',
69
+ '#age': 'age',
70
+ '#score': 'score',
71
+ });
72
+ expect(params.ExpressionAttributeValues).toEqual({
73
+ ':name_0': 'John Doe',
74
+ ':age_1': 30,
75
+ ':score_2': 100,
76
+ });
77
+ });
78
+
79
+ test('should combine single and multiple SET operations', () => {
80
+ const key: Partial<TestModel> = { pk: 'USER#1', sk: 'USER#1' };
81
+ const params = createUpdateBuilder<TestModel>(tableName, key, client)
82
+ .set('name', 'John Doe')
83
+ .set({ age: 30, score: 100 })
84
+ .dbParams();
85
+
86
+ expect(params.UpdateExpression).toBe('SET #name = :name_0, #age = :age_1, #score = :score_2');
87
+ expect(params.ExpressionAttributeNames).toEqual({
88
+ '#name': 'name',
89
+ '#age': 'age',
90
+ '#score': 'score',
91
+ });
92
+ expect(params.ExpressionAttributeValues).toEqual({
93
+ ':name_0': 'John Doe',
94
+ ':age_1': 30,
95
+ ':score_2': 100,
96
+ });
97
+ });
55
98
  });
56
99
 
57
100
  describe('REMOVE operations', () => {
@@ -59,8 +59,49 @@ export function createUpdateBuilder<Model>(
59
59
  );
60
60
  },
61
61
 
62
- set(attr, value) {
63
- const attrName = normalizeAttr(attr);
62
+ set(attrOrUpdates: keyof Model | AttrRef | Partial<Model>, value?: any) {
63
+ // Check if this is a multiple updates object
64
+ // An AttrRef has only 'name' property and is a string value
65
+ // Multiple updates is an object with no value parameter
66
+ if (
67
+ value === undefined &&
68
+ typeof attrOrUpdates === 'object' &&
69
+ attrOrUpdates !== null &&
70
+ !(
71
+ Object.keys(attrOrUpdates).length === 1 &&
72
+ 'name' in attrOrUpdates &&
73
+ typeof (attrOrUpdates as any).name === 'string'
74
+ )
75
+ ) {
76
+ // Multiple updates case
77
+ const updates = attrOrUpdates as Partial<Model>;
78
+ const newActions: UpdateAction[] = [];
79
+
80
+ for (const [attr, val] of Object.entries(updates)) {
81
+ const attrName = attr;
82
+ const valueName = getUniqueValueName(attrName);
83
+ newActions.push({
84
+ expression: `#${attrName} = :${valueName}`,
85
+ names: { [`#${attrName}`]: attrName },
86
+ values: { [`:${valueName}`]: val },
87
+ });
88
+ }
89
+
90
+ return createUpdateBuilder(
91
+ tableName,
92
+ key,
93
+ client,
94
+ conditions,
95
+ { ...updateActions, set: [...updateActions.set, ...newActions] },
96
+ returnMode,
97
+ valueCounter,
98
+ enableTimestamps,
99
+ logger
100
+ );
101
+ }
102
+
103
+ // Single update case
104
+ const attrName = normalizeAttr(attrOrUpdates as keyof Model | AttrRef);
64
105
  const valueName = getUniqueValueName(attrName);
65
106
  const action: UpdateAction = {
66
107
  expression: `#${attrName} = :${valueName}`,
@@ -267,16 +308,13 @@ export function createUpdateBuilder<Model>(
267
308
  const response = await client.send(new UpdateCommand(params));
268
309
  logger?.log('UpdateCommand', params, response);
269
310
 
270
- // Return the updated item based on returnMode
271
- if (returnMode === 'ALL_NEW' && response.Attributes) {
272
- return response.Attributes as Model;
273
- }
274
- if (returnMode === 'UPDATED_NEW' && response.Attributes) {
311
+ // Return the item based on returnMode
312
+ if (response.Attributes) {
275
313
  return response.Attributes as Model;
276
314
  }
277
315
 
278
- // For other modes, return the key as a fallback
279
- return key as Model;
316
+ // If no attributes returned (NONE mode), return undefined
317
+ return undefined as unknown as Model;
280
318
  },
281
319
  });
282
320