You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Code transformer plugin powered by [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) that transforms TypeScript object to Amazon DynamoDB attributes.
3
+
Code transformer plugin powered by [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) that transforms TypeScript object from/to Amazon DynamoDB attributes.
4
4
5
-
## How it works
5
+
## Description
6
6
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`.
7
+
This plugin replaces the TypeScript function invocation with the generated object code. In short, this plugin generates the code for every property of type `T`.
This plugin replaces `dynamodbRecord<T>(obj: T)` invocation 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.
8
13
9
14
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.
10
15
11
-
Manual making the translation layer between the object and DynamoDB's Record is no longer needed!
This replaces `fromDynamodbRecord<T>(attrs: Record<string, AttributeValue>)` invocation with the object which has type `T`. This method is responsible to translate the DynamoDB attributes to the actual TypeScript object, i.e. unmarshalling.
12
19
13
20
## Motivations
14
21
15
22
- To do automatic generation of the [DynamoDB attribute data type](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html) code that is recognizable by [aws-sdk-js-v3](https://github.com/aws/aws-sdk-js-v3), with type safety.
23
+
- Manual making the translation layer between the object and DynamoDB's Record is no longer needed!
16
24
- Performance. This uses TypeScript Compiler API, so it generates/determine the DynamoDB attribute code at the compiling timing. This means the logic doesn't have to do a reflection on the fly so this contributes to a good performance.
17
25
18
26
### Benchmark
19
27
20
28
The benchmark result between this project and [kayomarz/dynamodb-data-types](https://github.com/kayomarz/dynamodb-data-types) is the following:
21
29
30
+
marshalling:
31
+
22
32
```
23
33
node version: v16.17.0
24
-
dynamodb-data-types marshalling x 3,475,450 ops/sec ±0.45% (96 runs sampled)
25
-
ts-dynamodb-attributes-transformer marshalling x 13,405,409 ops/sec ±0.43% (91 runs sampled)
34
+
dynamodb-data-types marshalling x 3,845,247 ops/sec ±0.63% (90 runs sampled)
35
+
ts-dynamodb-attributes-transformer marshalling x 13,614,974 ops/sec ±0.24% (100 runs sampled)
26
36
Fastest is ts-dynamodb-attributes-transformer marshalling
27
37
```
28
38
39
+
unmarshalling:
40
+
41
+
```
42
+
node version: v16.17.0
43
+
dynamodb-data-types unmarshalling x 1,800,718 ops/sec ±0.30% (96 runs sampled)
44
+
ts-dynamodb-attributes-transformer unmarshalling x 3,493,272 ops/sec ±0.50% (98 runs sampled)
45
+
Fastest is ts-dynamodb-attributes-transformer unmarshalling
46
+
```
47
+
29
48
Please see also [benchmark](./examples/benchmark) project.
30
49
31
50
## Synopsis
32
51
52
+
### Marshalling into DynamoDB record from the Typescript Object
This plugin exports a function that has the signature `dynamodbRecord<T extends object>(item: T): Record<keyof T, AttributeValue>`.
216
+
This plugin exports the functions that have the signature `functiondynamodbRecord<T extends object>(item:T, shouldLenientTypeCheck?:boolean): Record<keyof T, AttributeValue>` and `function fromDynamodbRecord<T extends object>(attrs: Record<string, AttributeValue>, shouldLenientTypeCheck?: boolean): T`.
112
217
113
-
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:
218
+
These functions are the markers to indicate to the transformer to replace the function invocation with the generated code. Therefore, there are some restrictions:
114
219
115
220
- 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.
116
-
- Type `T` must be class or interface.
221
+
- Type `T` must be class or interface. If it needs to do unmarshalling, this type `T` must be a derived type of the **interface**.
117
222
118
223
### Examples
119
224
@@ -187,11 +292,11 @@ NOTE: if the TypeScript property has `unknown` type and the value is `null` then
187
292
188
293
## Options
189
294
190
-
### `TS_DYNAMODB_ATTR_TRANSFORMER_LENIENT_TYPE_CHECK` env var (default: `<empty>`)
295
+
### Lenient type checking (default: `false`)
191
296
192
297
By default, if this plugin encounters unsupported types, it raises the error and halts the transformation.
193
298
194
-
But if `TS_DYNAMODB_ATTR_TRANSFORMER_LENIENT_TYPE_CHECK` environment variable is not empty, it proceeds the transformation with ignoring the unsupported typed property even if it gets the unsupported types.
299
+
But if `true` value is given through the second argument of the function, it proceeds the transformation with ignoring the unsupported typed property even if it gets the unsupported types.
0 commit comments