Skip to content

Commit f3e5ed7

Browse files
authored
fix(parameters): Tokenize attribute names in DynamoDBProvider (aws-powertools#1239)
* 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
1 parent 3620bb2 commit f3e5ed7

File tree

7 files changed

+85
-23
lines changed

7 files changed

+85
-23
lines changed

Diff for: docs/utilities/parameters.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Depending on the provider you want to use, install the library and the correspon
4343

4444
=== "DynamoDBProvider"
4545
```bash
46-
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb
46+
npm install @aws-lambda-powertools/parameters @aws-sdk/client-dynamodb @aws-sdk/util-dynamodb
4747
```
4848

4949
???+ tip

Diff for: package-lock.json

+26-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/parameters/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@
6060
"aws-sdk-client-mock-jest": "^2.0.1"
6161
},
6262
"dependencies": {
63-
"@aws-sdk/util-base64": "^3.208.0"
63+
"@aws-sdk/util-base64-node": "^3.209.0"
6464
}
6565
}

Diff for: packages/parameters/src/BaseProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromBase64 } from '@aws-sdk/util-base64';
1+
import { fromBase64 } from '@aws-sdk/util-base64-node';
22
import { GetOptions } from './GetOptions';
33
import { GetMultipleOptions } from './GetMultipleOptions';
44
import { ExpirableValue } from './ExpirableValue';

Diff for: packages/parameters/src/dynamodb/DynamoDBProvider.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ class DynamoDBProvider extends BaseProvider {
4949
...(options?.sdkOptions || {}),
5050
TableName: this.tableName,
5151
Key: marshall({ [this.keyAttr]: name }),
52-
ProjectionExpression: this.valueAttr,
52+
ProjectionExpression: '#value',
53+
ExpressionAttributeNames: {
54+
'#value': this.valueAttr,
55+
}
5356
};
5457
const result = await this.client.send(new GetItemCommand(sdkOptions));
5558

@@ -63,9 +66,14 @@ class DynamoDBProvider extends BaseProvider {
6366
const sdkOptions: QueryCommandInput = {
6467
...(options?.sdkOptions || {}),
6568
TableName: this.tableName,
66-
KeyConditionExpression: `${this.keyAttr} = :key`,
69+
KeyConditionExpression: '#key = :key',
6770
ExpressionAttributeValues: marshall({ ':key': path }),
68-
ProjectionExpression: `${this.sortAttr}, ${this.valueAttr}`,
71+
ExpressionAttributeNames: {
72+
'#key': this.keyAttr,
73+
'#sk': this.sortAttr,
74+
'#value': this.valueAttr,
75+
},
76+
ProjectionExpression: '#sk, #value',
6977
};
7078
const paginationOptions: PaginationConfiguration = {
7179
client: this.client,

Diff for: packages/parameters/tests/unit/BaseProvider.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
GetParameterError,
1010
TransformParameterError
1111
} from '../../src';
12-
import { toBase64 } from '@aws-sdk/util-base64';
12+
import { toBase64 } from '@aws-sdk/util-base64-node';
1313

1414
const encoder = new TextEncoder();
1515

Diff for: packages/parameters/tests/unit/DynamoDBProvider.test.ts

+44-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ describe('Class: DynamoDBProvider', () => {
5959
Key: marshall({
6060
id: parameterName,
6161
}),
62-
ProjectionExpression: 'value',
62+
ExpressionAttributeNames: {
63+
'#value': 'value',
64+
},
65+
ProjectionExpression: '#value',
6366
});
6467
expect(parameter).toEqual(parameterValue);
6568

@@ -91,7 +94,10 @@ describe('Class: DynamoDBProvider', () => {
9194
Key: marshall({
9295
key: parameterName,
9396
}),
94-
ProjectionExpression: 'val',
97+
ExpressionAttributeNames: {
98+
'#value': 'val',
99+
},
100+
ProjectionExpression: '#value',
95101
});
96102
expect(parameter).toEqual(parameterValue);
97103

@@ -125,7 +131,10 @@ describe('Class: DynamoDBProvider', () => {
125131
Key: marshall({
126132
id: parameterName,
127133
}),
128-
ProjectionExpression: 'value',
134+
ExpressionAttributeNames: {
135+
'#value': 'value',
136+
},
137+
ProjectionExpression: '#value',
129138
ConsistentRead: true,
130139
});
131140
expect(parameter).toEqual(parameterValue);
@@ -164,7 +173,10 @@ describe('Class: DynamoDBProvider', () => {
164173
Key: marshall({
165174
id: parameterName,
166175
}),
167-
ProjectionExpression: 'value',
176+
ExpressionAttributeNames: {
177+
'#value': 'value',
178+
},
179+
ProjectionExpression: '#value',
168180
});
169181

170182
});
@@ -206,11 +218,16 @@ describe('Class: DynamoDBProvider', () => {
206218
// Assess
207219
expect(client).toReceiveCommandWith(QueryCommand, {
208220
TableName: 'test-table',
209-
KeyConditionExpression: `id = :key`,
221+
KeyConditionExpression: `#key = :key`,
210222
ExpressionAttributeValues: marshall({
211223
':key': parameterPath,
212224
}),
213-
ProjectionExpression: 'sk, value',
225+
ExpressionAttributeNames: {
226+
'#key': 'id',
227+
'#sk': 'sk',
228+
'#value': 'value'
229+
},
230+
ProjectionExpression: '#sk, #value',
214231
});
215232
expect(parameters).toEqual({
216233
a: 'parameter-a',
@@ -256,11 +273,16 @@ describe('Class: DynamoDBProvider', () => {
256273
// Assess
257274
expect(client).toReceiveCommandWith(QueryCommand, {
258275
TableName: 'test-table',
259-
KeyConditionExpression: `key = :key`,
276+
KeyConditionExpression: `#key = :key`,
260277
ExpressionAttributeValues: marshall({
261278
':key': parameterPath,
262279
}),
263-
ProjectionExpression: 'sort, val',
280+
ExpressionAttributeNames: {
281+
'#key': 'key',
282+
'#sk': 'sort',
283+
'#value': 'val'
284+
},
285+
ProjectionExpression: '#sk, #value',
264286
});
265287
expect(parameters).toEqual({
266288
a: 'parameter-a',
@@ -308,11 +330,16 @@ describe('Class: DynamoDBProvider', () => {
308330
// Assess
309331
expect(client).toReceiveCommandWith(QueryCommand, {
310332
TableName: 'test-table',
311-
KeyConditionExpression: `id = :key`,
333+
KeyConditionExpression: `#key = :key`,
312334
ExpressionAttributeValues: marshall({
313335
':key': parameterPath,
314336
}),
315-
ProjectionExpression: 'sk, value',
337+
ExpressionAttributeNames: {
338+
'#key': 'id',
339+
'#sk': 'sk',
340+
'#value': 'value'
341+
},
342+
ProjectionExpression: '#sk, #value',
316343
ConsistentRead: true,
317344
});
318345
expect(parameters).toEqual({
@@ -419,11 +446,16 @@ describe('Class: DynamoDBProvider', () => {
419446
// Assess
420447
expect(client).toReceiveCommandWith(QueryCommand, {
421448
TableName: 'test-table',
422-
KeyConditionExpression: `id = :key`,
449+
KeyConditionExpression: `#key = :key`,
423450
ExpressionAttributeValues: marshall({
424451
':key': parameterPath,
425452
}),
426-
ProjectionExpression: 'sk, value',
453+
ExpressionAttributeNames: {
454+
'#key': 'id',
455+
'#sk': 'sk',
456+
'#value': 'value'
457+
},
458+
ProjectionExpression: '#sk, #value',
427459
ConsistentRead: true,
428460
Limit: 10,
429461
});

0 commit comments

Comments
 (0)