Giter Club home page Giter Club logo

pg_hashids's Introduction

pg_hashids, generate short unique ids from integers

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. I've used the extension on several production databases.

Tested PostgreSQL versions : 9.5.X and 9.6.X (Should work on older versions, just not tested)

It's using hashids.c under the hood. More information about hashids and it's implementations here: hashids.org

Installation

Make sure you have development packages installed for postgres and build tools in general.

make; sudo make install

Then in a psql session issue:

CREATE extension pg_hashids;

Update

Install as usual.

make; sudo make install

Then in a psql session issue:

ALTER EXTENSION pg_hashids UPDATE;

or

DROP EXTENSION pg_hashids;
CREATE EXTENSION pg_hashids;

Check

SELECT default_version, installed_version FROM pg_available_extensions WHERE name = 'pg_hashids';

Usage

Encoding

Returns a hash using the default alphabet and empty salt.

SELECT id_encode(1001); -- Result: jNl

Returns a hash using the default alphabet and supplied salt.

SELECT id_encode(1234567, 'This is my salt'); -- Result: Pdzxp

Returns a hash using the default alphabet, salt and minimum hash length.

SELECT id_encode(1234567, 'This is my salt', 10); -- Result: PlRPdzxpR7

Returns a hash using the supplied alphabet, salt and minimum hash length.

SELECT id_encode(1234567, 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 3GJ956J9B9

Decoding

You can also decode previouslt generated hashes. Just use the same salt, otherwise you'll get wrong results.

SELECT id_decode('PlRPdzxpR7', 'This is my salt', 10); -- Result: 1234567

Using a custom alphabet

SELECT id_decode('3GJ956J9B9', 'This is my salt', 10, 'abcdefghijABCDxFGHIJ1234567890'); -- Result: 1234567

pg_hashids's People

Contributors

icyberon avatar wind39 avatar

Watchers

 avatar  avatar  avatar

Forkers

sasha-alias

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.