View Source EctoKsuid (ecto_ksuid v0.3.0)
Custom Ecto type to support ksuids
Link to this section Summary
Types
A database_ksuid is the value of the EctoKsuid in the database.
A runtime_ksuid is the value of the EctoKsuid in the elixir application.
Functions
Returns the primitive migration type for an EctoKsuid.
Generates a new ksuid.
Removes the configured prefix from an EctoKsuid.
Link to this section Types
@type database_ksuid() :: t()
A database_ksuid is the value of the EctoKsuid in the database.
It contains just the ksuid without the configured prefix.
@type runtime_ksuid() :: t()
A runtime_ksuid is the value of the EctoKsuid in the elixir application.
It contains the configured prefix and the ksuid stored in the database.
@type t() :: String.t()
An EctoKsuid
is a base62 encoded string. That may or may not include an
optional configure prefix.
Link to this section Functions
@spec column() :: :"char(27)"
Returns the primitive migration type for an EctoKsuid.
This can be used in your migrations, so you don't have to rememeber exactly how large a ksuid is.
examples
Examples
in-an-ecto-migration
In an ecto migration:
def change() do
create table(:table_name, primary_key: false) do
add(:id, EctoKsuid.column(), primary_key: true)
end
end
in-your-repo-config
In your repo config
config :my_app, MyApp.Repo,
migration_primary_key: [name: :id, type: :"char(27)"],
migration_foreign_key: [name: :id, type: :"char(27)"]
@spec generate() :: t()
Generates a new ksuid.
This function returns a 20 byte Ksuid which has 4 bytes as timestamp and 16 bytes of crypto string bytes.
This function delegates to the Ksuid
library and is functionally similar to
Ecto.UUID.generate/0
examples
Examples
iex> EctoKsuid.generate()
"2mepXBjQq2rZCfwuX68tO53L4uc"
@spec remove_prefix(String.t()) :: database_ksuid()
@spec remove_prefix(String.t(), EctoKsuid.Options.t()) :: database_ksuid()
Removes the configured prefix from an EctoKsuid.
This can be used to get the raw ksuid without the runtime prefix.
examples
Examples
iex> EctoKsuid.remove_prefix("prefix_2EXLuF3dU8v4L4JLInQXNWpjKE0", %EctoKsuid.Options{prefix: "prefix_"})
"2EXLuF3dU8v4L4JLInQXNWpjKE0"
iex> EctoKsuid.remove_prefix("2EXLuF3dU8v4L4JLInQXNWpjKE0", %EctoKsuid.Options{prefix: ""})
"2EXLuF3dU8v4L4JLInQXNWpjKE0"