Skip to content

Instantly share code, notes, and snippets.

@hos
hos / knex-typed-clip-columns.ts
Last active August 8, 2024 13:39
Typed function to use with knex tables, to remove some columns from provided object.
import 'types/schemas/knex-tables'
import { Tables } from 'knex/types/tables'
type TableColumns<TTableName extends keyof Tables = keyof Tables> = {
[T in TTableName]: (keyof Tables[T]['base'])[]
}
const deleteColumns: Partial<TableColumns> = {
users: ['name'],
}
export function bigIntP(value: bigint, percentage: number, scalingFactor = 1000000000): bigint {
const scaledPercentage = BigInt(Math.round(percentage * 100 * scalingFactor))
const result = (value * scaledPercentage) / (BigInt(100) * BigInt(scalingFactor))
return result
}
@hos
hos / InjectRuntimePublicEnv.ts
Last active October 8, 2024 08:56
Workaround to use runtime variables in the next.js app (App Router), so you can change them after build.
import Script from 'next/script'
// Add this file to some Server Component, in my case I use this in `app/layout.tsx`
// Next.js, will inline all the process.env.SOMETHING variables into the code at build time.
// Which will force us to build the app for each environment we want to deploy to, with the
// only different being few environment variables. Instead, what we want is to get the environment
// dynamically from the runtime, so if we change them in the env and run the app again, it must
// use the new environment variables. To do this, we will inject the environment variables into
// the window object, so we can access them in the runtime.
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 9 columns, instead of 6 in line 7.
city 1, city 2 distance, city 2, city 3 distance, city 3, city 4 distance, city 4, city 5 distance, city5
Bronx , 23 , Brooklyn , 11 , Manhattan , 18 , New York , 13 , Queens
Bronx , 23 , Brooklyn , 11 , Manhattan , 13 , Queens , 18 , New York
Bronx , 23 , Brooklyn , 18 , New York , 11 , Manhattan , 13 , Queens
Bronx , 23 , Brooklyn , 18 , New York , 13 , Queens , 11 , Manhattan
Bronx , 23 , Brooklyn , 13 , Queens , 11 , Manhattan , 18 , New York
Bronx , 23 , Brooklyn , 13 , Queens , 18 , New York , 11 , Manhattan
Bronx , 11 , Manhattan , 23 , Brooklyn , 1
@hos
hos / supabase-client.ts
Last active October 25, 2024 09:03
Validate telegram mini app init data in PostgreSQL, can be useful when you want to use supabase client in TMA, pass `initDataRaw` to headers, then in SQL queries get init data (username, user id...)
const supabase = useMemo(() => {
return createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_KEY!,
{
global: {
headers: {
telegram_init_data: initDataRaw || "",
},
},
--[[
=====================================================================
==================== READ THIS BEFORE CONTINUING ====================
=====================================================================
Kickstart.nvim is *not* a distribution.
Kickstart.nvim is a template for your own configuration.
The goal is that you can read every line of code, top-to-bottom, and understand
import { jsonParse } from "@dataplan/json";
import { access, context, lambda, listen } from "grafast";
import { gql, makeExtendSchemaPlugin } from "graphile-utils";
const PackagePlugin = makeExtendSchemaPlugin((build) => {
const { sql } = build;
if (!sql) {
throw new Error("sql not found");
}
import cors from "cors";
import express, { Express, NextFunction } from "express";
import { IncomingMessage, Server, ServerResponse } from "http";
import inspector from "inspector";
import { Duplex } from "stream";
import { isDev, isLocal } from "./config.js";
import * as middleware from "./middleware/index.js";
import { firebaseAuthMiddleware } from "./middleware/installFirebaseAuth.js";
import { makeShutdownActions, ShutdownAction } from "./shutdownActions.js";
import { PgSelectSingleStep } from "@dataplan/pg";
import { Episode } from "@mythrillfiction/types";
import { context, lambda } from "grafast";
import { gql, makeExtendSchemaPlugin, makeWrapPlansPlugin, PlanWrapperFn } from "graphile-utils";
import { WorkerUtils } from "graphile-worker";
import _ from "lodash";
import * as yup from "yup";
import { SCHEMA_NAME } from "../config.js";
import { adminDb } from "../firebase.js";
import { jsonParse } from "@dataplan/json";
import { access, context, lambda, listen } from "grafast";
import { gql, makeExtendSchemaPlugin } from "graphile-utils";
import { SCHEMA_NAME } from "../config.js";
/* The JSON object that `tg__graphql_subscription()` delivers via NOTIFY */
interface TgGraphQLSubscriptionPayload {
event: string;
subject: string | null;