"UUIDs in 10 Languages" Part 7: UUID Generation in Even More Languages

    August 3, 2024
    10 min read
    Tutorial
    Code examples
    uuid
    distributed-systems

    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

    bash
    npm install uuid

    📦 Generate UUIDs

    ts
    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

    scala
    import java.util.UUID
    
    val id = UUID.randomUUID()
    println(s"Generated UUID: $id")

    💡 In Play Framework / JSON

    scala
    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.

    sql
    CREATE EXTENSION IF NOT EXISTS "pgcrypto";
    
    SELECT gen_random_uuid();

    ✅ Use in Tables

    sql
    CREATE TABLE users (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      name TEXT
    );

    🔹 MySQL

    MySQL (8.0+) supports UUID() and UUID_TO_BIN().

    sql
    SELECT UUID();
    
    -- Better storage
    SELECT UUID_TO_BIN(UUID(), true);

    🌄 Elixir & Erlang

    🔧 Elixir

    Use Ecto.UUID or the uuid package:

    elixir
    UUID.uuid4()
    # "550e8400-e29b-41d4-a716-446655440000"

    Add to mix.exs:

    elixir
    {:uuid, "~> 1.1"}

    Use Ecto.UUID to auto-generate DB keys in Phoenix:

    elixir
    @primary_key {:id, :binary_id, autogenerate: true}

    🧱 Erlang

    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.

    bash
    uuidgen
    # Output: 550e8400-e29b-41d4-a716-446655440000

    🔄 Pipe to API, Script, or DB Insert

    bash
    curl -X POST -d "id=$(uuidgen)" https://your-api

    📘 Summary Table

    Language/ToolFunctionNotes
    TypeScriptuuid.v4()Strong typing, validation helpers
    ScalaUUID.randomUUID()JVM-native, interoperable
    PostgreSQLgen_random_uuid()Compact, indexed, native UUID
    MySQLUUID_TO_BIN(UUID())16-byte binary storage preferred
    ElixirUUID.uuid4()Easy integration with Ecto
    BashuuidgenGreat 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.

    Generate Your Own UUIDs

    Ready to put this knowledge into practice? Try our UUID generators:

    Generate a Single UUID

    Create a UUID with our fast, secure generator

    Bulk UUID Generator

    Need multiple UUIDs? Generate them in bulk

    Summary

    This bonus entry in the UUID series expands beyond the initial 10 languages to include UUID generation in TypeScript, Scala, SQL (PostgreSQL/MySQL), Elixir/Erlang, and Bash. With idiomatic examples and key tips, you'll be UUID-ready across any tech stack.

    TLDR;

    UUIDs are supported across nearly every modern language and platform — if you know where to look.

    Key takeaways:

    • TypeScript and Node use uuid with strong type support
    • SQL databases offer native UUID types or functions (gen_random_uuid(), UUID())
    • Elixir/Erlang use binary UUIDs with Ecto.UUID and :uuid libs
    • Even Bash can generate UUIDs on most Unix systems via uuidgen

    This part rounds out the series with tools you may use every day in ops, scripting, or functional stacks.

    Cookie Consent

    We use cookies to enhance your experience on our website. By accepting, you agree to the use of cookies in accordance with our Privacy Policy.