Skip to content

Instantly share code, notes, and snippets.

@zxt-tzx
zxt-tzx / vector.ts
Created February 27, 2025 07:26
Drizzle helper functions: typesafe cosineDistance
import type { SQL } from "drizzle-orm";
import { sql } from "drizzle-orm";
import type { AnyColumn } from "drizzle-orm/column";
import type { TypedQueryBuilder } from "drizzle-orm/query-builders/query-builder";
import type { SQLWrapper } from "drizzle-orm/sql";
function toSql(value: number[] | string[]): string {
return JSON.stringify(value);
}
@zxt-tzx
zxt-tzx / json.ts
Last active May 7, 2025 11:53
Drizzle helper functions: json.ts
import type { AnyColumn, SQL, Table } from "drizzle-orm";
import { sql } from "drizzle-orm";
import type { PgColumn } from "drizzle-orm/pg-core";
import { type SelectedFields } from "drizzle-orm/pg-core";
import { type SelectResultFields } from "drizzle-orm/query-builders/select.types";
import { jsonBuildObject } from "./external-utils";
import type {
ExtractColumnData,
PathsToStringProperty,
@zxt-tzx
zxt-tzx / explain.ts
Last active May 7, 2025 11:55
Drizzle helper functions: explain
import type { SQLWrapper } from "drizzle-orm";
import { sql } from "drizzle-orm";
import type { DbClient } from "@/db";
export const explainAnalyze = async <T extends SQLWrapper>(
db: DbClient,
query: T,
) => {
const debugResult = await db.execute(sql`EXPLAIN ANALYZE ${query.getSQL()}`);
@zxt-tzx
zxt-tzx / conflict.ts
Created February 17, 2025 11:05
Drizzle helper functions: handling conflicts
import type { SQL, Table } from "drizzle-orm";
import { getTableColumns, sql } from "drizzle-orm";
// from https://github.com/drizzle-team/drizzle-orm/issues/1728
export function conflictUpdateAllExcept<
T extends Table,
E extends (keyof T["$inferInsert"])[],
>(table: T, except: E) {
const columns = getTableColumns(table);
const updateColumns = Object.entries(columns).filter(
@zxt-tzx
zxt-tzx / sync.sh
Created December 27, 2022 14:42
prioritise iCloud syncing processes
#!/bin/sh
bird_pid=$(pgrep bird)
cloudd_pid=$(pgrep cloudd)
nsurlsessiond=$(pgrep nsurlsessiond)
# check all the values are not empty
# refactor to not use [[ ]] and use [ ] instead
if [[ -z "$bird_pid" || -z "$cloudd_pid" || -z "$nsurlsessiond" ]]; then
echo "bird, cloudd or nsurlsessiond is not running"
exit 1
@zxt-tzx
zxt-tzx / kill-bird.sh
Last active February 16, 2023 08:23
shell script to kill bird (iCloud syncing)
#!/bin/sh
pid=$(ps -fe | grep 'bird' | grep -v grep | awk '{print $2}')
if [[ -n $pid ]]; then
kill $pid
else
echo "bird is not running"
fi