@ftschopp/dynatable-core 1.4.2 → 1.4.4

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 (86) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/builders/query/create-query-builder.d.ts.map +1 -1
  3. package/dist/builders/query/create-query-builder.js +19 -2
  4. package/dist/builders/update/create-update-builder.d.ts.map +1 -1
  5. package/dist/builders/update/create-update-builder.js +25 -0
  6. package/dist/core/types.d.ts +5 -0
  7. package/dist/core/types.d.ts.map +1 -1
  8. package/dist/utils/model-utils.d.ts +9 -3
  9. package/dist/utils/model-utils.d.ts.map +1 -1
  10. package/dist/utils/model-utils.js +15 -7
  11. package/package.json +19 -2
  12. package/eslint.config.mjs +0 -4
  13. package/jest.config.js +0 -11
  14. package/src/builders/README.md +0 -339
  15. package/src/builders/batch-get/README.md +0 -43
  16. package/src/builders/batch-get/create-batch-get-builder.test.ts +0 -193
  17. package/src/builders/batch-get/create-batch-get-builder.ts +0 -108
  18. package/src/builders/batch-get/index.ts +0 -2
  19. package/src/builders/batch-get/types.ts +0 -28
  20. package/src/builders/batch-write/README.md +0 -204
  21. package/src/builders/batch-write/create-batch-write-builder.test.ts +0 -173
  22. package/src/builders/batch-write/create-batch-write-builder.ts +0 -49
  23. package/src/builders/batch-write/index.ts +0 -2
  24. package/src/builders/batch-write/types.ts +0 -33
  25. package/src/builders/delete/create-delete-builder.test.ts +0 -294
  26. package/src/builders/delete/create-delete-builder.ts +0 -100
  27. package/src/builders/delete/index.ts +0 -2
  28. package/src/builders/delete/types.ts +0 -17
  29. package/src/builders/get/create-get-builder.test.ts +0 -272
  30. package/src/builders/get/create-get-builder.ts +0 -117
  31. package/src/builders/get/index.ts +0 -2
  32. package/src/builders/get/types.ts +0 -39
  33. package/src/builders/index.ts +0 -14
  34. package/src/builders/put/create-put-builder.test.ts +0 -213
  35. package/src/builders/put/create-put-builder.ts +0 -160
  36. package/src/builders/put/index.ts +0 -2
  37. package/src/builders/put/types.ts +0 -24
  38. package/src/builders/query/create-query-builder.test.ts +0 -770
  39. package/src/builders/query/create-query-builder.ts +0 -455
  40. package/src/builders/query/index.ts +0 -2
  41. package/src/builders/query/types.ts +0 -109
  42. package/src/builders/scan/create-scan-builder.test.ts +0 -416
  43. package/src/builders/scan/create-scan-builder.ts +0 -265
  44. package/src/builders/scan/index.ts +0 -2
  45. package/src/builders/scan/types.ts +0 -88
  46. package/src/builders/shared/conditions.ts +0 -58
  47. package/src/builders/shared/index.ts +0 -4
  48. package/src/builders/shared/operators.test.ts +0 -270
  49. package/src/builders/shared/operators.ts +0 -208
  50. package/src/builders/shared/projection.ts +0 -28
  51. package/src/builders/shared/types.ts +0 -101
  52. package/src/builders/transact-get/README.md +0 -167
  53. package/src/builders/transact-get/create-transact-get-builder.test.ts +0 -239
  54. package/src/builders/transact-get/create-transact-get-builder.ts +0 -67
  55. package/src/builders/transact-get/index.ts +0 -2
  56. package/src/builders/transact-get/types.ts +0 -31
  57. package/src/builders/transact-write/README.md +0 -202
  58. package/src/builders/transact-write/create-transact-write-builder.test.ts +0 -288
  59. package/src/builders/transact-write/create-transact-write-builder.ts +0 -124
  60. package/src/builders/transact-write/index.ts +0 -2
  61. package/src/builders/transact-write/types.ts +0 -71
  62. package/src/builders/update/create-update-builder.test.ts +0 -573
  63. package/src/builders/update/create-update-builder.ts +0 -375
  64. package/src/builders/update/index.ts +0 -2
  65. package/src/builders/update/types.ts +0 -63
  66. package/src/core/types.test.ts +0 -641
  67. package/src/core/types.ts +0 -366
  68. package/src/entity/create-entity-api.ts +0 -224
  69. package/src/entity/index.ts +0 -19
  70. package/src/entity/middleware/factories.ts +0 -15
  71. package/src/entity/middleware/types.ts +0 -7
  72. package/src/entity/middleware/with-middleware.ts +0 -37
  73. package/src/entity/types.ts +0 -79
  74. package/src/entity/validation/key-validation.ts +0 -34
  75. package/src/index.ts +0 -23
  76. package/src/table.ts +0 -116
  77. package/src/utils/dynamodb-logger.test.ts +0 -246
  78. package/src/utils/dynamodb-logger.ts +0 -175
  79. package/src/utils/model-utils.test.ts +0 -452
  80. package/src/utils/model-utils.ts +0 -188
  81. package/src/utils/zod-utils.test.ts +0 -363
  82. package/src/utils/zod-utils.ts +0 -56
  83. package/tests/integration/instagram-clone.integration.test.ts +0 -1019
  84. package/tests/integration/pagination-timestamps.integration.test.ts +0 -374
  85. package/tests/integration/transactions.integration.test.ts +0 -528
  86. package/tsconfig.json +0 -12
@@ -1,43 +0,0 @@
1
- # BatchGet Builder
2
-
3
- The `BatchGetBuilder` retrieves multiple items by key in a single DynamoDB `BatchGetItem` request. It is consumed via the entity API (`table.entities.<Entity>.batchGet([...])`), and `execute()` returns a flat `Model[]`.
4
-
5
- ## Features
6
-
7
- - Retrieve multiple items by their keys
8
- - Supports attribute projection (select only specific fields)
9
- - Supports consistent read
10
-
11
- ## Basic Usage
12
-
13
- ```typescript
14
- const users = await table.entities.User.batchGet([
15
- { username: 'alice' },
16
- { username: 'bob' },
17
- { username: 'charlie' },
18
- ]).execute();
19
-
20
- users.forEach((user) => console.log(user.username));
21
- ```
22
-
23
- ## With Attribute Projection
24
-
25
- ```typescript
26
- const users = await table.entities.User.batchGet([{ username: 'alice' }, { username: 'bob' }])
27
- .select(['username', 'name', 'followerCount'])
28
- .execute();
29
- ```
30
-
31
- ## With Consistent Read
32
-
33
- ```typescript
34
- const users = await table.entities.User.batchGet([{ username: 'alice' }, { username: 'bob' }])
35
- .consistentRead()
36
- .execute();
37
- ```
38
-
39
- ## DynamoDB Limitations
40
-
41
- - Maximum 100 items per request
42
- - Maximum 16 MB of data per request
43
- - Unprocessed items are returned in `UnprocessedKeys` and must be retried
@@ -1,193 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
3
- import { createBatchGetBuilder } from './create-batch-get-builder';
4
-
5
- interface User {
6
- pk: string;
7
- sk: string;
8
- username: string;
9
- name: string;
10
- followerCount: number;
11
- followingCount: number;
12
- }
13
-
14
- describe('BatchGetBuilder', () => {
15
- const client = new DynamoDBClient({});
16
-
17
- describe('dbParams', () => {
18
- test('should build basic BatchGetItem params', () => {
19
- const requestItems = {
20
- Users: {
21
- Keys: [
22
- { pk: 'USER#alice', sk: 'USER#alice' },
23
- { pk: 'USER#bob', sk: 'USER#bob' },
24
- ],
25
- },
26
- };
27
-
28
- const builder = createBatchGetBuilder(requestItems, client);
29
- const params = builder.dbParams();
30
-
31
- expect(params).toEqual({
32
- RequestItems: {
33
- Users: {
34
- Keys: [
35
- { pk: 'USER#alice', sk: 'USER#alice' },
36
- { pk: 'USER#bob', sk: 'USER#bob' },
37
- ],
38
- },
39
- },
40
- });
41
- });
42
-
43
- test('should build BatchGetItem params with projection', () => {
44
- const requestItems = {
45
- Users: {
46
- Keys: [
47
- { pk: 'USER#alice', sk: 'USER#alice' },
48
- { pk: 'USER#bob', sk: 'USER#bob' },
49
- ],
50
- },
51
- };
52
-
53
- const builder = createBatchGetBuilder<User>(requestItems, client).select([
54
- 'username',
55
- 'name',
56
- 'followerCount',
57
- ]);
58
- const params = builder.dbParams();
59
-
60
- expect(params).toEqual({
61
- RequestItems: {
62
- Users: {
63
- Keys: [
64
- { pk: 'USER#alice', sk: 'USER#alice' },
65
- { pk: 'USER#bob', sk: 'USER#bob' },
66
- ],
67
- ProjectionExpression: '#username, #name, #followerCount',
68
- ExpressionAttributeNames: {
69
- '#username': 'username',
70
- '#name': 'name',
71
- '#followerCount': 'followerCount',
72
- },
73
- },
74
- },
75
- });
76
- });
77
-
78
- test('should build BatchGetItem params with consistent read', () => {
79
- const requestItems = {
80
- Users: {
81
- Keys: [
82
- { pk: 'USER#alice', sk: 'USER#alice' },
83
- { pk: 'USER#bob', sk: 'USER#bob' },
84
- ],
85
- },
86
- };
87
-
88
- const builder = createBatchGetBuilder(requestItems, client).consistentRead();
89
- const params = builder.dbParams();
90
-
91
- expect(params).toEqual({
92
- RequestItems: {
93
- Users: {
94
- Keys: [
95
- { pk: 'USER#alice', sk: 'USER#alice' },
96
- { pk: 'USER#bob', sk: 'USER#bob' },
97
- ],
98
- ConsistentRead: true,
99
- },
100
- },
101
- });
102
- });
103
-
104
- test('should build BatchGetItem params with multiple tables', () => {
105
- const requestItems = {
106
- Users: {
107
- Keys: [
108
- { pk: 'USER#alice', sk: 'USER#alice' },
109
- { pk: 'USER#bob', sk: 'USER#bob' },
110
- ],
111
- },
112
- Photos: {
113
- Keys: [
114
- { pk: 'UP#alice', sk: 'PHOTO#photo1' },
115
- { pk: 'UP#bob', sk: 'PHOTO#photo2' },
116
- ],
117
- },
118
- };
119
-
120
- const builder = createBatchGetBuilder(requestItems, client);
121
- const params = builder.dbParams();
122
-
123
- expect(params).toEqual({
124
- RequestItems: {
125
- Users: {
126
- Keys: [
127
- { pk: 'USER#alice', sk: 'USER#alice' },
128
- { pk: 'USER#bob', sk: 'USER#bob' },
129
- ],
130
- },
131
- Photos: {
132
- Keys: [
133
- { pk: 'UP#alice', sk: 'PHOTO#photo1' },
134
- { pk: 'UP#bob', sk: 'PHOTO#photo2' },
135
- ],
136
- },
137
- },
138
- });
139
- });
140
-
141
- test('should build BatchGetItem params with projection and consistent read', () => {
142
- const requestItems = {
143
- Users: {
144
- Keys: [
145
- { pk: 'USER#alice', sk: 'USER#alice' },
146
- { pk: 'USER#bob', sk: 'USER#bob' },
147
- ],
148
- },
149
- };
150
-
151
- const builder = createBatchGetBuilder<User>(requestItems, client)
152
- .select(['username', 'name'])
153
- .consistentRead();
154
- const params = builder.dbParams();
155
-
156
- expect(params).toEqual({
157
- RequestItems: {
158
- Users: {
159
- Keys: [
160
- { pk: 'USER#alice', sk: 'USER#alice' },
161
- { pk: 'USER#bob', sk: 'USER#bob' },
162
- ],
163
- ProjectionExpression: '#username, #name',
164
- ExpressionAttributeNames: {
165
- '#username': 'username',
166
- '#name': 'name',
167
- },
168
- ConsistentRead: true,
169
- },
170
- },
171
- });
172
- });
173
-
174
- test('reserved DynamoDB words like "name", "date", "status" can be projected', () => {
175
- const requestItems = {
176
- Users: {
177
- Keys: [{ pk: 'USER#alice', sk: 'USER#alice' }],
178
- },
179
- };
180
-
181
- const builder = createBatchGetBuilder<User>(requestItems, client).select(['name']);
182
- const params = builder.dbParams();
183
-
184
- expect(params.RequestItems!['Users']).toEqual({
185
- Keys: [{ pk: 'USER#alice', sk: 'USER#alice' }],
186
- ProjectionExpression: '#name',
187
- ExpressionAttributeNames: {
188
- '#name': 'name',
189
- },
190
- });
191
- });
192
- });
193
- });
@@ -1,108 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
3
- import {
4
- BatchGetCommand,
5
- BatchGetCommandInput,
6
- BatchGetCommandOutput,
7
- } from '@aws-sdk/lib-dynamodb';
8
- import { buildProjectionExpression } from '../shared';
9
- import { BatchGetBuilder } from './types';
10
- import { DynamoDBLogger } from '../../utils/dynamodb-logger';
11
-
12
- /**
13
- * Creates a BatchGetBuilder to retrieve multiple items by their keys.
14
- *
15
- * @param requestItems - An object where keys are table names and values are objects containing Keys array
16
- * @param client - DynamoDB client instance
17
- * @param options - Optional configuration (projection, consistentRead)
18
- * @param logger - Optional logger instance
19
- */
20
- export function createBatchGetBuilder<Model>(
21
- requestItems: Record<string, { Keys: any[] }>,
22
- client: DynamoDBClient,
23
- options?: {
24
- projection?: (keyof Model)[];
25
- consistentRead?: boolean;
26
- },
27
- logger?: DynamoDBLogger
28
- ): BatchGetBuilder<Model> {
29
- const projection = options?.projection ?? [];
30
- const isConsistent = options?.consistentRead ?? false;
31
-
32
- return {
33
- /**
34
- * Select only specific attributes to retrieve.
35
- */
36
- select(attrs) {
37
- return createBatchGetBuilder(
38
- requestItems,
39
- client,
40
- {
41
- projection: attrs,
42
- consistentRead: isConsistent,
43
- },
44
- logger
45
- );
46
- },
47
-
48
- /**
49
- * Enable strongly consistent read.
50
- */
51
- consistentRead() {
52
- return createBatchGetBuilder(
53
- requestItems,
54
- client,
55
- {
56
- projection,
57
- consistentRead: true,
58
- },
59
- logger
60
- );
61
- },
62
-
63
- /**
64
- * Build the underlying DynamoDB input parameters.
65
- */
66
- dbParams(): BatchGetCommandInput {
67
- // Build the projection once (with #-placeholders so reserved words
68
- // like name/date/status/type don't blow up at DynamoDB) and reuse
69
- // it per-table.
70
- const proj =
71
- projection.length > 0 ? buildProjectionExpression(projection.map(String)) : undefined;
72
-
73
- const enhancedRequestItems = Object.entries(requestItems).reduce(
74
- (acc, [tableName, tableRequest]) => {
75
- acc[tableName] = {
76
- Keys: tableRequest.Keys,
77
- ...(proj && {
78
- ProjectionExpression: proj.ProjectionExpression,
79
- ExpressionAttributeNames: proj.ExpressionAttributeNames,
80
- }),
81
- ...(isConsistent && { ConsistentRead: true }),
82
- };
83
- return acc;
84
- },
85
- {} as Record<string, any>
86
- );
87
-
88
- return {
89
- RequestItems: enhancedRequestItems,
90
- };
91
- },
92
-
93
- /**
94
- * Execute the BatchGetItem command and return the items as a flat array.
95
- */
96
- async execute(): Promise<Model[]> {
97
- const params = this.dbParams();
98
- const result: BatchGetCommandOutput = await client.send(new BatchGetCommand(params));
99
- logger?.log('BatchGetCommand', params, result);
100
-
101
- if (!result.Responses) {
102
- return [];
103
- }
104
-
105
- return Object.values(result.Responses).flat() as unknown as Model[];
106
- },
107
- };
108
- }
@@ -1,2 +0,0 @@
1
- export * from './types';
2
- export * from './create-batch-get-builder';
@@ -1,28 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { BatchGetItemCommandInput } from '@aws-sdk/client-dynamodb';
3
-
4
- /**
5
- * BatchGetBuilder allows you to retrieve multiple items from one or more tables
6
- * in a single request.
7
- */
8
- export type BatchGetBuilder<Model> = {
9
- /**
10
- * Select only specific attributes to retrieve for all items.
11
- */
12
- select(attrs: (keyof Model)[]): BatchGetBuilder<Model>;
13
-
14
- /**
15
- * Enable strongly consistent read for all items.
16
- */
17
- consistentRead(): BatchGetBuilder<Model>;
18
-
19
- /**
20
- * Build the underlying DynamoDB BatchGetItem input parameters.
21
- */
22
- dbParams(): BatchGetItemCommandInput;
23
-
24
- /**
25
- * Execute the BatchGetItem command and return the items as a flat array.
26
- */
27
- execute(): Promise<Model[]>;
28
- };
@@ -1,204 +0,0 @@
1
- # BatchWrite Builder
2
-
3
- The `BatchWriteBuilder` allows writing (put) or deleting (delete) multiple items from one or more tables in a single request to DynamoDB.
4
-
5
- ## Features
6
-
7
- - Create multiple items (PutRequest)
8
- - Delete multiple items (DeleteRequest)
9
- - Mix Put and Delete operations in a single request
10
- - Supports multiple tables in a single request
11
- - Handles unprocessed items (UnprocessedItems)
12
-
13
- ## Basic Usage - Create Items
14
-
15
- ```typescript
16
- import { createBatchWriteBuilder } from './builders/batch-write';
17
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
18
-
19
- const client = new DynamoDBClient({});
20
-
21
- // Create multiple users
22
- const result = await createBatchWriteBuilder(
23
- {
24
- Users: [
25
- {
26
- PutRequest: {
27
- Item: {
28
- pk: 'USER#alice',
29
- sk: 'USER#alice',
30
- username: 'alice',
31
- name: 'Alice Smith',
32
- followerCount: 0,
33
- followingCount: 0,
34
- },
35
- },
36
- },
37
- {
38
- PutRequest: {
39
- Item: {
40
- pk: 'USER#bob',
41
- sk: 'USER#bob',
42
- username: 'bob',
43
- name: 'Bob Jones',
44
- followerCount: 0,
45
- followingCount: 0,
46
- },
47
- },
48
- },
49
- ],
50
- },
51
- client
52
- ).execute();
53
- ```
54
-
55
- ## Delete Items
56
-
57
- ```typescript
58
- const result = await createBatchWriteBuilder(
59
- {
60
- Users: [
61
- {
62
- DeleteRequest: {
63
- Key: {
64
- pk: 'USER#alice',
65
- sk: 'USER#alice',
66
- },
67
- },
68
- },
69
- {
70
- DeleteRequest: {
71
- Key: {
72
- pk: 'USER#bob',
73
- sk: 'USER#bob',
74
- },
75
- },
76
- },
77
- ],
78
- },
79
- client
80
- ).execute();
81
- ```
82
-
83
- ## Mix Put and Delete
84
-
85
- ```typescript
86
- const result = await createBatchWriteBuilder(
87
- {
88
- Users: [
89
- {
90
- PutRequest: {
91
- Item: {
92
- pk: 'USER#alice',
93
- sk: 'USER#alice',
94
- username: 'alice',
95
- name: 'Alice Smith Updated',
96
- },
97
- },
98
- },
99
- {
100
- DeleteRequest: {
101
- Key: {
102
- pk: 'USER#bob',
103
- sk: 'USER#bob',
104
- },
105
- },
106
- },
107
- ],
108
- },
109
- client
110
- ).execute();
111
- ```
112
-
113
- ## Using the Table Class
114
-
115
- ```typescript
116
- // Create multiple items using the Table class
117
- const result = await table
118
- .batchWrite({
119
- InstagramClone: [
120
- {
121
- PutRequest: {
122
- Item: {
123
- pk: 'USER#alice',
124
- sk: 'USER#alice',
125
- username: 'alice',
126
- name: 'Alice Smith',
127
- },
128
- },
129
- },
130
- {
131
- DeleteRequest: {
132
- Key: {
133
- pk: 'USER#bob',
134
- sk: 'USER#bob',
135
- },
136
- },
137
- },
138
- ],
139
- })
140
- .execute();
141
-
142
- // Handle unprocessed items
143
- if (result.unprocessedItems) {
144
- console.log('Unprocessed items:', result.unprocessedItems);
145
- // Retry unprocessed items
146
- }
147
- ```
148
-
149
- ## Bulk Operations
150
-
151
- ```typescript
152
- // Create multiple photos and likes in a single request
153
- await table
154
- .batchWrite({
155
- InstagramClone: [
156
- {
157
- PutRequest: {
158
- Item: {
159
- pk: 'UP#alice',
160
- sk: 'PHOTO#photo1',
161
- username: 'alice',
162
- photoId: 'photo1',
163
- url: 'https://example.com/photo1.jpg',
164
- likesCount: 0,
165
- commentCount: 0,
166
- },
167
- },
168
- },
169
- {
170
- PutRequest: {
171
- Item: {
172
- pk: 'UP#alice',
173
- sk: 'PHOTO#photo2',
174
- username: 'alice',
175
- photoId: 'photo2',
176
- url: 'https://example.com/photo2.jpg',
177
- likesCount: 0,
178
- commentCount: 0,
179
- },
180
- },
181
- },
182
- {
183
- PutRequest: {
184
- Item: {
185
- pk: 'PL#photo1',
186
- sk: 'LIKE#bob',
187
- photoId: 'photo1',
188
- likingUsername: 'bob',
189
- likeId: 'like123',
190
- },
191
- },
192
- },
193
- ],
194
- })
195
- .execute();
196
- ```
197
-
198
- ## DynamoDB Limitations
199
-
200
- - Maximum 25 items per request
201
- - Maximum 16 MB of data per request
202
- - Does not support conditions (ConditionExpression)
203
- - Unprocessed items are returned in `UnprocessedItems` and must be retried
204
- - Put operations completely overwrite the existing item