Skip to content

Instantly share code, notes, and snippets.

View capaj's full-sized avatar
🏠
Always working-be it from home or elsewhere

Jiri Spac capaj

🏠
Always working-be it from home or elsewhere
View GitHub Profile
import { encode } from '@toon-format/toon'
import { sql } from 'drizzle-orm'
import { db } from './src/db/db' // replace
import { env } from './src/env' // replace
/**
* Run a SQL query against the current database from .env
*/
async function runQuery() {
// Get the SQL query from the command line arguments
@capaj
capaj / inotify.sh
Last active November 29, 2025 08:02
inotify ubuntu sane defaults
sudo tee /etc/sysctl.d/99-inotify.conf >/dev/null <<'EOF'
fs.inotify.max_user_watches = 1048576
fs.inotify.max_user_instances = 2048
fs.inotify.max_queued_events = 32768
EOF
sudo sysctl --system
pnpm dlx shadcn@latest add accordion alert-dialog alert aspect-ratio avatar badge breadcrumb button calendar card carousel checkbox collapsible command context-menu dialog drawer dropdown-menu form hover-card input-otp input label menubar navigation-menu pagination popover progress radio-group resizable scroll-area select separator sheet skeleton slider sonner switch table tabs textarea tooltip
@capaj
capaj / dropAllTables.ts
Last active July 1, 2024 17:50
drizzle-utils
import { getTableName, sql } from 'drizzle-orm'
import * as schema from '../src/schema'
import { db } from '../src/db'
const allTableNames = Object.values(schema).map((table) => getTableName(table))
for (const table of allTableNames) {
console.log(`Dropping table ${table}`)
await db.run(sql.raw(`DROP TABLE ${table}`))
}
import { Column, sql } from 'drizzle-orm'
import { SQLiteTable, SQLiteUpdateSetSource } from 'drizzle-orm/sqlite-core'
export function conflictUpdateSet<TTable extends SQLiteTable>(
table: TTable,
columns: (keyof TTable['_']['columns'] & keyof TTable)[]
): SQLiteUpdateSetSource<TTable> {
return Object.assign(
{},
...columns.map((k) => ({
@capaj
capaj / content-king.ts
Created February 21, 2024 07:22
interview excercise for node.js dev role
import puppeteer from 'puppeteer'
const requestTimings = [] as {
url: string
startTime: number
}[]
async function run() {
const browser = await puppeteer.launch({
headless: false
@capaj
capaj / knex out
Created March 28, 2023 07:27
knex vs prisma
limit $3 undefined +0ms
knex:bindings [ 'c609097c-9d6d-470f-b3f6-200f52e19fe4', 74277, 1 ] undefined +0ms
knex:query select "id" from "favorites" where "aws_user_id" = $1 and "post_id" = $2 limit $3 undefined +3ms
knex:bindings [ 'c609097c-9d6d-470f-b3f6-200f52e19fe4', 74400, 1 ] undefined +2ms
knex:query select "id" from "favorites" where "aws_user_id" = $1 and "post_id" = $2 limit $3 undefined +1ms
knex:bindings [ 'c609097c-9d6d-470f-b3f6-200f52e19fe4', 84433, 1 ] undefined +1ms
knex:query select "id" from "favorites" where "aws_user_id" = $1 and "post_id" = $2 limit $3 undefined +0ms
knex:bindings [ 'c609097c-9d6d-470f-b3f6-200f52e19fe4', 88374, 1 ] undefined +1ms
knex:query select "id" from "favorites" where "aws_user_id" = $1 and "post_id" = $2 limit $3 undefined +1ms
knex:bindings [ 'c609097c-9d6d-470f-b3f6-200f52e19fe4', 89165, 1 ] undefined +0ms
@capaj
capaj / term.txt
Created January 15, 2023 19:38
drizzle-orm benchmark
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------- -----------------------------
• select * from customer
------------------------------------------------- -----------------------------
b3 253.87 µs/iter (243.63 µs … 2.77 ms) 248.21 µs 402.05 µs 520.94 µs
b3:p 242.2 µs/iter (235.88 µs … 1.18 ms) 239.09 µs 341.76 µs 379.37 µs
drizzle 207.76 µs/iter (193.1 µs … 1.01 ms) 203.32 µs 378.86 µs 686.33 µs
drizzle:p 172.78 µs/iter (167.05 µs … 1 ms) 170.92 µs 204.4 µs 356.89 µs
knex 303.22 µs/iter (282.06 µs … 1.34 ms) 301 µs 478.92 µs 915.14 µs
kysely 270.4 µs/iter (257.37 µs … 1.21 ms) 265.37 µs 426.33 µs 773.33 µs
@capaj
capaj / setAllSequencesPrismaPostgres.ts
Last active November 1, 2022 10:28
can be used to fix the sequences after migrating postgre using AWS DMS for example
import 'dotenv/config'
import { prismaClient } from '../src/lib/prismaClient'
const setSequenceToMaxInTable = async (props: {
table_name: string
sequence_name: string
column_name: string
}) => {
const setval = `
SELECT setval('"${props.sequence_name}"', COALESCE((SELECT MAX(${props.column_name})+1 FROM ${props.table_name}), 1), false);
@capaj
capaj / zodSchemaDefaults.ts
Created September 20, 2022 12:51
generates default object for a given zod schema, you can supply an object to override the defaults
import { z } from 'zod'
/**
* generates default object for a given zod schema, you can supply an object to override the defaults
*/
export const zodSchemaDefaults = <Schema extends z.ZodFirstPartySchemaTypes>(
schema: Schema,
override?: Partial<z.TypeOf<Schema>>
): z.TypeOf<Schema> => {
if (override !== undefined && typeof override !== 'object') {