Created
September 29, 2022 08:09
-
-
Save MikeBild/5b8f46fff89ae32379ed9855fb55e217 to your computer and use it in GitHub Desktop.
DynamoDB single table helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DynamoDB } from 'aws-sdk'; | |
export interface Entity { | |
id: string; | |
} | |
export default (tableName: string) => { | |
const ddb = new DynamoDB(); | |
return (dataType: string) => { | |
return { | |
list: <T>() => list<T>(tableName, dataType), | |
get: <T>(id: string) => get<T>(id, tableName, dataType), | |
put: <T extends Entity>(item: T) => put<T>(item, tableName, dataType), | |
remove: (id: string) => remove(id, tableName, dataType) | |
}; | |
}; | |
async function list<T>( | |
tableName: string, | |
dataType: string, | |
items: T[] = [], | |
startKey?: any | |
): Promise<T[]> { | |
const result = await ddb | |
.scan({ | |
TableName: tableName, | |
ConsistentRead: true, | |
ExclusiveStartKey: startKey, | |
FilterExpression: `#datatype = :datatype`, | |
ExpressionAttributeValues: { | |
':datatype': { S: dataType } | |
}, | |
ExpressionAttributeNames: { | |
'#datatype': 'datatype' | |
} | |
}) | |
.promise(); | |
if (result.Items) { | |
items = [...items, ...result.Items?.map((x) => DynamoDB.Converter.unmarshall(x) as T)]; | |
} | |
if (result.LastEvaluatedKey) { | |
return await list(tableName, dataType, items, result.LastEvaluatedKey); | |
} | |
return items; | |
} | |
async function get<T>(id: string, tableName: string, dataType: string): Promise<T | undefined> { | |
const result = await ddb | |
.getItem({ | |
TableName: tableName, | |
ConsistentRead: true, | |
Key: DynamoDB.Converter.marshall({ | |
id, | |
datatype: dataType | |
}) | |
}) | |
.promise(); | |
return result.Item ? (DynamoDB.Converter.unmarshall(result.Item) as T) : undefined; | |
} | |
async function put<T extends Entity>( | |
item: T, | |
tableName: string, | |
dataType: string | |
): Promise<void> { | |
const result = await ddb | |
.putItem({ | |
TableName: tableName, | |
Item: DynamoDB.Converter.marshall({ ...item, datatype: dataType }) | |
}) | |
.promise(); | |
} | |
async function remove(id: string, tableName: string, dataType: string): Promise<void> { | |
await ddb | |
.deleteItem({ | |
TableName: tableName, | |
Key: DynamoDB.Converter.marshall({ id, datatype: dataType }) | |
}) | |
.promise(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment