Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / get_data.sql
Created October 23, 2024 18:45
PSQL to JSON: creating data for tests based on database data
select json_agg(row_to_json(t)) from (select * from "my_table" LIMIT 1) as t;
-- This ⬆️ will return a json of the results, which you could use for test data/mock data, you name it.
@tomfa
tomfa / understand-unknown-db.sql
Created September 29, 2024 06:18
MSSQL find all referencing FKs
SELECT
referencing_schema_name, referencing_entity_name, referencing_id,
referencing_class_desc, is_caller_dependent
FROM
sys.dm_sql_referencing_entities ('REPLACE_WITH_TABLE_NAME', 'OBJECT');
GO
@tomfa
tomfa / Component.tsx
Created August 15, 2024 12:49
React Query: async fetch from cache or api
import { useQueryClient } from '@tanstack/react-query';
/*
* Demonstration of how to fetch data from API in a promise,
* but utilizing already cached data from React Query
* or (if not present) populate the cache after fetch.
*
* Motivation: reduce unnecessary data fetching.
*/
@tomfa
tomfa / manifest.json
Last active June 16, 2024 11:25
iOS push notifications
{
"name": "Notification app",
"short_name": "Notifs",
"start_url": ".",
"display": "standalone",
"background_color": "#1f2937",
"orientation": "portrait",
"description": "Notifications made easy.",
"icons": [{
"src": "img/logo_144.png",
@tomfa
tomfa / auth.ts
Last active June 15, 2024 13:33
Next Auth Email OTP Provider
export const authOptions: NextAuthOptions = {
// [...other options]
providers: [
env.NODE_ENV === "development"
? ConsoleOtpProvider()
: EmailOtpProvider({ from: env.EMAIL_FROM, server: env.EMAIL_URL }),
],
};
@tomfa
tomfa / auth.ts
Last active June 15, 2024 10:39
NextAuth: link in console for development
import { type NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { ConsoleEmailLink } from "./dev.auth.ts";
/**
* ConsoleEmailLink will print a login link to the console.
* Enables you to login while developing without internet,
* and makes local setup easier.
*/
@tomfa
tomfa / urls.test.ts
Last active November 15, 2024 05:51
NextJS route vs pathname matcher
describe('matchesPath', () => {
const matches = [
['/cake', '/cake'],
['/cake', '/cake/'],
['/cake', '/cake?frige=warm'],
['/cake', '/cake?frige=warm&freezer=cold'],
['/[id]', '/cake'],
['/[anything-goes]', '/cake'],
['/c/[id]/practitioner/[pid]/[anything-goes]', '/c/1/practitioner/2/3'],
['/[...rest]', '/cake'],
@tomfa
tomfa / schema.prisma
Created October 3, 2023 11:33
Lago invoicing modelling
// autogenerated from Lago database model (https://github.com/getlago/lago)
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
@tomfa
tomfa / .meta.json
Last active October 1, 2023 16:46
Autohotkey script examples (found in 2012-archive)
{
"slides": "https://docs.google.com/presentation/d/1WyicsfMJirQDpJYTVOFwRxwiwgfAVq6fRyylFXc6_G4/edit?usp=sharing",
"date": "September 2012"
}
@tomfa
tomfa / imports-add-ext.js
Created September 20, 2023 20:57
Codemod convert commonJS to esm import path style
/*
jscodeshift transform: adds the '.js' extension
to all import declarations with relative specifiers:
From './file' to './file.js', and
from '../file' to '../file.js'.
*/
module.exports = function (fileInfo, api) {