Skip to content

Instantly share code, notes, and snippets.

@monacoremo
monacoremo / dbstructure.sh
Last active May 1, 2020 14:57
DbStructure JSON SQL query
test/with_tmp_db psql -f dbstructure.sql | sed -n '4p' | jq . > out.json
CREATE EXTENSION pgcrypto;
CREATE OR REPLACE FUNCTION totp(key BYTEA, clock_offset INT DEFAULT 0) RETURNS INT AS $$
DECLARE
c BYTEA := '\x000000000' || TO_HEX(FLOOR(EXTRACT(EPOCH FROM NOW()) / 30)::INT + clock_offset);
mac BYTEA := HMAC(c, key, 'sha1');
trunc_offset INT := GET_BYTE(mac, 19) % 16;
result INT := SUBSTRING(SET_BIT(SUBSTRING(mac FROM 1 + trunc_offset FOR 4), 7, 0)::TEXT, 2)::BIT(32)::INT % 1000000;
BEGIN
RETURN result;
END;
@monacoremo
monacoremo / foundation.nix
Last active May 22, 2022 06:09
(Ab-)using Nix for full stack development environments
let
pkgs =
let
pinnedPkgs =
builtins.fetchGit {
name = "nixos-unstable-2019-12-05";
url = https://github.com/nixos/nixpkgs/;
rev = "cc6cf0a96a627e678ffc996a8f9d1416200d6c81";
};
in
@monacoremo
monacoremo / sumtype.sql
Created January 9, 2020 19:50
Sum types / ADTs in Postgres
/* Experiment on how to represent sum types in Postgres.
In this example, we try to represent the following sum type:
type Animal
= Dog { name : String, age : Integer }
| Bird { song : String }
*/