Skip to content

Instantly share code, notes, and snippets.

View moroz's full-sized avatar

Karol Moroz moroz

  • Senior Full Stack Developer at virtualQ GmbH
  • Bay Area, Kaohsiung, TW
  • 19:25 (UTC +08:00)
View GitHub Profile
@moroz
moroz / DESCRIPTION.md
Last active December 18, 2023 13:58
Introduction to databases in Go

Homework for 12/17

Write a program that connects to a Postgres database and stores contact information in a table.

Sample input:

$ go run .
First name: John
Last name: Smith
#!/usr/bin/env -S bash -e
if [[ "$1" = "" ]]; then
echo "Usage: ./calculate_time.sh FILE.csv"
exit 1
fi
FILE="$(realpath $1)"
if [[ ! -f "$FILE" ]]; then
@moroz
moroz / import.sh
Created July 4, 2023 08:08
Import a CSV file into postgres with dynamic paths
#!/bin/bash -e
SAMPLES_PATH="$(pwd)/samples.csv"
uname="$(uname)"
# When on Windows, you need to pass the path as c:/path/to/file
# rather than /c/path/to/file (as expected by PostgreSQL).
if [[ "$uname" != "Linux" && "$uname" != "Darwin" ]]; then
# remove the leading slash and replace the second slash with :/
@moroz
moroz / escape.ex
Created March 7, 2023 00:14
Escape non-alphanumerics in HTML strings
escape = fn str ->
str
|> Base.encode16()
|> String.replace_prefix("", "&#x")
|> String.replace_suffix("", ";")
end
String.replace(string, ~r/\W/, escape)
@moroz
moroz / clean_up.sh
Created February 9, 2023 15:51
Clean up AWS ECS task definition template using jq
#!/bin/bash
if [ "$1" = "" ]; then
echo "No input file!"
exit 1
fi
cat $1 | \
jq -S 'del(.compatibilities, .registeredAt, .registeredBy, .requiresAttributes, .revision, .status, .taskDefinitionArn)' | \
sponge $1
@moroz
moroz / empty_ecr_repo.sh
Created December 16, 2022 23:18
Empty an ECR repository
#!/bin/sh
# Implies that AWS_PROFILE and AWS_REGION are set
aws ecr list-images --repository-name $1 \
| jq -r '.imageIds[].imageTag' \
| xargs printf -- 'imageTag=%s\n' \
| xargs aws ecr batch-delete-image --repository-name $1 --image-ids
@moroz
moroz / paginatable.ex
Created November 17, 2022 22:55
Pagination behavior for scrivener_ecto
defmodule UnterEats.Paginatable do
@callback base_query() :: Ecto.Queryable.t()
@callback filter_by_params({atom(), term()}, Ecto.Queryable.t()) :: Ecto.Queryable.t()
@optional_callbacks filter_by_params: 2
alias UnterEats.Repo
def filter_and_paginate_resource(module, params) when is_atom(module) do
query = module.base_query()
@moroz
moroz / unnest_helpers.ex
Last active October 5, 2022 14:13
Flatten and unflatten a nested map
defmodule UnnestHelpers do
@doc """
Converts an arbitrarily nested map of translations to a flat map
with all key levels combined to period-separated strings.
Useful when you want to convert a nested structure like Rails
i18n YAML translation files into a flat structure, like an Excel
spreadsheet.
## Examples
@moroz
moroz / application.ex
Created September 30, 2022 16:42
Automatically migrate Ecto repo in release
defmodule Gg.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
@migrator if Mix.env() in [:prod, :staging], do: [MyApp.Migrator], else: []
use Application
@impl true
@moroz
moroz / macros.ex
Created May 14, 2022 15:17
Absinthe resolve field with batch
defmodule MyAppWeb.Api.Macros do
@moduledoc """
Not actual Absinthe middleware.
"""
use Absinthe.Schema.Notation
@doc """
Macro to reuse the common pattern of preloading associations using
a batch function using `id` as the key.