Skip to main content

zero-server

A community Python server SDK for Zero. It is not affiliated with or endorsed by Rocicorp.

It ports what @rocicorp/zero/server gives a TypeScript backend: the query endpoint (ZERO_QUERY_URL), the mutate endpoint (ZERO_MUTATE_URL), the ZQL query builder, defineQueries and defineMutators, and tx.mutate and tx.run on Postgres. The client and zero-cache stay in TypeScript, and a Python backend serves the same TypeScript client a TypeScript backend would.

Compatibility is pinned to one Zero release at a time. This version is tested against @rocicorp/zero 1.11.3 (see ZERO_VERSION).

Usage

pip install "zero-server[sqlalchemy,asyncpg,starlette]"   # or [asyncpg], or [psycopg]

The schema stays in your TypeScript schema.ts, because the client needs it. Emit it as JSON, and generate Python types from it:

// emit-schema.ts
import {schema} from './schema.ts';
process.stdout.write(JSON.stringify(schema));
node emit-schema.ts > schema.json
zero-server codegen schema.json -o zero_schema.py

zero_schema.py has a TypedDict per table for a row, an insert, an update and a key, and a builder and tx.mutate typed by them. A server then looks like this:

from pathlib import Path

from pydantic import BaseModel
from sqlalchemy.ext.asyncio import create_async_engine
from starlette.applications import Starlette
from zero_schema import Project, create_builder, mutate

from zero_server import ApplicationError, define_mutator, define_mutators, define_queries, define_query, load_schema
from zero_server.integrations.starlette import zero_routes
from zero_server.server.adapters.sqlalchemy import SQLAlchemyDatabase

schema = load_schema(Path("schema.json"))
zql = create_builder(schema)
db = SQLAlchemyDatabase(create_async_engine("postgresql+asyncpg://..."), schema)


@define_query
def my_projects(*, args, ctx):
    return zql.project.where("ownerID", ctx["userID"]).related("tasks").order_by("name", "asc")


class NewTask(BaseModel):
    id: str
    projectID: str
    title: str


@define_mutator(NewTask)
async def create_task(*, tx, ctx, args: NewTask):
    project: Project | None = await tx.run(zql.project.where("id", args.projectID).one())
    if project is None or project["ownerID"] != ctx["userID"]:
        raise ApplicationError("Not your project")
    await mutate(tx).task.insert({"id": args.id, "projectID": args.projectID, "title": args.title})


app = Starlette(
    routes=zero_routes(
        schema=schema,
        database=db,
        queries=define_queries({"projects": {"mine": my_projects}}),
        mutators=define_mutators({"task": {"create": create_task}}),
        authenticate=verify_token,  # your function: request -> user ID, or None when logged out
    )
)

Point ZERO_QUERY_URL and ZERO_MUTATE_URL at /query and /mutate. With FastAPI, app.router.routes.extend(zero_routes(...)). With another framework, call handle_query_request and handle_mutate_request yourself; zero_server/integrations/starlette.py is short enough to copy. The types to annotate those handlers (QueryRequestHandler, TransactFn, MutateResponse, MutationResponse, ServerPushFailed and the rest) import from zero_server, and server_ast(query, schema) returns the AST zero-cache receives for a query.

  • Databases. SQLAlchemyDatabase (SQLAlchemy's asyncio engine on asyncpg or psycopg; the sqlalchemy extra installs neither driver), AsyncpgDatabase (an asyncpg pool) and PsycopgDatabase (a psycopg 3 async pool), under zero_server.server.adapters. A push's mutations run on one connection from the pool, taken at its first transaction and returned when the push ends. So mutators in one push share session state (a SET without LOCAL, session advisory locks, temp tables), and a handler that takes a second connection from the same pool while its push holds one can deadlock once the pool has fewer connections than there are concurrent pushes. Another driver plugs into ZQLDatabase as a DBConnection whose query takes $1 placeholders.
  • Mutator results. What a mutator returns, if not None, reaches the client as the mutation's result data, and is also written to zero-cache's mutations table so it survives a lost response. Python has one None, so a mutator cannot return JSON null as data the way a TypeScript mutator can.
  • Re-running a mutator. tx.retry_on(predicate) asks predicate(error, attempt), sync or async, whether to run the mutator again in a fresh transaction when the run fails, including at COMMIT. Only True means yes. A mutator runs at most 5 times per push, and tx is unusable once its transaction has ended, inside the predicate too.
  • Isolation level. Each adapter takes isolation_level="read committed" | "repeatable read" | "serializable" for the transactions mutations run in. Unset, the database's default applies.
  • Validation. @define_query(Model) and @define_mutator(Model) parse args with a Pydantic model or TypeAdapter first; a failure reaches the client as upstream's validation error.
  • Queries are shared. Like upstream's, the builder hands back the query it built before when a step repeats one, and the query endpoint reuses a query's mapped AST and its JSON across responses. Treat a built query and its AST as immutable.
  • Strict building. create_builder(schema, strict=True) refuses an unknown column when the query is built. Upstream only refuses it when the endpoint maps the query, failing the whole batch, so this is for your tests.

How compatibility is checked

The published npm package is the definition of correct. Every test diffs Python against it:

  • Wire. harness/ runs the real handleMutateRequest, handleQueryRequest, PushProcessor and the query builder under Node. Hypothesis generates pushes (with duplicates, gaps, refusals and cleanup mutations) and builder programs, sends each through both sides against identical Postgres databases, and diffs the responses and every row written.
  • HTTP. The raw bytes of a request go to upstream's handlers as a Fetch Request and to the Starlette routes, so query-string parsing, body decoding and JSON.parse's error messages are compared too, along with the response as JSON.stringify writes it.
  • SQL. The same generated queries run through upstream's ZQLDatabase.run and through tx.run against one seeded database, and the result rows are compared, never the SQL text.
  • Adapters. The wire and SQL tiers run against every adapter.
  • End to end. e2e/run.py starts Postgres, two rocicorp/zero containers at the pinned version, the TypeScript reference app server and the Python one. An unmodified Zero client runs the same script through each, and the run fails if the client ends up with different data, the databases differ, or the reference run strays from e2e/expected.json.

Run them with uv run pytest (needs Node 24 and Docker, or ZERO_SERVER_PG pointing at a Postgres) and uv run --group e2e python e2e/run.py.

A diff that passes only covers the inputs tried, so uv run python -m tests.compat then checks the inputs were enough. Every line and branch of the upstream modules the port cites must have run inside the npm package during the tests, and every line and branch of zero_server must have run during a differential test. docs/compat.md explains the check, lists what it leaves out and why, and records the known differences.

Performance

bench/ runs the same workloads through zero-server and the published package. The CPU-only cases (the query endpoint, building and name-mapping queries, request parsing, the mutate endpoint over an in-memory database, z2s) are CodSpeed benchmarks on both sides, and CodSpeed flags a pull request that makes any of them slower. uv run python -m bench times every case on the wall clock, including those that wait on Postgres (tx.run, mutate, PushProcessor, per adapter), and prints each case's Python/TypeScript ratio. CI shows that table without gating on it. bench/__init__.py has the commands.

Python/TypeScript ratios at ec0768a against @rocicorp/zero 1.11.3, on an Apple Silicon laptop (median of the rounds; below 1 means Python is faster):

case Py/TS
Query endpoint with JSON, 1 to 100 queries (query_json) 1.3 to 1.7
Query endpoint, parsed body in, objects out (query) 1.3 to 2.4
Building a query (build), mapping its AST (map) 6 to 14
Parsing 1000-item bodies from JSON (parse_json) 1.4 to 1.5
Mutate endpoint over an in-memory database (mutate_cpu) 2.1 to 4.0
Compiling ZQL to SQL (z2s) 0.8 to 1.1
tx.run on Postgres, every adapter (run) 0.8 to 1.1
Mutate endpoint and PushProcessor on Postgres, every adapter 0.7 to 1.0
Process start with imports, asyncpg / psycopg / SQLAlchemy 1.1 / 1.5 / 2.0

Building and mapping cost microseconds on either side; the endpoint reuses both for a repeated query. Wherever Postgres is involved, the database dominates, and Python is within 10% of npm or faster.

Known differences

docs/compat.md has the full list. The two most likely to matter: the deprecated upstream exports (handleTransformRequest, getMutation, the positional handler overloads, tx.query and the rest) are not ported, and where upstream's result depends on its JavaScript driver, zero-server does what the postgres.js adapter does.

License

Apache-2.0. Zero itself is Apache-2.0, and code ported from rocicorp/mono keeps its notices.

Metadata

Release files for zero-server 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zero-server 0.0.5
File Size Uploaded Provenance Actions
zero_server-0.0.5.tar.gz 93.9 kB With provenance Details for zero_server-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for zero-server 0.0.5
File Interpreter ABI Platform Actions
zero_server-0.0.5-py3-none-any.whl Python 3 none any Details for zero_server-0.0.5-py3-none-any.whl

Total release size: 207.5 kB

Release files / zero_server-0.0.5.tar.gz

Download URL zero_server-0.0.5.tar.gz
Size 93.9 kB
Tags Source
SHA-256 checksum
How to use checksums
819464d30cb7a4d8cd00fb46ff6bd2d939f0d71b761330a013d6efc1463b73a5
BLAKE2b-256 checksum
How to use checksums
c7a68af5f57167b62cc59bab0e0513d36762fd7736600e205ce56a0d69544ab3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / zero_server-0.0.5-py3-none-any.whl

Download URL zero_server-0.0.5-py3-none-any.whl
Size 113.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7212b25ebf10a15847e5fa5b1a500543c280094fa95d6d01fa51095dc663e8ba
BLAKE2b-256 checksum
How to use checksums
e59051140e5e109a978f322bc8e933135a665cf3c107b409b9d32732e0e214de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for zero-server 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for zero_server-0.0.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page