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

Records in Files

A resource’s source keeps its records as files: one file per record in a folder of the project, read into the table when the application starts and written back on every save.

# descriptors/posts.yaml
entity: posts
path: /posts
title: Posts
permission: acme.blog.manage_posts
source:
  kind: git
  path: content/posts
  format: markdown
  key: slug
  body: body
  slug_from: title
list:
  creatable: true
  deletable: true
  columns:
    title: { searchable: true }
form:
  title: Post
  fields:
    slug: {}
    title: { rules: [required] }
    pinned: { type: switch }
    body: { type: textarea, rows: 12 }
OptionDescription
pathThe folder, relative to the project root. Required.
kindfiles writes the file; git also commits it; or the code of a source a module registered. Default files.
formatyaml, json, or markdown (YAML front matter and a body). Default yaml.
keyThe text field that names the file. Default slug.
bodyWith markdown, the field the body under the front matter holds. Optional.
slug_fromA field the key is derived from when a save leaves it blank. Optional.

Scaffold it

lat make:resource article --field title:text --source content/articles --format markdown --git
FlagDescription
--source <DIR>The folder; a slug column names each file.
--formatyaml, json or markdown; markdown adds a body column. Default yaml.
--gitCommit every save.

Declare it from Rust

use laterite_admin::Resource;
use laterite_core::source::{Format, Source};

let posts = Resource::new("/posts", "Posts", list)
    .form(form)
    .permission("acme.blog.manage_posts")
    .source(
        Source::git("content/posts", Format::Markdown)
            .body("body")
            .slug_from("title"),
    );
ConstructorSource
Source::files(path, format)The folder, written.
Source::git(path, format)The folder, written and committed.
.body(column), .slug_from(column)The options above.

Write a file

<!-- content/posts/hello.md -->
---
title: Hello
pinned: true
---
# Hello

The first post.
FormatFileHolds
yamlhello.yamlThe form’s fields as a YAML document.
jsonhello.jsonThe form’s fields as a JSON object.
markdownhello.mdThe fields as front matter, the body field under it.

The file’s name is the key field: letters, digits, dots, dashes and underscores. The fields are written in the form’s order. A switch is true or false, a number input is a number, a repeater is a list, anything else is text.

Starting the application reads the folder: a file’s record is created or updated by key, and a record whose file is gone is removed. A file naming a field the form does not have fails the start, naming the file and the field.

Fit the table

Table::create()
    .table(Alias::new("posts"))
    .col(ColumnDef::new(Alias::new("id")).big_integer().not_null().auto_increment().primary_key())
    .col(ColumnDef::new(Alias::new("slug")).text().not_null())
    .col(ColumnDef::new(Alias::new("title")).text().not_null())
    .col(bool_col(Alias::new("pinned")).null())
    .col(ColumnDef::new(Alias::new("body")).text().null())
    .to_owned()
ColumnRule
The key fieldA text column; the form’s unique rule keeps one file per key.
A form fieldAny column type the field stores.
A column the form does not haveNeeds a default, or null.

Commit every save

source:
  kind: git
  path: content/posts
ActionCommit
CreateAdded content/posts/hello.md (ana)
EditUpdated content/posts/hello.md (ana)
Rename the keyThe old file is removed in the same commit.
DeleteRemoved content/posts/hello.md (ana)

The repository is the project root; git is on the path with a user name and email configured. Push the branch to publish: a host that builds on push rebuilds the site.

git push origin main

Build a page per record

use laterite_web::theme::build::{PathsReg, TableKeys};

registry.add(PathsReg::new(
    "/posts/:slug",
    Arc::new(TableKeys::new("posts", "slug")),
));
# pages/post.htm
url: /posts/:slug
components:
  post: { component: "acme.posts", slug: "{{ :slug }}" }
NameGives
PathsReg::new(pattern, paths)The pattern’s pages: the build renders it once per set of parameters paths answers.
TableKeys::new(table, column)One set per value of column, under the parameter of that name; .param(name) renames it.
PathsA trait for any other answer: async fn params(&self, db) -> Vec<Params>.

lat make:resource --source writes a src/site/<entity>.rs with the component and the PathsReg for /<table>/:slug, registered from the module. A pattern no module answers for is skipped by the build.

Register a source

use laterite_core::source::{PullReport, Pushed, RecordSource, RecordSourceReg, SourceCx, SourceError};

pub struct Sheets;

#[async_trait]
impl RecordSource for Sheets {
    async fn pull(&self, cx: &SourceCx<'_>) -> Result<PullReport, SourceError> {
        let sheet = cx.source.options["sheet"].as_str().unwrap_or_default();
        todo!("read {sheet} into {}", cx.entity)
    }
    async fn push(&self, cx: &SourceCx<'_>, pushed: &Pushed) -> Result<(), SourceError> {
        let row = cx.row(pushed.id).await?;
        todo!("write {row:?}")
    }
    async fn remove(&self, cx: &SourceCx<'_>, key: &str) -> Result<(), SourceError> {
        todo!("remove {key} from {}", cx.source.path)
    }
}

registry.add(RecordSourceReg::new("acme.sheets", Arc::new(Sheets)));
source:
  kind: acme.sheets
  path: content/guests
  sheet: Guests
MethodCalled
pullWhen the application starts. Creates or updates each row by key and removes the rest.
pushAfter a save commits, with the row’s id, the operation, and the key before a rename.
removeAfter a delete commits, with the row’s key.

cx.row(id) gives the row as the key, the columns by shape, and the body; cx.source.options holds the keys beyond the ones above; cx.actor names who is writing.