Docs
  • 0.57.0latest
  • 0.56.0
  • 0.55.0
  • 0.54.0
  • 0.53.0
  • 0.52.1
  • 0.52.0
  • 0.51.0
  • 0.48.0
  • 0.47.0
  • 0.46.0
  • 0.45.0
  • 0.44.0
  • 0.43.0
  • 0.42.0
  • 0.41.0
  • 0.40.0
  • 0.39.6
  • 0.39.5
  • 0.39.3
  • 0.39.2
  • 0.39.1
  • 0.39.0
  • 0.38.4
  • 0.38.2
  • 0.38.1
  • 0.37.1
  • 0.37.0
  • 0.36.2
  • 0.36.1
  • 0.36.0
  • 0.35.3
  • 0.35.2
  • 0.35.1
  • 0.35.0
  • 0.34.4
  • 0.34.3-beta
  • 0.34.2-beta
  • 0.34.1-beta
  • 0.34.0-beta
  • 0.33.4-beta
  • 0.33.3-beta
  • 0.33.2-beta
  • 0.33.1-beta
  • 0.33.0-beta
  • 0.32.0-beta

Writing safely

Replace and merge are different words, a write names its record or states a condition, RETURN shows you what moved, and THROW undoes the lot.

Every write in TessariQL says how far it reaches — by naming the one record it changes, or by stating a condition. No write reaches an unstated number of records, and that is the first safety property on this page rather than a missing feature.

DEFINE TABLE users (name string, age int, city string);
DEFINE TABLE r (at int);
CREATE users:1 = { name: 'ada', age: 17 };
CREATE users:2 = { name: 'bob', age: 30 };

r is declared here because the section on bounded deletes further down writes to it. Everything on this page runs in the order it is written, against the store those four statements leave behind.

Creating twice is refused

CREATE users:1 = { name: 'dup' };

Refused — RecordExists.

error: record 1 already exists — say `UPDATE` to replace it

CREATE means this is new. When you do not know whether it is, say so:

UPSERT users:2 = { name: 'ada2' };   -- there: replaced
UPSERT users:9 = { name: 'new' };    -- not there: created

Replace and merge are different words

This is the distinction most worth having in your fingers, because both spellings succeed and only one of them is usually what you meant.

UPDATE users:1 SET age = 18 RETURN AFTER;
{ age: 18, name: 'ada' }
UPDATE users:1 MERGE { city: 'Paris' } RETURN AFTER;
{ age: 18, city: 'Paris', name: 'ada' }
UPSERT users:1 = { name: 'only' } RETURN AFTER;
{ name: 'only' }

The age is gone. = { … } is the whole record, so anything the object does not mention is not kept — which is right for CREATE and is a data-loss bug in a save handler that reposts a form with three of the record's five fields.

Reach for MERGE when the object you hold is a patch, and = { … } only when it is the record. SET changes named fields and leaves the rest alone, like MERGE, and reads better when the new value is computed rather than supplied.

RETURN BEFORE and RETURN AFTER

A write answers with ok unless you ask it for something:

UPDATE users:1 SET age = 18 RETURN BEFORE;
{ name: 'only' }

That is the record the previous section left behind — no age, because the UPSERT above replaced it away. Which is the point of asking for BEFORE: it answers with what was actually there, not with what you assumed was there.

BEFORE is the record as it stood; AFTER is what it became. BEFORE is the one people forget exists, and it is the cheapest audit trail there is — the previous value, returned by the write itself, in the same transaction, with no second read that could see something else.

DELETE takes it too, which is the only chance you get:

DELETE users:9 RETURN BEFORE;
{ name: 'new' }

A write says how far it reaches

A write either names one record, or states a condition. It is never allowed to leave the reach unsaid: a write over every record of a table spells that out.

DELETE FROM r;

Refused — UnexpectedToken.

error: expected `WHERE` and what to remove, found `;`

A condition is enough, and a bound is optional (from 0.38.0):

DELETE FROM r WHERE at < 5;             -- every match
DELETE FROM r WHERE at < 5 LIMIT 1;     -- at most one
DELETE FROM r WHERE at < 5 LIMIT ALL;   -- every match, said out loud

Until 0.38.0 the bound was required and LIMIT ALL was the only way to say every match; it still parses, so a script written then runs unchanged. The condition stays required — a delete over the whole table says WHERE true, and emptying a table is TRUNCATE.

A delete over a set takes RETURN BEFORE too (from 0.35.2) and answers the records it removed, each with its identity: DELETE FROM r WHERE at < 5 RETURN BEFORE. Without it the answer is ok.

An UPDATE names its record, or states a condition:

UPDATE users SET age = 0;

Refused — UnexpectedToken.

error: expected `WHERE` and which records to change, or `:` and one record's identity, found `;`

With a condition it changes every record the condition holds for, in one transaction, and answers how many — 0 when nothing matches, which is not an error:

UPDATE users SET age = 0 WHERE age > 150;
UPDATE users MERGE { checked: true } WHERE age > 150 LIMIT 10 RETURN AFTER;

Nobody here is over 150, so the first answers 0 and the second no records.

LIMIT n changes at most that many, and RETURN BEFORE or RETURN AFTER answers the records instead of the count. A WHERE on an UPDATE that names its record (UPDATE users:1 SET … WHERE …) is still a compare-and-set: there a false condition refuses the write (ConditionNotMet) rather than selecting nothing.

THROW abandons the whole transaction

BEGIN;
  UPDATE users:1 SET age = 99;
  THROW 'no';
COMMIT;

Refused — Thrown.

error: no

And afterwards:

SELECT * FROM users:1;
1: { age: 18, name: 'only' }
(1 record(s), via record)

The update is not there. THROW raises the message you give it, and nothing inside the transaction survives — so a check written in the middle of a multi-statement write can refuse the whole thing without having to undo the part that already ran.

Use it for the condition the schema cannot state: a balance that must not go negative, a status the record is not allowed to move to from where it is, a count that has to match something read a moment earlier. See Transactions for what BEGIN and COMMIT guarantee around it.