Not sure what is behind "canot be duplicated".
I'd assume you want to ensure no other process/call is happen to generate the same value for different purpose.
Then you might use crypto checksums (MD5, SHA1) on sufficiently unique data
(e.g. session id + current time + random number). If you are using a function that is returning binary data, then use a bas64 encoding (btoa) on this value.
This still is not perfectly unique. You would suffer from collisions on encoded values. However, with crypto checksums this is rather unlikely.
If you need to ensure uniqueness under all circumstances (also depnds onthe life span of those values) you need to query the database for existing keys (or rely on unique key violation errors) for detecting duplicates (and repeat the whole generating until success).
But, if you are using a database, why are you not using serials, sequences or whatever your database provides for getting uniq data (you may then still use base64 encoding to get safe character strings)