Skip to content

Commit

Permalink
Use value from service param for creating DynamoDB DocumentClient (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 6, 2023
1 parent 309bcce commit 6b09a0d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-seas-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use value from service param for creating DynamoDB DocumentClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import AWS from "aws-sdk";

const params = { region: "us-west-2" };
const documentClient = new AWS.DynamoDB.DocumentClient({
service: new AWS.DynamoDB(params),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const params = { region: "us-west-2" };
const documentClient = DynamoDBDocument.from(new DynamoDB(params));
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";

import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";

export interface GetDynamoDBForDocClientOptions {
v2ClientLocalName?: string;
}

export const getDynamoDBForDocClient = (
j: JSCodeshift,
v2DocClientNewExpression: ASTPath<NewExpression>,
{ v2ClientLocalName }: GetDynamoDBForDocClientOptions
) => {
// Return value in `service` param if it's provided.
if (v2DocClientNewExpression.node.arguments.length > 0) {
const params = v2DocClientNewExpression.node.arguments[0];
if (params.type === "ObjectExpression") {
const serviceProperty = params.properties.find((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
return false;
}
const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
return false;
}
if (propertyKey.name === "service") {
return true;
}
}) as Property | ObjectProperty | undefined;

if (serviceProperty) {
// The value here will work in most Document Client creations.
// Adding typecast to skip TypeScript errors.
return serviceProperty.value as NewExpression;
}
}
}

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

import { DYNAMODB } from "../config";
import { getDocClientNewExpression } from "../utils";
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";

export interface ReplaceDocClientCreationOptions {
v2ClientLocalName: string;
Expand All @@ -19,7 +19,7 @@ export const replaceDocClientCreation = (
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")),
[j.newExpression(j.identifier(DYNAMODB), v2DocClientNewExpression.node.arguments)]
[getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName })]
)
);
}
Expand All @@ -28,7 +28,7 @@ export const replaceDocClientCreation = (
.find(j.NewExpression, getDocClientNewExpression({ v2ClientLocalName }))
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")), [
j.newExpression(j.identifier(v2ClientLocalName), v2DocClientNewExpression.node.arguments),
getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName }),
])
);
};

0 comments on commit 6b09a0d

Please sign in to comment.