@nikovirtala/typesafe-dynamodb 0.0.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 +202 -0
- package/README.md +333 -0
- package/img/get-item-attributes.gif +0 -0
- package/img/get-item-projection.gif +0 -0
- package/img/get-item-response.gif +0 -0
- package/img/get-item.gif +0 -0
- package/img/get-order.png +0 -0
- package/img/put-item-expression.gif +0 -0
- package/img/query-expression.gif +0 -0
- package/lib/attribute-value.d.ts +43 -0
- package/lib/attribute-value.js +31 -0
- package/lib/callback.d.ts +2 -0
- package/lib/callback.js +3 -0
- package/lib/client-v2.d.ts +18 -0
- package/lib/client-v2.js +3 -0
- package/lib/client-v3.d.ts +26 -0
- package/lib/client-v3.js +3 -0
- package/lib/create-set.d.ts +10 -0
- package/lib/create-set.js +3 -0
- package/lib/delete-document-command.d.ts +3 -0
- package/lib/delete-document-command.js +8 -0
- package/lib/delete-item-command.d.ts +3 -0
- package/lib/delete-item-command.js +8 -0
- package/lib/delete-item.d.ts +18 -0
- package/lib/delete-item.js +3 -0
- package/lib/document-client-field-mappings.d.ts +17 -0
- package/lib/document-client-field-mappings.js +3 -0
- package/lib/document-client-v2.d.ts +18 -0
- package/lib/document-client-v2.js +3 -0
- package/lib/document-client-v3.d.ts +27 -0
- package/lib/document-client-v3.js +3 -0
- package/lib/expression-attributes.d.ts +20 -0
- package/lib/expression-attributes.js +3 -0
- package/lib/expression.d.ts +34 -0
- package/lib/expression.js +3 -0
- package/lib/get-command.d.ts +10 -0
- package/lib/get-command.js +3 -0
- package/lib/get-document-command.d.ts +3 -0
- package/lib/get-document-command.js +8 -0
- package/lib/get-item-command.d.ts +3 -0
- package/lib/get-item-command.js +8 -0
- package/lib/get-item.d.ts +14 -0
- package/lib/get-item.js +3 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +6 -0
- package/lib/json-format.d.ts +7 -0
- package/lib/json-format.js +10 -0
- package/lib/key.d.ts +6 -0
- package/lib/key.js +3 -0
- package/lib/letter.d.ts +6 -0
- package/lib/letter.js +3 -0
- package/lib/marshall.d.ts +14 -0
- package/lib/marshall.js +7 -0
- package/lib/narrow.d.ts +3 -0
- package/lib/narrow.js +3 -0
- package/lib/projection.d.ts +24 -0
- package/lib/projection.js +3 -0
- package/lib/put-document-command.d.ts +3 -0
- package/lib/put-document-command.js +8 -0
- package/lib/put-item-command.d.ts +3 -0
- package/lib/put-item-command.js +8 -0
- package/lib/put-item.d.ts +17 -0
- package/lib/put-item.js +3 -0
- package/lib/query-command.d.ts +3 -0
- package/lib/query-command.js +8 -0
- package/lib/query-document-command.d.ts +3 -0
- package/lib/query-document-command.js +8 -0
- package/lib/query.d.ts +18 -0
- package/lib/query.js +3 -0
- package/lib/scan-command.d.ts +3 -0
- package/lib/scan-command.js +8 -0
- package/lib/scan-document-command.d.ts +3 -0
- package/lib/scan-document-command.js +8 -0
- package/lib/scan.d.ts +17 -0
- package/lib/scan.js +3 -0
- package/lib/simplify.d.ts +3 -0
- package/lib/simplify.js +3 -0
- package/lib/stream-event.d.ts +18 -0
- package/lib/stream-event.js +3 -0
- package/lib/update-document-command.d.ts +3 -0
- package/lib/update-document-command.js +8 -0
- package/lib/update-item-command.d.ts +3 -0
- package/lib/update-item-command.js +8 -0
- package/lib/update-item.d.ts +20 -0
- package/lib/update-item.js +3 -0
- package/package.json +116 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
# typesafe-dynamodb
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/typesafe-dynamodb)
|
|
4
|
+
|
|
5
|
+
`typesafe-dynamodb` is a type-only library which replaces the type signatures of the AWS SDK's DynamoDB client. It substitutes `getItem`, `putItem`, `deleteItem` and `query` API methods with type-safe alternatives that are aware of the data in your tables and also adaptive to the semantics of the API request, e.g. by validating `ExpressionAttributeNames` and `ExpressionAttributeValues` contain all the values used in a `ConditionExpression` string, or by understanding the effect of a `ProjectionExpression` on the returned data type.
|
|
6
|
+
|
|
7
|
+
The end goal is to provide types that have total understanding of the AWS DynamoDB API and enable full utilization of the TypeScript type system for modeling complex DynamoDB tables, such as the application of union types and template string literals for single-table designs.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install --save-dev typesafe-dynamodb
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
This library contains type definitions for both AWS SDK v2 (`aws-sdk`) and AWS SDK v3 (`@aws-sdk/client-dynamodb`);
|
|
20
|
+
|
|
21
|
+
### AWS SDK v2
|
|
22
|
+
|
|
23
|
+
To use `typesafe-dynamodb` with the AWS SDK v2, there is no need to change anything about your existing runtime code. It is purely type definitions, so you only need to cast an instance of `AWS.DynamoDB` to the `TypeSafeDynamoDBv2<T, HashKey, RangeKey>` interface and use the client as normal, except now you can enjoy a dynamic, type-safe experience in your IDE instead.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { DynamoDB } from "aws-sdk";
|
|
27
|
+
|
|
28
|
+
const client = new DynamoDB();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Start by declaring a standard TypeScript interface which describes the structure of data in your DynamoDB Table:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
interface Record {
|
|
35
|
+
key: string;
|
|
36
|
+
sort: number;
|
|
37
|
+
attribute: string;
|
|
38
|
+
// all types are allowed, such as recursive nested types
|
|
39
|
+
records?: Record[];
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then, cast the `DynamoDB` client instance to `TypeSafeDynamoDB`;
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { TypeSafeDynamoDBv2 } from "typesafe-dynamodb/lib/client-v2";
|
|
47
|
+
|
|
48
|
+
const typesafeClient: TypeSafeDynamoDBv2<Record, "key", "sort"> = client;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`"key"` is the name of the Hash Key attribute, and `"sort"` is the name of the Range Key attribute.
|
|
52
|
+
|
|
53
|
+
Finally, use the client as you normally would, except now with intelligent type hints and validations.
|
|
54
|
+
|
|
55
|
+
### AWS SDK v3
|
|
56
|
+
|
|
57
|
+
#### Option 1 - DynamoDB (similar to SDK v2)
|
|
58
|
+
|
|
59
|
+
`DynamoDB` is an almost identical implementation to the AWS SDK v2, except with minor changes such as returning a `Promise` by default. It is a convenient way of using the DynamoDB API, except it is not optimized for tree-shaking (for that, see Option 2).
|
|
60
|
+
|
|
61
|
+
To override the types, follow a similar method to v2, except by importing TypeSafeDynamoDBv3 (instead of v2):
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
|
|
65
|
+
import { TypeSafeDynamoDBv3 } from "typesafe-dynamodb/lib/client-v3";
|
|
66
|
+
|
|
67
|
+
const client = new DynamoDB({..});
|
|
68
|
+
const typesafeClient: TypeSafeDynamoDBv3<Record, "key", "sort"> = client;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Option 2 - DynamoDBClient (a Command-Response interface optimized for tree-shaking)
|
|
72
|
+
|
|
73
|
+
`DynamoDBClient` is a generic interface with a single method, `send`. To invoke an API, call `send` with an instance of the API's corresponding `Command`.
|
|
74
|
+
|
|
75
|
+
This option is designed for optimal tree-shaking when bundling code, ensuring that the bundle only includes code for the APIs your application uses.
|
|
76
|
+
|
|
77
|
+
For this option, type-safety is achieved by declaring your own commands and then calling the standard `DynamoDBClient`:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
interface MyType {
|
|
81
|
+
key: string;
|
|
82
|
+
sort: number;
|
|
83
|
+
list: string[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const client = new DynamoDBClient({});
|
|
87
|
+
|
|
88
|
+
const GetMyTypeCommand = TypeSafeGetItemCommand<MyType, "key", "sort">();
|
|
89
|
+
|
|
90
|
+
await client.send(
|
|
91
|
+
new GetMyTypeCommand({
|
|
92
|
+
..
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Document Client
|
|
98
|
+
|
|
99
|
+
Both the AWS SDK v2 and v3 provide a javascript-friendly interface called the `DocumentClient`. Instead of using the AttributeValue format, such as `{ S: "hello" }` or `{ N: "123" }`, the `DocumentClient` enables you to use native javascript types, e.g. `"hello"` or `123`.
|
|
100
|
+
|
|
101
|
+
#### AWS SDK V2
|
|
102
|
+
|
|
103
|
+
For the SDK V2 client, cast it to `TypeSafeDocumentClientV2`.
|
|
104
|
+
|
|
105
|
+
See: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { DynamoDB } from "aws-sdk";
|
|
109
|
+
import { TypeSafeDocumentClientV2 } from "typesafe-dynamodb/lib/document-client-v2";
|
|
110
|
+
|
|
111
|
+
const table = new DynamoDB.DocumentClient() as TypeSafeDocumentClientV2<
|
|
112
|
+
MyItem,
|
|
113
|
+
"pk",
|
|
114
|
+
"sk"
|
|
115
|
+
>;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### AWS SDK V3
|
|
119
|
+
|
|
120
|
+
When defining your Command types, use the corresponding `TypeSafe*DocumentCommand` type, for example `TypeSafeGetDocumentCommand` instead of `TypeSafeGetItemCommand`:
|
|
121
|
+
|
|
122
|
+
- GetItem - `TypeSafeGetDocumentCommand`
|
|
123
|
+
- PutItem - `TypeSafePutDocumentCommand`
|
|
124
|
+
- DeleteItem - `TypeSafeDeleteDocumentCommand`
|
|
125
|
+
- UpdateItem - `TypeSafeUpdateDocumentCommand`
|
|
126
|
+
- Query - `TypeSafeQueryDocumentCommand`
|
|
127
|
+
- Scan - `TypeSafeScanDocumentCommand`
|
|
128
|
+
|
|
129
|
+
See: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_lib_dynamodb.html
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { JsonFormat } from "typesafe-dynamodb";
|
|
133
|
+
import { TypeSafeGetDocumentCommand } from "typesafe-dynamodb/lib/get-document-command";
|
|
134
|
+
|
|
135
|
+
const MyGetItemCommand = TypeSafeGetDocumentCommand<MyType, "key", "sort">();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
For the SDK V3 client, cast it to `TypeSafeDynamoDBv3`.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
|
142
|
+
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
|
|
143
|
+
import { TypeSafeDocumentClientV3 } from "typesafe-dynamodb/lib/document-client-v3";
|
|
144
|
+
|
|
145
|
+
const client = new DynamoDBClient({});
|
|
146
|
+
|
|
147
|
+
const docClient = DynamoDBDocumentClient.from(
|
|
148
|
+
client
|
|
149
|
+
) as TypeSafeDocumentClientV3<MyType, "key", "sort">;
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
And then call `.send` with an instance of your TypeSafe Command:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
docClient.send(new MyGetItemCommand({
|
|
156
|
+
..
|
|
157
|
+
}));
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Features
|
|
161
|
+
|
|
162
|
+
### Type-aware Input and Output
|
|
163
|
+
|
|
164
|
+
The type of the `Key` is derived from the `Record` type.
|
|
165
|
+
|
|
166
|
+

|
|
167
|
+
|
|
168
|
+
Same for the `Item` in the response:
|
|
169
|
+
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+
### Single Table Design
|
|
173
|
+
|
|
174
|
+
Below are two `interface` declarations, representing two types of data stored in a single DynamoDB table - `User` and `Order`. Single table design in DynamoDB is achieved by creating "composite keys", e.g. `USER#${UserID}`. In TypeScript, we use template literal types to encode this in the Type System.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
interface User<UserID extends string = string> {
|
|
178
|
+
PK: `USER#${UserID}`;
|
|
179
|
+
SK: `#PROFILE#${UserID}`;
|
|
180
|
+
Username: string;
|
|
181
|
+
FullName: string;
|
|
182
|
+
Email: string;
|
|
183
|
+
CreatedAt: Date;
|
|
184
|
+
Address: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface Order<
|
|
188
|
+
UserID extends string = string,
|
|
189
|
+
OrderID extends string = string
|
|
190
|
+
> {
|
|
191
|
+
PK: `USER#${UserID}`;
|
|
192
|
+
SK: `ORDER#${OrderID}`;
|
|
193
|
+
Username: string;
|
|
194
|
+
OrderID: string;
|
|
195
|
+
Status: "PLACED" | "SHIPPED";
|
|
196
|
+
CreatedAt: Date;
|
|
197
|
+
Address: string;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
With these two types defined, you can now use a union type to declare a `TypeSafeDynamoDB` instance aware of the two types of data in your tables:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
const client: TypeSafeDynamoDB<User | Order, "PK", "SK">;
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
When making calls such as `getItem`, TypeScript will narrow the returned data type to the corresponding type based on the structure of the `Key` in your request:
|
|
208
|
+
|
|
209
|
+

|
|
210
|
+
|
|
211
|
+
### Type-Safe DynamoDBStreamEvent
|
|
212
|
+
|
|
213
|
+
Leverage your data types in Lambda Functions attached to the DynamoDB Table Stream:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { DynamoDBStreamEvent } from "typesafe-dynamodb/lib/stream-event";
|
|
217
|
+
|
|
218
|
+
export async function handle(
|
|
219
|
+
event: DynamoDBStreamEvent<User | Order, "PK", "SK", "KEYS_ONLY">
|
|
220
|
+
) {
|
|
221
|
+
..
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
The event's type is derived from the data type and the the `StreamViewType`, e.g. `"NEW_IMAGE" | "OLD_IMAGE" | "KEYS_ONLY" | "NEW_AND_OLD_IMAGES"`.
|
|
226
|
+
|
|
227
|
+
### Filter result with ProjectionExpression
|
|
228
|
+
|
|
229
|
+
The `ProjectionExpression` field is parsed and applied to filter the returned type of `getItem` and `query`.
|
|
230
|
+
|
|
231
|
+

|
|
232
|
+
|
|
233
|
+
### Filter with AttributesToGet
|
|
234
|
+
|
|
235
|
+
If you specify `AttributesToGet`, then the returned type only contains those properties.
|
|
236
|
+
|
|
237
|
+

|
|
238
|
+
|
|
239
|
+
### Validate ExpressionAttributeNames and ExpressionAttributeValues
|
|
240
|
+
|
|
241
|
+
If you add a `ConditionExpression` in `putItem`, you will be prompted for any `#name` or `:valu` placeholders:
|
|
242
|
+
|
|
243
|
+

|
|
244
|
+
|
|
245
|
+
Same is true for a `query`:
|
|
246
|
+
|
|
247
|
+

|
|
248
|
+
|
|
249
|
+
### Marshall a JS Object to an AttributeMap
|
|
250
|
+
|
|
251
|
+
A better type definition `@aws-sdk/util-dynamodb`'s `marshall` function is provided which maintains the typing information of the value passed to it and also adapts the output type based on the input `marshallOptions`
|
|
252
|
+
|
|
253
|
+
Given an object literal:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
const myObject = {
|
|
257
|
+
key: "key",
|
|
258
|
+
sort: 123,
|
|
259
|
+
binary: new Uint16Array([1]),
|
|
260
|
+
buffer: Buffer.from("buffer", "utf8"),
|
|
261
|
+
optional: 456,
|
|
262
|
+
list: ["hello", "world"],
|
|
263
|
+
record: {
|
|
264
|
+
key: "nested key",
|
|
265
|
+
sort: 789,
|
|
266
|
+
},
|
|
267
|
+
} as const;
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Call the `marshall` function to convert it to an AttributeMap which maintains the exact structure in the type system:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
import { marshall } from "typesafe-dynamodb/lib/marshall";
|
|
274
|
+
// marshall the above JS object to its corresponding AttributeMap
|
|
275
|
+
const marshalled = marshall(myObject)
|
|
276
|
+
|
|
277
|
+
// typing information is carried across exactly, including literal types
|
|
278
|
+
const marshalled: {
|
|
279
|
+
readonly key: S<"key">;
|
|
280
|
+
readonly sort: N<123>;
|
|
281
|
+
readonly binary: B;
|
|
282
|
+
readonly buffer: B;
|
|
283
|
+
readonly optional: N<456>;
|
|
284
|
+
readonly list: L<readonly [S<"hello">, S<"world">]>;
|
|
285
|
+
readonly record: M<...>;
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Unmarshall an AttributeMap back to a JS Object
|
|
290
|
+
|
|
291
|
+
A better type definition `@aws-sdk/util-dynamodb`'s `unmarshall` function is provided which maintains the typing information of the value passed to it and also adapts the output type based on the input `unmarshallOptions`.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
import { unmarshall } from "typesafe-dynamodb/lib/marshall";
|
|
295
|
+
|
|
296
|
+
// unmarshall the AttributeMap back into the original object
|
|
297
|
+
const unmarshalled = unmarshall(marshalled);
|
|
298
|
+
|
|
299
|
+
// it maintains the literal typing information (as much as possible)
|
|
300
|
+
const unmarshalled: {
|
|
301
|
+
readonly key: "key";
|
|
302
|
+
readonly sort: 123;
|
|
303
|
+
readonly binary: NativeBinaryAttribute;
|
|
304
|
+
readonly buffer: NativeBinaryAttribute;
|
|
305
|
+
readonly optional: 456;
|
|
306
|
+
readonly list: readonly [...];
|
|
307
|
+
readonly record: Unmarshall<...>;
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
If you specify `{wrapNumbers: true}`, then all `number` types will be wrapped as `{ value: string }`:
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
const unmarshalled = unmarshall(marshalled, {
|
|
315
|
+
wrapNumbers: true,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// numbers are wrapped in { value: string } because of `wrappedNumbers: true`
|
|
319
|
+
unmarshalled.sort.value; // string
|
|
320
|
+
|
|
321
|
+
// it maintains the literal typing information (as much as possible)
|
|
322
|
+
const unmarshalled: {
|
|
323
|
+
readonly key: "key";
|
|
324
|
+
// notice how the number is wrapped in the `NumberValue` type?
|
|
325
|
+
// this is because `wrapNumbers: true`
|
|
326
|
+
readonly sort: NumberValue<123>;
|
|
327
|
+
readonly binary: NativeBinaryAttribute;
|
|
328
|
+
readonly buffer: NativeBinaryAttribute;
|
|
329
|
+
readonly optional: NumberValue<...>;
|
|
330
|
+
readonly list: readonly [...];
|
|
331
|
+
readonly record: Unmarshall<...>;
|
|
332
|
+
};
|
|
333
|
+
```
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/img/get-item.gif
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type AttributeValue = undefined | S | N | B | BOOL | NULL | L<ArrayLike<AttributeValue>> | M<Record<string, AttributeValue>>;
|
|
2
|
+
export type AttributeMap = Record<string, AttributeValue>;
|
|
3
|
+
export type NativeBinaryAttribute = ArrayBuffer | BigInt64Array | BigUint64Array | Buffer | DataView | Float32Array | Float64Array | Int16Array | Int32Array | Int8Array | Uint16Array | Uint32Array | Uint8Array | Uint8ClampedArray;
|
|
4
|
+
export type DocumentValue = undefined | null | boolean | number | string | DocumentValue[] | NativeBinaryAttribute | {
|
|
5
|
+
[key: string]: DocumentValue;
|
|
6
|
+
};
|
|
7
|
+
export type ToAttributeMap<T extends object> = ToAttributeValue<T>["M"];
|
|
8
|
+
/**
|
|
9
|
+
* Computes the JSON representation of an object, {@link T}.
|
|
10
|
+
*/
|
|
11
|
+
export type ToAttributeValue<T> = T extends undefined ? undefined : T extends null ? NULL : T extends boolean ? BOOL<T> : T extends string ? S<T> : T extends number ? N<T> : T extends Date ? N<number> : T extends NativeBinaryAttribute ? B : T extends ArrayLike<unknown> ? L<{
|
|
12
|
+
[i in keyof T]: i extends "length" ? T[i] : ToAttributeValue<T[i]>;
|
|
13
|
+
}> : M<{
|
|
14
|
+
[prop in keyof T]: ToAttributeValue<T[prop]>;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function isS(a: any): a is S;
|
|
17
|
+
export interface S<T extends string = string> {
|
|
18
|
+
S: T;
|
|
19
|
+
}
|
|
20
|
+
export declare function isB(a: any): a is B;
|
|
21
|
+
export interface B {
|
|
22
|
+
B: Buffer;
|
|
23
|
+
}
|
|
24
|
+
export declare function isBOOL(a: any): a is BOOL;
|
|
25
|
+
export interface BOOL<T = boolean> {
|
|
26
|
+
BOOL: T;
|
|
27
|
+
}
|
|
28
|
+
export declare function isM(a: any): a is M;
|
|
29
|
+
export interface M<T extends Record<string, AttributeValue> = Record<string, AttributeValue>> {
|
|
30
|
+
M: T;
|
|
31
|
+
}
|
|
32
|
+
export declare function isN(a: any): a is N;
|
|
33
|
+
export interface N<T extends number = number> {
|
|
34
|
+
N: `${T}`;
|
|
35
|
+
}
|
|
36
|
+
export declare function isNULL(a: any): a is NULL;
|
|
37
|
+
export interface NULL {
|
|
38
|
+
NULL: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function isL(a: any): a is L;
|
|
41
|
+
export interface L<T extends ArrayLike<AttributeValue> = ArrayLike<AttributeValue>> {
|
|
42
|
+
L: T;
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isS = isS;
|
|
4
|
+
exports.isB = isB;
|
|
5
|
+
exports.isBOOL = isBOOL;
|
|
6
|
+
exports.isM = isM;
|
|
7
|
+
exports.isN = isN;
|
|
8
|
+
exports.isNULL = isNULL;
|
|
9
|
+
exports.isL = isL;
|
|
10
|
+
function isS(a) {
|
|
11
|
+
return a !== undefined && "S" in a;
|
|
12
|
+
}
|
|
13
|
+
function isB(a) {
|
|
14
|
+
return a !== undefined && "B" in a;
|
|
15
|
+
}
|
|
16
|
+
function isBOOL(a) {
|
|
17
|
+
return a !== undefined && "BOOL" in a;
|
|
18
|
+
}
|
|
19
|
+
function isM(a) {
|
|
20
|
+
return a !== undefined && "M" in a;
|
|
21
|
+
}
|
|
22
|
+
function isN(a) {
|
|
23
|
+
return a !== undefined && "N" in a;
|
|
24
|
+
}
|
|
25
|
+
function isNULL(a) {
|
|
26
|
+
return a !== undefined && "NULL" in a;
|
|
27
|
+
}
|
|
28
|
+
function isL(a) {
|
|
29
|
+
return a !== undefined && "L" in a;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0cmlidXRlLXZhbHVlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2F0dHJpYnV0ZS12YWx1ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQXdFQSxrQkFFQztBQU1ELGtCQUVDO0FBTUQsd0JBRUM7QUFNRCxrQkFFQztBQVFELGtCQUVDO0FBTUQsd0JBRUM7QUFNRCxrQkFFQztBQXBERCxTQUFnQixHQUFHLENBQUMsQ0FBTTtJQUN4QixPQUFPLENBQUMsS0FBSyxTQUFTLElBQUksR0FBRyxJQUFJLENBQUMsQ0FBQztBQUNyQyxDQUFDO0FBTUQsU0FBZ0IsR0FBRyxDQUFDLENBQU07SUFDeEIsT0FBTyxDQUFDLEtBQUssU0FBUyxJQUFJLEdBQUcsSUFBSSxDQUFDLENBQUM7QUFDckMsQ0FBQztBQU1ELFNBQWdCLE1BQU0sQ0FB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4sXG4+IHtcbiAgTDogVDtcbn1cbiJdfQ==
|
package/lib/callback.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2FsbGJhY2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY2FsbGJhY2sudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB0eXBlIHsgQVdTRXJyb3IgfSBmcm9tIFwiYXdzLXNka1wiO1xuXG5leHBvcnQgdHlwZSBDYWxsYmFjazxEYXRhLCBFcnIgPSBBV1NFcnJvcj4gPSAoZXJyOiBFcnIsIGRhdGE6IERhdGEpID0+IHZvaWQ7XG4iXX0=
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AWSError, DynamoDB, Request } from "aws-sdk";
|
|
2
|
+
import { Callback } from "./callback";
|
|
3
|
+
import { DeleteItemInput, DeleteItemOutput } from "./delete-item";
|
|
4
|
+
import { GetItemInput, GetItemOutput } from "./get-item";
|
|
5
|
+
import { JsonFormat } from "./json-format";
|
|
6
|
+
import { TableKey } from "./key";
|
|
7
|
+
import { PutItemInput, PutItemOutput } from "./put-item";
|
|
8
|
+
import { QueryInput, QueryOutput } from "./query";
|
|
9
|
+
import { ScanInput, ScanOutput } from "./scan";
|
|
10
|
+
import { UpdateItemInput, UpdateItemOutput } from "./update-item";
|
|
11
|
+
export interface TypeSafeDynamoDBv2<Item extends object, PartitionKey extends keyof Item, RangeKey extends keyof Item | undefined = undefined> extends Omit<DynamoDB, "getItem" | "deleteItem" | "putItem" | "query" | "scan" | "updateItem"> {
|
|
12
|
+
getItem<Key extends TableKey<Item, PartitionKey, RangeKey, JsonFormat.AttributeValue>, AttributesToGet extends keyof Item | undefined = undefined, ProjectionExpression extends string | undefined = undefined>(params: GetItemInput<Item, PartitionKey, RangeKey, Key, AttributesToGet, ProjectionExpression, JsonFormat.AttributeValue>, callback?: Callback<GetItemOutput<Item, PartitionKey, RangeKey, Key, AttributesToGet, ProjectionExpression, JsonFormat.AttributeValue>, AWSError>): Request<GetItemOutput<Item, PartitionKey, RangeKey, Key, AttributesToGet, ProjectionExpression, JsonFormat.AttributeValue>, AWSError>;
|
|
13
|
+
deleteItem<Key extends TableKey<Item, PartitionKey, RangeKey, JsonFormat.AttributeValue>, ConditionExpression extends string | undefined, ReturnValue extends DynamoDB.ReturnValue = "NONE">(params: DeleteItemInput<Item, PartitionKey, RangeKey, Key, ConditionExpression, ReturnValue, JsonFormat.AttributeValue>, callback?: Callback<DeleteItemOutput<Item, ReturnValue, JsonFormat.AttributeValue>, AWSError>): Request<DeleteItemOutput<Item, ReturnValue, JsonFormat.AttributeValue>, AWSError>;
|
|
14
|
+
putItem<ConditionExpression extends string | undefined, ReturnValue extends DynamoDB.ReturnValue = "NONE">(params: PutItemInput<Item, ConditionExpression, ReturnValue, JsonFormat.AttributeValue>, callback?: Callback<PutItemOutput<Item, ReturnValue, JsonFormat.AttributeValue>, AWSError>): Request<PutItemOutput<Item, ReturnValue, JsonFormat.AttributeValue>, AWSError>;
|
|
15
|
+
updateItem<Key extends TableKey<Item, PartitionKey, RangeKey, JsonFormat.AttributeValue>, UpdateExpression extends string, ConditionExpression extends string | undefined, ReturnValue extends DynamoDB.ReturnValue = "NONE">(params: UpdateItemInput<Item, PartitionKey, RangeKey, Key, UpdateExpression, ConditionExpression, ReturnValue, JsonFormat.AttributeValue>, callback?: Callback<UpdateItemOutput<Item, PartitionKey, RangeKey, Key, ReturnValue, JsonFormat.AttributeValue>, AWSError>): Request<UpdateItemOutput<Item, PartitionKey, RangeKey, Key, ReturnValue, JsonFormat.AttributeValue>, AWSError>;
|
|
16
|
+
query<KeyConditionExpression extends string | undefined = undefined, FilterExpression extends string | undefined = undefined, ProjectionExpression extends string | undefined = undefined, AttributesToGet extends keyof Item | undefined = undefined>(params: QueryInput<Item, KeyConditionExpression, FilterExpression, ProjectionExpression, AttributesToGet, JsonFormat.AttributeValue>, callback?: Callback<QueryOutput<Item, AttributesToGet, JsonFormat.AttributeValue>, AWSError>): Request<QueryOutput<Item, AttributesToGet, JsonFormat.AttributeValue>, AWSError>;
|
|
17
|
+
scan<FilterExpression extends string | undefined = undefined, ProjectionExpression extends string | undefined = undefined, AttributesToGet extends keyof Item | undefined = undefined>(params: ScanInput<Item, FilterExpression, ProjectionExpression, AttributesToGet, JsonFormat.AttributeValue>, callback?: Callback<ScanOutput<Item, AttributesToGet, JsonFormat.AttributeValue>, AWSError>): Request<ScanOutput<Item, AttributesToGet, JsonFormat.AttributeValue>, AWSError>;
|
|
18
|
+
}
|