spa5k / uids-postgres

PostgreSQL Extension to generate various type of Universally Unique IDS
45 stars 1 forks source link

Question: Custom alphabet for nanoid #1

Closed anli-xsigns closed 2 years ago

anli-xsigns commented 2 years ago

Hello, thank you for this great work. I'm interested to use this to generate nanoids. Unfortunately I need a custom alphabet for nanoid as explained in https://github.com/ai/nanoid#custom-alphabet-or-size. How can I achieve this? Thanks a lot for a short answer in advance! Have a great day!

spa5k commented 2 years ago

It won't be just available, i will need to add this functionality, wait for an update

spa5k commented 2 years ago

Check now, I've added the function - generate_nanoid_c(), more in readme.

anli-xsigns commented 2 years ago

Wow, thanks a lot for this lightning-fast update! It works like charm! The only backdraft is that I have to use the second parameter all the time where I use the function (obviously!). Is there a chance to make another function - I only need to get rid a of the default included _ and -, so I can use A-Za-z0-9 - but giving parameter ABCDEF....4567890 to achieve that seems a little bit exhausting :-)

spa5k commented 2 years ago

lol mate, wait a min then

spa5k commented 2 years ago

It has been added in the latest c, generate_nanoid_c, it only takes one argument, and that is of the alphabets. remember to star

anli-xsigns commented 2 years ago

Perfect, thanks a lot 👍🥳

anli-xsigns commented 2 years ago

Sorry - I had a quick look at the source and didn't find it - I need something like:

    pub(crate) fn generate_nanoid_length(length: i32) -> String {
        let length_as_usize = length as usize;
        let alphabets_vec = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".chars().collect::<Vec<char>>();
        let id = nanoid!(length_as_usize , &alphabets_vec);
        id
    }

Sorry for pseudo-code - I don't know Rust yet :-( So I can only specifiy the length and then generate an ID only consisting of A-Za-z0-9. Do you think this is possible? Thanks a lot for your help!

spa5k commented 2 years ago

That's too specifically of a function to be added. You can follow the nanoid functions that takes alphabets and the length in order to generate your custom configuration. If you don't like adding it every time, create a new sql functionality that takes length as argument and interact with my function.

anli-xsigns commented 2 years ago

Ok, I understand that 👍