Created
August 10, 2023 13:22
-
-
Save gusfune/9edcd4cf8e8213ca5fb7be4c103b4b58 to your computer and use it in GitHub Desktop.
collision resistance prefixed ID for PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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