Skip to content

Instantly share code, notes, and snippets.

@ItsWendell
ItsWendell / Dockerfile
Created October 22, 2023 09:58
Postgres Dockerfile with Custom Extensions using pgxn
## Alternatives: postgres:15-alpine
ARG BASE_IMAGE=postgis/postgis:15-3.4-alpine
## Custom Alpine Postgres docker file with custom extensions
FROM ${BASE_IMAGE} as builder
# Install required dependencies
RUN apk --no-cache add \
python3 \
@aiherrera
aiherrera / embeddings-generator.ts
Last active January 6, 2024 04:31
Supabase EDGE Function - Embeddings generator
// Import the serve function to start a Deno server
import { serve } from "std/server";
// Import pipeline utility from Hugging Face Transformers
import { pipeline } from "transformers";
// Import Supabase client
import { createClient } from "supabase/client";
// Shared CORS headers
declare module "jdatas" {
function ID(cod: number): Promise<string>;
class File {
direct: string;
constructor(path: string, name: string);
create<T extends object>(data: T, encoding: string): Promise<T>;
@hos
hos / firestore-new-id.sql
Created February 11, 2023 05:48
Firestore's newId function implemented in PostgreSQL. Converted using ChatGPT.
create or replace function public.gen_id()
returns text as $$
declare
chars text := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
max_multiple integer := floor(256 / char_length(chars)) * char_length(chars);
i integer;
auto_id text := '';
target_length integer := 20;
bytes bytea;
begin
-- Tested on PostgreSQL 15.1.
-- https://gist.github.com/srfrog/ef8c9a3a4282b865fb93f429d671d63b
-- https://github.com/paralleldrive/cuid2 56c54483bd7d5dd12343211c14588265e00f0887
CREATE SEQUENCE IF NOT EXISTS "cuid2_seq";
CREATE OR REPLACE FUNCTION _counter() RETURNS VARCHAR AS $$
DECLARE
val bigint;
BEGIN
@jalallinux
jalallinux / postgre_change_logs.sql
Last active December 5, 2022 04:01
The PostgreSQL trigger script is recording all events with their changed values.
-- Define audit_logs table
CREATE TABLE IF NOT EXISTS public.audit_logs
(
uuid uuid NOT NULL,
operation character varying(10) COLLATE pg_catalog."default" NOT NULL,
userid text COLLATE pg_catalog."default" NOT NULL,
tablename text COLLATE pg_catalog."default" NOT NULL,
changed json NOT NULL,
stamp timestamp without time zone NOT NULL
);
@oprietop
oprietop / template.go
Last active October 15, 2023 21:01
golang snippet template using structs, goroutines with limited paralellism, http requests, json unmarshall, safe access to variables
package main
import (
"os"
"fmt"
"time"
"io"
"io/ioutil"
"sync"
"log"
@pveierland
pveierland / jsonb_set_deep.sql
Created September 16, 2022 21:57
jsonb_set_deep
CREATE OR REPLACE FUNCTION jsonb_set_deep(target jsonb, path text[], val jsonb)
RETURNS jsonb AS $$
DECLARE
k text;
p text[];
BEGIN
IF (path = '{}') THEN
RETURN val;
ELSE
IF (target IS NULL) THEN
@kizdolf
kizdolf / jsonb_recursive_merge.sql
Created September 1, 2022 14:51
jsonb_recursive_merge
-- from https://medium.com/hootsuite-engineering/recursively-merging-jsonb-in-postgresql-efd787c9fad7
-- with unique array values merge
CREATE OR REPLACE FUNCTION jsonb_recursive_merge(A jsonb, B jsonb)
RETURNS jsonb LANGUAGE SQL AS $$
SELECT
jsonb_object_agg(
coalesce(ka, kb),
CASE
WHEN va isnull THEN vb
CREATE FUNCTION jsonb_replace_by_key(obj jsonb, search text, substitute jsonb) RETURNS jsonb
STRICT LANGUAGE SQL AS $$
SELECT CASE jsonb_typeof(obj)
WHEN 'object' THEN
(SELECT coalesce(jsonb_object_agg(key, CASE WHEN key = search
THEN substitute
ELSE jsonb_replace_by_key(value, search, substitute)
END), '{}'::jsonb)
FROM jsonb_each(obj))
WHEN 'array' THEN