Osy#betaRun it on Osyrin: osyrin.com

Reference / Entity

[Stored]

[Stored] <Type> <Member> => <expression over this row>;

A computed member the platform keeps in a column: worked out from the row's own members whenever one of them is written, so the store can filter, count and index it. The expression is pure and reads only its own row.

preview3 examples compiled by CIentitymodeldata

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(…) or Insert(…) — every row it writes;
  • an edit of the Markdown text — 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:

RuleRefused
Its own row onlya reference hop (Owner.Name), a query, a collection
Purethe 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 inputsa computed member that is not [Stored] (mark it [Stored] too, or write its expression in place)
A single valuea reference, a list, Json, Markdown
Never written by codean 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#

Related

entity members

The typed members an entity holds — text, numbers, dates, booleans, Guids, enums and references. A member's type…

invariant

A row-level rule spanning several members, checked when the row is written. Use it when a constraint on one member is…

deny … except via

Names the ONLY functions whose own code may change something a deny withholds: `deny update UnitsLeft except via…

The data model (entities and their rules)

An entity is a table — and the rules that are true about it. Not just its columns: what must hold for a row to exist…

Security.IsMasked — was this value withheld from me?

A field a `deny read` withholds reads as its type's default — 0, false, DateTime.MinValue, the enum's zero; null for a…

deny on an inherited field

A subtype may NARROW a field it inherits — `deny read Notes when !IsReviewer;`, `deny update Code except via Recode;` —…

audit read access (app.Audit)

`app.Audit` configures the app's audit trails — WHO may read each one, and whether it is recorded at all. An app that…