Skip to content

Instantly share code, notes, and snippets.

View robzhu's full-sized avatar

Robert Zhu robzhu

View GitHub Profile
async function conditionalVersionRemoveFriendByValue(friendName) {
const Key = { id: "1234" };
// fetch the document
const document = (await DynamoDB.get({
TableName,
Key
}).promise()).Item;
// find the index
let indexToRemove = document.friends.indexOf(friendName);
let version = document.version;
DynamoDB.put({ TableName, Item: {
id: "1234",
name: "carl",
friends: ["meatwad", "frylock", "shake"],
version: 1,
}
});
// helper function to return the error in a promise
async function updateWithErrorWrapper(params) {
return new Promise(resolve => {
DynamoDB.update(params, (err, data) => {
resolve({ err, data });
});
});
}
async function conditionalRemoveFriendByValue(friendName) {
{
TableName,
Key,
UpdateExpression: `REMOVE friends[${indexToRemove}]`,
ConditionExpression: `friends[${indexToRemove}] = :valueToRemove`,
ExpressionAttributeValues: {
":valueToRemove": friendName
}
}
async function removeFriendByValue(friendName: string) {
const Key = { id: "1234" };
// fetch the document
let result = await DynamoDB.get({
TableName,
Key
}).promise();
// find the index
const indexToRemove = result.Item.friends.indexOf(friendName);
if (indexToRemove === -1) {
DynamoDB.put({ TableName, Item: {
id: "1234",
name: "carl",
friends: ["meatwad", "frylock", "shake"]
}
});
@robzhu
robzhu / chat.cs
Created January 21, 2019 14:52
async stream
await foreach (string message in GetChatMessagesAsync()) {
Render(message);
}
const WebSocket = require('ws');
const readline = require('readline');
const url = process.argv[2];
const ws = new WebSocket(url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
console.log('disconnected');
@robzhu
robzhu / index.js
Created January 7, 2019 22:56
Lambda code for the echo route
const AWS = require('aws-sdk');
// apply the patch
require('./patch.js');
let send = undefined;
function init(event) {
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
@robzhu
robzhu / patch.js
Created January 7, 2019 22:37
aws-sdk patch for WebSocket API
require('aws-sdk/lib/node_loader');
var AWS = require('aws-sdk/lib/core');
var Service = AWS.Service;
var apiLoader = AWS.apiLoader;
apiLoader.services['apigatewaymanagementapi'] = {};
AWS.ApiGatewayManagementApi = Service.defineService('apigatewaymanagementapi', ['2018-11-29']);
Object.defineProperty(apiLoader.services['apigatewaymanagementapi'], '2018-11-29', {
get: function get() {
var model = {