pg_hashids:短 UID
pg_hashids 提供了一种安全的方式,从数字生成短小、独特且非连续的 ID。这些哈希旨在成为小巧、易记的标识符,可以用来(可选地)通过密码、字母表和盐来混淆数据。例如,你可能想隐藏像用户 ID、订单号或追踪码这样的数据,而使用 pg_hashid 的独特标识符。
启用扩展 #
🌐 Enable the extension
- 在仪表板中转到数据库页面。
- 点击侧边栏的 扩展。
- 搜索 "pg_hashids" 并启用该扩展。
用法 #
🌐 Usage
假设我们有一个存储订单信息的表,我们想给客户一个唯一的标识符,而不暴露连续的 id 列。为此,我们可以使用 pg_hashid 的 id_encode 函数。
🌐 Suppose we have a table that stores order information, and we want to give customers a unique identifier without exposing the sequential id column. To do this, we can use pg_hashid's id_encode function.
1create table orders (2 id serial primary key,3 description text,4 price_cents bigint5);67insert into orders (description, price_cents)8values ('a book', 9095);910select11 id,12 id_encode(id) as short_id,13 description,14 price_cents15from16 orders;1718 id | short_id | description | price_cents19----+----------+-------------+-------------20 1 | jR | a book | 909521(1 row)要把 short_id 反过来变成 id,有一个对应的函数叫做 id_decode。
🌐 To reverse the short_id back into an id, there is an equivalent function named id_decode.
资源 #
🌐 Resources