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

Getting Started

Add dependencies

The default afastorm features are derive and sqlite. Enable migrate if you want the migration API and CLI:

[dependencies]
afastorm = { version = "0.0.1", features = ["migrate"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde_json = "1"

For PostgreSQL, disable defaults and select the needed features:

afastorm = { version = "0.0.1", default-features = false, features = [
    "derive", "postgres", "migrate", "uuid", "chrono"
] }

SQLite and PostgreSQL features may also be enabled together. Optional Rust SQLx types require their matching afastorm feature. See SQLite Types and PostgreSQL Types for the exact mappings. The full feature list and each feature’s dependencies are in Cargo Features.

Connect

Orm::connect chooses a built-in driver from the connection URL. SQLite:

let orm = Orm::connect("sqlite://app.db?mode=rwc").await?;

PostgreSQL:

let orm = Orm::connect("postgres://user:password@127.0.0.1:5432/app").await?;

Keep credentials outside source control; connection strings can be read from environment variables such as DATABASE_URL.

Register and use a model

use afastorm::{ModelRegistry, Orm};

let orm = Orm::connect(database_url)
    .await?
    .with_models(ModelRegistry::new().register::<User>());
let users = orm.repo::<User>()?;

Repositories retain the ORM handle, so operations do not take a pool parameter. Models must be registered before calling repo; registration does not create tables. Use reviewed migrations to provision the schema.

For complete examples, see Models, the CRUD Guide, and Repositories and Queries.

Run tests

SQLite tests:

cargo test --no-default-features --features derive,sqlite,migrate

PostgreSQL integration tests are ignored unless explicitly selected and require a dedicated test database. Set AFASTORM_TEST_DATABASE_URL and run:

cargo test --no-default-features --features derive,postgres,migrate --test orm_postgres -- --ignored

These tests create tables and migration records in the configured database. Never point them at a production or shared database.