Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Introduction

rind (Rind Is Not Django) is a framework for writing REST APIs on PostgreSQL, in Rust. It is written for people who know Django and Django REST framework, and who want to find the same ideas in Rust: models, a query layer, migrations, serializers, filters, permissions, viewsets.

Here is a complete endpoint, a list of trains that anyone signed in can read, that the staff can change, searchable and sortable:

#![allow(unused)]
fn main() {
#[derive(Resource)]
#[resource(model = Train, path = "trains", out = TrainOut, filters = TrainFilters, input = TrainIn, delete)]
#[rule(staff)]
#[rule(authenticated, actions(list, retrieve))]
#[search(Train::name(), unaccent)]
#[ordering(by(id, number, name), default("number"))]
pub struct TrainResource;
}

If you know DRF, you can probably read it already: a ModelViewSet with its serializer, its filterset, its permissions, its search_fields and its ordering_fields.

Not a port

rind is not Django translated into Rust. It keeps Django’s ideas, and changes two things about them.

It decides at compile time what Django decides at run time. A field that does not exist, a relation followed the wrong way, a rule that names an action the resource does not have: these are errors of the compiler, not exceptions in production.

It does nothing behind your back.

  • Nothing is filtered silently. A model whose table holds rows that most code must not read has no default query at all, so every use says which rows it reads.
  • Nothing runs because a row was saved, unless the model says so. There are no signals. What every write of a model does is listed on the model (#[model(behaviours(Tracking))]); anything else is a function, called by name.
  • A filter through a “many” relation never repeats a row. It is written as an EXISTS, not a join, so there is no .distinct() to remember.
  • A list reads only the columns its fields need, not the whole row, and nested objects are loaded once per page, never once per row.
  • Pages never overlap. Every ordering ends with the primary key.

How to read this book

  • New to rind? Follow the tutorial. In six parts, it builds a small library API from an empty folder to a tested application, and introduces every important idea on the way.
  • Looking for the rind way of something you know in Django? rind for Django developers maps the Django and DRF names to rind’s, with links to the chapters.
  • Working on a subject? The topic guides explain one subject each, from the common case to the rare one, and end with a reference section. They are grouped as in the table of contents:

The words with a precise meaning (domain, resource, mode, scope…) are defined in the glossary.

The API documentation of the crates (rustdoc) is beside this book when it is read on the project’s site.

Where it stands

rind is young. It was grown against a real Django application of some size (several hundred models), whose answers it was compared with, request by request, and it is tested on PostgreSQL. It has not been released yet: the names in this book can still change.

It runs on axum and on actix-web. The core knows neither: each has its adapter, behind a Cargo feature of the same name, and your code is the same with both.

The crates

You depend on rind; it re-exports the others.

CrateWhat it is
rindwhat an application depends on: it re-exports the crates below, and holds the plugins, each behind a Cargo feature
rind-querythe query layer, without any database driver
rind-dbPostgreSQL: running queries, migrations, replicas, reading a database’s schema
rind-apiresources, serializers, permissions, the two web adapters, the hooks plugins add
rind-macrosthe derives: Model, Resource, Serializer, Input, FilterSet…
rind-clithe rind tool: new projects, domains, models, resources, a project written from a database
rind-authsigning in: tokens, passwords, password reset, sign-in codes. A separate dependency, not re-exported by rind (Signing in)

Most examples in the topic guides come from the sample application of the repository, the railway: you can read it whole, run it and change it. The tutorial builds its own, smaller application.