Skip to content

Commit

Permalink
fix(parameters): Tokenize attribute names in DynamoDBProvider (#1239)
Browse files Browse the repository at this point in the history
* docs: fixed install command

* build: restored @aws-sdk/util-base64-node as dependency

* fix: projection names

* tests: fixed unit tests after changes in prev commit
  • Loading branch information
dreamorosi committed Jan 13, 2023
1 parent 3620bb2 commit f3e5ed7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/utilities/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Depending on the provider you want to use, install the library and the correspon

=== "DynamoDBProvider"
```bash
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb @aws-sdk/util-dynamodb
```

???+ tip
Expand Down
30 changes: 26 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/parameters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
"aws-sdk-client-mock-jest": "^2.0.1"
},
"dependencies": {
"@aws-sdk/util-base64": "^3.208.0"
"@aws-sdk/util-base64-node": "^3.209.0"
}
}
2 changes: 1 addition & 1 deletion packages/parameters/src/BaseProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromBase64 } from '@aws-sdk/util-base64';
import { fromBase64 } from '@aws-sdk/util-base64-node';
import { GetOptions } from './GetOptions';
import { GetMultipleOptions } from './GetMultipleOptions';
import { ExpirableValue } from './ExpirableValue';
Expand Down
14 changes: 11 additions & 3 deletions packages/parameters/src/dynamodb/DynamoDBProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class DynamoDBProvider extends BaseProvider {
...(options?.sdkOptions || {}),
TableName: this.tableName,
Key: marshall({ [this.keyAttr]: name }),
ProjectionExpression: this.valueAttr,
ProjectionExpression: '#value',
ExpressionAttributeNames: {
'#value': this.valueAttr,
}
};
const result = await this.client.send(new GetItemCommand(sdkOptions));

Expand All @@ -63,9 +66,14 @@ class DynamoDBProvider extends BaseProvider {
const sdkOptions: QueryCommandInput = {
...(options?.sdkOptions || {}),
TableName: this.tableName,
KeyConditionExpression: `${this.keyAttr} = :key`,
KeyConditionExpression: '#key = :key',
ExpressionAttributeValues: marshall({ ':key': path }),
ProjectionExpression: `${this.sortAttr}, ${this.valueAttr}`,
ExpressionAttributeNames: {
'#key': this.keyAttr,
'#sk': this.sortAttr,
'#value': this.valueAttr,
},
ProjectionExpression: '#sk, #value',
};
const paginationOptions: PaginationConfiguration = {
client: this.client,
Expand Down
2 changes: 1 addition & 1 deletion packages/parameters/tests/unit/BaseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GetParameterError,
TransformParameterError
} from '../../src';
import { toBase64 } from '@aws-sdk/util-base64';
import { toBase64 } from '@aws-sdk/util-base64-node';

const encoder = new TextEncoder();

Expand Down
56 changes: 44 additions & 12 deletions packages/parameters/tests/unit/DynamoDBProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
});
expect(parameter).toEqual(parameterValue);

Expand Down Expand Up @@ -91,7 +94,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
key: parameterName,
}),
ProjectionExpression: 'val',
ExpressionAttributeNames: {
'#value': 'val',
},
ProjectionExpression: '#value',
});
expect(parameter).toEqual(parameterValue);

Expand Down Expand Up @@ -125,7 +131,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
ConsistentRead: true,
});
expect(parameter).toEqual(parameterValue);
Expand Down Expand Up @@ -164,7 +173,10 @@ describe('Class: DynamoDBProvider', () => {
Key: marshall({
id: parameterName,
}),
ProjectionExpression: 'value',
ExpressionAttributeNames: {
'#value': 'value',
},
ProjectionExpression: '#value',
});

});
Expand Down Expand Up @@ -206,11 +218,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
});
expect(parameters).toEqual({
a: 'parameter-a',
Expand Down Expand Up @@ -256,11 +273,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `key = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sort, val',
ExpressionAttributeNames: {
'#key': 'key',
'#sk': 'sort',
'#value': 'val'
},
ProjectionExpression: '#sk, #value',
});
expect(parameters).toEqual({
a: 'parameter-a',
Expand Down Expand Up @@ -308,11 +330,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
ConsistentRead: true,
});
expect(parameters).toEqual({
Expand Down Expand Up @@ -419,11 +446,16 @@ describe('Class: DynamoDBProvider', () => {
// Assess
expect(client).toReceiveCommandWith(QueryCommand, {
TableName: 'test-table',
KeyConditionExpression: `id = :key`,
KeyConditionExpression: `#key = :key`,
ExpressionAttributeValues: marshall({
':key': parameterPath,
}),
ProjectionExpression: 'sk, value',
ExpressionAttributeNames: {
'#key': 'id',
'#sk': 'sk',
'#value': 'value'
},
ProjectionExpression: '#sk, #value',
ConsistentRead: true,
Limit: 10,
});
Expand Down

0 comments on commit f3e5ed7

Please sign in to comment.