Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreystepanov/hashids.sql
PL/pgSQL implementation of hashids library
https://github.com/andreystepanov/hashids.sql
aws aws-rds aws-rds-postgres hashids id plpgsql postgresql rds shortid sql
Last synced: 28 days ago
JSON representation
PL/pgSQL implementation of hashids library
- Host: GitHub
- URL: https://github.com/andreystepanov/hashids.sql
- Owner: andreystepanov
- License: mit
- Created: 2018-10-24T14:02:36.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-04T17:49:58.000Z (about 2 years ago)
- Last Synced: 2024-09-28T18:23:10.781Z (about 1 month ago)
- Topics: aws, aws-rds, aws-rds-postgres, hashids, id, plpgsql, postgresql, rds, shortid, sql
- Language: PLpgSQL
- Homepage: https://hashids.org/
- Size: 7.81 KB
- Stars: 49
- Watchers: 2
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - andreystepanov/hashids.sql - PL/pgSQL implementation of hashids library (PLpgSQL)
README
# PL/pgSQL implementation of [Hashids](https://hashids.org/)
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](http://hashids.org)
**Suitable for hosted environments like AWS RDS**.
*But if you are looking for an PG extention, then please check out [pg_hashids](https://github.com/iCyberon/pg_hashids) repository.*
# Usage
#### Encoding
Returns a hash using the default `alphabet` and specified `min_length` and `salt` parameters.```sql
select hashids.encode( number := 999, min_length := 6, salt := 'salt'); -- dkrMl8
````number` parameter can also be an array of numbers:
```sql
select hashids.encode( number := array[111,222], min_length := 6, salt := 'salt'); -- VyAHPK
```It's also could be helpful to use it with [sequences](https://www.postgresql.org/docs/current/static/sql-createsequence.html):
```sql
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.
```sql
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:
```sql
select hashids.encode( number := 999, salt := 'salt', alphabet := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); -- D3Q5
```
#### Encode hex instead of numbersUseful if you want to encode [Mongo](https://www.mongodb.com/)'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).
```sql
select hashids.encode_hex( hex := '507f1f77bcf86cd799439011', salt := 'salt'); -- zro2yr9M4ZCzZ9zd9xYv
```
These ids can be also easily decoded back to their original values:```sql
select hashids.decode_hex( id := 'zro2yr9M4ZCzZ9zd9xYv', salt := 'salt'); -- 507f1f77bcf86cd799439011
```