Skip to content

Instantly share code, notes, and snippets.

View robzhu's full-sized avatar

Robert Zhu robzhu

View GitHub Profile
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"execute-api:ManageConnections"
],
"Resource": "*"
}
export function fromArrayToSet(values: string[]) {
if (!values || values.length === 0) {
throw Error('Cannot convert empty array into a set');
}
return DynamoDB.createSet(values);
}
// credentials are stored in a .env file with the following contents:
//
// accessKeyId=
// secretAccessKey=
// region=
// tableName=
// See: https://github.com/motdotla/dotenv
require("dotenv").config();
async function deleteFriendByValue(friendName: string) {
const Key = { id: "1234" };
// Delete the value from the set. This operation is idempotent and will not
// produce an error if the value(s) are missing.
const { err, data } = await updateWithErrorWrapper({
TableName,
Key,
UpdateExpression: `DELETE friends :valuesToRemove`,
ExpressionAttributeValues: {
":valuesToRemove": DynamoDB.createSet([friendName])
// credentials are stored in a .env file with the following contents:
//
// accessKeyId=
// secretAccessKey=
// region=
// tableName=
// See: https://github.com/motdotla/dotenv
require("dotenv").config();
function toArrayfromSet(set) {
if (!set) return [];
if (Array.isArray(set)) return set;
return Array.isArray(set.values) ? set.values : [];
}
const result = await DynamoDB.get({
TableName,
Key: { id: "1234" }
}).promise();
console.log(result.Item.friends);
console.log(Array.isArray(result.Item.friends.values));
// Output:
Set {
wrapperName: 'Set',
async function deleteFriendByValue(friendName) {
const Key = { id: "1234" };
// fetch the document
let result = await DynamoDB.get({
TableName,
Key
}).promise();
// Delete the value from the set. This operation is idempotent and will not produce an error if the value(s) are missing.
import * as AWS from "aws-sdk";
const DynamoDB = new AWS.DynamoDB.DocumentClient();
DynamoDB.put({
TableName,
Item: {
id: "1234",
name: "carl",
friends: DynamoDB.createSet(["meatwad", "frylock", "shake"])
}
async function conditionalVersionRemoveFriendByValue(friendName) {
const Key = { id: "1234" };
// fetch the document
const document = (await DynamoDB.get({
TableName,
Key
}).promise()).Item;
// find the index