|
1 | 1 | # ts-dynamodb-attributes-transformer |
2 | 2 |
|
3 | | -TypeScript Compiler API Transformer for Amazon DynamoDB attributes. |
| 3 | +Code transformer plugin for Amazon DynamoDB attributes powered by [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API). |
4 | 4 |
|
| 5 | +## How it works |
| 6 | + |
| 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`. |
| 8 | + |
| 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. |
| 10 | + |
| 11 | +Manual making the translation layer between the object and DynamoDB's Record is no longer needed! |
| 12 | + |
| 13 | +## Motivations |
| 14 | + |
| 15 | +- 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. |
| 16 | +- 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 | + |
| 18 | +## Synopsis |
| 19 | + |
| 20 | +```ts |
| 21 | +import { AttributeValue } from '@aws-sdk/client-dynamodb'; |
| 22 | +import { dynamodbRecord } from '@moznion/ts-dynamodb-attributes-transformer'; |
| 23 | + |
| 24 | +interface User { |
| 25 | + readonly id: number; |
| 26 | + readonly name: string; |
| 27 | + readonly tags: Map<string, string>; |
| 28 | +} |
| 29 | + |
| 30 | +const record: Record<string, AttributeValue> = dynamodbRecord<User>({ |
| 31 | + id: 12345, |
| 32 | + name: 'John Doe', |
| 33 | + tags: new Map<string, string>([ |
| 34 | + ['foo', 'bar'], |
| 35 | + ['buz', 'qux'], |
| 36 | + ]), |
| 37 | +}); |
| 38 | + |
| 39 | +/* |
| 40 | + * Then you can use this record value on the aws-sdk-js-v3's DynamoDB client; for example, |
| 41 | + * |
| 42 | + * const dyn = new DynamoDBClient(...); |
| 43 | + * await dyn.send(new PutItemCommand({ |
| 44 | + * TableName: "...", |
| 45 | + * Item: record, // <= HERE! |
| 46 | + * })); |
| 47 | + */ |
| 48 | +``` |
| 49 | + |
| 50 | +Then this plugin transforms the above TypeScript code like the following JavaScript code: |
| 51 | + |
| 52 | +```js |
| 53 | +const record = function () { |
| 54 | + var arg; |
| 55 | + arg = { |
| 56 | + id: 12345, |
| 57 | + name: 'John Doe', |
| 58 | + tags: new Map([ |
| 59 | + ['foo', 'bar'], |
| 60 | + ['buz', 'qux'], |
| 61 | + ]), |
| 62 | + } |
| 63 | + return { |
| 64 | + id: { |
| 65 | + N: arg.id.toString() |
| 66 | + }, |
| 67 | + name: { |
| 68 | + S: arg.name |
| 69 | + }, |
| 70 | + tags: { |
| 71 | + M: function () { |
| 72 | + var m; |
| 73 | + m = {} |
| 74 | + for (const kv of arg.tags) { |
| 75 | + m[kv[0]] = { S: kv[1] } |
| 76 | + } |
| 77 | + return m; |
| 78 | + }() |
| 79 | + } |
| 80 | + }; |
| 81 | +}(); |
| 82 | +/* |
| 83 | + * This record is equal to the following object: |
| 84 | + * |
| 85 | + * { |
| 86 | + * id: { N: "12345" }, |
| 87 | + * name: { S: "John Doe" }, |
| 88 | + * tags: { |
| 89 | + * M: { |
| 90 | + * foo: { S: "bar" }, |
| 91 | + * buz: { S: "qux" } |
| 92 | + * } |
| 93 | + * } |
| 94 | + * } |
| 95 | + */ |
| 96 | +``` |
| 97 | + |
| 98 | +## How to use this transformer |
| 99 | + |
| 100 | +This plugin exports a function that has the signature `dynamodbRecord<T extends object>(item: T): Record<string, AttributeValue>`. |
| 101 | + |
| 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: |
| 103 | + |
| 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. |
| 105 | +- Type `T` must be class or interface. |
| 106 | + |
| 107 | +### Examples |
| 108 | + |
| 109 | +#### ttypescript |
| 110 | + |
| 111 | +[ttypescript](https://github.com/cevek/ttypescript) is a custom TypeScript compiler that triggers the specified transformers in the tsconfig.json. |
| 112 | + |
| 113 | +Please refer to the [examples/ttypescript](./examples/ttypescript) project directory and [ttypescript official README](https://github.com/cevek/ttypescript) for more details. |
| 114 | + |
| 115 | +Anyway, the important thing is specifying `compilerOptions.plugins` in tsconfig.json like the following: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "compilerOptions": { |
| 120 | + // ... |
| 121 | + "plugins": [ |
| 122 | + { "transform": "@moznion/ts-dynamodb-attributes-transformer/transformer" } |
| 123 | + ] |
| 124 | + }, |
| 125 | + // ... |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### ts-jest |
| 130 | + |
| 131 | +If you use [ts-jest](https://github.com/kulshekhar/ts-jest) with this transformer, one of the easiest ways is using that with ttypescript toghether. |
| 132 | + |
| 133 | +It needs ttypescript configuration and additionally the jest configuration in `jest.config.js` like the below: |
| 134 | + |
| 135 | +```js |
| 136 | +module.exports = { |
| 137 | + // ... |
| 138 | + transform: { |
| 139 | + '^.+\\.tsx?$': [ |
| 140 | + 'ts-jest', |
| 141 | + { |
| 142 | + compiler: 'ttypescript', |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + // ... |
| 147 | +}; |
| 148 | + |
| 149 | +``` |
| 150 | + |
| 151 | +## TypeScript types to DynamoDB types |
| 152 | + |
| 153 | +Please see also [Supported data types and naming rules in Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html) for more details about the DynamoDB types. |
| 154 | + |
| 155 | +### Scalar Types |
| 156 | + |
| 157 | +| TypeScript | DynamoDB | |
| 158 | +|----------------|------------| |
| 159 | +| number, BigInt | N | |
| 160 | +| string | S | |
| 161 | +| Uint8Array | B | |
| 162 | +| boolean | BOOL | |
| 163 | +| unknown | NULL | |
| 164 | + |
| 165 | +NOTE: if the TypeScript property has `unknown` type and the value is `null` then DynamoDB attribute becomes `{ NULL: true }`. Else, that attribute value is `{ NULL: false }`. |
| 166 | + |
| 167 | +### Document Types |
| 168 | + |
| 169 | +| TypeScript | DynamoDB | |
| 170 | +|----------------------------------------------------------------|----------| |
| 171 | +| `Set<string>` | SS | |
| 172 | +| `Set<number>`, `Set<BigInt>` | NS | |
| 173 | +| `Set<Uint8Array>` | BS | |
| 174 | +| `List<$SCALAR_TYPE>` | L | |
| 175 | +| `Map<string, $SCALAR_TYPE>`, `{ [key: string]: $SCALAR_TYPE }` | M | |
| 176 | + |
| 177 | +## Options |
| 178 | + |
| 179 | +### `TS_DYNAMODB_ATTR_TRANSFORMER_LENIENT_TYPE_CHECK` env var (default: `<empty>`) |
| 180 | + |
| 181 | +By default, if this plugin encounters unsupported types, it raises the error and halts the transformation. |
| 182 | + |
| 183 | +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. |
| 184 | + |
| 185 | +## Note |
| 186 | + |
| 187 | +This transformer plugin referred to the various things from [kimamula/ts-transformer-keys](https://github.com/kimamula/ts-transformer-keys) |
| 188 | + |
| 189 | +## Authors |
| 190 | + |
| 191 | +moznion (<moznion@mail.moznion.net>) |
0 commit comments