Skip to content

Commit 00266aa

Browse files
authored
Use getNewExpressionCount for DynamoDB.DocumentClient (#526)
1 parent 3a9465b commit 00266aa

File tree

9 files changed

+98
-104
lines changed

9 files changed

+98
-104
lines changed

src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts

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

3-
import { DYNAMODB } from "../config";
4-
import { getClientNewExpression, getDocClientNewExpression } from "../utils";
3+
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
4+
import { getClientNewExpression } from "../utils";
55

66
export interface GetClientIdNamesFromNewExprOptions {
77
v2ClientName: string;
@@ -50,7 +50,11 @@ export const getClientIdNamesFromNewExpr = (
5050
);
5151
if (v2ClientName === DYNAMODB) {
5252
namesFromGlobalModule.push(
53-
...getNames(j, source, getDocClientNewExpression({ v2GlobalName }))
53+
...getNames(
54+
j,
55+
source,
56+
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
57+
)
5458
);
5559
}
5660
}
@@ -59,7 +63,11 @@ export const getClientIdNamesFromNewExpr = (
5963
);
6064
if (v2ClientName === DYNAMODB) {
6165
namesFromServiceModule.push(
62-
...getNames(j, source, getDocClientNewExpression({ v2ClientLocalName }))
66+
...getNames(
67+
j,
68+
source,
69+
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
70+
)
6371
);
6472
}
6573
}

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

Lines changed: 10 additions & 4 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";
4-
import { getDocClientNewExpression } from "../utils";
3+
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
4+
import { getClientNewExpression } from "../utils";
55
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
66

77
export interface ReplaceDocClientCreationOptions {
@@ -19,7 +19,10 @@ export const replaceDocClientCreation = (
1919

2020
if (v2GlobalName) {
2121
source
22-
.find(j.NewExpression, getDocClientNewExpression({ v2GlobalName }))
22+
.find(
23+
j.NewExpression,
24+
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
25+
)
2326
.replaceWith((v2DocClientNewExpression) =>
2427
j.callExpression(
2528
j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")),
@@ -29,7 +32,10 @@ export const replaceDocClientCreation = (
2932
}
3033

3134
source
32-
.find(j.NewExpression, getDocClientNewExpression({ v2ClientLocalName }))
35+
.find(
36+
j.NewExpression,
37+
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
38+
)
3339
.replaceWith((v2DocClientNewExpression) =>
3440
j.callExpression(j.memberExpression(j.identifier("DynamoDBDocument"), j.identifier("from")), [
3541
getDynamoDBForDocClient(j, v2DocClientNewExpression, { v2ClientLocalName }),

src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts

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

3-
import { getClientNewExpression, getDocClientNewExpression } from "../utils";
3+
import { DYNAMODB_DOCUMENT_CLIENT } from "../config";
4+
import { getClientNewExpression } from "../utils";
45

56
export const getNamesFromNewExpr = (
67
j: JSCodeshift,
@@ -14,7 +15,10 @@ export const getNamesFromNewExpr = (
1415
(newExpression) => ((newExpression.callee as MemberExpression).property as Identifier).name
1516
),
1617
...source
17-
.find(j.NewExpression, getDocClientNewExpression({ v2GlobalName }))
18+
.find(
19+
j.NewExpression,
20+
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
21+
)
1822
.nodes()
1923
.map(
2024
(newExpression) =>

src/transforms/v2-to-v3/modules/addClientModules.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
isS3GetSignedUrlApiUsed,
99
isS3UploadApiUsed,
1010
} from "../apis";
11+
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
1112
import { getV3ClientTypesCount } from "../ts-type";
1213
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
13-
import { getDocClientNewExpressionCount } from "./getDocClientNewExpressionCount";
1414
import { getNewExpressionCount } from "./getNewExpressionCount";
1515
import { hasImportEquals } from "./hasImportEquals";
1616
import { hasRequire } from "./hasRequire";
@@ -79,12 +79,22 @@ export const addClientModules = (
7979
}
8080
}
8181

82-
const docClientNewExpressionCount = getDocClientNewExpressionCount(j, source, options);
83-
if (docClientNewExpressionCount > 0) {
84-
addClientNamedModule(j, source, {
82+
if (options.v2ClientName === DYNAMODB) {
83+
const { v2ClientLocalName } = options;
84+
const docClientOptions = {
8585
...options,
86-
importedName: "DynamoDBDocument",
87-
v3ClientPackageName: "@aws-sdk/lib-dynamodb",
88-
});
86+
v2ClientName: DYNAMODB_DOCUMENT_CLIENT,
87+
...(v2ClientLocalName && {
88+
v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}`,
89+
}),
90+
};
91+
const docClientNewExpressionCount = getNewExpressionCount(j, source, docClientOptions);
92+
if (docClientNewExpressionCount > 0) {
93+
addClientNamedModule(j, source, {
94+
...options,
95+
importedName: "DynamoDBDocument",
96+
v3ClientPackageName: "@aws-sdk/lib-dynamodb",
97+
});
98+
}
8999
}
90100
};

src/transforms/v2-to-v3/modules/getDocClientNewExpressionCount.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/getNewExpressionCount.ts

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

3+
import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
34
import { getClientNewExpression } from "../utils";
4-
import { getDocClientNewExpressionCount } from "./getDocClientNewExpressionCount";
55
import { ClientModulesOptions } from "./types";
66

77
export const getNewExpressionCount = (
@@ -26,7 +26,15 @@ export const getNewExpressionCount = (
2626
);
2727
newExpressionCount += newExpressionsFromClientLocalName.length;
2828

29-
newExpressionCount += getDocClientNewExpressionCount(j, source, options);
29+
if (v2ClientName === DYNAMODB) {
30+
newExpressionCount += getNewExpressionCount(j, source, {
31+
...options,
32+
v2ClientName: DYNAMODB_DOCUMENT_CLIENT,
33+
...(v2ClientLocalName && {
34+
v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}`,
35+
}),
36+
});
37+
}
3038

3139
return newExpressionCount;
3240
};

src/transforms/v2-to-v3/utils/getClientNewExpression.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,58 @@ export const getClientNewExpression = ({
2424
}
2525

2626
if (v2GlobalName) {
27+
if (v2ClientName) {
28+
// Support for DynamoDB.DocumentClient
29+
const [clientName, subClientName] = v2ClientName.split(".");
30+
31+
if (subClientName) {
32+
return {
33+
type: "NewExpression",
34+
callee: {
35+
type: "MemberExpression",
36+
object: {
37+
type: "MemberExpression",
38+
object: { type: "Identifier", name: v2GlobalName },
39+
property: { type: "Identifier", name: clientName },
40+
},
41+
property: { type: "Identifier", name: subClientName },
42+
},
43+
} as NewExpression;
44+
}
45+
46+
return {
47+
type: "NewExpression",
48+
callee: {
49+
object: { type: "Identifier", name: v2GlobalName },
50+
property: { type: "Identifier", name: clientName },
51+
},
52+
} as NewExpression;
53+
}
54+
2755
return {
2856
type: "NewExpression",
2957
callee: {
3058
object: { type: "Identifier", name: v2GlobalName },
31-
property: { type: "Identifier", ...(v2ClientName && { name: v2ClientName }) },
59+
property: { type: "Identifier" },
60+
},
61+
} as NewExpression;
62+
}
63+
64+
// Support for DynamoDB.DocumentClient
65+
const [clientName, subClientName] = v2ClientLocalName!.split(".");
66+
67+
if (subClientName) {
68+
return {
69+
type: "NewExpression",
70+
callee: {
71+
object: { type: "Identifier", name: clientName },
72+
property: { type: "Identifier", name: subClientName },
3273
},
3374
} as NewExpression;
3475
}
3576

3677
return {
3778
type: "NewExpression",
38-
callee: { type: "Identifier", name: v2ClientLocalName },
79+
callee: { type: "Identifier", name: clientName },
3980
} as NewExpression;
4081
};

src/transforms/v2-to-v3/utils/getDocClientNewExpression.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from "./getClientDeepImportPath";
22
export * from "./getClientNewExpression";
33
export * from "./getDefaultLocalName";
4-
export * from "./getDocClientNewExpression";
54
export * from "./isTypeScriptFile";

0 commit comments

Comments
 (0)