UUIDs in 10 Languages — Part 7: UUID Generation in Even More Languages
We’ve covered Python, JavaScript, Go, Rust, Java, Ruby, C++, and more. But UUIDs are everywhere — including in languages and tools not always seen as “UUID-native.”
In this bonus post, we explore five more environments:
- TypeScript (Node.js)
- Scala
- SQL databases (PostgreSQL & MySQL)
- Elixir/Erlang
- Bash (yes, really)
✨ TypeScript: Type-Safe UUIDs
Use the popular uuid
package:
🔧 Install
npm install uuid
📦 Generate UUIDs
import { v4 as uuidv4, validate, version } from 'uuid';
const id = uuidv4();
console.log("UUIDv4:", id);
console.log("Valid?", validate(id));
console.log("Version:", version(id));
✅ Tips
- Validate UUIDs before persisting or transmitting
- Use type-safe UUID wrappers in domain-driven projects (e.g.
Branded<T>
in TypeScript)
☕ Scala: Java UUIDs with Functional Flair
Scala uses java.util.UUID
from the JVM ecosystem.
📦 Example
import java.util.UUID
val id = UUID.randomUUID()
println(s"Generated UUID: $id")
💡 In Play Framework / JSON
import play.api.libs.json._
implicit val uuidFormat: Format[UUID] = Format(
Reads.uuid,
Writes { uuid => JsString(uuid.toString) }
)
🛢️ SQL: Native UUIDs in PostgreSQL and MySQL
🔹 PostgreSQL
Postgres has a native uuid
type.
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
SELECT gen_random_uuid();
✅ Use in Tables
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT
);
🔹 MySQL
MySQL (8.0+) supports UUID()
and UUID_TO_BIN()
.
SELECT UUID();
-- Better storage
SELECT UUID_TO_BIN(UUID(), true);
🌄 Elixir & Erlang
🔧 Elixir
Use Ecto.UUID
or the uuid
package:
UUID.uuid4()
# "550e8400-e29b-41d4-a716-446655440000"
Add to mix.exs
:
{:uuid, "~> 1.1"}
Use Ecto.UUID
to auto-generate DB keys in Phoenix:
@primary_key {:id, :binary_id, autogenerate: true}
🧱 Erlang
uuid:uuid_to_string(uuid:get_v4()).
Ensure the uuid
module is available (OTP 24+ or add rebar dependency).
🐚 Bash and Shell Scripts
Many Unix-like systems include uuidgen
.
uuidgen
# Output: 550e8400-e29b-41d4-a716-446655440000
🔄 Pipe to API, Script, or DB Insert
curl -X POST -d "id=$(uuidgen)" https://your-api
📘 Summary Table
Language/Tool | Function | Notes |
---|---|---|
TypeScript | uuid.v4() | Strong typing, validation helpers |
Scala | UUID.randomUUID() | JVM-native, interoperable |
PostgreSQL | gen_random_uuid() | Compact, indexed, native UUID |
MySQL | UUID_TO_BIN(UUID()) | 16-byte binary storage preferred |
Elixir | UUID.uuid4() | Easy integration with Ecto |
Bash | uuidgen | Great for automation and ops |
Final Thoughts
UUIDs are everywhere — not just in backend-heavy stacks, but in shell scripts, functional languages, and databases too.
Whether you're building microservices, writing migrations, scripting ops tasks, or crafting live dashboards, UUIDs remain one of the simplest and most effective building blocks.
🧩 And now that you’ve seen UUIDs in 10+ languages... you’re officially fluent.