Quickstart

This page is the smallest generic setup path for trying CrateStack in a Rust service.

1. Include A Schema

Example schema:
auth Principal {
  id String
  role String?
}

mixin AuditFields {
  createdAt DateTime @default(dbgenerated())
  updatedAt DateTime @default(dbgenerated())
}

model Post {
  @use(AuditFields)

  id String @id
  title String

  @@allow("read", auth() != null)
}
use cratestack::include_schema;

include_schema!("schema.cstack");

2. Build The Generated Runtime

let pool = sqlx::PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
let cool = cratestack_schema::CrateStack::builder(pool).build();

3. Use Delegates Directly

You can use CrateStack without mounting the generated REST layer.
let visible_posts = cool
    .post()
    .find_many()
    .where_expr(
        cratestack_schema::post::author()
            .email()
            .eq("owner@example.com")
            .and(cratestack_schema::post::published().is_true()),
    )
    .order_by(cratestack_schema::post::author().email().desc())
    .limit(20)
    .run(&ctx)
    .await?;

4. Optionally Bind Auth For Internal Callers

let bound = cool.bind_auth(Some(serde_json::json!({
    "id": 7,
    "role": "admin"
})))?;

let posts = bound.post().find_many().run().await?;

5. Optionally Mount Generated Routers

let app = axum::Router::new().nest(
    "/api",
    cratestack_schema::axum::model_router(
        cool.clone(),
        cratestack_codec_cbor::CborCodec,
        AppAuthProvider,
    ),
);
  1. ../guides/auth-provider for the host auth boundary
  2. ../architecture/transport-architecture for codec, framing, and envelope rules
  3. ../adoption/vaam-catalog for the longer repo-local adoption guide