Skip to content

Instantly share code, notes, and snippets.

@mtimbs
mtimbs / dynamicUpdate.ts
Last active October 5, 2021 02:10
Recursive Dynamic Update DynamoDB (WIP)
import { NativeAttributeValue } from '@aws-sdk/util-dynamodb/src/models';
import { AttributeValue } from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
interface DynamoUpdateParams {
UpdateExpression: string;
ExpressionAttributeNames: Record<string, string>;
ExpressionAttributeValues: Record<string, AttributeValue>;
}
@mtimbs
mtimbs / example-coupling-todos.vue
Last active March 17, 2021 13:27
Example of data coupling in Vue component
// TODO's Index
<template>
<ToDo v-for="todo in todos" :data="todo"/>
</template>
<script>
export default {
data() {
return {
todos: []
@mtimbs
mtimbs / cdk.json
Created February 23, 2021 10:59
CDK template - cdk.json
{
"app": "npx ts-node --prefer-ts-exts bin/example-cdk.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"@aws-cdk/core:stackRelativeExports": "true",
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
"@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
"@aws-cdk/aws-kms:defaultKeyPolicies": true,
"@aws-cdk/aws-s3:grantWriteWithoutAcl": true
@mtimbs
mtimbs / tsconfig.json
Last active April 28, 2024 06:15
basic default tsconfig for use in TypeScript projects
{
"compilerOptions": {
// project options
"lib": [
"ESNext",
"dom"
], // specifies which default set of type definitions to use ("DOM", "ES6", etc)
"outDir": "lib", // .js (as well as .d.ts, .js.map, etc.) files will be emitted into this directory.,
"removeComments": true, // Strips all comments from TypeScript files when converting into JavaScript- you rarely read compiled code so this saves space
"target": "ES6", // Target environment. Most modern browsers support ES6, but you may want to set it to newer or older. (defaults to ES3)
function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {
return Object.hasOwnProperty.call(obj, prop)
}
interface DynamoUpdateParams {
UpdateExpression: string;
ExpressionAttributeNames: Record<string, string>;
ExpressionAttributeValues: Record<string, unknown>;
}
export const DynamicUpdateExpressionFromObject = (map: Record<string, unknown>): DynamoUpdateParams => {
const ExpressionAttributeNames: Record<string, string> = Object.keys(map)
.reduce((acc, curr) => ({ ...acc, ...{ [`#${curr}`]: curr } }), {});
const UpdateExpression: string = Object.values(ExpressionAttributeNames)
@mtimbs
mtimbs / validateIp.ts
Last active August 29, 2020 12:43
IP4 validation
// Using Loops (and Regex)
const validateIP = ip => {
const regex = /^\d+$/;
const s = IP.split('.');
if(s.length !== 4) return false;
for(let i = 0; i < s.length; i++) {
if(!regex.test(s[i])) return false;
if(s[i].length > 1 && s[i].startsWith('0')) return false;
@mtimbs
mtimbs / Dockerfile.pipeline-node
Last active April 22, 2020 13:47
Example base image for Gitlab pipelines for node/typescript serverless projects
FROM node:12
WORKDIR /app
# install JRE for dynamo-offline (required for tests)
RUN apt-get update \
&& apt-get install -y default-jre \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
@mtimbs
mtimbs / .gitlab-ci.typescript-example.yml
Last active August 15, 2023 04:17
Example Gitlab CI config for a typescript serverless proeject
image: docker:latest
cache:
paths:
- node_modules/
stages:
- build
- precheck
- test
@mtimbs
mtimbs / SQSEventFactory.ts
Created March 11, 2020 00:32
Utility helper to generate test SQD events for integration testing lambda handlers
import sqsRecordGenerator from '@tests/utilities/sqsRecordFactory';
import { SQSEvent, SQSRecord } from 'aws-lambda';
export default (events?: SQSRecord[]): SQSEvent => ({
Records: events || [sqsRecordGenerator()],
});