Skip to content

Commit 537b467

Browse files
committed
docs: added DynamoDBProvider
1 parent 42f2bc0 commit 537b467

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

docs/utilities/parameters.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,117 @@ export const handler = async (_event, _context): Promise<void> => {
236236
const value = secrets.getSecret('my-secret');
237237
```
238238
239+
#### DynamoDBProvider
240+
241+
The DynamoDB Provider does not have any high-level functions, as it needs to know the name of the DynamoDB table containing the parameters.
242+
243+
**DynamoDB table structure for single parameters**
244+
245+
For single parameters, you must use `id` as the [partition key](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey) for that table.
246+
247+
???+ example
248+
249+
DynamoDB table with `id` partition key and `value` as attribute
250+
251+
| id | value |
252+
| ------------ | -------- |
253+
| my-parameter | my-value |
254+
255+
With this table, `dynamodbProvider.get('my-param')` will return `my-value`.
256+
257+
=== "index.ts"
258+
```typescript hl_lines="3 7"
259+
import { DynamoDBProvider } from '@aws-lambda-powertools/parameters';
260+
261+
const dynamodbProvider = new DynamoDBProvider({ tableName: 'my-table' });
262+
263+
export const handler = async (_event, _context): Promise<void> => {
264+
// Retrieve a value from DynamoDB
265+
const value = dynamodbProvider.get('my-parameter');
266+
```
267+
268+
=== "DynamoDB Local example"
269+
You can initialize the DynamoDB provider pointing to [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) using the `endpoint` field in the `clientConfig` parameter:
270+
271+
```typescript hl_lines="3"
272+
import { DynamoDBProvider } from '@aws-lambda-powertools/parameters';
273+
274+
const dynamodbProvider = new DynamoDBProvider({
275+
tableName: 'my-table',
276+
clientConfig: {
277+
endpoint: 'http://localhost:8000'
278+
}
279+
});
280+
```
281+
282+
**DynamoDB table structure for multiple values parameters**
283+
284+
You can retrieve multiple parameters sharing the same `id` by having a sort key named `sk`.
285+
286+
???+ example
287+
288+
DynamoDB table with `id` primary key, `sk` as sort key` and `value` as attribute
289+
290+
| id | sk | value |
291+
| ----------- | ------- | ---------- |
292+
| my-hash-key | param-a | my-value-a |
293+
| my-hash-key | param-b | my-value-b |
294+
| my-hash-key | param-c | my-value-c |
295+
296+
With this table, `dynamodbProvider.getMultiple('my-hash-key')` will return a dictionary response in the shape of `sk:value`.
297+
298+
=== "index.ts"
299+
```typescript hl_lines="3 8"
300+
import { DynamoDBProvider } from '@aws-lambda-powertools/parameters';
301+
302+
const dynamodbProvider = new DynamoDBProvider({ tableName: 'my-table' });
303+
304+
export const handler = async (_event, _context): Promise<void> => {
305+
// Retrieve multiple values by performing a Query on the DynamoDB table
306+
// This returns a dict with the sort key attribute as dict key.
307+
const parameters = dynamodbProvider.getMultiple('my-hash-key');
308+
for (const [ key, value ] of Object.entries(values)) {
309+
// key: param-a
310+
// value: my-value-a
311+
console.log(`${key}: ${value}`);
312+
}
313+
```
314+
315+
=== "parameters dict response"
316+
317+
```json
318+
{
319+
"param-a": "my-value-a",
320+
"param-b": "my-value-b",
321+
"param-c": "my-value-c"
322+
}
323+
```
324+
325+
**Customizing DynamoDBProvider**
326+
327+
DynamoDB provider can be customized at initialization to match your table structure:
328+
329+
| Parameter | Mandatory | Default | Description |
330+
| ------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------- |
331+
| **tableName** | **Yes** | *(N/A)* | Name of the DynamoDB table containing the parameter values. |
332+
| **keyAttr** | No | `id` | Hash key for the DynamoDB table. |
333+
| **sortAttr** | No | `sk` | Range key for the DynamoDB table. You don't need to set this if you don't use the `getMultiple()` method. |
334+
| **valueAttr** | No | `value` | Name of the attribute containing the parameter value. |
335+
336+
```typescript hl_lines="3-8" title="Customizing DynamoDBProvider to suit your table design"
337+
import { DynamoDBProvider } from '@aws-lambda-powertools/parameters';
338+
339+
const dynamodbProvider = new DynamoDBProvider(
340+
tableName='my-table',
341+
keyAttr='MyKeyAttr',
342+
sortAttr='MySortAttr',
343+
valueAttr='MyvalueAttr'
344+
)
345+
346+
export const handler = async (_event, _context): Promise<void> => {
347+
const value = dynamodbProvider.get('my-parameter')
348+
```
349+
239350
### Deserializing values with transform parameter
240351

241352
For parameters stored in JSON or Base64 format, you can use the `transform` argument for deserialization.

0 commit comments

Comments
 (0)