Remote / Totals over a loaded list
Show a total over a list you already loaded
An invoices page shows the list and, above it, "Unpaid: $1,240.00". Working
that out in the view means adding up every row on every render, so a page of
ten thousand invoices adds ten thousand numbers each time anyone types,
whether or not an invoice changed. Maintained.value from
foldkit-remote/maintained keeps the total between renders and updates it
from the invoices that changed.
It works on a list the page has already loaded into its Model with
Data.query, so it asks the server nothing. In the example below, Data is
your Remote domain (the value Remote.make returns), model is your
application's Model and invoices is a list read from Data, all set up as
the Remote page shows. The aggregate itself (the
rule "add up the cents") is written with @doeixd/metakit, a package that keeps rules
as data (pnpm add @doeixd/metakit), whose @doeixd/metakit/aggregates entry has the six
kinds this guide uses.
Unpaid is the registered query that selects unpaid invoices. The example
assumes you have loaded all pages of invoices and the fields Unpaid reads.
import { Schema } from 'effect'
import * as Rows from '@doeixd/metakit/aggregates'
import { Maintained } from 'foldkit-remote/maintained'
// What the list decodes each invoice to, and the sum of its cents.
const Row = Schema.Struct({ cents: Schema.Int, status: Schema.String })
const UnpaidCents = Rows.sum(Row, $ => $.cents)
const unpaid = Maintained.value(Data, model, invoices, UnpaidCents, { where: Unpaid, input: {} })
unpaid.value // 124000, the unpaid invoices' cents: $1,240.00
unpaid.complete // trueMaintained.value adds cents over the invoices Unpaid picks and returns
the total as value. It reads all loaded pages for the connection.
Row describes the fields the sum can read, and $ gives access to those
fields. Call Maintained.value again with a changed Model to update the total.
invoices is the list the page shows (Data.query(AllInvoices, …)).
input holds the arguments to Unpaid, which takes none here. Leave out
{ where, input } to count every loaded row whose selected fields are available.
"The list" here is every row the page has loaded for that query and input,
which can be more than one read of it shows. invoices asks for first: 50;
if another part of the page loaded 120 rows of the same query, the aggregate
is over the 120, though invoices itself shows 50 and says it has a next
page. The filtered read
uses the same population to find matching rows.
complete is true when the answer is about everything the server has for
the query: every loaded row could be judged by Unpaid, every counted row
holds the fields the list selects, the server has said there is no page before
the first loaded row or after the last, and no gap lies between the loaded
pages. It is false when any of those fails. value is then the total of the
rows that could be counted, so a view can label the total as partial or ask for
the rest. With 120 rows loaded to
both ends, complete is true beside a list that shows 50.