Skip to content

Commit 92838d6

Browse files
author
guillempc
committed
docs: explain the distinction between eventKeyJmesPath and dataIndexArgument.
1 parent e54c0e8 commit 92838d6

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

packages/idempotency/src/makeIdempotent.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,73 @@ const isOptionsWithDataIndexArgument = (
4444
};
4545

4646
/**
47-
* Use function wrapper to make your function idempotent.
47+
* Function wrapper to make any function idempotent.
48+
*
49+
* The `makeIdempotent` function is a higher-order function that takes another function and returns a new version of that function with idempotency behavior.
50+
* This means that if the function is called multiple times with the same input, it will return the same result without re-executing the original function logic.
51+
*
52+
* By default, the entire first argument is hashed to create the idempotency key. You can customize this behavior:
53+
* - Use {@link IdempotencyConfig.eventKeyJmesPath | `eventKeyJmesPath`} to hash only a subset of the payload
54+
* - Use {@link ItempotentFunctionOptions.dataIndexArgument | `dataIndexArgument`} to hash a different function argument
55+
*
56+
*
57+
* **Using a subset of the payload**
58+
*
4859
* @example
49-
* ```ts
50-
* // this is your processing function with an example record { transactionId: '123', foo: 'bar' }
51-
* const processRecord = (record: Record<string, unknown>): any => {
52-
* // you custom processing logic
60+
* ```typescript
61+
* import { makeIdempotent, IdempotencyConfig } from '@aws-lambda-powertools/idempotency';
62+
* import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
63+
*
64+
* const processRecord = (record: Record<string, unknown>): unknown => {
65+
* // your processing logic
5366
* return result;
5467
* };
5568
*
56-
* // we use wrapper to make processing function idempotent with DynamoDBPersistenceLayer
5769
* const processIdempotently = makeIdempotent(processRecord, {
58-
* persistenceStore: new DynamoDBPersistenceLayer()
70+
* persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'idempotency-table' }),
5971
* config: new IdempotencyConfig({
60-
* eventKeyJmesPath: 'transactionId', // this will use `transactionId` argument as idempotency payload
72+
* eventKeyJmesPath: 'transactionId', // hash only this field as idempotency key
6173
* }),
6274
* });
6375
*
64-
* export const handler = async (
65-
* _event: EventRecords,
66-
* _context: Context
67-
* ): Promise<void> => {
68-
* for (const record of _event.records) {
69-
* const result = await processIdempotently(record);
70-
* // do something with the result
76+
* export const handler = async (event: { records: Record<string, unknown>[] }) => {
77+
* for (const record of event.records) {
78+
* // use the idempotent function
79+
* const result = await processIdempotently(record);
80+
* // ... do something with the result
7181
* }
82+
* };
83+
*```
84+
*
85+
* **Using a different function argument (useful for multi-parameter functions)**
86+
*
87+
* @example
88+
* ```typescript
89+
* import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
90+
* import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
7291
*
73-
* return Promise.resolve();
92+
* const processRecord = (record: Record<string, unknown>, userId: string): unknown => {
93+
* // your processing logic
94+
* return result;
7495
* };
7596
*
97+
* const processIdempotently = makeIdempotent(processRecord, {
98+
* persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'idempotency-table' }),
99+
* dataIndexArgument: 1, // hash the userId (second argument) instead of first (record)
100+
* });
101+
*
102+
* export const handler = async (event: { records: Record<string,unknown>[]; userId: string }) => {
103+
* for (const record of event.records) {
104+
* const userId = event.userId;
105+
* // use the idempotent function
106+
* const result = await processIdempotently(record, userId);
107+
* // ... do something with the result
108+
* }
109+
* };
110+
* ```
111+
*
76112
* @param fn - the function to make idempotent
77113
* @param options - the options to configure the idempotency behavior
78-
* ```
79114
*/
80115
function makeIdempotent<Func extends AnyFunction>(
81116
fn: Func,

0 commit comments

Comments
 (0)