Skip to content
项目

关于

Short unique id generator for PostgreSQL, using hashids

数据与分析

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

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.

$ git clone https://github.com/iCyberon/pg_hashids
$ cd pg_hashids
# Make sure your path includes the bin directory that contains the correct `pg_config`
$ PATH=/path/to/pg/bin:$PATH
$ USE_PGXS=1 make
$ USE_PGXS=1 make install

Then in a psql session issue:

CREATE extension pg_hashids;

Update

Install as usual.

$ git clone https://github.com/iCyberon/pg_hashids
$ cd pg_hashids
# Make sure your path includes the bin directory that contains the correct `pg_config`
$ PATH=/path/to/pg/bin:$PATH
$ USE_PGXS=1 make
$ USE_PGXS=1 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';

Tests

$ USE_PGXS=1 make installcheck

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 previously 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

Decoding a single id

You can also decode a previously generated hash into a single integer. Just use the same salt, otherwise you'll get wrong results. The id_decode_once function is useful when you expect only one number to be encoded in the hash.

SELECT id_decode_once('jNl'); -- Result: 1001

Using salt:

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

Using salt and min_length:

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

Using a custom alphabet:

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

探索相关项目

加载中…