-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use value from service param for creating DynamoDB DocumentClient (#467)
- Loading branch information
Showing
5 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |
5 changes: 5 additions & 0 deletions
5
src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/param-service.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
43 changes: 43 additions & 0 deletions
43
src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters