Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjie benjie

View GitHub Profile
@benjie
benjie / run-in-console.js
Last active April 13, 2023 12:30
GitHub pull request JS snippet to mark all ".sql" files as viewed (substitute for any file extension)
View run-in-console.js
abort = false; [...$$('a[title$=".mermaid"]')].map(el => {const checks = el.parentNode.parentNode.parentNode.querySelectorAll('.js-reviewed-checkbox');if (checks.length !== 1) { throw new Error("Script out of date?");} return checks[0]}).forEach((inpt, i) => setTimeout(() => {if (abort || inpt.checked) return; inpt.click();}, i * 1000))
View taggedtltltl.js
a = () => () => () => 2;
console.log(a``````);
View README.md

PostGraphile reproduction script

Hello, you've probably been sent here because you've found an issue in your usage of PostGraphile. I (Benjie) deal with a lot of support requests that have insufficient information for me to diagnose the problem, so I'll ask for a minimal reproduction. Making a minimal reproduction allows me to diagnose (and potentially fix!) the issue much more easily, and is also a valuable exercise for you during your debugging - who knows, maybe whilst attempting to create the reproduction you'll figure out what went wrong and fix your own issue?

To use this, please download the script locally, and then populate the DATABASE

@benjie
benjie / swc.js
Last active October 26, 2021 12:03
A script for running SWC in a monorepo that uses TypeScript project references (`tsc --build` / `tsc -b`) compiling everything in parallel with optional watch mode. For this to work you MUST have `isolatedModules: true`.
View swc.js
const swc = require("@swc/core");
const chokidar = require("chokidar");
const glob = require("glob");
const { promises: fsp } = require("fs");
const { basename, dirname } = require("path");
const { createHash } = require("crypto");
const mkdirp = require("mkdirp");
const WATCH = process.argv[2] === "--watch";
View timethor.sh
#!/usr/bin/env bash
set -e
# What DB are we messing with? (THIS DB WILL BE OVERWRITTEN!)
DATABASE="timethor"
SCHEMA="main"
# Populate database
dropdb --if-exists "$DATABASE"
createdb "$DATABASE"
View makeSetDefaultOrderByForFieldPlugin.ts
import { Plugin } from 'postgraphile';
/**
* Allows you to override the default orderBy for a given field to be the
* orderBy with the given name. Usage:
*
* ```
* const MyOrderByPlugin = makeSetDefaultOrderByForFieldPlugin(
* 'MyTableName',
* 'myRelationName',
@benjie
benjie / strip_tags_from_introspection.js
Created May 21, 2021 15:24
A Graphile Engine plugin to strip all tags/descriptions from introspection results
View strip_tags_from_introspection.js
module.exports = (builder) => {
builder.hook("build", (build) => {
const oldPgAugmentIntrospectionResults =
build.pgAugmentIntrospectionResults;
build.pgAugmentIntrospectionResults = (inIntrospectionResult) => {
let pgIntrospectionResultsByKind = inIntrospectionResult;
if (oldPgAugmentIntrospectionResults) {
pgIntrospectionResultsByKind = oldPgAugmentIntrospectionResults(
pgIntrospectionResultsByKind,
);
View Map-vs-object-perf.js
const symbols = [];
for (let i = 0; i < 1000; ++i) {
symbols[i] = Symbol(`key_${i}`);
//symbols[i] = `key_${i}`;
}
function test1() {
const map = new Map();
for (let i = 0; i < 10000; ++i) {
View SizeOfTable.sql
SELECT
pg_size_pretty(total_bytes) AS total,
pg_size_pretty(index_bytes) AS index,
pg_size_pretty(toast_bytes) AS toast,
pg_size_pretty(total_bytes - index_bytes - toast_bytes) AS table
FROM (
SELECT
pg_total_relation_size(c.oid) AS total_bytes,
pg_indexes_size(c.oid) AS index_bytes,
coalesce(pg_total_relation_size(reltoastrelid), 0) AS toast_bytes
@benjie
benjie / loop_perf.js
Last active June 4, 2021 13:58
Comparing loop method performance in JS
View loop_perf.js
const arr = [];
const ARRAY_SIZE = parseInt(process.argv[2], 10) || 10;
const RUNS = Math.ceil(1000000000 / ARRAY_SIZE);
console.log(`Performing ${RUNS} runs of arrays with ${ARRAY_SIZE} elements`);
for (let i = 0; i < ARRAY_SIZE; i++) arr[i] = i;
function runTest(name, testFunction) {
if (global.gc) global.gc();