Skip to content

Instantly share code, notes, and snippets.

Avatar
🦊
Being foxy

Andy Ingram AndrewIngram

🦊
Being foxy
View GitHub Profile
@AndrewIngram
AndrewIngram / usage.py
Last active Jun 30, 2022
Python Entity or ID Pattern
View usage.py
def my_fun(user_or_id: UserOrId):
user = get_user(user_or_id)
# Do something with user
# These both do the same thing (but the first avoids the extra db hit)
my_fun(user)
my_fun(user.id)
@AndrewIngram
AndrewIngram / session.js
Created Feb 18, 2022
next-runtime session middleware
View session.js
import { json } from "next-runtime";
import jwt from "jsonwebtoken";
const SESSION_KEY = "some-secret";
export function readSession(cookies) {
const sessionCookie = cookies.get("session");
let data = {};
@AndrewIngram
AndrewIngram / utils.ts
Created Feb 2, 2022
Apply bitmask and shift
View utils.ts
/*
* Given an input and a mask, will return just part we care about
*
* e.g.
* input: 0b11101111
* mask: 0b00110000
* returns: 0b10
*/
function maskedValue(input: number, mask: number): number {
return (input & mask) >> Math.log2(mask & -mask);
View example.js
import { v5 as uuidv5, NIL } from 'uuid';
function stableUuid(namespace, name) {
return uuidv5(name, uuidv5(namespace, NIL))
}
// > stableUuid('Some namespace', 'some name')
// '019761ac-2321-5ae0-9086-70c9557bcc58'
// > stableUuid('Some namespace', 'some name')
// '019761ac-2321-5ae0-9086-70c9557bcc58'
@AndrewIngram
AndrewIngram / formData.js
Created Jan 18, 2022
Request/Response form handling with Next.js
View formData.js
import formidable from "formidable";
export default function formData(request) {
const form = formidable({ multiples: true });
return new Promise((resolve, reject) => {
form.parse(request, (err, fields, files) => {
if (err) {
reject(err);
return;
View gradent.css
.container {
background-color: hsl(346deg, 83%, 51%);
background-image: linear-gradient(
45deg,
hsla(240deg, 100%, 20%, 1) 0%,
hsla(240deg, 100%, 20%, 0) 50%
),
linear-gradient(
45deg,
hsla(55deg, 100%, 50%, 0) 50%,
@AndrewIngram
AndrewIngram / utils.py
Created Dec 10, 2021
Django on_save callback
View utils.py
import logging
from django.db import models, transaction
from django.db.models.signals import post_save
logger = logging.getLogger(__name__)
def on_save(obj: models.Model, callback):
"""
Attaches a one-off callback that's triggered after a Django model instance is
@AndrewIngram
AndrewIngram / example.sql
Last active Oct 28, 2021
Batch pagination
View example.sql
-- This is one technique for batching up the *first* page of results when doing pagination.
-- Potentially useful when using GraphQL and Dataloader.
--
-- In this example, we return the first 10 books whose author_id is 1,2,3,4 or 5 and genre
-- is "biography", ordered by "title"
--
-- For cursor-based (keyset) pagination, this technique won't work for anything after the
-- first "page", because the where clause needs to be the same for every entry in the
-- batch, which means you can't use different cursors. In practice, there isn't usually a
-- need to batch up subsequent pages, because your "next page" GQL queries will typically
View ProfileScreen.tsx
import * as React from "react";
import { graphql, usePreloadedQuery } from "react-relay";
import { Button, Heading, Stack, Text } from "native-base";
import { ProfileScreenQuery } from "~/__generated__/ProfileScreenQuery.graphql";
import useLogout from "~/hooks/useLogout";
import { TabParamListProps } from "~/navigation-types";
import withData, { IScreenContainerProps } from "~/utils/withData";
@AndrewIngram
AndrewIngram / 01_connections.py
Last active Dec 10, 2021
Strawberry Generic Connections (Python 3.10 syntax)
View 01_connections.py
from typing import Generic, TypeVar
import strawberry
ConnectionNode = TypeVar("ConnectionNode")
@strawberry.type
class Edge(Generic[ConnectionNode]):
node: ConnectionNode