Skip to content

Instantly share code, notes, and snippets.

@fnimick
fnimick / drizzle-orm+0.40.0.patch
Last active February 28, 2025 18:00
drizzle patch to enable postgres.js stack traces
diff --git a/node_modules/drizzle-orm/errors.cjs b/node_modules/drizzle-orm/errors.cjs
index 5cb1230..399b75d 100644
--- a/node_modules/drizzle-orm/errors.cjs
+++ b/node_modules/drizzle-orm/errors.cjs
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
var errors_exports = {};
__export(errors_exports, {
DrizzleError: () => DrizzleError,
- TransactionRollbackError: () => TransactionRollbackError
+ TransactionRollbackError: () => TransactionRollbackError,
@fnimick
fnimick / relations-enhancements.ts
Created February 11, 2025 23:44
Drizzle relation function enhancements to support one-to-one relation name and without fields, and explicitly optional reference for non-nullable fields
/**
* A patched Drizzle relations function that enables the following:
* - defining a one-to-one relation using non-nullable columns that is still nullable via
* `config.optional`: resolves https://github.com/drizzle-team/drizzle-orm/issues/1066
* - defining a one-to-one relation without fields which includes a relation name: resolves
* https://github.com/drizzle-team/drizzle-orm/issues/3763
*
* Usage: import `relations` from this file rather than `drizzle-orm/relations` and use as normal.
* `one()` now accepts a config object where the fields and references are optional, to enable use
* with relation name, and also has a new key `optional` which when set to true marks this as an
import { Record } from "effect";
function renameKeys<T extends Record<string | symbol, unknown>, M extends Record<string, string>>(
input: T,
map: M,
): { [Property in keyof T as Property extends keyof M ? M[Property] : Property]: T[Property] } {
return Record.mapKeys(input, (key) => (key in map ? map[key as keyof M] : key)) as any;
}
function renameKey<
@fnimick
fnimick / sveltekit-effect.ts
Last active September 22, 2024 00:20
glue code for using Effect and Sveltekit together
import { FetchHttpClient, HttpClient } from "@effect/platform";
import {
error as sveltekitError,
fail as sveltekitFail,
redirect as sveltekitRedirect,
type RequestEvent,
type ServerLoadEvent,
type ActionFailure as SveltekitActionFailure,
} from "@sveltejs/kit";
import { Cause, Context, Data, Effect, Exit, Layer, ManagedRuntime, Match, pipe } from "effect";
@fnimick
fnimick / supabase-rls-drizzle-client.ts
Created September 19, 2024 20:54
RLS drizzle client that hooks into `transaction` to set variables for supabase RLS policies
// rlsPgClient is a separate database connection using a postgres user that has
// permissions only to assume 'authenticated' and 'anon' roles
const rlsDrizzleBase = drizzle(rlsPgClient, { schema });
export function createRlsDrizzle({ session }: { session: Session | undefined | null }) {
  let claims = "";
  if (session) {
    const accessToken = session.access_token;
    const parsedJwt = JSON.parse(
      Buffer.from(z.string().parse(accessToken.split(".")[1]), "base64").toString(),
#!/bin/bash
set -euo pipefail
# initial export modeled on https://workos.com/docs/migrate/aws-cognito/1-exporting-cognito-user-data
if [ $# -eq 0 ]; then
echo "Usage: $0 <user-pool-id> <region>"
exit 1
fi
if ! [ -x "$(command -v aws)" ]; then
@fnimick
fnimick / prisma-rls-clients-support-transactions.ts
Last active August 5, 2024 19:53
Prisma utilities to create RLS clients that support transactions (unlike the official recommendation / other lib which ignores them silently)
// Necessary due to https://github.com/prisma/prisma/issues/20678
// Modeled on prisma-extension-enable-supabase-row-level-security:
// https://github.com/dthyresson/prisma-extension-supabase-rls
// Modified for transaction support using two clients, one for non-transactional operations, one for
// transactional operations. inefficient, but seems to work. Please test yourself in your own
// environment! I am not responsible for any transactional behavior failures.
// I mostly used this by creating two clients and using a given one explicitly based on whether or
@fnimick
fnimick / supabase_profile_sync.sql
Last active February 5, 2025 18:32
Create a `public.profile` table and keep it in sync with supabase `auth.users` for selected fields in both directions.
/**
* USERS
* Note: This table contains user data. Users should only be able to view and update their own data.
* Some data is synced back and forth to `auth.users` as described below.
*
* `full_name`: synced in both directions
* `email`: synced from user metadata to profile only
* `avatar_url`: synced from user metadata to profile only
* `terms_accepted_at`: synced from profile to user metadata only
*/
type ChurchInput<T> = (t: T) => T;
type ChurchNumeral<T> = (fn: ChurchInput<T>) => (t: T) => T;
function churchToInt(c: ChurchNumeral<number>) {
return c((x: number) => x + 1)(0);
}
function ZERO<T>(f: any) {
return (x: T) => x;
}
@fnimick
fnimick / README.md
Last active April 15, 2022 22:00 — forked from itsMapleLeaf/README.md
Typed remix helpers

Typed helpers for low-boilerplate type inference with Remix data.

  • I suffix them with *Typed so I don't accidentally import the core remix helpers.
  • This doesn't support regular Response objects to prevent accidentally using them with the typed helpers.