Skip to content

Instantly share code, notes, and snippets.


Tomas Fagerbekk tomfa

View GitHub Profile
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 / code.js
Created Jan 12, 2022
Connecting to multiple databases with Prisma
View code.js
* Source:
* 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 / 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 / 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 for a complete app
import {
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({
tomfa / types.ts
Last active Nov 23, 2021
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 /
Last active Oct 21, 2021
Demo on how to upload to BigQuery with python3. See
# 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"
import os
import json
from import bigquery
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
View .github_slash_workflows_slash_release.sdk
name: Release SDK
- master
tomfa /
Created May 21, 2021
Bash script to start any node project.
install() {
if test -f "package.json"; then
if test -f "package-lock.json"; then
npm install
elif test -f "yarn.lock"; then
echo "warning: no lock file"