Skip to content

Instantly share code, notes, and snippets.

@gusfune
Created August 10, 2023 13:22
Show Gist options
  • Save gusfune/9edcd4cf8e8213ca5fb7be4c103b4b58 to your computer and use it in GitHub Desktop.
Save gusfune/9edcd4cf8e8213ca5fb7be4c103b4b58 to your computer and use it in GitHub Desktop.
collision resistance prefixed ID for PostgreSQL
-- This requires nanoid to be installed to work
-- https://github.com/viascom/nanoid-postgres
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION prefixedid(
prefix text,
size int DEFAULT 24,
alphabet text DEFAULT '0123456789abcdefghijklmnopqrstuvwxyz'
)
RETURNS text
LANGUAGE plpgsql
volatile
AS
$$
DECLARE
idBuilder text := '';
BEGIN
idBuilder := idBuilder || prefix || '_' || nanoid(size,alphabet);
return idBuilder;
END
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment