Git Product home page Git Product logo

hashids.sql's Introduction

PL/pgSQL implementation of Hashids

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers. It converts numbers like 347 into strings like โ€œyr8โ€. You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.

You can use hashids to hide primary keys in your database.

More information about hashids and it's implementations can be found here: hashids.org

Suitable for hosted environments like AWS RDS.

But if you are looking for an PG extention, then please check out pg_hashids repository.

Usage

Encoding

Returns a hash using the default alphabet and specified min_length and salt parameters.

select hashids.encode( number := 999, min_length := 6, salt := 'salt'); -- dkrMl8

number parameter can also be an array of numbers:

select hashids.encode( number := array[111,222], min_length := 6, salt := 'salt'); -- VyAHPK

It's also could be helpful to use it with sequences:

select hashids.encode( number := nextval('schema.sequence_name'), min_length := 6, salt := 'salt');

You can also decode previously generated hashes. Just use the same parameters salt, min_length and alphabet, otherwise you'll get wrong results.

select hashids.decode( id := 'dkrMl8', min_length := 6, salt := 'salt'); -- {999}
select hashids.decode( id := 'VyAHPK', min_length := 6, salt := 'salt'); -- {111,222}

Using custom alphabet, only capitalized letters and numbers:

select hashids.encode( number := 999, salt := 'salt', alphabet := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); -- D3Q5

Encode hex instead of numbers

Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).

select hashids.encode_hex( hex := '507f1f77bcf86cd799439011', salt := 'salt'); -- zro2yr9M4ZCzZ9zd9xYv

These ids can be also easily decoded back to their original values:

select hashids.decode_hex( id := 'zro2yr9M4ZCzZ9zd9xYv', salt := 'salt'); -- 507f1f77bcf86cd799439011

hashids.sql's People

Contributors

pex avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.