Skip to Content
Mélodium 0.10.1 is now available!
DocsExamplesSQL User API

SQL User API

Source: 03_sql_user_api

A minimal REST API backed by SQLite (or PostgreSQL). The database connection pool and the HTTP server are both declared as models, initialised once and shared across all concurrent request tracks.

Running

melodium run 03_sql_user_api/Compo.toml # or with PostgreSQL: melodium run 03_sql_user_api/Compo.toml --db_url postgres://user:pass@localhost/mydb
$ curl http://127.0.0.1:8080/users OK $ curl -X POST http://127.0.0.1:8080/users -d '{"name":"Alice","email":"alice@example.com"}' {"name":"Alice","email":"alice@example.com"}

How it works

Two models are declared at the top of main:

model db: AppDb(db_url=db_url) model server: HttpServer(host=|from_ipv4(|localhost_ipv4()), port=port)

Both are instantiated once. AppDb wraps SqlPool with configurable min/max connections; HttpServer accepts all incoming requests and dispatches them to whichever sub-treatment registered a matching route.

Startup sequence

The connections in main enforce a strict startup order:

connect opens the pool. The connected source fires once the pool is up, triggering createTable. Only after createTable.done does the HTTP server start — ensuring the schema exists before any request arrives.

See in Compositeur Studio

Route handlers

listUsers and echoCreate each call connection[http_server=server](method=…, route=…) to register their route. They run concurrently and independently — Mélodium creates a new track for each incoming connection automatically.

The bodyTrigger: trigger<byte>() pattern converts the incoming Stream<byte> body into a Block<void> start event. Status, headers, and body processing are all driven from that single signal in parallel:

connection.data -> bodyTrigger.stream,start --> status.trigger,emit -> connection.status bodyTrigger.start --------> headers.trigger,emit -> connection.headers

The --> (double arrow) is fan-out: one output drives two inputs at once.

Dependencies

[dependencies] std = "0.10.1" # core flows, logging, data structures http = "0.10.1" # HTTP server and client net = "0.10.1" # IP address helpers json = "0.10.1" # JSON parsing and serialisation sql = "0.10.1" # SQL connection pool and queries encoding = "0.10.1" # UTF-8 encode / decode