Skip to content

Instantly share code, notes, and snippets.

@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(),
@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";
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 / 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
@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,