Skip to content

Instantly share code, notes, and snippets.

View elnygren's full-sized avatar

el3ng elnygren

  • Helsinki, Finland
View GitHub Profile
@elnygren
elnygren / ReasonMLStory.re
Created April 25, 2019 17:22
React Storybook ReasonML
open StoryBook;
let _module = [%bs.raw "module"];
storiesOf("ReasonML Example", _module)
->addDecorator(storyFn =>
<div style={ReactDOMRe.Style.make(~textAlign="center", ~marginTop="50px", ())}>
{storyFn()}
</div>
)
@elnygren
elnygren / sql.ts
Created June 11, 2019 14:02
Knex + Mocha tests with seeding & clearing db
import * as shelljs from 'shelljs'
import db from 'path-to-knex-client'
import config from 'src/config'
/**
Mocha hooks
*/
before(async () => {
@elnygren
elnygren / I18n.re
Last active January 16, 2020 15:48
Simple ReasonML i18n internationalization example with strong static typing and JS/TS support with GenType
/** Schema for translations, with @genType for TS/JS usage */
[@genType]
type translations = {
helloMessage: string,
helloMessageWithVariable: (~name: string) => string,
};
/** English translations */
[@genType]
let translationsEN = {
@elnygren
elnygren / MyGraphql.re
Created July 30, 2019 10:01
Using ReasonApollo with a configurable wrapper
module type MyGraphQLMutation = {
module GQL: ReasonApolloTypes.Config;
include (module type of ReasonApolloMutation.Make(GQL));
};
module type MyGraphQLQuery = {
module GQL: ReasonApolloTypes.Config;
include (module type of ReasonApolloQuery.Make(GQL));
};
@elnygren
elnygren / snowflake.sql
Last active November 17, 2022 21:14
PostgreSQL generate unique sorted IDs for timestamps
-- taken from https://rob.conery.io/2014/05/28/a-better-id-generator-for-postgresql/
create schema idgen;
create sequence idgen.global_id_sequence;
CREATE OR REPLACE FUNCTION idgen.id_generator(
myts timestamp,
OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
@elnygren
elnygren / id.sql
Last active April 2, 2020 13:47
PLPGSQL Order ID Calculation
-- needed for `gen_random_bytes`
CREATE EXTENSION IF NOT EXISTS pgcrypto;
/**
Generate a unique Order ID based on current_timestamp, base32 and two bits of entropy.
If two Orders are created at the exact same millisecond, we rely on the entropy to provide uniqueness.
Usage:
> select * from generate_order_id();
@elnygren
elnygren / Assignment.md
Last active November 3, 2020 14:42
FlatMapFilter

Assignment 1

Code filter function by using Array.map and Array.flat

Example usage:

> filter([1,2,3], x => x !== 2)
[1,3]
@elnygren
elnygren / DI.py
Last active August 12, 2021 15:01
Functional Dependency Injection in TypeScript
# dataclass saves the trouble of writing __init__ implementations
# frozen=True makes the class instances immutable (so no hidden mutable state)
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User:
return User.objects.get(id=id)
@dataclass(frozen=True)
class OrgConnector:
def get_org(id: str) -> Org:
@elnygren
elnygren / DI.py
Last active August 8, 2021 13:24
Simple Dependency Injection in Python
# I'm using Django models as inspiration in this example
from mymodels import User, Org
# You could also call these Getters or DAOs etc
# The point is that they perform some kind of
# side effect like talking to a DB or an API
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User: