ts-dynamodb-attributes-transformer

Code transformer plugin powered by TypeScript Compiler API that transforms TypeScript object from/to Amazon DynamoDB attributes

MIT License

Downloads
12
Stars
4
Committers
2
ts-dynamodb-attributes-transformer - v0.2.0 Latest Release

Published by moznion almost 2 years ago

What's Changed

Breaking Changes

Give "lenient type check" option through the function argument #23

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.

New Features

Unmarshaller support #16

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" },
 *     }
 *   }
 */

Support optional type (e.g. string | undefined, maybeString?: string) #14

This 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

Maintenance

  • Make CI check stable #15

Dependencies

  • Update dependency eslint to v8.28.0 #22
  • Update dependency typescript to v4.9.3 #21
  • Update typescript-eslint monorepo to v5.43.0 #20
  • Update dependency jest to v29.3.1 #19
  • Update dependency jest to v29.3.0 #18
  • Update typescript-eslint monorepo to v5.42.1 #17
  • Update typescript-eslint monorepo to v5.42.0 #13
  • Update dependency eslint to v8.27.0 #12
  • Update dependency jest to v29.2.2 #11
  • Update typescript-eslint monorepo to v5.40.1 #8
ts-dynamodb-attributes-transformer - v0.1.0

Published by moznion about 2 years ago

What's Changed

  • Change the return type's key type parameter of dynamodbRecord() #7
  • Bind the argument of dynamodbRecord() to the transformed immediately invoked function's parameter #6
ts-dynamodb-attributes-transformer - v0.0.1

Published by moznion about 2 years ago

Initial release.

Package Rankings
Top 17.18% on Npmjs.org
Badges
Extracted from project README
.github/workflows/check.yml npm version