Skip to content

Instantly share code, notes, and snippets.

import { Prisma } from '@prisma/client'
/**
** USAGE **
const withTx = async (fn: () => Promise<void>)): Promise<void> => {
// get the tx handle
const tx = await prisma.$begin()
// if fn throws an error, the tx will be rolled back automatically
await fn()
@mostlylikeable
mostlylikeable / freeze-time.ts
Created December 1, 2022 15:52
Jest Freeze Time/Date
const freezeTime = (): Date => {
const now = new Date();
jest.useFakeTimers();
jest.setSystemTime(now);
return now;
};
const unfreezeTime = () => {
jest.useRealTimers();
};
@mostlylikeable
mostlylikeable / pick-from-array-of-fields.ts
Created November 30, 2022 16:52
Pick / Omit from an array of field names
type Foo = {
bar: string;
baz: string;
qux: string;
};
// needs to be 'const' / immutable, otherwise ts doesn't know set of values
const includeFields = ['bar', 'baz'] as const;
// dynamic type to create a union of array values
@mostlylikeable
mostlylikeable / dynamo-example.ts
Created August 18, 2022 02:53
DynamoDb TypeScript Example
import { DynamoDB } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
const tableName = 'discography';
const artistAlbumsIndex = 'artist-albums';
const table = {
TableName: tableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'pk', AttributeType: 'S' },
@mostlylikeable
mostlylikeable / kotlin-mascot-ascii.txt
Last active April 27, 2022 13:59
Kotlin Mascot ASCII
# https://kotlinlang.org/docs/kotlin-mascot.html
,_ _
.H$H$L ╭H$$.
|HHHHHHL, ╭$HHHH:
|HHHHHHHH$>v$HHHHHH:
|HHHHHH .**. .**.`?:
|HHHHHH | | | | ?:
,?H|HHHHHH.`^^' `^^'.?:
.$H` "*HHHHHH$$$$$HHHHHH╯
@mostlylikeable
mostlylikeable / ts_monorepo.md
Last active April 27, 2022 14:12
typescript monorepo setup with yarn workspaces

Project Setup

Initial setup

# yarn 2
npm install -g yarn
yarn set version berry && yarn set version latest

Keybase proof

I hereby claim:

  • I am mostlylikeable on github.
  • I am mostlylikeable (https://keybase.io/mostlylikeable) on keybase.
  • I have a public key whose fingerprint is C217 F90F 1D51 FA20 97D8 5177 453A 7829 F617 92B8

To claim this, I am signing this object:

@mostlylikeable
mostlylikeable / go_tpl_random.go
Created March 24, 2021 05:38
Go Template Randomness
// Comment
`{{/* TODO: add more useful things */}}`
// Ternary var def with or
// $somevar := (.x ? .x : "")
`{{ $somevar := or .x "" }}`
// Ternary var def with and
// $somevar := (.x ? "" : .x)
`{{ $somevar := and .x "" }}`
@mostlylikeable
mostlylikeable / git-migrate-down.sh
Created March 8, 2021 22:27
Pull down things created with git-migrate-up.sh
#!/usr/bin/env bash
# git-migrate-down.sh
function _gm_not_dry_run() {
return 1 # set to 0 to run
}
# pulls all stashes down, re-stashes, and deletes local branch.
# - can uncomment line to also delete remote tmp stash branch
function pull_stashes() {
@mostlylikeable
mostlylikeable / git-migrate-up.sh
Created March 6, 2021 03:40
Push all the things from local to remote fork
#!/bin/bash
# script for stashing and pushing all changes, stashes, local branches to remote/fork (origin)
# so they can be pulled down on another machine.
function _gm_not_dry_run() {
return 1 # set to 0 to run
}
# recursively stash any pending changes for all subdirs where this is run
function rstash() {