Mutations
A mutation is a query and a patch together. The query selects the entities, and the patch gives the new state of each one. Stardust commits every change in one transaction. Each example reads the same five entities as Queries.
The document
A mutation document has query and patch. The query is an ordinary
query, and the patch follows the form in
Patches.
query {find [?e]
where [[?e dept ops]]}
patch {?e {status active}}
Each key of the patch is a target. A key that begins with ? uses an entity
variable bound by the query. A temporary name such as #_new makes a new entity, as in a patch.
Preview and commit
The native mutation run commits the selection and resolved patch together. The core also has a preview path that validates the same selection, resolution and store rules without writing facts. The public ABI does not currently expose that preview.
The result blocks on this page show the core report's status and counts. The public mutation result uses lowercase status names and also gives the commit time and generated entity ids.
{status Committed
transaction 7
facts 1
generated 0}The three statuses
| Status | Meaning |
|---|---|
Committed | The patch writes facts in a new transaction |
No_Change | The selection matches and every field already agrees |
No_Match | The selection matches no entity |
A second run of the same mutation changes nothing, so Stardust commits no new transaction.
{status No_Change
transaction 0
facts 0
generated 0}A selection that matches no entity gives the same counts. No entity is in the
department legal:
query {find [?e]
where [[?e dept legal]]}
patch {?e {status active}}
{status No_Match
transaction 0
facts 0
generated 0}New entities
A temporary name such as #_new makes an entity. The selection must give exactly
one row, because one new entity needs one parent row.
A {#link #_new} value in the same patch links to that entity. {#link #_tx} links to the committing transaction.
query {find [?e]
where [[?e dept ops]]
limit 1}
patch {#_new {status seed}}
{status Committed
transaction 9
facts 1
generated 1}The engine rejects the same patch against a selection of two rows. Two entities are in
the department eng:
query {find [?e]
where [[?e dept eng]]}
patch {#_new {status seed}}
Error
Generate_Requires_Single
Patch values and variables
The body for each target is a merge patch. It accepts every Stardust value: numbers, text, booleans, lists, maps, links and the other typed values. A null removes the field.
patch {?e {age 5}}
This sets age to 5 on every entity bound to ?e.
The query may bind a patch variable in where or expose it through find. Both
forms refer to the same row binding. A variable used as a patch target must bind
an entity. A variable used as a field value substitutes its complete bound
value.
query {find [?status]
where [[?e status ?status]
[= ?status pending]
[?owner name Ada]]}
patch {?e {status active
owner ?owner}}
Stardust rejects a mutation with a malformed patch or an unbound variable.
It also rejects a target that does not bind an entity or writes a protected
stardust/* field. An invalid typed value or a result that violates the
selected schema also causes rejection.
When a grouped mutation selects several schemas for an entity, the complete
patched document must satisfy all of them. Schema paths do not need to be group
keys. Groups removed by having, paging or other selection stages contribute
no schema constraints.
A mutation cannot write a definition. Stardust rejects a definition target with Definition_Target. Definitions
include schemas, queries and mutations. A patch that writes stardust/type
also gives Definition_Target. The preview gives the same
error, and no fact changes. Use the definition API to change a definition.
Computed values
Each field value is an expression evaluated for each selected row. A ?name
reads the row binding, plain text stays text, and maps are evaluated member by
member. #_tx metadata is evaluated once per run and may read caller inputs,
declared or implicit parameters, but no other row bindings.
A list whose first element names a function or macro is a call. Other lists
evaluate each element. Use list when the first element names a function or
macro but should stay data:
query {find [?e ?age]
where [[?e name Cy]
[?e age ?age]]}
patch {?e {band [if [< ?age 30] junior senior]
tags [design remote]
scores [1 2 3]
review {due [+ ?age 1]
by Ada}}}
{status Committed
transaction 10
facts 4
generated 0}Values from the query
A target's whole body can come from the query: {?e ?body} takes an object
of fields, or null, from the row binding ?body. A key can also come from
the query: in {?e {?field value}} the text bound to ?field names the field.
These bound values are data, not evaluated again.
query {find [?e ?field]
where [[?e name Cy]
[[if true band null] ?field]]}
patch {?e {?field null}}
{status Committed
transaction 11
facts 1
generated 0}Delete an entity
A null body deletes each selected data entity, as a null entity does in a
patch. Stardust refuses to delete a definition or an object, and
rejects a patch that deletes an entity and also writes a value to it.
query {find [?e]
where [[?e name Eve]]}
patch {?e null}
{status Committed
transaction 12
facts 4
generated 0}A computed patch
A list as the whole patch is an expression evaluated for each row. Its
result is a patch in the forms above, or null for no change to that row. The
result is data: its text values are not evaluated again, and it cannot write
#_tx.
query {find [?e ?dept]
where [[?e dept ?dept]]}
patch [if [= ?dept sales] {?e {region west}} null]
{status Committed
transaction 13
facts 2
generated 0}