-
-
Save harryhan24/f6ab884832e6c58b95913c950f58a0cd to your computer and use it in GitHub Desktop.
This file contains 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 { | |
DynamoDBClient, | |
PutItemCommand, | |
GetItemCommand, | |
UpdateItemCommand, | |
DeleteItemCommand, | |
} from '@aws-sdk/client-dynamodb'; | |
import { | |
marshall, | |
unmarshall | |
} from '@aws-sdk/util-dynamodb'; | |
import { SUCCESS, FAILURE } from '../constants'; | |
const TABLE_NAME = 'test-table'; | |
const REGION = 'ap-southeast-1'; | |
interface Result { | |
status: string, | |
data?: any | |
} | |
// 1. Add new item to the db | |
const add = async ( | |
item: { | |
PK: string | |
SK: string | |
}) => { | |
const client = new DynamoDBClient({ region: REGION }); | |
const params = { | |
TableName: TABLE_NAME, | |
Item: marshall({ | |
...item, | |
created: new Date().toISOString() | |
}) | |
}; | |
try { | |
const command = new PutItemCommand(params); | |
const result = await client.send(command); | |
return new Promise<Result>(resolve => { | |
if (result.$metadata.httpStatusCode === 200) { | |
resolve({ | |
status: SUCCESS | |
}); | |
} else { | |
resolve({ | |
status: FAILURE | |
}); | |
} | |
}); | |
} catch (e) { | |
throw e; | |
} | |
} | |
// 2. Query item from db | |
const get = async (key: { | |
PK: string | |
SK: string | |
}) => { | |
const client = new DynamoDBClient({ region: REGION }); | |
const params = { | |
TableName: TABLE_NAME, | |
Key: marshall(key) | |
}; | |
try { | |
const command = new GetItemCommand(params); | |
const result = await client.send(command); | |
return new Promise<Result>(resolve => { | |
if (result.$metadata.httpStatusCode === 200) { | |
resolve({ | |
status: SUCCESS, | |
data: unmarshall(result.Item) | |
}); | |
} else { | |
resolve({ | |
status: FAILURE | |
}); | |
} | |
}); | |
} catch (e) { | |
throw e; | |
} | |
} | |
// 3. Update one attribute on given record | |
const updateAttibute = async ({ | |
key, | |
item | |
}: { | |
key: { | |
PK: string | |
SK: string | |
}, | |
item: { | |
[key: string]: number | string | |
} | |
}) => { | |
if (Object.keys(item).length !== 1) { | |
throw Error('Cannot update multiple arributes at once.'); | |
} | |
const client = new DynamoDBClient({ region: REGION }); | |
const params = { | |
TableName: TABLE_NAME, | |
Key: marshall(key), | |
ConditionExpression: `attribute_exists(${Object.keys(item)[0]})`, | |
UpdateExpression: 'set #a = :x', | |
ExpressionAttributeNames: { '#a': Object.keys(item)[0] }, | |
ExpressionAttributeValues: marshall({ | |
':x': Object.values(item)[0] | |
}), | |
}; | |
try { | |
const command = new UpdateItemCommand(params); | |
const result = await client.send(command); | |
return new Promise<Result>(resolve => { | |
if (result.$metadata.httpStatusCode === 200) { | |
resolve({ | |
status: SUCCESS | |
}); | |
} else { | |
resolve({ | |
status: FAILURE | |
}); | |
} | |
}); | |
} catch (e) { | |
throw e; | |
} | |
} | |
// 4. Replace old record with new item | |
const replace = async ({ | |
key, | |
item | |
}: { | |
key: { | |
PK: string | |
SK: string | |
}, | |
item: { | |
[key: string]: number | string | |
} | |
}) => { | |
const client = new DynamoDBClient({ region: REGION }); | |
const params = { | |
TableName: TABLE_NAME, | |
Item: marshall({ | |
key, | |
...item, | |
updated: new Date().toISOString() | |
}) | |
}; | |
try { | |
const command = new PutItemCommand(params); | |
const result = await client.send(command); | |
return new Promise<Result>(resolve => { | |
if (result.$metadata.httpStatusCode === 200) { | |
resolve({ | |
status: SUCCESS | |
}); | |
} else { | |
resolve({ | |
status: FAILURE | |
}); | |
} | |
}); | |
} catch (e) { | |
throw e; | |
} | |
} | |
// 5. Remove record | |
const remove = async (key: { | |
PK: string | |
SK: string | |
}) => { | |
const client = new DynamoDBClient({ region: REGION }); | |
const params = { | |
TableName: TABLE_NAME, | |
Key: marshall(key), | |
}; | |
try { | |
const command = new DeleteItemCommand(params); | |
const result = await client.send(command); | |
return new Promise<Result>(resolve => { | |
if (result.$metadata.httpStatusCode === 200) { | |
resolve({ | |
status: SUCCESS | |
}); | |
} else { | |
resolve({ | |
status: FAILURE | |
}); | |
} | |
}); | |
} catch (e) { | |
throw e; | |
} | |
} | |
export default { | |
add, | |
get, | |
updateAttibute, | |
replace, | |
remove | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment