Metadata
A Table can carry metadata: Markdown
descriptions of the Table itself and of its columns, written by the developer
who knows what the data means. <perspective-viewer> shows them to the user, so
a dashboard can answer “gross or net?” or “which timezone?” where the question
comes up.
Metadata is given once, when the Table is created, and never changes
afterwards.
const table = await client.table(data, {
name: "orders",
metadata: {
description: "One row per order line, refreshed nightly.",
columns: {
Sales: { description: "**Gross** sales in USD, before returns." },
Profit: { description: "`Sales` less cost of goods and shipping." },
},
},
});
const metadata = await table.metadata();
// { description: "...", columns: { Sales: {...}, Profit: {...} } }
table = client.table(
data,
name="orders",
metadata={
"description": "One row per order line, refreshed nightly.",
"columns": {
"Sales": {"description": "**Gross** sales in USD, before returns."},
"Profit": {"description": "`Sales` less cost of goods and shipping."},
},
},
)
metadata = table.metadata()
# {"description": "...", "columns": {"Sales": {...}, "Profit": {...}}}
Both description fields are optional, and an empty description is the same as
none: Table::metadata reports only what was described, and an empty object for
a Table with no metadata. Describing a column the Table does not have is an
error, and the Table is not created. Table::clear and Table::replace keep
metadata; it is dropped when the Table is deleted.
Client::get_hosted_table_metadata returns the metadata of every hosted Table
which has any, by name.
Caching
Because metadata cannot change, a Client fetches it once per Table name and
caches it. It asks the server again only when the set of hosted Table names
changes, or when the Client itself creates or deletes a Table.
A subscription made with on_hosted_tables_update(callback, { metadata: true })
(metadata=True in Python) receives the hosted Tables with their metadata in
each notification, which keeps the cache current without further requests.
<perspective-viewer> subscribes this way.
The cache assumes that a Table name keeps its metadata for the life of the
Client. If another Client deletes a Table and creates a new one with the
same name but different metadata, a Client without a metadata subscription
can report the old metadata until the set of names next changes.
In <perspective-viewer>
A Table’s description is shown in the workspace panel’s Tables tab: a
described Table’s row has a marker which expands the description below it.
A column with a description gets an Info tab in its column settings, and the settings button on that column’s row is marked and enabled even when the column is not in use.
Derived and joined Tables
A Table made from a View with no metadata of its
own starts with the column descriptions of each column it shares, by name, with
the View’s own Table. A joined Table starts with the column
descriptions of both sources, the left one winning for a column they share. The
Table’s own description is not inherited, since a derived Table is a
different dataset. Giving a Table made from a View its own metadata
replaces inheritance entirely.
Virtual servers
A virtual server reports its data source’s own
comments. Metadata cannot be given to a Table created through a virtual
server: Client::table with metadata is rejected unless the server’s features
include metadata, so author comments with the database’s own tools.
| Virtual server | Table description | Column descriptions |
|---|---|---|
| DuckDB | COMMENT ON TABLE, COMMENT ON VIEW | COMMENT ON COLUMN |
| PostgreSQL | COMMENT ON TABLE (or view) | COMMENT ON COLUMN |
| ClickHouse | The table’s COMMENT | The column’s COMMENT |
| Polars | Nothing | Nothing |
A custom handler opts in by implementing tableMetadata(names)
(table_metadata(names) in Python), which returns the metadata of each of the
named Tables which has any, in the shape Table::metadata returns, and by
declaring metadata: true in its features. It is called only when a client asks
for metadata.
Client and server metadata
A Client and a server can each be given a name and a Markdown description when
they are created.
const server = new PerspectiveServer({
name: "Analytics",
metadata: { description: "Nightly **ERP** extracts." },
});
const client = await perspective.websocket("wss://example.com/ws", {
name: "Production",
metadata: { description: "Live positions and trades." },
});
client.get_name(); // "Production"
client.get_metadata(); // { description: "Live positions and trades." }
await client.get_server_name(); // "Analytics"
await client.get_server_metadata(); // { description: "Nightly **ERP** extracts." }
server = Server(name="Analytics", metadata={"description": "Nightly extracts."})
client = server.new_local_client(name="Local", metadata={"description": "..."})
await client.get_server_name() # "Analytics", on the async client
A Client’s name and metadata stay on the client: they are never sent to the
server. Its name must be unique among the Clients alive in the same process
(or page); a duplicate is an error, and terminate() frees the name. Without a
name, a Client is named client-1, client-2 and so on.
The server’s name and metadata reach every connected Client in its handshake,
with the server’s release. worker() and make_client() take the same options
as websocket(); VirtualServer and createMessageHandler take the server’s.
In <perspective-viewer>’s workspace panel, each Client’s section is headed
by its name and its server’s, and a marker next to them expands both
descriptions.