Skip to content

Commit

Permalink
Transform DynamoDB DocumentClient convertEmptyValues option (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Aug 2, 2023
1 parent 6729d70 commit 1a54680
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-camels-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform DynamoDB DocumentClient convertEmptyValues option
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

const documentClient = new AWS.DynamoDB.DocumentClient({
convertEmptyValues: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const documentClient = DynamoDBDocument.from(new DynamoDB(), {
marshallOptions: {
convertEmptyValues: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";

export interface GetDynamoDBDocClientArgsOptions {
v2ClientLocalName?: string;
}

export const getDynamoDBDocClientArgs = (
j: JSCodeshift,
v2DocClientNewExpression: ASTPath<NewExpression>,
options: GetDynamoDBDocClientArgsOptions
) => {
const dynamoDBDocClientOptions = j.objectExpression([]);

const v2DocClientArgs = v2DocClientNewExpression.node.arguments || [];

// Add DocumentClient option convertEmptyValues.
if (v2DocClientArgs.length > 0) {
const params = v2DocClientArgs[0];
if (params.type === "ObjectExpression") {
for (const property of params.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
continue;
}

const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
continue;
}

if (propertyKey.name === "convertEmptyValues") {
dynamoDBDocClientOptions.properties.push(
j.property(
"init",
j.identifier("marshallOptions"),
j.objectExpression([
j.property(
"init",
j.identifier("convertEmptyValues"),
(property as Property | ObjectProperty).value
),
])
)
);
}
}
}
}

return [
getDynamoDBForDocClient(j, v2DocClientNewExpression, options),
...(dynamoDBDocClientOptions.properties.length > 0 ? [dynamoDBDocClientOptions] : []),
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export const getDynamoDBForDocClient = (
v2DocClientNewExpression: ASTPath<NewExpression>,
{ v2ClientLocalName }: GetDynamoDBForDocClientOptions
) => {
const v2DocClientArgs = v2DocClientNewExpression.node.arguments || [];

// Return value in `service` param if it's provided.
if (v2DocClientNewExpression.node.arguments.length > 0) {
const params = v2DocClientNewExpression.node.arguments[0];
if (v2DocClientArgs.length > 0) {
const params = v2DocClientArgs[0];
if (params.type === "ObjectExpression") {
const serviceProperty = params.properties.find((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
Expand All @@ -36,8 +38,31 @@ export const getDynamoDBForDocClient = (
}
}

const v3DocClientArgs = v2DocClientArgs[0];
const v3DocClientNewExpressionArgs = [];

// Remove DocumentClient option convertEmptyValues.
if (v3DocClientArgs.type === "ObjectExpression") {
v3DocClientArgs.properties = v3DocClientArgs.properties.filter((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
return true;
}
const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
return true;
}
return propertyKey.name !== "convertEmptyValues";
});

if (v3DocClientArgs.properties.length > 0) {
v3DocClientNewExpressionArgs.push(v3DocClientArgs);
}
} else {
v3DocClientNewExpressionArgs.push(v3DocClientArgs);
}

return j.newExpression(
v2ClientLocalName ? j.identifier(v2ClientLocalName) : j.identifier(DYNAMODB),
v2DocClientNewExpression.node.arguments
v3DocClientNewExpressionArgs
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Collection, JSCodeshift } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs";

export interface ReplaceDocClientCreationOptions {
v2ClientName: string;
Expand All @@ -26,7 +26,7 @@ export const replaceDocClientCreation = (
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
[getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName })]
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, { v2ClientLocalName })
)
);
}
Expand All @@ -37,8 +37,9 @@ export const replaceDocClientCreation = (
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
)
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")), [
getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName }),
])
j.callExpression(
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, { v2ClientLocalName })
)
);
};

0 comments on commit 1a54680

Please sign in to comment.