Skip to content

Commit 3073c63

Browse files
authored
Merge pull request #7 from moznion/restrict_the_key_type_of_generated_record
Change the return type's key type parameter of `dynamodbRecord()`
2 parents be1ffe4 + a00f1d9 commit 3073c63

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Code transformer plugin for Amazon DynamoDB attributes powered by [TypeScript Co
44

55
## How it works
66

7-
This plugin replaces the TypeScript function invocation of `dynamodbRecord<T>(obj: T)` with `Record<string, AttributeValue>` value that is defined in aws-sdk-js-v3 according to the type `T` and the contents of the object. In short, this plugin generates the DynamoDB attribute code for every property of type `T`.
7+
This plugin replaces the TypeScript function invocation of `dynamodbRecord<T>(obj: T)` with `Record<keyof T, AttributeValue>` value that is defined in aws-sdk-js-v3 according to the type `T` and the contents of the object. In short, this plugin generates the DynamoDB attribute code for every property of type `T`.
88

9-
This plugin powers the users can do drop-in replacements for the existing `Record<string, AttributeValue>` value and/or the generator with `dynamodbRecord<T>(obj: T)` function.
9+
This plugin powers the users can do drop-in replacements for the existing `Record<keyof T, AttributeValue>` value and/or the generator with `dynamodbRecord<T>(obj: T)` function.
1010

1111
Manual making the translation layer between the object and DynamoDB's Record is no longer needed!
1212

@@ -27,7 +27,7 @@ interface User {
2727
readonly tags: Map<string, string>;
2828
}
2929

30-
const record: Record<string, AttributeValue> = dynamodbRecord<User>({
30+
const record: Record<keyof User, AttributeValue> = dynamodbRecord<User>({
3131
id: 12345,
3232
name: 'John Doe',
3333
tags: new Map<string, string>([
@@ -38,7 +38,7 @@ const record: Record<string, AttributeValue> = dynamodbRecord<User>({
3838

3939
/*
4040
* Then you can use this record value on the aws-sdk-js-v3's DynamoDB client; for example,
41-
*
41+
*
4242
* const dyn = new DynamoDBClient(...);
4343
* await dyn.send(new PutItemCommand({
4444
* TableName: "...",
@@ -81,7 +81,7 @@ const record = function () {
8181
}();
8282
/*
8383
* This record is equal to the following object:
84-
*
84+
*
8585
* {
8686
* id: { N: "12345" },
8787
* name: { S: "John Doe" },
@@ -97,11 +97,11 @@ const record = function () {
9797

9898
## How to use this transformer
9999

100-
This plugin exports a function that has the signature `dynamodbRecord<T extends object>(item: T): Record<string, AttributeValue>`.
100+
This plugin exports a function that has the signature `dynamodbRecord<T extends object>(item: T): Record<keyof T, AttributeValue>`.
101101

102-
This function is a marker to indicate to the transformer to replace this function invocation with the generated DynamoDB record code. Therefore, there are some restrictions:
102+
This function is a marker to indicate to the transformer to replace this function invocation with the generated DynamoDB record code. Therefore, there are some restrictions:
103103

104-
- Type parameter `T` is mandatory parameter (i.e. this mustn't be omitted). A transformer analyzes the type of the given `T` to collect the property information.
104+
- Type parameter `T` is mandatory parameter (i.e. this mustn't be omitted). A transformer analyzes the type of the given `T` to collect the property information.
105105
- Type `T` must be class or interface.
106106

107107
### Examples

examples/ttypescript/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface User {
77
readonly tags: Map<string, string>;
88
}
99

10-
const record: Record<string, AttributeValue> = dynamodbRecord<User>({
10+
const record: Record<keyof User, AttributeValue> = dynamodbRecord<User>({
1111
id: 12345,
1212
name: 'John Doe',
1313
tags: new Map<string, string>([

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { AttributeValue } from '@aws-sdk/client-dynamodb';
22

3-
export function dynamodbRecord<T extends object>(item: T): Record<string, AttributeValue>;
3+
export function dynamodbRecord<T extends object>(item: T): Record<keyof T, AttributeValue>;

tests/transformer.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('dynamodb record transform', () => {
3030
) {}
3131
}
3232

33-
const record: Record<string, AttributeValue> = dynamodbRecord<Clazz>(
33+
const record: Record<keyof Clazz, AttributeValue> = dynamodbRecord<Clazz>(
3434
new Clazz(
3535
123,
3636
234,
@@ -201,7 +201,7 @@ describe('dynamodb record transform', () => {
201201
constructor(private privateString: string, justArgument: string) {}
202202
}
203203

204-
const record: Record<string, AttributeValue> = dynamodbRecord<Clazz>(new Clazz('foo', 'bar'));
204+
const record: Record<keyof Clazz, AttributeValue> = dynamodbRecord<Clazz>(new Clazz('foo', 'bar'));
205205
expect(Object.keys(record)).toHaveLength(0);
206206
});
207207

@@ -220,7 +220,7 @@ describe('dynamodb record transform', () => {
220220
) {}
221221
}
222222

223-
const record: Record<string, AttributeValue> = dynamodbRecord<Clazz>(
223+
const record: Record<keyof Clazz, AttributeValue> = dynamodbRecord<Clazz>(
224224
new Clazz(
225225
/re/,
226226
[/re/],
@@ -247,7 +247,7 @@ describe('dynamodb record transform', () => {
247247
readonly kvMapToBigint: { [key: string]: BigInt },
248248
) {}
249249
}
250-
const record: Record<string, AttributeValue> = dynamodbRecord<Clazz>(
250+
const record: Record<keyof Clazz, AttributeValue> = dynamodbRecord<Clazz>(
251251
new Clazz(
252252
BigInt(123),
253253
[BigInt(123), BigInt(234)],
@@ -272,7 +272,7 @@ describe('dynamodb record transform', () => {
272272
readonly name: string;
273273
readonly tags: Map<string, string>;
274274
}
275-
const record: Record<string, AttributeValue> = dynamodbRecord<Interface>({
275+
const record: Record<keyof Interface, AttributeValue> = dynamodbRecord<Interface>({
276276
id: 12345,
277277
name: 'John Doe',
278278
tags: new Map<string, string>([

0 commit comments

Comments
 (0)