Skip to content

Instantly share code, notes, and snippets.

Avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / redis.ts
Created Aug 23, 2022
Connecting to AWS Elasticache (with ioredis + Bull)
View redis.ts
import Redis from 'ioredis';
const requiresTLS = process.env.NODE_ENV === 'production';
const client = new Redis({
host: process.env.REDIS_HOST as string,
password: process.env.REDIS_PASSWORD as string,
port: parseInt(process.env.REDIS_PORT, 10),
// {} prop enables TLS (typical for production, not while developing locally)
@tomfa
tomfa / code.js
Created Jan 12, 2022
Connecting to multiple databases with Prisma
View code.js
/*
* Source: https://github.com/prisma/prisma/issues/2443#issuecomment-630679118
* Client typings is generated with the cli commands below:
*
* prisma generate --schema prisma/schema1.prisma
* prisma generate --schema prisma/schema2.prisma
*/
import { PrismaClient as PrismaClient1 } from '../prisma/client1'
import { PrismaClient as PrismaClient2 } from '../prisma/client2'
View ImageIcon.stories.tsx
import React from 'react';
import { Meta } from '@storybook/react';
import Component from './ImageIcon';
export default {
title: 'components/ImageIcon/ImageIcon',
component: Component,
} as Meta;
const Template = (props: React.ComponentProps<typeof Component>) => <Component {...props} />;
@tomfa
tomfa / cache.ts
Last active Dec 29, 2021
Caching in Node with Redis and/or in-memory storage + Browser with localStorage
View cache.ts
import { ICache } from './types';
import { MemoryClient } from './memoryClient';
import { RedisClient } from './redisClient';
export class CacheClient implements ICache {
private client: ICache;
constructor({ redisUrl }: { redisUrl?: string }) {
if (redisUrl) {
this.client = new RedisClient({ redisUrl });
@tomfa
tomfa / create-signed-urls.ts
Last active Dec 20, 2021
Upload to S3 from Typescript
View create-signed-urls.ts
// Demo of creating upload urls to AWS S3.
// See github.com/tomfa/nextjs-s3-upload for a complete app
import {
S3Client,
PutObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({
@tomfa
tomfa / types.ts
Last active Nov 23, 2021
LogStructure.ts
View types.ts
type ApplicationLog = {
level: 'debug' | 'info' | 'warning' | 'danger' | 'critical',
message: string,
createdAt: number,
userId?: string,
action?: string, // Representing an business metric, e.g. SIGNUP
monetaryValue?: number,
requestId?: string,
// Common fields depending on your application use case
@tomfa
tomfa / bigquery_import.py
Last active Oct 21, 2021
Demo on how to upload to BigQuery with python3. See https://notes.webutvikling.org/starting-bigquery/
View bigquery_import.py
# 1. install dependencies
# 2. Set service account json in code
# or with env var "SA_ACCOUNT"
# 3. Run this file to import test data:
# "python3 bigquery_import.py"
import os
import json
from google.cloud import bigquery
@tomfa
tomfa / deploy.yml
Created Oct 20, 2021
Github Action: depend on test to succeed for deployment
View deploy.yml
# place this file in .github/workflows/ folder in repo
#
# This will run tests on push to master branch, then
# deploy if test succeeds
#
# This is controlled by the job "deploy" value "needs: test"
name: Deploy
on:
View .github_slash_workflows_slash_release.sdk
name: Release SDK
on:
push:
branches:
- master
jobs:
lint:
@tomfa
tomfa / yarn.npm.install.sh
Created May 21, 2021
Bash script to start any node project.
View yarn.npm.install.sh
install() {
if test -f "package.json"; then
if test -f "package-lock.json"; then
npm install
elif test -f "yarn.lock"; then
yarn
else
echo "warning: no lock file"
yarn
fi