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

Data access

The panel loads rows on its own pages. This chapter covers querying Toasty from your code: a page, a record function, or a public page. The Toasty guide covers the query API in full.

Getting the database

with(db) registers the Db on the app’s router, where every panel mounted on it reads it; tablo::db::db(cx)? returns it. Cloning a Db is cheap, and statements take it by &mut:

#![allow(unused)]
fn main() {
    let mut db = tablo::db::db(cx)?;
    let users = User::all().exec(&mut db).await?;
}

Inside a record function, run statements through the transaction you were handed (ex), never through a second handle: a single-connection pool such as sqlite::memory: would wait forever for the connection the transaction holds.

Querying

#![allow(unused)]
fn main() {
    User::filter(User::fields().email().eq("ada@example.com"));
    User::filter(User::fields().name().starts_with(prefix)).order_by(User::fields().name().asc());
}

Toasty binds values as parameters. If you build a LIKE pattern from user input, escape %, _ and your escape character first and pass it with like_with_escape, as the table search does.

Load a resource’s rows through scoped_query. See Tenancy.

A public page has no resource behind it, so it states its own filters, tenant included:

#![allow(unused)]
fn main() {
    let posts = Post::filter(Post::fields().status().eq(PostStatus::Published))
        .include(Post::fields().author())
        .exec(&mut db)
        .await?;
}

Relations

Toasty loads a relation only when the query includes it. Include every relation you read, in the same query:

#![allow(unused)]
fn main() {
    let posts = Post::all()
        .include(Post::fields().author())
        .exec(&mut db)
        .await?;
    for post in &posts {
        let _name = &post.author.get().name; // no extra query
    }
}

Reading a relation that was not included panics in get(); check is_unloaded() first where a missing include is possible. A table or detail column shows a relation with RelationColumn, which declares its include, or counts one with CountColumn, which queries the count: see Tables.

A resource’s table on your own page

A page can render a resource’s list table over its own query — here, only featured posts — with the same columns, filters and row actions as the resource’s list:

#![allow(unused)]
fn main() {
    let table = tablo::wired_table::<PostResource>(cx)?;
    let query = scoped_query::<PostResource>(cx)?.filter(Post::fields().featured().eq(true));
    let body = table.render(cx, query).await?;
}

wired_table is the table the request’s panel mounted for the resource, with the row actions its policy allows, so the page and the list cannot drift apart. WiredTable::render renders it live, as the resource’s list is: a sort or search reruns your page in place. A page that renders two tables gives each a prefix with .prefixed("posts").

Outside a panel

A request outside every panel’s prefix, such as a public page, belongs to no panel, however many the router mounts: scoped_query answers it with an error naming the resource. Keep the handle mounting the panel returns (below) on the router, and enter the panel for the request:

#![allow(unused)]
fn main() {
let cx = admin.enter(cx);
let posts = scoped_query::<PostResource>(&cx)?;
}

The entered context answers as the panel mounted its resources, and runs no auth gate: PanelHandle::enter has the details.

Outside a request

scoped_query, can, write_create and the other entry points that answer from a mounted def read the resources a panel mounts, so code that runs outside a request, such as a background job, reads them from the panel the router mounts. builder.panel(..) returns that panel’s PanelHandle, which holds the router’s database and the panel’s mounted resources and is cheap to clone into a job:

#![allow(unused)]
fn main() {
pub fn admin_router(db: Db) -> topcoat::Result<(Router, PanelHandle)> {
    let mut builder = Router::builder().discover().with(db);
    let admin = builder.panel(
        Panel::new("admin")
            .resource::<PostResource>()
            .resource::<CommentResource>()
            .resource::<AuthorResource>(),
    )?;
    Ok((builder.build(), admin))
}
}

admin.context() returns a context holding the database and those resources, without declaring the panel again. Add a tenant with .with(Tenant(id)):

#![allow(unused)]
fn main() {
    let cx = admin.context().with(tablo::Tenant(tenant));
    let mut ex = tablo::db::db(&cx)?;
    let drafts = scoped_query::<PostResource>(&cx)?
        .filter(Post::fields().status().eq(PostStatus::Draft))
        .exec(&mut ex)
        .await?;
}

A context is one unit of work, as a request is, so build one per job run; it carries no signed-in user. PanelHandle::context says what it holds and what a policy sees. The url helpers answer in it, so a job’s mail links to url::page::<BillingPage>(&cx) rather than a spelled path, and the context carries the router’s BaseUrl to make that link absolute. Panel::handle(&db) builds the same handle from a panel no router mounts, such as a test’s.

Schema setup

db.push_schema().await? creates every registered table, which suits a prototype or a test. A production app manages its schema with toasty-cli migrations.

Rendering rules

Topcoat may render a page’s regions concurrently and re-render them in place, so code that renders follows three rules:

  • No side effects. Pages, layouts and components only read. Writes belong in record functions or app routes.
  • Deterministic output. No HashMap iteration order, current time or random values in a region that re-renders: a re-render must produce the same markup for the same data.
  • Untrusted signals. A value read on the server with a signal’s get() or read() comes from the client. Validate it like any request input.

Share a query between components of one request with Topcoat’s #[memoize], and add a Toasty #[index] to columns you filter on.

A table updates through a rerun of its page, which runs the page’s layers and checks like any request. Topcoat serves a shard at its own runtime path, where the page’s checks do not run: a shard you write starts with auth::guard(cx)?, and checks can_list::<R>(cx) before it lists R’s rows. Declare it on its panel with Panel::endpoint, which says which panel a shard runs as otherwise.