Code transformer plugin powered by TypeScript Compiler API that transforms TypeScript object from/to Amazon DynamoDB attributes
MIT License
This makes it receive the specifier to let this plugin check the type leniently through the 2nd parameter of the function instead of the environment variable TS_DYNAMODB_ATTR_TRANSFORMER_LENIENT_TYPE_CHECK
on the prior implementation.
This change introduces a new function function fromDynamodbRecord<T extends object>(attrs: Record<string, AttributeValue>, shouldLenientTypeCheck?: boolean): T
that is a marker function that is replaced with the generated object code by this plugin.
example:
import { fromDynamodbRecord } from '@moznion/ts-dynamodb-attributes-transformer';
interface User {
readonly id?: number;
readonly name?: string;
readonly tags: Map<string, string>;
}
const user: User = fromDynamodbRecord<User>({
id: {
N: '12345',
},
name: {
S: 'John Doe',
},
tags: {
M: {
foo: {
S: 'bar',
},
buz: {
S: 'qux',
},
},
},
});
This plugin transforms the above TypeScript code like the following JavaScript code:
const user = (function (arg) {
return {
id: (function () {
const numStr = arg.id.N;
return numStr === undefined ? undefined : Number(numStr);
})(),
name: arg.name.S,
tags: (function () {
var m, r;
m = new Map();
r = arg['tags']?.M;
for (const k in r) {
m.set(k, r[k]?.S);
}
return m;
})(),
};
})({
id: {
N: '12345',
},
name: {
S: 'John Doe',
},
tags: {
M: {
foo: {
S: 'bar',
},
buz: {
S: 'qux',
},
},
},
});
/*
* This object is equal to the following:
*
* {
* id: 12345,
* name: "John Doe",
* tags: {
* foo: { S: "bar" },
* buz: { S: "qux" },
* }
* }
*/
string | undefined
, maybeString?: string
) #14This change brings a support for the optional (undefined-able) field like the following:
interface Interface {
optionalStr1: string | undefined,
optionalStr2: undefined | string,
optionalStr3?: string,
}
Performance:
node version: v16.17.0
dynamodb-data-types unmarshalling x 1,800,718 ops/sec ±0.30% (96 runs sampled)
ts-dynamodb-attributes-transformer unmarshalling x 3,493,272 ops/sec ±0.50% (98 runs sampled)
Fastest is ts-dynamodb-attributes-transformer unmarshalling
Published by moznion about 2 years ago
Published by moznion about 2 years ago
Initial release.