Skip to content

Commit

Permalink
feat(document-client): expose translateConfig options to DocumentClie…
Browse files Browse the repository at this point in the history
…ntV3
  • Loading branch information
whimzyLive committed Mar 25, 2023
1 parent 63e4283 commit 03f4343
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DynamoDBClient} from '@aws-sdk/client-dynamodb';
import {DEFAULT_TRANSLATE_CONFIG_V3} from '../../constants/translate-config';
import {DocumentClient} from '../base-document-client';
import {DocumentClientV3} from '../document-client-v3';

Expand All @@ -16,3 +17,44 @@ beforeEach(() => {
test('registers a valid documentClient instance', async () => {
expect(dc.version).toEqual(3);
});

it('uses default documentClientV3 translate config', () => {
const documentClientInstance = new DocumentClientV3(new DynamoDBClient({}));

expect(documentClientInstance.documentClient.config.translateConfig).toEqual(
DEFAULT_TRANSLATE_CONFIG_V3
);
});

it('overrides documentClientV3 translate config with custom values', () => {
const documentClientInstance = new DocumentClientV3(new DynamoDBClient({}), {
marshallOptions: {
convertEmptyValues: true,
},
unmarshallOptions: {
wrapNumbers: true,
},
});

expect(documentClientInstance.documentClient.config.translateConfig).toEqual({
marshallOptions: {
convertClassInstanceToMap: false,
convertEmptyValues: true,
removeUndefinedValues: false,
},
unmarshallOptions: {
wrapNumbers: true,
},
});
});

it('correctly merges documentClientV3 translate config with custom values when all values are not provided', () => {
const documentClientInstance = new DocumentClientV3(new DynamoDBClient({}), {
marshallOptions: {},
unmarshallOptions: undefined,
});

expect(documentClientInstance.documentClient.config.translateConfig).toEqual(
DEFAULT_TRANSLATE_CONFIG_V3
);
});
35 changes: 22 additions & 13 deletions packages/document-client/src/classes/document-client-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ import {
ScanCommand,
TransactGetCommand,
TransactWriteCommand,
TranslateConfig,
UpdateCommand,
} from '@aws-sdk/lib-dynamodb';
import {isEmptyObject} from '@typedorm/core/src/helpers/is-empty-object';
import {DEFAULT_TRANSLATE_CONFIG_V3} from '../constants/translate-config';
import {TransactionCancelledException} from '../exceptions';
import {DocumentClient} from './base-document-client';

Expand All @@ -44,23 +47,29 @@ export class DocumentClientV3<
readonly documentClient: DynamoDBDocumentClientType;
readonly version = 3;

constructor(dynamoDBClient: DynamoDBClient) {
constructor(
dynamoDBClient: DynamoDBClient,
customTranslateConfig?: TranslateConfig
) {
super();
const marshallOptions = {
// Whether to automatically convert empty strings, blobs, and sets to `null`.
convertEmptyValues: false, // false, by default.
// Whether to remove undefined values while marshalling.
removeUndefinedValues: false, // false, by default.
// Whether to convert typeof object to map attribute.
convertClassInstanceToMap: false, // false, by default.
};

const unmarshallOptions = {
// Whether to return numbers as a string instead of converting them to native JavaScript numbers.
wrapNumbers: false, // false, by default.
const translateConfig = {
marshallOptions:
(customTranslateConfig &&
!isEmptyObject(customTranslateConfig.marshallOptions) && {
...DEFAULT_TRANSLATE_CONFIG_V3.marshallOptions,
...customTranslateConfig.marshallOptions,
}) ||
DEFAULT_TRANSLATE_CONFIG_V3.marshallOptions,
unmarshallOptions:
(customTranslateConfig &&
!isEmptyObject(customTranslateConfig.marshallOptions) && {
...DEFAULT_TRANSLATE_CONFIG_V3.unmarshallOptions,
...customTranslateConfig.unmarshallOptions,
}) ||
DEFAULT_TRANSLATE_CONFIG_V3.unmarshallOptions,
};

const translateConfig = {marshallOptions, unmarshallOptions};
this.documentClient = DynamoDBDocumentClient.from(
dynamoDBClient,
translateConfig
Expand Down
14 changes: 14 additions & 0 deletions packages/document-client/src/constants/translate-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const DEFAULT_TRANSLATE_CONFIG_V3 = {
marshallOptions: {
// Whether to automatically convert empty strings, blobs, and sets to `null`.
convertEmptyValues: false, // false, by default.
// Whether to remove undefined values while marshalling.
removeUndefinedValues: false, // false, by default.
// Whether to convert typeof object to map attribute.
convertClassInstanceToMap: false, // false, by default.
},
unmarshallOptions: {
// Whether to return numbers as a string instead of converting them to native JavaScript numbers.
wrapNumbers: false, // false, by default.
},
};

0 comments on commit 03f4343

Please sign in to comment.