Skip to content
ERPForAll DocsDevelopment

    Add a store topic

    The web client loads what it shows with one call, POST /json/2/mail.store/store_fetch, that runs a list of named topics in one read-only transaction on the primary and answers one store payload with one as-of (Store wire). A topic is the unit you add: a name, parameters, an audience, an optional access check and a statement budget. Behavior is fixed by docs/specs/mail/store-fetch.md (SPEC-STOREFETCH-001 to 016).

    {"wire": 1, "topics": [{"name": "messaging.self"}, {"name": "schema"}]}

    The answer is {"store": <payload>, "topics": [<slot>, ...]}; slot i answers request item i: {"topic": name, "ok": true} (with data when the topic returns a document) or {"topic": name, "ok": false, "error": {"kind": "access", "message": ...}}.

    SituationResult
    Unknown topic (or one of an addon not installed), bad or oversized params, more than 16 topics, an unsupported wirethe whole call is refused with Validation before any topic runs; the wire refusal starts with client_outdated:
    The audience or the access check denies, or the topic raises Accessthat slot only; its staged records are dropped; the other topics answer
    Any other error from a topic, or a topic over its statement budgetthe whole call fails with that error

    A topic is never skipped or answered empty in place of an error.

    use erp_addon_mail::store_fetch::{
    Audience, ParamDecl, SchemaFragment, SchemaPart, StoreTopic, TopicFuture, TopicInput,
    TopicOutput,
    };
    use erp_sdk::Transaction;
    use erp_sdk::store_wire::{FieldKind, FieldSpec, FieldValue};
    const PARAMS: &[ParamDecl] = &[ParamDecl::int("limit", 1, 50)];
    fn run<'a>(
    tx: &'a mut Transaction,
    input: &'a TopicInput<'a>,
    out: &'a mut TopicOutput,
    ) -> TopicFuture<'a> {
    Box::pin(async move {
    let limit = input.params().int("limit")?;
    let rows = tx
    .query("SELECT id::int8 FROM res_partner ORDER BY id LIMIT $1::int8", &[&limit])
    .await?;
    for row in &rows {
    let id: i64 = row.get(0);
    let id = u64::try_from(id).expect("ids are positive");
    out.set("demo.partner", id, "known", FieldValue::Bool(true));
    }
    Ok(())
    })
    }
    erp_addon_mail::store_topic!(
    DEMO_TOPIC = StoreTopic::new("my_addon", "demo.partners", run)
    .audience(Audience::Internal)
    .params(PARAMS)
    .statements(1)
    );
    fn schema(fragment: &mut SchemaFragment) {
    fragment.field("demo.partner", "known", FieldSpec::new(FieldKind::Bool));
    }
    erp_addon_mail::store_schema_part!(DEMO_SCHEMA = SchemaPart::new("my_addon", schema));
    • Name: 1 to 64 bytes of a-z, 0-9, _ and ., starting with a letter; unique across addons.
    • Parameters: flat, declared with ParamDecl::int, text, boolean, ids; each has its own bound, all are required unless .optional(). The runner validates them before any topic runs; read them with input.params(). Params of one topic are at most 4096 bytes and 4 levels deep.
    • Audience: Internal (members of base.group_user) or LoggedIn (any user principal).
    • Access check: .access(f) adds a record-level check run after the audience; return an Access error to deny. Never put record data in an access message: it reaches the caller.
    • Statement budget: .statements(n) is the most statements the check and the run may send together; going over fails the call. Batch your reads; a statement per record is the mistake it catches. A parameter that selects a costlier shape of the topic raises the ceiling of the calls that pass it with .statements_plus(&[("around", 1)]): the named optional parameter adds that many statements (StoreTopic::budget_for(params) is the ceiling of one call, budget() that of the base shape). The registry refuses an entry that names a parameter the topic does not declare as optional, names one twice or adds 0 (Programming). A topic that asks for the caller’s groups (a standing check) declares .needs_groups(): the runner then resolves the caller, per topic after the audience check and once per call, outside the topic’s span, because the lazy first group check of a transaction would otherwise be charged to whichever topic came first. That one statement is still sent; it is the only one moved out of a topic’s count, so the whole call is the budget plus one (snapshot excluded). See the store-topic ADR, section (g).
    • Output: stage records with out.set and out.delete and a document with out.data. Nothing reaches the answer unless the topic succeeds; a record the schema refuses fails the call with Internal.
    • Schema: contribute the models and fields your topic sends with a SchemaPart. Two addons may declare the same field only identically. The schema generation is derived from the content, so you never bump a number.
    • Read-only: the transaction is READ ONLY; a write fails. Marking messages read is a separate write call.
    • Expected set: topics are contributed through a link-time slice, and the linker can drop a registration that nothing references; the topic is then unknown to callers without any other sign. Check that your topics are present: add your Expected::new("my_addon", &["demo.partners"], true) to the startup check of the link root, TopicRegistry::require_linked(&[...]), which fails naming the missing entry and the owner crate. Collect the linked items with erp_sdk::extensions::items::<StoreTopic>(TOPIC_SLOT) and items::<SchemaPart>(SCHEMA_SLOT), and the link root that ships your addon makes that call, and a test in a binary that links your crate can too.
    • Two topics, one record: topics of one call may stage the same value for a field of a record (it is applied once); different values for one field, or a delete in one topic and a field set in another for the same record, fail the call with an Internal error naming both topics.

    The example above is compiled by addons/mail/tests/store_topic_guide.rs.

    Thread models are in the store schema by themselves

    Section titled “Thread models are in the store schema by themselves”

    An addon that gives a model the thread mixin (#[extend(Model, addon = "my_addon", inherit = [MailThread])]) and a <chatter/> on its form declares nothing more for the mail store. The schema served to a database adds every concrete model of its registry that inherits mail.thread (TopicRegistry::schema_for, no fields: the web client holds the chatter’s record by it). The registry is the database’s installed set, so installing, upgrading or uninstalling such an addon changes the schema and its generation on every server process, and the next chatter fetches the new schema. A page that still holds an older schema renews it once when an answer names a newer generation.

    The list of thread models tells which applications are installed, which only internal users may know. A caller outside base.group_user (portal, public) is served a schema that names only the thread models it may read (TopicRegistry::schema_for_reader), under a generation of its own that never equals the internal one, so a client never holds the other audience’s schema; its answers are encoded against that schema. Today no shipped addon gives the portal group read access to a document model, so a portal user’s schema names none of them.

    The test every_thread_model_is_a_model_of_the_store_schema (crates/erp-cli/tests/chatter_on_forms_install.rs) installs product and mail and checks every thread model is in the served schema; mail_on_a_populated_database_gives_entries_orders_and_transfers_their_chatter (crates/erp-cli/tests/document_chatter.rs) checks that installing sale after a first schema request adds sale.order under a new generation.

    Fields beyond the model name still come from a SchemaPart, and until installed-only filtering of the parts exists only the mail addon may contribute one: a part of another addon makes the linked registry refuse with a Programming error.