Skip to content

Commit

Permalink
feat(aws-apigateway-dynamodb): add optional resourceName parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
fargito committed Feb 13, 2023
1 parent 8fe04b9 commit e438583
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface ApiGatewayToDynamoDBProps {
* @default - Default properties are used.
*/
readonly apiGatewayProps?: api.RestApiProps;
/**
* Optional resource name on the API
* This property is useful if your integration does not directly use the partition key name
*
* @default - partition key name, retrieved from the DynamoDB table object
*/
readonly resourceName?: string;
/**
* Whether to deploy an API Gateway Method for POST HTTP operations on the DynamoDB table (i.e. dynamodb:PutItem).
*
Expand Down Expand Up @@ -228,6 +235,8 @@ export class ApiGatewayToDynamoDB extends Construct {
partitionKeyName = getPartitionKeyNameFromTable(props.existingTableObj);
}

const resourceName = props.resourceName ?? partitionKeyName;

// Since we are only invoking this function with an existing Table or tableProps,
// (not a table interface), we know that the implementation will always return
// a Table object and we can safely cast away the optional aspect of the type.
Expand All @@ -246,7 +255,7 @@ export class ApiGatewayToDynamoDB extends Construct {
});

// Setup the API Gateway Resource
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + partitionKeyName + "}");
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + resourceName + "}");

// Setup API Gateway Method
// Create
Expand All @@ -269,10 +278,10 @@ export class ApiGatewayToDynamoDB extends Construct {
const readRequestTemplate = props.readRequestTemplate ??
`{ \
"TableName": "${this.dynamoTable.tableName}", \
"KeyConditionExpression": "${partitionKeyName} = :v1", \
"KeyConditionExpression": "${resourceName} = :v1", \
"ExpressionAttributeValues": { \
":v1": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
} \
}`;
Expand Down Expand Up @@ -310,8 +319,8 @@ export class ApiGatewayToDynamoDB extends Construct {
`{ \
"TableName": "${this.dynamoTable.tableName}", \
"Key": { \
"${partitionKeyName}": { \
"S": "$input.params('${partitionKeyName}')" \
"${resourceName}": { \
"S": "$input.params('${resourceName}')" \
} \
}, \
"ReturnValues": "ALL_OLD" \
Expand Down

0 comments on commit e438583

Please sign in to comment.