Backend Development

This section covers building Stately backends with Rust, including entity definitions, state management, API generation, and plugin integration.

Core Crates

CrateDescriptionAPI Reference
statelyCore types, traits, and collectionsdocs.rs/stately
stately-deriveProcedural macros for code generationdocs.rs/stately-derive

Plugin Crates

CrateDescriptionAPI Reference
stately-filesFile upload and versioningdocs.rs/stately-files
stately-arrowData connectivity and SQL queriesdocs.rs/stately-arrow

Macros

The stately-derive crate provides three main macros:

  • #[stately::entity] - Define entities with name traits
  • #[stately::state] - Generate state management infrastructure
  • #[stately::axum_api] - Generate HTTP API handlers

See Entities and State for detailed usage.

Core Types

  • EntityId - UUID v7 identifiers
  • Link<T> - Entity relationships (see Links)
  • Collection<T> - Entity collections
  • Singleton<T> - Single-instance containers

Quick Reference

Minimal Entity

use serde::{Deserialize, Serialize};

#[stately::entity]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MyEntity {
    pub name: String,
}

Minimal State

#[stately::state(openapi)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AppState {
    my_entities: MyEntity,
}

Minimal API

use std::sync::Arc;
use tokio::sync::RwLock;

#[stately::axum_api(AppState, openapi)]
#[derive(Clone)]
pub struct ApiState {
    #[state]
    pub app: Arc<RwLock<AppState>>,
}

impl ApiState {
    pub fn router(self) -> Router<Self> {
        Self::router(self)
    }
}

Feature Flags

FeatureDefaultDescription
openapiYesOpenAPI schema generation
axumNoAxum framework integration

Enable features in Cargo.toml:

[dependencies]
stately = { version = "0.*", features = ["axum"] }