Build an app: live lists, rows that belong to people, and a chat
A site can be an app: pages that stay up to date while they are open, visitors who sign in, and rows that belong to the people who wrote them. This guide is four worked patterns. The language itself is on the card; this is how its pieces fit.
1. A list that stays up to date
Section titled “1. A list that stays up to date”Give the part of a page that shows rows an id and the word region. When
a row it read is added, changed or removed, the region is drawn again and
the open page shows it, with no reload and no script of the site’s own.
<script server> const going = tables.guests.filter(g => g.coming == true).sortBy(g => g.name)</script>
<section id="guests" region note="Who is coming. Updates as people answer."> <h2>{plural(going.length, { one: "# guest so far", other: "# guests so far" })}</h2> <ul> <li :for={guest in going} key={guest.id}>{guest.name}</li> </ul></section>-
A region needs an
id. A loop inside it may give each item akey; without one, each row’s number is used, and either way one item changes without the others being drawn again. -
A region reads only what the page may read. A private table’s rows never reach a region.
-
A long list is read from its end.
tail(50)gives the newest fifty, oldest first, initems, and the addresses of the windows before and after inearlierandlater(null at either end):const messages = tables.messages.filter(m => m.chat == chat).tail(50) -
region staticis drawn when the page loads and when a form in it is sent, and not after a write. Use it for a form that should answer in place, or for a list of everybody, which would otherwise be drawn again on every open copy of the page each time anybody joins.
2. Rows that belong to people
Section titled “2. Rows that belong to people”A visitor signs in as a row of a table the site names for its sessions.
From then on a page knows them as session, and session.row is their row.
The session table is any table you like, named in site.json beside
the page a visitor who is not signed in is sent to:
{ "edition": 1, "session": { "table": "people", "sign_in": "/join" }}{ "name": "People", "access": { "read": "session", "remove": "row == session.row", "code": ["insert"] }, "fields": [ { "name": "name", "type": "text", "label": "Name", "required": true, "filterable": true, "sortable": true } ]}Signing in is a function. It adds or finds the row, then calls
session.start(row). Signing out calls session.end():
<script server> function join(input) { const person = tables.people.insert({ name: input.name }) session.start(person) redirect("/") }
function leave() { const me = tables.people.get(session.row.id) tables.people.delete(me) session.end() redirect("/") }</script>
<form id="join" action={join} :if={!session} class="flex flex-row gap-sm"> <input name="name" required maxlength="40" autocomplete="nickname" aria-label="Your name"> <button class="btn-primary">Join</button> <p :if={form.failed} class="text-accent">{form.message}</p></form>
<form id="leave" action={leave} :if={session}><button>Leave for good</button></form>While a name is the only way in, signing out leaves a row nobody can be again, their name still in every list. So remove the row as you end the session, and say on the button that leaving is for good. Removing a row signs out everyone signed in as it, which is also how the owner bans somebody from the Database screen.
A page for some visitors only says who in its server script, and
everybody else is sent to the sign_in page, with where they were going
in ?back=:
<script server> export const access = "session"</script>Who may do what to a table’s rows is its access: read, write
(add a row), change and remove. Each is "owner" (no visitor, and
every verb is this until it says otherwise), "public" (anybody, on
read and write only), or a rule about the row and the session:
"session": anybody who has signed in."row == session.row": on the session table, the person the row is."row.author == session.row": the person that field points at."row.members.includes(session.row)": anybody in that list."mayRead(row.chat)": whoever may read the row that field points at. A message is read, and written, by whoever may read its chat. A chain of them (a comment through its card, a card through its board) may go three steps.
Rules join with &&, || and !, as in "row.author == session.row || row.members.includes(session.row)".
"access": { "read": "mayRead(row.chat)", "write": "mayRead(row.chat)", "change": "row.author == session.row", "remove": "row.author == session.row"}A row a visitor may not read is not found: its page answers 404, and a
list leaves it out. The table’s rule is applied to every read and every
write, so write no check of your own. Reading a table whose rule goes
through a row names that row, .filter(m => m.chat == chat), and the
checker refuses a read that does not.
What the platform fills in:
"fill": "session"on a reference to the session table is the row of whoever added the row. Nothing a form sends can change it."fill": "now"on adatetimeis when the row was added."includes": "session"on a list of the session table’s rows (references) puts whoever adds a row in its list, so whoever starts a chat is in it.
A list of people a rule reads changes only as its own keys say. "add"
is a rule for who may put somebody in it besides the owner. "leave": true
lets a person take themselves out. Nobody else is added or taken out by a
visitor’s form.
The owner approves what widens. A merge waits for the owner’s approval
when it gives visitors more than they had (a rule that lets
more people at more rows, a list more people may join) or lets visitors
learn more, and the Vizitka app cannot take that approval yet. When
merge_branch answers that it waits, pass its sentences on to the owner.
3. A chat
Section titled “3. A chat”A chat is the first two patterns together, with three more words: who is online, who is typing, and a mark for what each person has read.
Four tables. people as above, the session table, with
"presence": true in tables/people.json so a page can say who is online.
chats: a title, its members (a list of people with "includes": "session" and "max": 50), whether it is between two people, how many
messages it has had (sent), and its last message. messages: its
chat, its author ("fill": "session"), its text and when it was
sent ("fill": "now"). reads: how far each person has read each chat
(a chat, a person with "fill": "session", and the number seen),
with "read" and "change" both "row.person == session.row" and
"write": "mayRead(row.chat)", so each person reads and moves only their
own mark.
{ "name": "Chats", "access": { "read": "row.members.includes(session.row)", "write": "session", "change": "row.members.includes(session.row)" }, "fields": [ { "name": "title", "type": "text", "label": "Title" }, { "name": "members", "type": "references", "table": "people", "label": "Members", "required": true, "filterable": true, "includes": "session", "max": 50 }, { "name": "direct", "type": "boolean", "label": "Between two people", "filterable": true }, { "name": "sent", "type": "number", "label": "Messages sent" }, { "name": "last", "type": "reference", "table": "messages", "label": "Last message" } ]}One page per chat, pages/c/[slug].vzk, for people who signed in. Its regions
are who is here, the messages, and the box to write in:
<script server> export const access = "session"
const chat = tables.chats.row(params.slug) const messages = tables.messages.filter(m => m.chat == chat).tail(50) const writing = typing(chat)
function send(input) { const message = tables.messages.insert({ chat, text: input.text }) tables.chats.update(chat, { sent: (chat.sent ?? 0) + 1, last: message }) }
// The mark only moves forward, and never past what the chat has had. function read(input) { const upto = min(input.upto, chat.sent ?? 0) const mine = tables.reads.find(r => r.chat == chat && r.person == session.row) if (mine == null) { tables.reads.insert({ chat, seen: upto }) } else if (upto > mine.seen) { tables.reads.update(mine, { seen: upto }) } }</script>
<header id="who" region> <ul> <li :for={person in chat.members} key={person.id}> {person.name}<span :if={online(person)}> (online)</span> </li> </ul> <p :if={writing.length > 0}>{writing.map(p => p.name).join(", ")} typing</p></header>
<section id="messages" region> <ol> <li :for={message in messages.items} key={message.id}>{message.text}</li> </ol> <form id="read" action={read} seen="messages" hidden> <input type="number" name="upto" value={chat.sent ?? 0} required> </form></section>
<footer id="write" region static> <form id="send" action={send} typing={chat}> <textarea name="text" required maxlength="4000" aria-label="Message"></textarea> <button class="btn-primary">Send</button> </form></footer>online(person)is true while the site counts that person online, on a site whose session table says"presence": true.typing={chat}on a form names the row it is about, and says while somebody types in it that they are typing there.typing(chat)is the people typing in such a form, the visitor left out. It clears itself a few seconds after the last key.seen="messages"on a form that runs a function sends it by itself when the page is shown, and each time the region of that name changes while the visitor is looking: that is how a chat marks itself read with no button and no script of the site’s own. The form says what the region showed (upto, a number input inside the region, so it is kept current with it), and its function only moves the mark forward, so the form can be sent twice. Its function must not write what that region reads, or it would send itself for ever (the checker warns,VZK0220), and with JavaScript off it is never sent.- A chat nobody may read is a 404 for whoever is not in it, by the table’s own rule.
Prove it before asking to merge. Write scenarios for what the app
promises: two people sign in, one starts a chat, a message reaches the
other without a reload, the typing mark (.types("send"), and after("7 s")
for it to clear), changing and removing one’s own message,
and a 404 for somebody signed in who is not in the chat. Commit them and run
them with vzk test. The owner’s merge shows that they
pass.
4. A password
Section titled “4. A password”A name alone lets anybody be anybody who has not joined yet. A secret
field holds a password nobody reads back: no page, list, feed or answer
shows it, and it must be at least 8 characters. A function asks whether a
text is it with secret.matches(row.field, text):
{ "name": "Members", "access": { "read": "public", "write": "public", "remove": "row == session.row", "code": ["read"] }, "fields": [ { "name": "name", "type": "text", "label": "Name", "required": true, "filterable": true }, { "name": "password", "type": "secret", "label": "Password", "required": true } ]}<script server> function join(input) { if (tables.members.count(m => m.name == input.name) > 0) { fail({ name: "Somebody has joined by that name." }) } const member = tables.members.insert({ name: input.name, password: input.password }) session.start(member) redirect("/") }
function signIn(input) { const member = tables.members.find(m => m.name == input.name) if (!member || !secret.matches(member.password, input.password)) { fail("Wrong name or password") } session.start(member) redirect("/") }</script>
<form id="come-back" action={signIn}> <p :if={form.failed}>{form.message}</p> <label>Your name <input name="name" required></label> <label>Password <input type="password" name="password" required autocomplete="current-password"></label> <button>Sign in</button></form>Say the same thing for a wrong name and a wrong password, so the form does not tell a stranger who has joined. The owner sees “Set” or “Not set” for a password on the Database screen and may set a new one there; nobody can read one.