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
@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') {
@capaj
capaj / AuthyToOtherAuthenticator.md
Last active February 8, 2022 14:55 — forked from gboudreau/AuthyToOtherAuthenticator.md
Export TOTP tokens from Authy to Authier

Generating Authy passwords on other authenticators


There is an increasing count of applications which use Authy for two-factor authentication. However many users who aren't using Authy, have their own authenticator setup up already and do not wish to use two applications for generating passwords.

Since I use 1Password for all of my password storing/generating needs, I was looking for a solution to use Authy passwords on that. I couldn't find any completely working solutions, however I stumbled upon a gist by Brian Hartvigsen. His post had a neat code with it to generate QR codes for you to use on your favorite authenticator.

His method is to extract the secret keys using Authy's Google Chrome app via Developer Tools. If this was not possible, I guess people would be reverse engineering the Android app or something like that. But when I tried that code, nothing appeared on the screen. My guess is that Brian used the

document.body.querySelectorAll('input').forEach((inputEl) => {
inputEl.addEventListener('input', (ev) => {
console.log('ev', ev)
})
})
@capaj
capaj / PostTypesTweaked.ts
Created December 12, 2021 10:30
prisma-oop
import { Field, ID, ObjectType, Int } from 'type-graphql'
import { UserGQL } from './User'
import { Prisma, PrismaClient, Post, prisma } from '@prisma/client'
import { plainToInstance } from 'class-transformer'
import { prismaClient } from './../../prisma/prismaClient'
type Constructor<T> = {
new (): T
relations: Record<keyof Prisma.PostInclude, Function>
baseRelations: Record<keyof Prisma.PostInclude, Function>