pika_id
Types
Pika generator type.
pub type Pika {
Pika(
gen: fn(String) -> Result(String, String),
deconstruct: fn(String) -> Result(PikaIdMetadata, String),
)
}
Constructors
-
Pika( gen: fn(String) -> Result(String, String), deconstruct: fn(String) -> Result(PikaIdMetadata, String), )
Deconstructed format of a Pika ID. Contains metas from ID’s prefix and snowflake.
pub type PikaIdMetadata {
PikaIdMetadata(
id: String,
prefix: String,
description: String,
secure: Bool,
tail: String,
decoded_tail: String,
snowflake: Int,
timestamp: Int,
epoch: Int,
node_id: Int,
seq: Int,
)
}
Constructors
-
PikaIdMetadata( id: String, prefix: String, description: String, secure: Bool, tail: String, decoded_tail: String, snowflake: Int, timestamp: Int, epoch: Int, node_id: Int, seq: Int, )
Options for pika ID setup.
- WithEpoch: customize the epoch (millis) that IDs are derived from - by default, this is 1640995200000 (Jan 1 2022)
- WithNodeId: by default, Node IDs are calculated by finding the MAC address of the first public network interface device, then calculating the modulo against 1024
pub type PikaOption {
WithEpoch(Int)
WithNodeId(Int)
}
Constructors
-
WithEpoch(Int)
-
WithNodeId(Int)
When creating a pika ID, you must specify the prefix to be prepended - the general rule of thumb should be to use a different prefix for each object type (e.g. user, team, post, etc). Type prefixes should be lowercase, short, alphanumeric strings.
Examples
Prefix("user", "User ID", False) // Correct
Prefix("sk", "Secret key", True) // Correct
Prefix("conn_acc", "ID for connected accounts", False)
// -> "One of the prefixes is invalid (prefix 'conn_acc' must be Alphanumeric)"
Prefix("pa$$word", "password id here", False)
// -> "One of the prefixes is invalid (prefix 'pa$$word' must be Alphanumeric)"
pub type Prefix {
Prefix(name: String, description: String, secure: Bool)
}
Constructors
-
Prefix(name: String, description: String, secure: Bool)
Functions
pub fn pika_custom_init(
prefixes: List(Prefix),
options: List(PikaOption),
) -> Result(Pika, String)
Initialize Pika with custom Epoch and/or Node ID.
To guarantee that developers use the correct pre-defined prefix types for the right object types, pika requires you to “register” them before they’re used to prevent warnings from being thrown.
This is also where you define if a prefix type should be cryptographically secure or not.
If you dont need to specify options, use pika_init
instead. (or pass [] as options).
Examples
Options
let assert Ok(pika) =
pika_custom_init(
[
Prefix("user", "User ID", False),
Prefix("sk", "Secret key", True)
],
[WithEpoch(1_000_000_000), WithNodeId(640)],
)
// -> Pika
No options
let assert Ok(pika) = pika_custom_init([
Prefix("user", "User ID", False),
Prefix("sk", "Secret key", True)
], [])
// -> Pika
pub fn pika_deconstruct(
pika: Pika,
id: String,
) -> Result(PikaIdMetadata, String)
Deconstruct a pika ID into metadata (PikaIDMetadata
)
Examples
pika.deconstruct("user_MzMyNTAwNTIwMDQwNjExODQw")
// ->
Ok(
PikaIdMetadata(
"user_MzMyNTAwNTIwMDQwNjExODQw",
"user",
"User ID",
False,
"MzMyNTAwNTIwMDQwNjExODQw",
"332500520040611840",
332500520040611840,
1720269501538,
1640995200000,
628,
0
)
)
pub fn pika_gen(
pika: Pika,
prefix: String,
) -> Result(String, String)
Generate a pika ID. Tail will be generated based on prefix secure status.
Example of a normal decoded tail: 129688685040889861
Example of a cryptographically secure decoded tail: s_387d0775128c383fa8fbf5fd9863b84aba216bcc6872a877_129688685040889861
Examples
pika.gen("user")
// -> Ok("user_MzMyNTAwNTIwMDUzMTk0NzUy")
pika |> pika_gen("user")
// -> Ok("user_MzMyNTAwNTIwMDQwNjExODQw")
pika |> pika_gen("core")
// -> Error("Prefix is undefined")
pub fn pika_init(prefixes: List(Prefix)) -> Result(Pika, String)
Initialize Pika with default Epoch and Node ID.
Pika.gen(String) | pika_gen(Pika, String)
- generate ID
Pika.deconstruct(String) | pika_deconstruct(Pika, String)
- deconstruct ID
To guarantee that developers use the correct pre-defined prefix types for the right object types, pika requires you to “register” them before they’re used to prevent warnings from being thrown.
This is also where you define if a prefix type should be cryptographically secure or not.
Examples
let assert Ok(pika) =
pika_init([
Prefix("user", "User ID", False),
Prefix("sk", "Secret key", True),
])
// -> Pika
let assert Ok(pika) =
pika_init([
Prefix("u!ser", "User ID", False),
])
// -> "Unable to initialize pika: "One of the prefixes is invalid (prefix 'u!ser' must be Alphanumeric)""