diff --git a/.changeset/many-seas-listen.md b/.changeset/many-seas-listen.md new file mode 100644 index 000000000..e34c97ce3 --- /dev/null +++ b/.changeset/many-seas-listen.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Use value from service param for creating DynamoDB DocumentClient diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.input.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.input.js new file mode 100644 index 000000000..1e9d6d3a6 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.input.js @@ -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), +}); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.output.js new file mode 100644 index 000000000..25d87b415 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.output.js @@ -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)); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts new file mode 100644 index 000000000..235e7b504 --- /dev/null +++ b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts @@ -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, + { 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 + ); +}; diff --git a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts index 3ac766673..80d5f1cfd 100644 --- a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts +++ b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts @@ -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; @@ -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 })] ) ); } @@ -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 }), ]) ); };