Skip to content
ERPForAll DocsDevelopment

    The client messaging store: reading records and adding typed fields

    One MessagingStore per browser tab serves the chatter, Discuss, chat windows and the messaging menu (ADR 0148 D1). Surfaces never keep message, thread, channel or partner data of their own: they read it here. Package: @erpforall/mail-store (web/packages/mail-store). Behavior: docs/specs/mail/messaging-client-store.md. Executable examples: web/packages/mail-store/tests/ (the test addon is extension.test.ts).

    const message = store.record("mail.message", 5); // a handle: (model, id)
    message.get("body"); // tracked per (record, field) inside a reactive scope
    message.one("author")?.get("name"); // many2one: a handle on the target record
    message.many("attachments").map((a) => a.get("name")); // x2many: handles in ascending id order
    store.record("mail.thread", 1).counter("unread"); // { value, saturated, stale }

    A handle is a view, not a copy: a read that finds nothing (not loaded, evicted, deleted) is undefined, an empty list or exists === false. A model that is neither in the schema nor local, a field the model lacks, or an id of the wrong kind throws MessagingStoreError (unknown_model, unknown_field, bad_id); nothing is skipped silently. Wire records are read-only: only payloads write them (the store applies applyAnswer and applyFrame).

    Types are compile-time declarations that you add to StoreModels. The runtime checks every name against the schema the server sent, so a declaration cannot invent a field.

    import type { Many, One } from "@erpforall/mail-store";
    declare module "@erpforall/mail-store" {
    interface StoreModels {
    "mail.message": { starred: boolean }; // a field your addon's `_to_store` adds
    "acme.note": { text: string; owner: One<"res.partner">; tags: Many<"acme.tag"> };
    }
    }

    Plain TS types stand for scalars; One<M>, Many<M>, Counter and Reference stand for relation, counter and polymorphic reference fields, so one(), many() and counter() are typed. Two addons declaring fields of one model merge. A model with no declaration is dynamically typed (unknown values).

    store.delegate("discuss.channel", { via: "thread" }) makes the channel read fields it does not have from the record its many2one field thread names (Odoo’s _inherits). Declare the delegated fields on the delegating model’s StoreModels entry so they type. Delegation is read-only, follows chains, allows one rule per model, and refuses cycles and a via that is not a many2one field of the schema (delegation).

    const drafts = store.defineLocalModel("acme.draft", ["text", "threadId"]);
    drafts.create("draft-1", { text: "" }); // ids are non-empty strings
    drafts.set("draft-1", { text: "hello" });
    store.record("acme.draft", "draft-1").get("text");

    Local records live in the same keyed tables and are reactive like server records, but they are never sent to the server and never appear in a resync request. The model name must not be a schema model (local_model_clash, also when a later schema introduces it). store.persistable(model) returns local data for your own persistence (D7) and refuses server models.

    Add a read helper to every handle with patch(RecordHandle, ...) and declare it so it types:

    declare module "@erpforall/mail-store" {
    interface RecordHandle<M extends string = string> { acmeLabel(): string }
    }
    patch(RecordHandle, () => ({ acmeLabel() { return `${this.model}#${String(this.id)}`; } }));

    The returned function removes the patch. Register patches, delegation and local models from your addon’s register(ctx) callback (see client-extensions.md).

    A surface that displays a record calls store.hold(model, id) and lease.release() when it leaves. collect() (also run when a request settles) drops, beyond maxThreads, the message windows of threads nobody holds (least recently used first), then messages no window or lease holds, then collectable models (default res.partner, ir.attachment; add yours through config.collectModels) that nothing retained references. Models that are neither messages nor collectable are roots and are never swept by the core. A message that only a root record’s many2one field references is evicted unless a window or lease holds it (relations are followed into collectable models only). A thread’s messages live in a window (store.windows): one contiguous run capped at pageSize * maxWindowPages (defaults 30 and 5). After eviction, windows.get(thread) is undefined: fetch the latest page and call windows.replace.

    The caps are configuration (new MessagingStore({ config })); their effect on speed and memory is measured at milestone end, not claimed here.

    MessagingStoreError carries a closed code and a message naming the model, field and value. Payload refusals (WireError) are passed to the reportError you give the store, and the result of applyAnswer/applyFrame is refused with a resync request; an unknown schema generation is needs_schema.