Modular Building Blocks for Modern Scala Applications
The philosophy is simple: use what you need, nothing more. Each block is independently useful and designed to compose with other blocks or your existing code.
libraryDependencies += "dev.zio" %% "zio-blocks-schema" % "0.0.56"val jsonStr = alice.toJsonString // {"name":"Alice","age":30}
Works with ZIO 2.x · Cats Effect 3.x · Kyo · Ox · Akka · Plain Scala
01 Principles
Use What You Need, Nothing More
- 01 Zero Lock-In No dependency on ZIO, Cats Effect, or any other effect system.
- 02 Modular Each block is a separate artifact.
- 03 Cross-Platform Most blocks cross-build for JVM and Scala.js on Scala 2.13 and 3.x. Adopt Scala 3 on your timeline.
- 04 High Performance Implementations that avoid boxing, minimize allocations, and use platform-specific features where they pay off.
- 05 Type Safety Scala's type system carries the correctness guarantees, without runtime overhead.
02 Deep Dives
Four Blocks, in Code
Schema
The Schema block brings dynamic-language productivity to statically-typed Scala. Define your data types once, and derive codecs, validators, optics, and more automatically.
The Problem
In statically-typed languages, you often maintain separate codec implementations for each data format (JSON, Avro, Protobuf, etc.). Meanwhile, dynamic languages handle data effortlessly:
// JavaScript: one line and doneconst data = await res.json();
The Solution
ZIO Blocks Schema derives everything from a single schema definition:
case class Person(name: String, age: Int)object Person {implicit val schema: Schema[Person] = Schema.derived}// Derive codecs for any format:val jsonCodec = Schema[Person].derive(JsonFormat) // JSONval avroCodec = Schema[Person].derive(AvroFormat) // Avroval toonCodec = Schema[Person].derive(ToonFormat) // TOON (LLM-optimized)val msgpackCodec = Schema[Person].derive(MessagePackFormat) // MessagePackval thriftCodec = Schema[Person].derive(ThriftFormat) // Thrift
One Schema, Many Formats
- JSON
- Avro
- BSON
- CSV
- MessagePack
- Thrift
- TOON
- XML
- YAML
- Schema reference — the full API surface, from
ReflectandBindingthrough optics, validation, and schema evolution - Migrating from ZIO Schema — a step-by-step port from ZIO Schema 1.x
Scope
Compile-time verified resource safety for synchronous Scala code. Scope prevents resource leaks at compile time by tagging values with an unnameable type-level identity—values allocated in a scope can only be used within that scope. Child scope values cannot escape to parent scopes, enforced by both the abstract scope-tagged type and the Unscoped constraint on scoped.
The Problem
Resource management in Scala is error-prone:
// Classic try/finally - verbose and easy to get wrongval db = openDatabase()try {val tx = db.beginTransaction()try {doWork(tx)tx.commit()} finally tx.close() // What if commit() throws?} finally db.close()// Using - better, but doesn't prevent returning resourcesUsing(openDatabase()) { db =>db // Oops! Returned the resource - use after close!}
The Solution
Scope makes resource leaks a compile error, not a runtime bug:
import zio.blocks.scope.*Scope.global.scoped { scope =>import scope.*val db: $[Database] = allocate(Resource(openDatabase()))// Methods are hidden - can't call db.query() directly// Must use $ to access:val result: String = $(db)(_.query("SELECT 1"))// Trying to return `db` would be a compile error!result // Only pure data (String) escapes}// db.close() called automatically
- Compile-Time Resource Safety with Scope — the step-by-step tutorial, from basic resource management through dependency injection
- Resource Management reference —
Scope,Resource,Wire,Unscoped, and finalization order
Async
A lightweight, zero-dependency asynchronous effect type. A ready Async[A] is an A, so synchronous code composed with map / flatMap allocates nothing on the happy path while still suspending on genuinely asynchronous work.
The Problem
Asynchronous Scala forces a choice between two costs. Future allocates for every combinator and needs an ExecutionContext threaded everywhere, even when the value is already available. Full effect systems avoid that but ask you to adopt a runtime, a set of type classes, and a programming model across your whole codebase—a heavy price for a library that only occasionally suspends.
The Solution
Async[A] is a value, not a wrapper. When the result is already known, the representation is the result, so composing ready values costs nothing:
import zio.blocks.async._// Constructors collapse to bare values; transformers inline with no allocationval computed: Int =Async.succeed(20).map(_ + 1).flatMap(n => Async.succeed(n * 2)).block
- Getting Started with Async — create, compose, and run async effects
- Async reference — the full API, including
zip,catchAll,collectAll, theAsync.promisecallback bridge, andFuture/CompletionStageinterop async-examples— a single-file order-fulfillment demo (cd async-examples && sbt run)
SQL
A thin, type-safe JDBC wrapper that maps Scala case classes to database tables using the same Schema you use for JSON and Avro codecs. No ORM runtime, no code generation — just composable SQL fragments, a derived repository abstraction, and a direct ZIO integration.
The Problem
JDBC is powerful but tedious: manual ResultSet traversal, index-based parameter binding, and repetitive CRUD boilerplate make even simple database access error-prone. ORMs solve the boilerplate but add heavy runtimes, hidden queries, and opaque magic.
The Solution
ZIO Blocks SQL derives everything from a single Schema[A]:
case class User(id: Long, name: String, email: String)object User:given Schema[User] = Schema.derived// Derive the table, codec, and repository in one lineval repo = Repo.derived[User, Long]// Use the sql"..." interpolator for custom queriesval frag = sql"SELECT * FROM user WHERE email = ${"alice@example.com"}"
- SQL reference —
DbCodec,Frag,Table,Repo,Transactor, dialects, and DDL generation - Query DSL guide — a four-part series building a type-safe query language on reified optics
03 Block Catalog
Take Only What You Need
Each block is a separate artifact under dev.zio. Copy the artifact name, add it to your build, and use it.
Meta Programming
Schema
Type-safe schemas with automatic codec, optic, and validator derivation
zio-blocks-schemaJVM · JS / Scala 2.13 · 3.x
Learn More about SchemaTypeId
Compile-time type identity with rich metadata
zio-blocks-typeidJVM · JS / Scala 2.13 · 3.x
Learn More about TypeId
Codecs
JSON support is built into zio-blocks-schema; the modules below add further formats.
Avro Codec
Apache Avro binary serialization with automatic schema generation
zio-blocks-schema-avroJVM / Scala 2.13 · 3.x
Learn More about Avro CodecBSON Codec
MongoDB-compatible BSON serialization with native type support
zio-blocks-schema-bsonJVM / Scala 2.13 · 3.x
Learn More about BSON CodecCSV Codec
RFC 4180-compliant CSV serialization
zio-blocks-schema-csvJVM · JS / Scala 2.13 · 3.x
Learn More about CSV CodecMessagePack Codec
Compact binary serialization with optimized streaming
zio-blocks-schema-messagepackJVM · JS / Scala 2.13 · 3.x
Learn More about MessagePack CodecThrift Codec
Apache Thrift binary serialization with TBinaryProtocol
zio-blocks-schema-thriftJVM / Scala 2.13 · 3.x
Learn More about Thrift CodecTOON Codec
Token-oriented notation 30–60% smaller than JSON, tuned for LLM prompts
zio-blocks-schema-toonJVM · JS / Scala 2.13 · 3.x
Learn More about TOON CodecXML Codec
Zero-dependency XML serialization with fluent navigation and patching
zio-blocks-schema-xmlJVM · JS / Scala 2.13 · 3.x
Learn More about XML CodecYAML Codec
Human-readable YAML serialization with JSON interop
zio-blocks-schema-yamlJVM · JS / Scala 2.13 · 3.x
Learn More about YAML Codec
Resource Management
Scope
Compile-time safe resource boundaries that keep values from escaping their lifetime
zio-blocks-scopeJVM · JS / Scala 2.13 · 3.x
Learn More about Scope
Dependency Injection
Context
Type-indexed heterogeneous collections
zio-blocks-contextJVM · JS / Scala 2.13 · 3.x
Learn More about Context
Configuration
Configuration
Typed configuration loading, feature flags, and rollout rules
zio-blocks-configJVM · JS / Scala 2.13 · 3.x
Learn More about ConfigurationConfig YAML
YAML source adapter for
ConfigSourcezio-blocks-config-yamlJVM · JS / Scala 2.13 · 3.x
Learn More about Config YAMLConfig JSON
JSON source adapter for
ConfigSourcezio-blocks-config-jsonJVM · JS / Scala 2.13 · 3.x
Learn More about Config JSONConfig HOCON
HOCON source adapter for
ConfigSourcezio-blocks-config-hoconJVM · JS / Scala 2.13 · 3.x
Learn More about Config HOCON
Web & HTTP
MediaType
Type-safe IANA media types with 2,600+ predefined types
zio-blocks-mediatypeJVM · JS / Scala 2.13 · 3.x
Learn More about MediaTypeHTTP Model
Pure HTTP data model with URL parsing, headers, cookies, and forms
zio-blocks-http-modelJVM · JS / Scala 2.13 · 3.x
Learn More about HTTP ModelHTTP Model Schema
Schema-based typed access to the HTTP model
zio-blocks-http-model-schemaJVM · JS / Scala 2.13 · 3.x
Learn More about HTTP Model SchemaEndpoint
Type-safe HTTP endpoint descriptors with composable codecs and typed auth
zio-blocks-endpointJVM · JS / Scala 2.13 · 3.x
Learn More about EndpointOpenAPI
Type-safe OpenAPI 3.1 specification generation and rendering
zio-blocks-openapiJVM · JS / Scala 2.13 · 3.x
Learn More about OpenAPIJWT
Zero-dependency JWT signing and verification with HMAC, RSA, ECDSA and EdDSA support
zio-blocks-jwtJVM · JS / Scala 2.13 · 3.x
Learn More about JWTHTML
Type-safe HTML templating with XSS protection
zio-blocks-htmlJVM · JS / Scala 2.13 · 3.x
Learn More about HTMLDatastar
Typed Datastar attribute and signal DSL, plus the SSE events that patch a live page
zio-blocks-datastarJVM · JS / Scala 3.x
Learn More about DatastarHTMX
Typed HTMX DSL for compile-time-checked HTMX attributes
zio-blocks-http-htmxJVM · JS / Scala 3.x
Learn More about HTMX
Data Types
Chunk
High-performance immutable indexed sequences with zero-boxing builders
zio-blocks-chunkJVM · JS / Scala 2.13 · 3.x
Learn More about ChunkMaybe
Low-allocation optional values backed by
nullzio-blocks-maybeJVM · JS / Scala 2.13 · 3.x
Learn More about MaybeCombinators
Compile-time composition and decomposition of tuples, eithers, and unions
zio-blocks-combinatorsJVM · JS / Scala 2.13 · 3.x
Learn More about Combinators
Concurrency
Async
Zero-allocation asynchronous effect type with direct-style
awaitzio-blocks-asyncJVM · JS / Scala 2.13 · 3.x
Learn More about AsyncMux
Thread-safe multiplexer for HTTP/2, QUIC, and WebSocket-style protocols
zio-blocks-muxJVM · JS / Scala 2.13 · 3.x
Learn More about MuxRingBuffer
Lock-free bounded ring buffers (SPSC, SPMC, MPSC, MPMC)
zio-blocks-ringbufferJVM · JS / Scala 2.13 · 3.x
Learn More about RingBuffer
Streaming
Streams
Pull-based streaming with typed errors, zero boxing, and synchronous or asynchronous execution
zio-blocks-streamsJVM · JS / Scala 2.13 · 3.x
Learn More about Streams
Telemetry
Telemetry
Zero-dependency OpenTelemetry-aligned tracing, logging, and metrics
zio-blocks-telemetryJVM · JS / Scala 2.13 · 3.x
Learn More about TelemetryOTLP Export
OTLP exporters bridging telemetry signals to an OpenTelemetry collector
zio-blocks-telemetry-otelJVM / Scala 2.13 · 3.x
Learn More about OTLP Export
Persistence
SQL Module
Type-safe JDBC wrapper with schema-derived codecs and a CRUD repository
zio-blocks-sqlJVM · JS / Scala 3.x
Learn More about SQL ModuleZIO Integration
ZIO integration with
ZIO.attemptBlockingandZLayerzio-blocks-sql-zioJVM / Scala 3.x
Learn More about ZIO IntegrationData Migration
Typed, online database schema migrations in three execution models, with no hand-written SQL
zio-blocks-data-migrationJVM · JS / Scala 3.x
Learn More about Data MigrationProjection
Event-sourced projections with per-entity SQLite storage
zio-blocks-projectionJVM / Scala 3.x
Learn More about Projection
Tooling & Codegen
Code Generation
Generic Scala code generation IR and emitter
zio-blocks-codegenJVM / Scala 2.13 · 3.x
Learn More about Code GenerationDocs
GitHub Flavored Markdown parsing, rendering, and programmatic construction
zio-blocks-markdownJVM · JS / Scala 2.13 · 3.x
Learn More about DocsSmithy
Smithy IDL parser and AST library for API modeling
zio-blocks-smithyJVM / Scala 2.13 · 3.x
Learn More about Smithy