Skip to content

Commit 6b09a0d

Browse files
authored
Use value from service param for creating DynamoDB DocumentClient (#467)
1 parent 309bcce commit 6b09a0d

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

.changeset/many-seas-listen.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+
Use value from service param for creating DynamoDB DocumentClient
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import AWS from "aws-sdk";
2+
3+
const params = { region: "us-west-2" };
4+
const documentClient = new AWS.DynamoDB.DocumentClient({
5+
service: new AWS.DynamoDB(params),
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
2+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
3+
4+
const params = { region: "us-west-2" };
5+
const documentClient = DynamoDBDocument.from(new DynamoDB(params));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
2+
3+
import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
4+
5+
export interface GetDynamoDBForDocClientOptions {
6+
v2ClientLocalName?: string;
7+
}
8+
9+
export const getDynamoDBForDocClient = (
10+
j: JSCodeshift,
11+
v2DocClientNewExpression: ASTPath<NewExpression>,
12+
{ v2ClientLocalName }: GetDynamoDBForDocClientOptions
13+
) => {
14+
// 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 (params.type === "ObjectExpression") {
18+
const serviceProperty = params.properties.find((property) => {
19+
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
20+
return false;
21+
}
22+
const propertyKey = (property as Property | ObjectProperty).key;
23+
if (propertyKey.type !== "Identifier") {
24+
return false;
25+
}
26+
if (propertyKey.name === "service") {
27+
return true;
28+
}
29+
}) as Property | ObjectProperty | undefined;
30+
31+
if (serviceProperty) {
32+
// The value here will work in most Document Client creations.
33+
// Adding typecast to skip TypeScript errors.
34+
return serviceProperty.value as NewExpression;
35+
}
36+
}
37+
}
38+
39+
return j.newExpression(
40+
v2ClientLocalName ? j.identifier(v2ClientLocalName) : j.identifier(DYNAMODB),
41+
v2DocClientNewExpression.node.arguments
42+
);
43+
};

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

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

3-
import { DYNAMODB } from "../config";
43
import { getDocClientNewExpression } from "../utils";
4+
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
55

66
export interface ReplaceDocClientCreationOptions {
77
v2ClientLocalName: string;
@@ -19,7 +19,7 @@ export const replaceDocClientCreation = (
1919
.replaceWith((v2DocClientNewExpression) =>
2020
j.callExpression(
2121
j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")),
22-
[j.newExpression(j.identifier(DYNAMODB), v2DocClientNewExpression.node.arguments)]
22+
[getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName })]
2323
)
2424
);
2525
}
@@ -28,7 +28,7 @@ export const replaceDocClientCreation = (
2828
.find(j.NewExpression, getDocClientNewExpression({ v2ClientLocalName }))
2929
.replaceWith((v2DocClientNewExpression) =>
3030
j.callExpression(j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")), [
31-
j.newExpression(j.identifier(v2ClientLocalName), v2DocClientNewExpression.node.arguments),
31+
getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName }),
3232
])
3333
);
3434
};

0 commit comments

Comments
 (0)