Skip to content

Commit 1a54680

Browse files
authored
Transform DynamoDB DocumentClient convertEmptyValues option (#536)
1 parent 6729d70 commit 1a54680

File tree

6 files changed

+107
-8
lines changed

6 files changed

+107
-8
lines changed

.changeset/empty-camels-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Transform DynamoDB DocumentClient convertEmptyValues option
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import AWS from "aws-sdk";
2+
3+
const documentClient = new AWS.DynamoDB.DocumentClient({
4+
convertEmptyValues: true,
5+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
2+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
3+
4+
const documentClient = DynamoDBDocument.from(new DynamoDB(), {
5+
marshallOptions: {
6+
convertEmptyValues: true
7+
}
8+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
2+
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
3+
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
4+
5+
export interface GetDynamoDBDocClientArgsOptions {
6+
v2ClientLocalName?: string;
7+
}
8+
9+
export const getDynamoDBDocClientArgs = (
10+
j: JSCodeshift,
11+
v2DocClientNewExpression: ASTPath<NewExpression>,
12+
options: GetDynamoDBDocClientArgsOptions
13+
) => {
14+
const dynamoDBDocClientOptions = j.objectExpression([]);
15+
16+
const v2DocClientArgs = v2DocClientNewExpression.node.arguments || [];
17+
18+
// Add DocumentClient option convertEmptyValues.
19+
if (v2DocClientArgs.length > 0) {
20+
const params = v2DocClientArgs[0];
21+
if (params.type === "ObjectExpression") {
22+
for (const property of params.properties) {
23+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
24+
continue;
25+
}
26+
27+
const propertyKey = (property as Property | ObjectProperty).key;
28+
if (propertyKey.type !== "Identifier") {
29+
continue;
30+
}
31+
32+
if (propertyKey.name === "convertEmptyValues") {
33+
dynamoDBDocClientOptions.properties.push(
34+
j.property(
35+
"init",
36+
j.identifier("marshallOptions"),
37+
j.objectExpression([
38+
j.property(
39+
"init",
40+
j.identifier("convertEmptyValues"),
41+
(property as Property | ObjectProperty).value
42+
),
43+
])
44+
)
45+
);
46+
}
47+
}
48+
}
49+
}
50+
51+
return [
52+
getDynamoDBForDocClient(j, v2DocClientNewExpression, options),
53+
...(dynamoDBDocClientOptions.properties.length > 0 ? [dynamoDBDocClientOptions] : []),
54+
];
55+
};

src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export const getDynamoDBForDocClient = (
1111
v2DocClientNewExpression: ASTPath<NewExpression>,
1212
{ v2ClientLocalName }: GetDynamoDBForDocClientOptions
1313
) => {
14+
const v2DocClientArgs = v2DocClientNewExpression.node.arguments || [];
15+
1416
// Return value in `service` param if it's provided.
15-
if (v2DocClientNewExpression.node.arguments.length > 0) {
16-
const params = v2DocClientNewExpression.node.arguments[0];
17+
if (v2DocClientArgs.length > 0) {
18+
const params = v2DocClientArgs[0];
1719
if (params.type === "ObjectExpression") {
1820
const serviceProperty = params.properties.find((property) => {
1921
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
@@ -36,8 +38,31 @@ export const getDynamoDBForDocClient = (
3638
}
3739
}
3840

41+
const v3DocClientArgs = v2DocClientArgs[0];
42+
const v3DocClientNewExpressionArgs = [];
43+
44+
// Remove DocumentClient option convertEmptyValues.
45+
if (v3DocClientArgs.type === "ObjectExpression") {
46+
v3DocClientArgs.properties = v3DocClientArgs.properties.filter((property) => {
47+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
48+
return true;
49+
}
50+
const propertyKey = (property as Property | ObjectProperty).key;
51+
if (propertyKey.type !== "Identifier") {
52+
return true;
53+
}
54+
return propertyKey.name !== "convertEmptyValues";
55+
});
56+
57+
if (v3DocClientArgs.properties.length > 0) {
58+
v3DocClientNewExpressionArgs.push(v3DocClientArgs);
59+
}
60+
} else {
61+
v3DocClientNewExpressionArgs.push(v3DocClientArgs);
62+
}
63+
3964
return j.newExpression(
4065
v2ClientLocalName ? j.identifier(v2ClientLocalName) : j.identifier(DYNAMODB),
41-
v2DocClientNewExpression.node.arguments
66+
v3DocClientNewExpressionArgs
4267
);
4368
};

src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
44
import { getClientNewExpression } from "../utils";
5-
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
5+
import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs";
66

77
export interface ReplaceDocClientCreationOptions {
88
v2ClientName: string;
@@ -26,7 +26,7 @@ export const replaceDocClientCreation = (
2626
.replaceWith((v2DocClientNewExpression) =>
2727
j.callExpression(
2828
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
29-
[getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName })]
29+
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, { v2ClientLocalName })
3030
)
3131
);
3232
}
@@ -37,8 +37,9 @@ export const replaceDocClientCreation = (
3737
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
3838
)
3939
.replaceWith((v2DocClientNewExpression) =>
40-
j.callExpression(j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")), [
41-
getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName }),
42-
])
40+
j.callExpression(
41+
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
42+
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, { v2ClientLocalName })
43+
)
4344
);
4445
};

0 commit comments

Comments
 (0)