Skip to content

Instantly share code, notes, and snippets.

View LucasVera's full-sized avatar

Lucas Vera Toro LucasVera

View GitHub Profile
@LucasVera
LucasVera / updateNestedPropDynamoDb.js
Created July 30, 2024 14:43
This gist shows a simple example of how to update a nested property using dynamodb aws sdk v3 for javascript. This targeted updates are more cost-efficient and faster than updating the whole object
const { UpdateItemCommand, DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { marshall } = require('@aws-sdk/util-dynamodb');
const { inspect } = require('util');
const client = new DynamoDBClient({ region: 'us-east-1' });
// Update an item's property which is nested in a map
const main = async () => {
try {
@LucasVera
LucasVera / getApiKey.js
Created July 9, 2024 17:10
Simple implementation of how to get an API key from aws secrets manager, which is more secure than hard-coding the API key in the code
const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');
const secretsManagerClient = new SecretsManagerClient({ region: 'us-east-1' }); // Replace with configured region in the IaC or environment
const getSecretValue = (SecretId) => {
const params = {
SecretId,
};
const command = new GetSecretValueCommand(params);
@LucasVera
LucasVera / updateItemResolver.js
Created June 18, 2024 16:50
Simple example showing a javascript appsync resolver that leverages the use of directy dynamodb integration to perform an "update item" on a dynamodb table.
export function request (ctx) {
const { args } = ctx;
const { blogPost } = args;
// Any validation logic or transformation/mapping logic
return {
operation: 'UpdateItem',
key: util.dynamodb.toMapValues({ BlogPostId: blogPost.id }),
update: {
@LucasVera
LucasVera / getUserOrders.ts
Created May 27, 2024 13:16
DynamoDb Single Table Design Query Example
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"
import { QueryCommand } from "@aws-sdk/lib-dynamodb"
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"
const client = new DynamoDBClient({})
const ddbDocClient = DynamoDBDocumentClient.from(client)
/**
* Get all orders for a user
* Using single table design, we can query the table to get all orders for a user
@LucasVera
LucasVera / retryWithJitter.ts
Last active May 16, 2024 21:38
Simple example in typescript to work with retries and jitter. It helps to mitigate the "thundering herd" problem when building a retriable function. For example, an http network call to a third party that may fail and needs to be retried for a set amount of times
const sleep = (ms: number) => new Promise(resolve => setTimeout(() => resolve('done'), ms))
type RetrierOptions<T> = {
maxRetries?: number
delay?: number
functionToRetry: () => Promise<T>
}
const withRetry = async <T>(options: RetrierOptions<T>): Promise<T> => {
const { maxRetries = 3, delay = 1000, functionToRetry } = options