Summary#
A computed member (bool Ready => …;) is worked out each time it is read, so the store cannot filter, count or index
it. Mark it [Stored] and the platform keeps its value in a column instead, written in the same transaction as every
write that changes one of the members it reads. Readers who may not read one of those members may not read it either;
the audit trail records it with the write that changed it; live queries keep it like any other column.
Signature#
entity <Name> {
[Stored] <Type> <Member> => <expression over this row's members>;
}Description#
How do I keep a derived value the store can count?#
Write it as a computed member and mark it [Stored]. It may read the row's plain members and other stored members, in
any declaration order:
entity Job {
[MaxLength(200)] string Title;
int Estimate;
int Spent;
[Stored] int Left => Estimate - Spent;
[Stored] bool Over => Left < 0;
}Over reads Left, so the platform works out Left first. Two stored members that read each other are refused: a
circle has no first one.
The expression may call the app's own functions, as long as they are pure, and it may read a Markdown member as its
text:
using Osysharp.Markdown;
entity Brief {
[MaxLength(200)] string Title;
Markdown Goal;
[Stored] bool Ready => Mentions(Goal, "Done when");
}
bool Mentions(string? text, string phrase) => text != null && text.Contains(phrase);A list of briefs can then count the ready ones in the store: Brief.Where(b => b.Ready).Count().
When the value is written#
The platform writes the value in the same transaction as the write that changed one of its members, on every path:
- a unit of work that creates the row, or assigns or adds to (
+=) a member the expression reads; - a set-based
Update(…)orInsert(…)— every row it writes; - an edit of the
Markdowntext — whether through the Markdown editor or an assignment. No function of yours runs when the editor saves, and the value still follows the text.
It reads the row back after the write, so the value is worked out from exactly what is committed — including another user's edit that committed first. The row is written only when the value changes, and never stamped as modified for it. Inside a unit of work, a stored member reads what the last commit wrote (nothing yet, for a row not committed) until the unit of work commits.
Who may read a stored member#
Whoever may read every member it is worked out from. A reader withheld one of them — by a deny read, with or
without a where, or by a classification — is withheld the stored member too, on that row: it reads as its type's
default, a filter or a count over it does not see it, and a live read never learns it. Otherwise a reader denied the
text could learn about it from the value worked out of it.
using Osysharp.Markdown;
[Principal] entity User {
[MaxLength(255)] string Email;
security { allow read when IsAuthenticated; }
}
entity Note {
[MaxLength(200)] string Title;
User? Owner;
Markdown Body;
[Stored] bool Decided => Mentions(Body, "Decision");
security {
allow read, create, update when IsAuthenticated;
deny read Body where Owner != user; // Decided is withheld from them too
}
}
bool Mentions(string? text, string phrase) => text != null && text.Contains(phrase);The rule follows the inputs all the way down: a stored member that reads another stored member is withheld wherever
any member that one reads is. A deny read on the stored member itself works as on any member, and an allow read
naming it lifts only its own deny — never one it takes from its inputs. As for an
inherited field, a deny narrows and an allow never widens.
In the audit trail#
A changed stored value is recorded in the same entry as the write that changed it, marked "derived": true beside its
old and new — the platform worked it out; nobody wrote it. A Markdown editor save writes only the text's sections, so
the row whose value changed gets an entry of its own in the same capture. A stored member whose input is redacted from
the trail (a deny read, your redaction list, a classification) is redacted too. See audit read access (app.Audit).
In live queries#
It is a column like any other: a live list, Count, Sum or grouping over it is kept from the change that moved it,
exactly as for any column. That includes an edit of the Markdown text it reads: the editor's save is told to live readers as a
change of the row, with the stored value before and after, so a live Brief.Count(b => b.Ready) moves by one when a
save makes a brief ready.
Organisation policy#
A stored member is not a write of its own. The organisation's rules judge the write that changed its inputs, and the
stored value lands with that write or not at all. It cannot be [OrgPolicy] itself; govern the members it is computed
from.
What the expression may read#
A stored value is worked out again only when its row is written, so it may depend only on that row. The compiler holds these rules, and each refusal says what to write instead:
| Rule | Refused |
|---|---|
| Its own row only | a reference hop (Owner.Name), a query, a collection |
| Pure | the clock, a random value, a setting, a secret, a call past the platform, a read of the store — in the expression or in any function it calls |
| Stored inputs | a computed member that is not [Stored] (mark it [Stored] too, or write its expression in place) |
| A single value | a reference, a list, Json, Markdown |
| Never written by code | an assignment, an initializer or a bulk Update of it — true of every computed member |
| Not governed | [OrgPolicy] on it — put the organisation's rule on the members it is computed from |
The row itself may not be handed on as a value (F(this)): pass a function the members it needs.
A fact from another row#
A value that depends on another row would go stale when that row changes. Keep that fact on this row as a plain field,
written by the one function that changes it, and guard it with except via so no other path
can write it. A stored member may then read the field:
entity Slice {
int? NewestEstimate; // written only by RecordEstimate
[Stored] bool Sized => NewestEstimate != null;
security { deny update NewestEstimate except via RecordEstimate; }
}When the expression changes#
A deploy keeps the column right before the new version serves:
- Added to rows already there — the deploy works out the value of every row.
- A changed expression — or a changed function it calls, or a changed stored member it reads: every row is worked out again. Moving or reformatting the declaration is not a change.
- An earlier version put back — its values are worked out again by its own expression. Each column records which expression worked its values out, and that record travels with the database, so a backup restored to its own version holds values that agree with it.
- No longer
[Stored]— the column is dropped and the member is worked out on read again. When a plain member of the same name replaces it, the column stays as that member's, with the last values worked out.
The deploy writes only the rows whose value changes, and never stamps a row as modified for it.
Not a class member#
A class has no rows, so [Stored] on a class member is refused: its computed members are already worked out on read.
See also#
- entity members — the members a stored member reads
- deny … except via — keeping a fact from another row on this one
- invariant — the other rule the platform judges on every write of a row
- Security.IsMasked — was this value withheld from me? — what a withheld member reads as
- audit read access (app.Audit) — the trail a stored value is recorded in