Skip to content

Commit e329884

Browse files
committed
#24 remove dynamo attributes from API responses
1 parent 1a49f6e commit e329884

1 file changed

Lines changed: 52 additions & 4 deletions

File tree

serverless/lib/StorageClient.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@ import {
1717
} from '../../shared/types';
1818
import { NameSearchData } from '../../shared/types/Search';
1919

20+
// DynamoDB-specific attributes that should be removed from API responses
21+
const DYNAMO_ATTRIBUTES = ['PK', 'SK', 'ttl', 'GSI1PK', 'GSI1SK'];
22+
23+
/**
24+
* Removes DynamoDB-specific attributes from an object or array of objects
25+
* @param data The data to clean
26+
* @returns Cleaned data without DynamoDB attributes
27+
*/
28+
function removeDynamoAttributes<T>(data: T): T {
29+
if (!data) {
30+
return data;
31+
}
32+
33+
if (Array.isArray(data)) {
34+
return data.map(removeDynamoAttributes) as unknown as T;
35+
}
36+
37+
if (typeof data === 'object' && data !== null) {
38+
const cleanedObject = { ...data as object } as any;
39+
40+
// Remove DynamoDB attributes
41+
DYNAMO_ATTRIBUTES.forEach(attr => {
42+
if (attr in cleanedObject) {
43+
delete cleanedObject[attr];
44+
}
45+
});
46+
47+
// Recursively clean nested objects and arrays
48+
Object.keys(cleanedObject).forEach(key => {
49+
if (typeof cleanedObject[key] === 'object' && cleanedObject[key] !== null) {
50+
cleanedObject[key] = removeDynamoAttributes(cleanedObject[key]);
51+
}
52+
});
53+
54+
return cleanedObject;
55+
}
56+
57+
return data;
58+
}
59+
2060
export interface DynamoCompositeKey {
2161
PK: string;
2262
SK: string;
@@ -114,7 +154,7 @@ export const BatchHelper = {
114154
// Find the original key for this item
115155
const key = keys.find(k => k.PK === item.PK && k.SK === item.SK);
116156
if (key) {
117-
resultMap.set(key, item as T);
157+
resultMap.set(key, removeDynamoAttributes(item as T));
118158
}
119159
});
120160
}
@@ -137,7 +177,11 @@ async function get<T>(key: DynamoCompositeKey): Promise<T | null> {
137177
})
138178
);
139179

140-
return (result.Item as T) || null;
180+
if (!result.Item) {
181+
return null;
182+
}
183+
184+
return removeDynamoAttributes(result.Item as T);
141185
}
142186

143187
/**
@@ -146,14 +190,18 @@ async function get<T>(key: DynamoCompositeKey): Promise<T | null> {
146190
* @param item The item data to save (without PK and SK)
147191
* @returns Promise that resolves when the item is saved
148192
*/
149-
async function save<T>(key: DynamoCompositeKey, item: T): Promise<void> {
193+
async function save<T>(
194+
key: DynamoCompositeKey,
195+
item: T,
196+
_options?: { removeUndefinedValues?: boolean } // Kept for backward compatibility but ignored
197+
): Promise<void> {
150198
await dynamoDb.send(
151199
new PutCommand({
152200
TableName: TABLE_NAME,
153201
Item: {
154202
...key,
155203
...item,
156-
},
204+
}
157205
})
158206
);
159207
}

0 commit comments

Comments
 (0)