iCyberon / pg_hashids

Short unique id generator for PostgreSQL, using hashids
MIT License
280 stars 24 forks source link

Encoder/decoder for multiple numbers #1

Closed iCyberon closed 7 years ago

somebee commented 7 years ago

Great plugin :) Is encoder/decoder for multiple numbers coming? And is there any way to change the alphabet?

iCyberon commented 7 years ago

@somebee Support for multiple numbers are definitely coming, I've already implemented and using it. I'll try to push the update ASAP (most probably within 12 hours). Regarding the custom alphabet, do you want to specify it every time you call the function or supply it once and use it later?

iCyberon commented 7 years ago

Hey @somebee, I just pushed an update which adds ability to encode multiple numbers. I'll update the readme soon.

Just pass an array instead of int to get the desired result. salt, min_length and alphabet are optional. id_decode will return BIGINT[]. But if you would like to get just first number or if you're sure the hash contains only one number you can use id_decode_once, which will return BIGINT.

SELECT id_encode(ARRAY[1,2,3,20], 'hello', 20, 'abcdefghijABCDxFGHIJ1234567890');
SELECT id_decode('gjA34a5eI6C1hAba186D', 'hello', 20, 'abcdefghijABCDxFGHIJ1234567890');

SELECT id_decode_once('3GJ956J9B9', 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890');

Let me know if you have any questions :)