Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use value from service param for creating DynamoDB DocumentClient #467

Merged
merged 6 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }),
])
);
};