Skip to content

Instantly share code, notes, and snippets.

View tyrauber's full-sized avatar

Ty Rauber tyrauber

View GitHub Profile
@tyrauber
tyrauber / gist:dd16f76e78f7cc33386054051a00888f
Created June 28, 2024 20:42
expo-location background tracking with WhenInUse
// Contrary to what the expo-location documentation says:
// > To use Background Location methods, the following requirements apply:
// > Location permissions must be granted. On iOS it must be granted with Always option.
// It is possible to use expo-location to follow location updates in the background or from the lock screen, with the WhenInIUse permission.
// The trick is to only request foreground permissions, but use `startLocationUpdatesAsync` and `expo-task-manager`.
// This context demonstrates how:
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
import React, { useState, useEffect } from 'react';
@tyrauber
tyrauber / middleware.ts
Created October 4, 2023 18:47
Koa / Express Shared Typescript Middleware
import Express, { Application, Request, Response, NextFunction } from 'express';
import Koa, { Context, Next } from 'koa';
const middleware = async (req: Request | Context['request'], res: Response | Context['response'], next: NextFunction) => {
console.log('Time:', Date.now());
await next();
}
interface Options {}
@tyrauber
tyrauber / sst-shared-bucket
Created April 13, 2023 15:44
SST Shared Bucket and Cloudfront Distribution across Stack
import { Api, Bucket, NextjsSite, Function, StackContext } from "sst/constructs";
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
import * as cloudfrontOrigins from "aws-cdk-lib/aws-cloudfront-origins";
export default function Default({ stack }: StackContext) {
const bucket = new Bucket(stack, "Bucket", {
cors: true,
cdk: {
bucket: {
@tyrauber
tyrauber / daylight maps buildscript
Last active November 3, 2023 15:42
Builds Daylight Maps v1.33
# nohup ./daylight.sh 1.33 >> daylight.log 2>&1 &
(
echo "Getting daylight $1 planet file"
wget -c --quiet https://daylight-map-distribution.s3.amazonaws.com/release/v$1/planet-v$1.osm.pbf
) &\
(
(
echo "Getting daylight $1 buildings file"
wget -c --quiet https://daylight-map-distribution.s3.amazonaws.com/release/v$1/ml-buildings-v$1.osm.pbf
) &\
@tyrauber
tyrauber / numPadEnc
Created January 19, 2021 16:49
numPadEnc && numPadDec : Numeric Compression in Postgres
CREATE OR REPLACE FUNCTION numPadEnc(
IN source NUMERIC DEFAULT (extract(epoch from now())*1000000)::bigint,
IN prefix TEXT DEFAULT '',
IN pad TEXT DEFAULT '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
IN seed INTEGER DEFAULT floor(random() * 10 + 1)::int
) RETURNS TEXT AS $$
DECLARE
calc TEXT := source::text;
output TEXT := prefix;
chr TEXT := '';
@tyrauber
tyrauber / webpack.config.js
Created May 12, 2020 19:00
Serverless Vue Webpack 4 Config
// ******************************
// Serverless Vue Webpack Config
// ******************************
const Path = require('path')
const TerserJSPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
@tyrauber
tyrauber / create_triggers
Created November 27, 2018 23:28 — forked from colophonemes/create_triggers
Postgres TRIGGER to call NOTIFY with a JSON payload
CREATE TRIGGER person_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
'email',
'username'
);
CREATE TRIGGER income_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
@tyrauber
tyrauber / gist:ed4e0f6053d49608bee7
Created March 4, 2016 21:33
HSTORE Model View Statistics
Let's say you got a Job model and you want to track index_count, show_count, etc.
Why not use Hstore?
Add a migration:
class AddHstoreCountsOnJobs < ActiveRecord::Migration
def change
add_column :jobs, :index_count, :hstore, default: {}
add_column :jobs, :show_count, :hstore, default: {}
@tyrauber
tyrauber / gist:5de42bf4f66c4084b99e
Created September 16, 2015 23:18
HTML5 Safari Form Validation Polyfill
validate_form = ->
$('form').submit (event) ->
$.each $(this).find('input'), (i,el) ->
validate_field(el);
$(el).on 'blur', -> validate_field(el) ;
window.validate_form = validate_form
validate_field = (el) ->
if ($(el)[0].checkValidity())
@tyrauber
tyrauber / Postgres time_ago_in_words(date) function
Created October 27, 2014 22:43
Postgres time_ago_in_words(date) function
CREATE OR REPLACE FUNCTION time_ago_in_words(timestamp with time zone)
RETURNS text
LANGUAGE SQL
AS $$
SELECT CASE
WHEN date_part('year', age(current_timestamp, $1)) = 1 THEN concat(date_part('year', age(current_timestamp, $1)), ' year ago')
WHEN date_part('year', age(current_timestamp, $1)) > 1 THEN concat(date_part('year', age(current_timestamp, $1)), ' years ago')
WHEN date_part('month', age(current_timestamp, $1)) = 1 THEN concat(date_part('month', age(current_timestamp, $1)), ' month ago')
WHEN date_part('month', age(current_timestamp, $1)) > 1 THEN concat(date_part('month', age(current_timestamp, $1)), ' months ago')
WHEN date_part('day', age(current_timestamp, $1)) = 1 THEN concat(date_part('day', age(current_timestamp, $1)), ' day ago')