@aws-blocks/bb-distributed-table 0.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.
- package/LICENSE +174 -0
- package/README.md +292 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +135 -0
- package/dist/gsi-manager-lambda/index.js +221 -0
- package/dist/gsi-manager-lambda.d.ts +26 -0
- package/dist/gsi-manager-lambda.d.ts.map +1 -0
- package/dist/gsi-manager-lambda.js +244 -0
- package/dist/index.aws.d.ts +59 -0
- package/dist/index.aws.d.ts.map +1 -0
- package/dist/index.aws.js +310 -0
- package/dist/index.browser.d.ts +5 -0
- package/dist/index.browser.d.ts.map +1 -0
- package/dist/index.browser.js +7 -0
- package/dist/index.cdk.d.ts +27 -0
- package/dist/index.cdk.d.ts.map +1 -0
- package/dist/index.cdk.js +180 -0
- package/dist/index.cdk.test.d.ts +2 -0
- package/dist/index.cdk.test.d.ts.map +1 -0
- package/dist/index.cdk.test.js +93 -0
- package/dist/index.mock.d.ts +101 -0
- package/dist/index.mock.d.ts.map +1 -0
- package/dist/index.mock.js +301 -0
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +555 -0
- package/dist/parity.test.d.ts +2 -0
- package/dist/parity.test.d.ts.map +1 -0
- package/dist/parity.test.js +557 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/package.json +49 -0
- package/src/errors.ts +145 -0
- package/src/gsi-manager-lambda.ts +305 -0
- package/src/index.aws.ts +400 -0
- package/src/index.browser.ts +8 -0
- package/src/index.cdk.test.ts +107 -0
- package/src/index.cdk.ts +220 -0
- package/src/index.mock.ts +363 -0
- package/src/index.test.ts +657 -0
- package/src/parity.test.ts +763 -0
- package/src/types.ts +163 -0
- package/src/version.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
package/README.md
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# DistributedTable
|
|
2
|
+
|
|
3
|
+
Structured data storage backed by DynamoDB with secondary indexes and rich query capabilities.
|
|
4
|
+
|
|
5
|
+
**When to use:** You need to query by multiple fields, use composite keys, or perform sort-key-based range queries. Good for entities with relationships, time-series data, and access patterns that require multiple indexes.
|
|
6
|
+
|
|
7
|
+
**When NOT to use:** If you only need single-key lookups, use `KVStore`. If you need full SQL (joins, aggregations), use `Database`.
|
|
8
|
+
|
|
9
|
+
## API
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
const table = new DistributedTable(scope, id, options)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
> **Type inference (important):** Do **not** pass a single explicit type argument like `new DistributedTable<MyType>(...)`. Doing so pins only `T` and lets the key/index generics fall back to their broad defaults, which breaks key-type inference — `get()` and `query({ where })` will then demand *every* field of the type instead of just the key fields. Either let all generics infer (`new DistributedTable(scope, id, { schema, key, indexes })` with no explicit `<...>`), or pass all three generics. Note that `as const` alone does **not** fix it.
|
|
16
|
+
|
|
17
|
+
| Method | Returns | Description |
|
|
18
|
+
|--------|---------|-------------|
|
|
19
|
+
| `get(key)` | `Promise<T \| null>` | Retrieve a single item by primary key. |
|
|
20
|
+
| `put(item, options?)` | `Promise<void>` | Store an item. Overwrites unless conditions are set. |
|
|
21
|
+
| `delete(key, options?)` | `Promise<void>` | Remove an item by primary key. |
|
|
22
|
+
| `query(options)` | `AsyncIterable<T>` | Query items by index or primary key. |
|
|
23
|
+
| `scan(options?)` | `AsyncIterable<T>` | Enumerate all items. Expensive on large datasets. |
|
|
24
|
+
| `getBatch(keys)` | `Promise<(T \| null)[]>` | Retrieve multiple items by key. |
|
|
25
|
+
| `putBatch(items)` | `Promise<void>` | Store multiple items. |
|
|
26
|
+
| `deleteBatch(keys)` | `Promise<void>` | Remove multiple items by key. |
|
|
27
|
+
| `DistributedTable.fromExisting(tableName)` | `ExternalTableRef` | Wrap a pre-existing DynamoDB table. |
|
|
28
|
+
|
|
29
|
+
**Runtime only.** Data methods (`get`, `put`, `delete`, `query`, `scan`, `getBatch`, `putBatch`, `deleteBatch`) run at request time — call them inside an `ApiNamespace` method, `RawRoute` handler, job handler, or a runtime script, **not** at the top level of your `aws-blocks/index.ts`. Top-level code runs during CDK synth, where the block resolves to its infrastructure construct (no data methods), so a top-level call throws `table.<method> is not a function` (throws `TypeError` at runtime if called during CDK synth). To seed data, do it from inside a handler or a separate runtime script. Constructing the block at module scope is fine; only method calls must move into handlers.
|
|
30
|
+
|
|
31
|
+
> **Collecting `scan()` results:** like `query()`, `scan()` returns an `AsyncIterable` — collect with `await Array.fromAsync(table.scan())` or iterate with `for await`. Prefer `query()` over `scan()` (scans read every item).
|
|
32
|
+
|
|
33
|
+
### Options
|
|
34
|
+
|
|
35
|
+
| Option | Type | Required | Description |
|
|
36
|
+
|--------|------|----------|-------------|
|
|
37
|
+
| `schema` | `StandardSchemaV1` | Yes | Runtime validation schema (Zod, Valibot, ArkType, etc.). Type `T` is inferred from the schema. |
|
|
38
|
+
| `key` | `TableKeyConfig<T>` | Yes | Primary key configuration: `{ partitionKey, sortKey? }`. Field names must exist in the schema. |
|
|
39
|
+
| `indexes` | `Record<string, TableKeyConfig<T>>` | No | Global secondary index definitions. |
|
|
40
|
+
| `ttl` | `keyof T & string` | No | Enable DynamoDB TTL on the specified attribute. The field should contain a Unix epoch timestamp in seconds. |
|
|
41
|
+
| `table` | `ExternalTableRef` | No | Wrap an existing DynamoDB table instead of creating one. |
|
|
42
|
+
| `logger` | `ChildLogger` | No | Optional logger for internal operations. When omitted, a default Logger at error level is created. |
|
|
43
|
+
|
|
44
|
+
### Key Object Pattern
|
|
45
|
+
|
|
46
|
+
All methods that accept a key (`get`, `delete`, `getBatch`, `deleteBatch`) take a key object with the partition key field (and sort key field if defined). The key type is computed from your schema and key configuration — TypeScript enforces exactly the right fields:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Table with partition key + sort key
|
|
50
|
+
const orders = new DistributedTable(scope, 'orders', {
|
|
51
|
+
schema: orderSchema,
|
|
52
|
+
key: { partitionKey: 'userId', sortKey: 'orderId' },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Key object requires both fields
|
|
56
|
+
await orders.get({ userId: 'alice', orderId: '001' });
|
|
57
|
+
|
|
58
|
+
// Table with partition key only
|
|
59
|
+
const settings = new DistributedTable(scope, 'settings', {
|
|
60
|
+
schema: settingsSchema,
|
|
61
|
+
key: { partitionKey: 'settingId' },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Key object requires only the partition key
|
|
65
|
+
await settings.get({ settingId: 'theme' });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Query
|
|
69
|
+
|
|
70
|
+
`query()` takes a single options object. Specify `index` to query a GSI, or omit it to query the primary key. The `where` clause is type-safe — field names and condition types are computed from the index (or primary key) definition.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// Query a GSI
|
|
74
|
+
for await (const order of orders.query({
|
|
75
|
+
index: 'byStatus',
|
|
76
|
+
where: { status: { equals: 'pending' } },
|
|
77
|
+
limit: 10,
|
|
78
|
+
order: 'desc',
|
|
79
|
+
})) {
|
|
80
|
+
console.log(order);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Query the primary key (omit index)
|
|
84
|
+
for await (const order of orders.query({
|
|
85
|
+
where: { userId: { equals: 'alice' }, orderId: { beginsWith: '2024-' } },
|
|
86
|
+
})) {
|
|
87
|
+
console.log(order);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
> **Tip:** When collecting all results into an array, use `Array.fromAsync()` instead of a manual loop:
|
|
92
|
+
> ```typescript
|
|
93
|
+
> const pending = await Array.fromAsync(orders.query({
|
|
94
|
+
> index: 'byStatus',
|
|
95
|
+
> where: { status: { equals: 'pending' } },
|
|
96
|
+
> }));
|
|
97
|
+
> ```
|
|
98
|
+
|
|
99
|
+
**Query options:**
|
|
100
|
+
|
|
101
|
+
| Field | Type | Required | Description |
|
|
102
|
+
|-------|------|----------|-------------|
|
|
103
|
+
| `index` | `keyof Indexes` | No | GSI to query. Omit to query the primary key. |
|
|
104
|
+
| `where` | `KeyCondition<T, K>` | Yes | Key conditions. Partition key requires `{ equals }`. Sort key supports `equals`, `greaterThan`, `lessThan`, `between`, `beginsWith`, etc. |
|
|
105
|
+
| `limit` | `number` | No | Maximum number of items to return. |
|
|
106
|
+
| `order` | `'asc' \| 'desc'` | No | Sort direction on the sort key. Defaults to `'asc'`. |
|
|
107
|
+
|
|
108
|
+
### Conditional Operations
|
|
109
|
+
|
|
110
|
+
Both `put` and `delete` accept optional conditions:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// Only write if key doesn't exist (idempotent create)
|
|
114
|
+
await table.put(item, { ifNotExists: true });
|
|
115
|
+
|
|
116
|
+
// Only write if existing item's field matches (optimistic locking)
|
|
117
|
+
await table.put(updatedItem, { ifFieldEquals: { version: 3 } });
|
|
118
|
+
|
|
119
|
+
// Only delete if item exists
|
|
120
|
+
await table.delete(key, { ifExists: true });
|
|
121
|
+
|
|
122
|
+
// Only delete if field matches
|
|
123
|
+
await table.delete(key, { ifFieldEquals: { status: 'archived' } });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
All condition failures throw with `error.name === DistributedTableErrors.ConditionalCheckFailed`.
|
|
127
|
+
|
|
128
|
+
> **No partial update:** There is no `update()` or `patch()` method. To change a field, do a read-modify-write — `get()` the item, mutate it, then `put()` the full item back. For safe concurrent updates, pass `{ ifFieldEquals: { version: <previous> } }` to `put()` so the write fails (via `ConditionalCheckFailed`) if another writer changed the item in the meantime (optimistic locking).
|
|
129
|
+
|
|
130
|
+
### Error Handling
|
|
131
|
+
|
|
132
|
+
Errors thrown by DistributedTable carry an `error.name` you can match with `isBlocksError`:
|
|
133
|
+
|
|
134
|
+
| Constant | `error.name` | Thrown when |
|
|
135
|
+
|----------|--------------|-------------|
|
|
136
|
+
| `DistributedTableErrors.ConditionalCheckFailed` | `ConditionalCheckFailedException` | An `ifNotExists` / `ifExists` / `ifFieldEquals` condition failed. |
|
|
137
|
+
| `DistributedTableErrors.ValidationFailed` | `ValidationFailedException` | An item failed the configured `schema` validation on `put()` / `putBatch()`. |
|
|
138
|
+
| `DistributedTableErrors.InvalidQuery` | `InvalidQueryException` | The query/condition shape is wrong: missing `where`, partition key not given as `{ equals }`, unknown index, multiple sort-key conditions, or an empty `ifFieldEquals`. A caller bug. |
|
|
139
|
+
| `DistributedTableErrors.ItemTooLarge` | `ItemTooLargeException` | A `put`/`putBatch` item exceeds DynamoDB's 400 KB per-item size limit. |
|
|
140
|
+
| `DistributedTableErrors.BatchIncomplete` | `BatchIncompleteException` | A batch op left entries unprocessed after the retry budget (sustained throttling). AWS runtime only. |
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { isBlocksError } from '@aws-blocks/core';
|
|
144
|
+
import { DistributedTableErrors } from '@aws-blocks/bb-distributed-table';
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
await table.put(item, { ifNotExists: true });
|
|
148
|
+
} catch (e: unknown) {
|
|
149
|
+
if (isBlocksError(e, DistributedTableErrors.ConditionalCheckFailed)) {
|
|
150
|
+
// item already exists
|
|
151
|
+
}
|
|
152
|
+
if (isBlocksError(e, DistributedTableErrors.ItemTooLarge)) {
|
|
153
|
+
// item > 400 KB — split it or store a reference instead
|
|
154
|
+
}
|
|
155
|
+
throw e;
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Examples
|
|
160
|
+
|
|
161
|
+
### Basic CRUD
|
|
162
|
+
|
|
163
|
+
> The examples use Zod, but `schema` accepts any StandardSchemaV1 implementation (Zod, Valibot, ArkType). Install your chosen library, e.g. `npm install zod`.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { z } from 'zod';
|
|
167
|
+
|
|
168
|
+
const orderSchema = z.object({
|
|
169
|
+
userId: z.string(),
|
|
170
|
+
orderId: z.string(),
|
|
171
|
+
total: z.number(),
|
|
172
|
+
status: z.string(),
|
|
173
|
+
createdAt: z.number(),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const orders = new DistributedTable(scope, 'orders', {
|
|
177
|
+
schema: orderSchema,
|
|
178
|
+
key: { partitionKey: 'userId', sortKey: 'orderId' },
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
export const api = new ApiNamespace(scope, 'api', (context) => ({
|
|
182
|
+
async getOrder(userId: string, orderId: string) {
|
|
183
|
+
return await orders.get({ userId, orderId });
|
|
184
|
+
},
|
|
185
|
+
async createOrder(order: z.infer<typeof orderSchema>) {
|
|
186
|
+
await orders.put(order, { ifNotExists: true });
|
|
187
|
+
},
|
|
188
|
+
async deleteOrder(userId: string, orderId: string) {
|
|
189
|
+
await orders.delete({ userId, orderId });
|
|
190
|
+
},
|
|
191
|
+
}));
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Query with Sort Key Conditions
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
const orders = new DistributedTable(scope, 'orders', {
|
|
198
|
+
schema: orderSchema,
|
|
199
|
+
key: { partitionKey: 'userId', sortKey: 'orderId' },
|
|
200
|
+
indexes: {
|
|
201
|
+
byDate: { partitionKey: 'userId', sortKey: 'createdAt' },
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Ctrl+Space on the where object shows: userId, createdAt
|
|
206
|
+
// userId requires { equals }, createdAt supports greaterThan, between, etc.
|
|
207
|
+
const results = [];
|
|
208
|
+
for await (const order of orders.query({
|
|
209
|
+
index: 'byDate',
|
|
210
|
+
where: {
|
|
211
|
+
userId: { equals: 'alice' },
|
|
212
|
+
createdAt: { greaterThan: Date.now() - 86400000 },
|
|
213
|
+
},
|
|
214
|
+
})) {
|
|
215
|
+
results.push(order);
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### TTL (Auto-Expiring Items)
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const sessions = new DistributedTable(scope, 'sessions', {
|
|
223
|
+
schema: sessionSchema,
|
|
224
|
+
key: { partitionKey: 'sessionId' },
|
|
225
|
+
ttl: 'expiresAt',
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// DynamoDB automatically deletes items after the TTL timestamp
|
|
229
|
+
await sessions.put({
|
|
230
|
+
sessionId: 'abc123',
|
|
231
|
+
userId: 'alice',
|
|
232
|
+
expiresAt: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
233
|
+
});
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Batch Operations
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
// Write many items
|
|
240
|
+
await orders.putBatch([order1, order2, order3]);
|
|
241
|
+
|
|
242
|
+
// Read many items
|
|
243
|
+
const items = await orders.getBatch([
|
|
244
|
+
{ userId: 'alice', orderId: '001' },
|
|
245
|
+
{ userId: 'bob', orderId: '002' },
|
|
246
|
+
]);
|
|
247
|
+
|
|
248
|
+
// Delete many items
|
|
249
|
+
await orders.deleteBatch([
|
|
250
|
+
{ userId: 'alice', orderId: '001' },
|
|
251
|
+
{ userId: 'bob', orderId: '002' },
|
|
252
|
+
]);
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Wrapping an Existing Table
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
const legacy = new DistributedTable(scope, 'legacy', {
|
|
259
|
+
schema: orderSchema,
|
|
260
|
+
key: { partitionKey: 'userId', sortKey: 'orderId' },
|
|
261
|
+
table: DistributedTable.fromExisting('my-existing-table'),
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Best Practices
|
|
266
|
+
|
|
267
|
+
- Design partition keys for even data distribution (e.g., `userId`, `tenantId`)
|
|
268
|
+
- Use sort keys for range queries (e.g., timestamps, alphabetical ordering)
|
|
269
|
+
- Define GSIs upfront for known access patterns — adding them later requires backfill
|
|
270
|
+
- Use `{ ifNotExists: true }` for idempotent creates
|
|
271
|
+
- Use `{ ifFieldEquals }` for optimistic locking when multiple writers are possible
|
|
272
|
+
- Prefer `query()` over `scan()` — scans read every item and are expensive
|
|
273
|
+
|
|
274
|
+
## Scaling & Cost (AWS)
|
|
275
|
+
|
|
276
|
+
- **Billing:** PAY_PER_REQUEST — no provisioned capacity to manage
|
|
277
|
+
- **Latency:** Single-digit ms reads and writes
|
|
278
|
+
- **Throughput:** Scales automatically, no upper limit on table size
|
|
279
|
+
- **Item size limit:** 400 KB per item
|
|
280
|
+
- **GSI limit:** Up to 20 global secondary indexes per table
|
|
281
|
+
- **Cost:** ~$1.25 per million writes, ~$0.25 per million reads
|
|
282
|
+
- **Durability:** 99.999999999% (11 nines) across 3 AZs
|
|
283
|
+
|
|
284
|
+
## Local Development
|
|
285
|
+
|
|
286
|
+
Mock data persists to disk at `.bb-data/{fullId}/` across dev server restarts. Wipe with `rm -rf .bb-data`. The mock validates the 400 KB item size limit, schema validation, and conditional check failures, matching AWS behavior. Index queries are implemented via in-memory filtering — correctness is preserved but performance characteristics differ from DynamoDB.
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
## See Also
|
|
291
|
+
|
|
292
|
+
- [DESIGN.md](./DESIGN.md) — Infrastructure details, mock parity gaps, and design decisions
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error constants for DistributedTable. Use with `isBlocksError()` in catch blocks.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { isBlocksError } from '@aws-blocks/core';
|
|
7
|
+
* import { DistributedTableErrors } from '@aws-blocks/bb-distributed-table';
|
|
8
|
+
*
|
|
9
|
+
* try {
|
|
10
|
+
* await table.put(item, { ifNotExists: true });
|
|
11
|
+
* } catch (e: unknown) {
|
|
12
|
+
* if (isBlocksError(e, DistributedTableErrors.ConditionalCheckFailed)) {
|
|
13
|
+
* // item already exists
|
|
14
|
+
* }
|
|
15
|
+
* throw e;
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare const DistributedTableErrors: {
|
|
20
|
+
readonly ConditionalCheckFailed: "ConditionalCheckFailedException";
|
|
21
|
+
readonly ValidationFailed: "ValidationFailedException";
|
|
22
|
+
/**
|
|
23
|
+
* The query or condition shape is invalid and was rejected before reaching
|
|
24
|
+
* DynamoDB: a missing `where` clause, a partition key not given as
|
|
25
|
+
* `{ equals: value }`, an unknown index, more than one sort-key condition, or
|
|
26
|
+
* an empty `ifFieldEquals`. These are all caller bugs — something the caller
|
|
27
|
+
* can fix by correcting the call. Catchable via
|
|
28
|
+
* `isBlocksError(e, DistributedTableErrors.InvalidQuery)`.
|
|
29
|
+
*
|
|
30
|
+
* Kept distinct from {@link ItemTooLarge} (a runtime data condition) so a
|
|
31
|
+
* customer can tell "my query is wrong" from "this item is too big" by name
|
|
32
|
+
* alone rather than string-matching the message.
|
|
33
|
+
*/
|
|
34
|
+
readonly InvalidQuery: "InvalidQueryException";
|
|
35
|
+
/**
|
|
36
|
+
* An item exceeds DynamoDB's 400 KB per-item size limit. Unlike an invalid
|
|
37
|
+
* query, this is not necessarily a caller bug — the size of a given item may
|
|
38
|
+
* be outside the caller's control — so callers may want to branch on it
|
|
39
|
+
* (skip, split, or store a reference instead). Catchable via
|
|
40
|
+
* `isBlocksError(e, DistributedTableErrors.ItemTooLarge)`.
|
|
41
|
+
*
|
|
42
|
+
* The mock checks serialized byte length client-side and throws this directly.
|
|
43
|
+
* On AWS, DynamoDB raises a generic `ValidationException` for oversized items;
|
|
44
|
+
* the runtime detects the size-specific message and re-maps it to this name so
|
|
45
|
+
* both layers are catchable with the same code. Other `ValidationException`
|
|
46
|
+
* causes (malformed expressions, type mismatches) propagate as-is.
|
|
47
|
+
*/
|
|
48
|
+
readonly ItemTooLarge: "ItemTooLargeException";
|
|
49
|
+
/**
|
|
50
|
+
* A batch operation could not complete all entries within the retry budget.
|
|
51
|
+
* DynamoDB batch APIs return UnprocessedKeys/UnprocessedItems (HTTP 200) under
|
|
52
|
+
* sustained throttling; when retries are exhausted we surface this so callers
|
|
53
|
+
* can back off and resubmit rather than silently losing writes or mistaking a
|
|
54
|
+
* throttled read for a missing item.
|
|
55
|
+
*
|
|
56
|
+
* The in-memory mock never throttles, so it never produces this error — the
|
|
57
|
+
* constant is shared purely so catch-site handling is identical across both.
|
|
58
|
+
*/
|
|
59
|
+
readonly BatchIncomplete: "BatchIncompleteException";
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* @internal Build an Error whose `name` carries the typed error code (so callers
|
|
63
|
+
* can match it with `isBlocksError`). Shared by the mock and AWS runtime so both
|
|
64
|
+
* produce identically shaped errors.
|
|
65
|
+
*/
|
|
66
|
+
export declare function blocksError(name: string, message: string): Error;
|
|
67
|
+
/**
|
|
68
|
+
* @internal Normalize a sort-key condition before it drives a query. Shared by
|
|
69
|
+
* the mock and AWS runtime so both treat the same inputs identically:
|
|
70
|
+
*
|
|
71
|
+
* - **Zero defined fields** (`undefined`, or a present-but-empty `{}` /
|
|
72
|
+
* `{ createdAt: undefined }`) → returns `undefined`, i.e. "no sort-key filter,
|
|
73
|
+
* query the whole partition". A present-but-empty object would otherwise
|
|
74
|
+
* diverge: the mock's per-item matcher accepts everything (returns the whole
|
|
75
|
+
* partition) while the AWS runtime registers `#sk` in `ExpressionAttributeNames`
|
|
76
|
+
* with no clause that uses it, which DynamoDB rejects with `ValidationException`.
|
|
77
|
+
* - **Exactly one defined field** → returns the condition unchanged.
|
|
78
|
+
* - **More than one defined field** → throws `InvalidQuery`, because DynamoDB allows
|
|
79
|
+
* only one sort-key condition per `KeyConditionExpression` (use `between` for ranges).
|
|
80
|
+
*
|
|
81
|
+
* @throws {DistributedTableErrors.InvalidQuery} If more than one sort-key field is defined.
|
|
82
|
+
*/
|
|
83
|
+
export declare function normalizeSortKeyCondition<C extends Record<string, unknown>>(condition: C | undefined): C | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* @internal Validation messages shared by the mock and AWS runtime. Centralised
|
|
86
|
+
* here so the two implementations stay byte-for-byte in lockstep — parity tests
|
|
87
|
+
* assert the same wording against both.
|
|
88
|
+
*/
|
|
89
|
+
export declare const DistributedTableMessages: {
|
|
90
|
+
readonly indexNotFound: (index: string | undefined) => string;
|
|
91
|
+
readonly whereRequired: (pkField: string) => string;
|
|
92
|
+
readonly partitionKeyEqualsRequired: (pkField: string) => string;
|
|
93
|
+
readonly multipleSortKeyConditions: (conditionKeys: string[]) => string;
|
|
94
|
+
readonly emptyIfFieldEquals: "ifFieldEquals must contain at least one field with a non-undefined value";
|
|
95
|
+
readonly itemTooLarge: (bytes: number) => string;
|
|
96
|
+
readonly batchIncomplete: (operation: string, remaining: number, attempts: number) => string;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* @internal Re-map DynamoDB's generic `ValidationException` to the intent-revealing
|
|
100
|
+
* `ItemTooLarge` name when (and only when) it was raised for an oversized item.
|
|
101
|
+
*
|
|
102
|
+
* DynamoDB raises a single `ValidationException` for many unrelated conditions, so
|
|
103
|
+
* we narrow on the size-specific message ("size has exceeded") before re-mapping —
|
|
104
|
+
* other `ValidationException` causes (malformed expressions, type mismatches) are
|
|
105
|
+
* left untouched and propagate as-is. This mirrors the mock's client-side size
|
|
106
|
+
* check so both layers are catchable with `isBlocksError(e, ItemTooLarge)`. The
|
|
107
|
+
* original DynamoDB error is preserved as `cause` (kept server-side per D-003) so
|
|
108
|
+
* its stack and requestId remain available for debugging.
|
|
109
|
+
*/
|
|
110
|
+
export declare function remapItemTooLarge(err: unknown): unknown;
|
|
111
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,sBAAsB;;;IAGlC;;;;;;;;;;;OAWG;;IAEH;;;;;;;;;;;;OAYG;;IAEH;;;;;;;;;OASG;;CAEM,CAAC;AAEX;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAIhE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1E,SAAS,EAAE,CAAC,GAAG,SAAS,GACtB,CAAC,GAAG,SAAS,CAQf;AAED;;;;GAIG;AACH,eAAO,MAAM,wBAAwB;oCACb,MAAM,GAAG,SAAS;sCAChB,MAAM;mDAEO,MAAM;wDAED,MAAM,EAAE;;mCAI7B,MAAM;0CAEC,MAAM,aAAa,MAAM,YAAY,MAAM;CAG/D,CAAC;AAEX;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAOvD"}
|