@protobase/cli 0.99.3 → 0.99.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/package.js +1 -1
- package/dist/doctor/page-shapes.js +85 -76
- package/dist/doctor/read-shapes.js +1 -1
- package/dist/doctor/run.js +3 -0
- package/dist/doctor/slot-samples.js +76 -0
- package/package.json +8 -8
- package/skills/protobase/references/features/composed-pages.md +1 -0
- package/skills/protobase/references/reference/cli.md +3 -1
- package/skills/protobase/references/reference/custom-components.md +12 -0
- package/skills/protobase/references/reference/layouts.md +24 -5
package/dist/cli/package.js
CHANGED
|
@@ -1,95 +1,104 @@
|
|
|
1
|
-
import { configFilter, configSort, sortText } from "./shape-filters.js";
|
|
1
|
+
import { all, anyOf, configFilter, configSort, sortText } from "./shape-filters.js";
|
|
2
|
+
import { fitSlots, parseSampled } from "./slot-samples.js";
|
|
2
3
|
import { listSort } from "./view-shapes.js";
|
|
3
|
-
import {
|
|
4
|
+
import { compileReadPlan, elementAt } from "@protobase/layout";
|
|
5
|
+
import { printFilter } from "@protobase/schema";
|
|
6
|
+
import { splitRelated } from "@protobase/schema/internal";
|
|
4
7
|
//#region src/doctor/page-shapes.ts
|
|
5
|
-
const listing = [
|
|
6
|
-
"RecordRow",
|
|
7
|
-
"RecordCards",
|
|
8
|
-
"Table",
|
|
9
|
-
"CardRow",
|
|
10
|
-
"RecordCard",
|
|
11
|
-
"Stat",
|
|
12
|
-
"Thread"
|
|
13
|
-
];
|
|
14
8
|
const text = (value) => typeof value === "string" ? value : void 0;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
/** A filter's terms ANDed at the top, `related()` ones apart: the rest as the index rule reads it, the related field as `in()`. */
|
|
10
|
+
const judged = (model, ast) => {
|
|
11
|
+
if (!ast) return { terms: [] };
|
|
12
|
+
const { terms, rest } = splitRelated(ast);
|
|
13
|
+
const fields = terms.flatMap((term) => model.fields[term.field.path[0]] ? [anyOf(model.fields[term.field.path[0]])] : []);
|
|
14
|
+
return {
|
|
15
|
+
terms,
|
|
16
|
+
filter: all([rest && configFilter(model, printFilter(fitSlots(rest, model))), ...fields])
|
|
17
|
+
};
|
|
20
18
|
};
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const filter =
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const from = `page "${page.name}", <Chart${named ? ` "${named}"` : ""}>`;
|
|
19
|
+
const relatedShapes = (term, models, from) => {
|
|
20
|
+
const model = models[term.resource];
|
|
21
|
+
if (!model) return [];
|
|
22
|
+
const { terms, filter } = judged(model, term.filter);
|
|
23
|
+
const sort = configSort(model, term.orderBy);
|
|
24
|
+
const key = model.fields[term.key];
|
|
25
|
+
const selects = key && key.type !== "json" ? key : void 0;
|
|
26
|
+
const params = [...term.filter ? [`filter=${printFilter(term.filter)}`] : [], ...sort ? [`order_by=${sortText(sort)}`] : []];
|
|
30
27
|
return [{
|
|
31
28
|
resource: model.name,
|
|
32
|
-
request
|
|
33
|
-
from
|
|
34
|
-
query: {
|
|
35
|
-
...filter && { filter },
|
|
36
|
-
groupBy
|
|
37
|
-
}
|
|
38
|
-
}, ...splitBy === void 0 ? [] : [{
|
|
39
|
-
resource: model.name,
|
|
40
|
-
request: `${request}&split_by=${splitBy}`,
|
|
41
|
-
from,
|
|
29
|
+
request: `GET /${model.name}${params.length > 0 ? `?${params.join("&")}` : ""}`,
|
|
30
|
+
from: `${from} related ${term.resource}.${term.key}`,
|
|
42
31
|
query: {
|
|
43
32
|
...filter && { filter },
|
|
44
|
-
|
|
33
|
+
...sort && { sort },
|
|
34
|
+
...selects && { selects }
|
|
45
35
|
}
|
|
46
|
-
}]
|
|
36
|
+
}, ...terms.flatMap((nested) => relatedShapes(nested, models, from))];
|
|
37
|
+
};
|
|
38
|
+
const title = (page, read) => {
|
|
39
|
+
const node = elementAt(page.tree, read.block);
|
|
40
|
+
const named = node && (text(node.props.title) ?? text(node.props.label));
|
|
41
|
+
const part = read.part === "calls" ? " calls" : read.part === "chosen" ? " chosen row" : read.part === "condition" ? " condition" : "";
|
|
42
|
+
return `page "${page.name}", <${node?.type ?? "Page"}${named ? ` "${named}"` : ""}>${part}`;
|
|
43
|
+
};
|
|
44
|
+
const readSort = (page, read, model, view) => {
|
|
45
|
+
if (read.orderBy !== void 0) return configSort(model, read.orderBy);
|
|
46
|
+
const type = elementAt(page.tree, read.block)?.type;
|
|
47
|
+
return type === "RecordRow" || type === "RecordCards" ? listSort(model, view) : void 0;
|
|
47
48
|
};
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const model = models[calls.resource];
|
|
49
|
+
const readShapes = (page, read, models, views) => {
|
|
50
|
+
const model = models[read.resource];
|
|
51
51
|
if (!model) return [];
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
52
|
+
const from = title(page, read);
|
|
53
|
+
const { terms, filter } = judged(model, parseSampled(read.filter));
|
|
54
|
+
const filterParam = read.filter?.trim() ? [`filter=${read.filter}`] : [];
|
|
55
|
+
const related = terms.flatMap((term) => relatedShapes(term, models, from));
|
|
56
|
+
if (read.kind === "aggregate") {
|
|
57
|
+
const [groupBy, splitBy] = read.groupBy ?? [];
|
|
58
|
+
const request = `GET /${model.name}:aggregate?group_by=${groupBy}${filterParam.map((param) => `&${param}`).join("")}`;
|
|
59
|
+
return [
|
|
60
|
+
{
|
|
61
|
+
resource: model.name,
|
|
62
|
+
request,
|
|
63
|
+
from,
|
|
64
|
+
query: {
|
|
65
|
+
...filter && { filter },
|
|
66
|
+
...groupBy && { groupBy }
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
...splitBy === void 0 ? [] : [{
|
|
70
|
+
resource: model.name,
|
|
71
|
+
request: `${request}&split_by=${splitBy}`,
|
|
72
|
+
from,
|
|
73
|
+
query: {
|
|
74
|
+
...filter && { filter },
|
|
75
|
+
groupBy: splitBy
|
|
76
|
+
}
|
|
77
|
+
}],
|
|
78
|
+
...related
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
const sort = read.kind === "count" ? void 0 : readSort(page, read, model, views.find((view) => view.resource === model.name && !view.roles));
|
|
82
|
+
const per = read.limitPer?.field;
|
|
83
|
+
const params = [
|
|
84
|
+
...filterParam,
|
|
85
|
+
...sort ? [`order_by=${sortText(sort)}`] : [],
|
|
86
|
+
...read.limitPer ? [`limit_per=${read.limitPer.field}:${read.limitPer.count}`] : []
|
|
87
|
+
];
|
|
56
88
|
return [{
|
|
57
89
|
resource: model.name,
|
|
58
90
|
request: `GET /${model.name}${params.length > 0 ? `?${params.join("&")}` : ""}`,
|
|
59
|
-
from
|
|
91
|
+
from,
|
|
60
92
|
query: {
|
|
61
93
|
...filter && { filter },
|
|
62
|
-
...sort && { sort }
|
|
63
|
-
|
|
64
|
-
}];
|
|
65
|
-
};
|
|
66
|
-
/** The reads of a composed page: each block that lists records, with its filter and sort, a thread's tool calls, and each chart's grouping. */
|
|
67
|
-
const pageShapes = (page, models, views) => {
|
|
68
|
-
const shapes = [];
|
|
69
|
-
walkTree(page.tree, (node) => {
|
|
70
|
-
const model = node.custom ? void 0 : models[text(node.props.resource) ?? ""];
|
|
71
|
-
if (model && node.type === "Chart") {
|
|
72
|
-
shapes.push(...chartShapes(page, node, model));
|
|
73
|
-
return;
|
|
94
|
+
...sort && { sort },
|
|
95
|
+
...per && { per }
|
|
74
96
|
}
|
|
75
|
-
|
|
76
|
-
const source = text(node.props.filter);
|
|
77
|
-
const filter = configFilter(model, source);
|
|
78
|
-
const sort = node.type === "Stat" ? void 0 : blockSort(node, model, views.find((view) => view.resource === model.name && !view.roles));
|
|
79
|
-
const params = [...source?.trim() ? [`filter=${source}`] : [], ...sort ? [`order_by=${sortText(sort)}`] : []];
|
|
80
|
-
const named = title(node);
|
|
81
|
-
shapes.push({
|
|
82
|
-
resource: model.name,
|
|
83
|
-
request: `GET /${model.name}${params.length > 0 ? `?${params.join("&")}` : ""}`,
|
|
84
|
-
from: `page "${page.name}", <${node.type}${named ? ` "${named}"` : ""}>`,
|
|
85
|
-
query: {
|
|
86
|
-
...filter && { filter },
|
|
87
|
-
...sort && { sort }
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
if (node.type === "Thread" && node.props.calls !== void 0) shapes.push(...callShapes(page, node, models));
|
|
91
|
-
});
|
|
92
|
-
return shapes;
|
|
97
|
+
}, ...related];
|
|
93
98
|
};
|
|
99
|
+
/** The reads of a composed page, as its read plan makes them, each `related()` term's records among them. */
|
|
100
|
+
const pageShapes = (page, models, views) => compileReadPlan(page.tree, { models }).plan.reads.flatMap((read) => readShapes(page, read, models, views));
|
|
101
|
+
/** The custom components of a page that declare no reads, which read by themselves once they draw. */
|
|
102
|
+
const unplannedBlocks = (page, models) => compileReadPlan(page.tree, { models }).plan.outside.map(({ type }) => `page "${page.name}", <${type}>`);
|
|
94
103
|
//#endregion
|
|
95
|
-
export { pageShapes };
|
|
104
|
+
export { pageShapes, unplannedBlocks };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { fieldShapes, lookupShapes } from "./field-shapes.js";
|
|
2
1
|
import { viewShapes } from "./view-shapes.js";
|
|
3
2
|
import { pageShapes } from "./page-shapes.js";
|
|
3
|
+
import { fieldShapes, lookupShapes } from "./field-shapes.js";
|
|
4
4
|
//#region src/doctor/read-shapes.ts
|
|
5
5
|
/**
|
|
6
6
|
* Every read the API takes of the resources and the app makes of them, which the scan guard judges when it runs: what
|
package/dist/doctor/run.js
CHANGED
|
@@ -7,6 +7,7 @@ import { readOnlyDb } from "../db/read-only-db.js";
|
|
|
7
7
|
import { builtInIndexIssues, builtInModels } from "./built-in-models.js";
|
|
8
8
|
import { checkResource } from "./check.js";
|
|
9
9
|
import { indexIssues } from "./index-issues.js";
|
|
10
|
+
import { unplannedBlocks } from "./page-shapes.js";
|
|
10
11
|
import { readShapes } from "./read-shapes.js";
|
|
11
12
|
import { relationIssues } from "./relation-issues.js";
|
|
12
13
|
import { sortIssues } from "./sort-issues.js";
|
|
@@ -47,6 +48,8 @@ const runDoctor = async (options, out) => {
|
|
|
47
48
|
...indexes[i]
|
|
48
49
|
])];
|
|
49
50
|
for (const issue of issues) out(format(issue));
|
|
51
|
+
const byName = Object.fromEntries(models.map((model) => [model.name, model]));
|
|
52
|
+
for (const block of config.pages.flatMap((page) => unplannedBlocks(page, byName))) out(` note ${block} declares no reads, so it is outside the page's read plan and reads whatever it reads once it draws\n`);
|
|
50
53
|
const errors = issues.filter((i) => i.severity === "error").length;
|
|
51
54
|
const counted = userTables.models.length > 0 ? `${config.models.length} resources, ${userTables.models.length} user tables` : `${config.models.length} resources`;
|
|
52
55
|
out(`Checked ${builtIn.models.length > 0 ? `${counted} and ${builtIn.models.length} built-in ones` : counted}: ${errors} errors, ${issues.length - errors} warnings.\n`);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { parseFilter } from "@protobase/schema";
|
|
2
|
+
import { findField } from "@protobase/schema/internal";
|
|
3
|
+
//#region src/doctor/slot-samples.ts
|
|
4
|
+
const sentinel = "__pb_doctor_slot__";
|
|
5
|
+
const span = {
|
|
6
|
+
start: 0,
|
|
7
|
+
end: 0
|
|
8
|
+
};
|
|
9
|
+
const text = (value) => ({
|
|
10
|
+
kind: "string",
|
|
11
|
+
value,
|
|
12
|
+
span
|
|
13
|
+
});
|
|
14
|
+
const sampleFor = (field) => {
|
|
15
|
+
switch (field?.type) {
|
|
16
|
+
case "integer":
|
|
17
|
+
case "bigint":
|
|
18
|
+
case "decimal": return {
|
|
19
|
+
kind: "number",
|
|
20
|
+
value: 0,
|
|
21
|
+
raw: "0",
|
|
22
|
+
span
|
|
23
|
+
};
|
|
24
|
+
case "boolean": return {
|
|
25
|
+
kind: "boolean",
|
|
26
|
+
value: true,
|
|
27
|
+
span
|
|
28
|
+
};
|
|
29
|
+
case "enum": return text(field.enumValues?.[0] ?? "x");
|
|
30
|
+
case "timestamp": return {
|
|
31
|
+
kind: "timestamp",
|
|
32
|
+
value: "2000-01-01T00:00:00Z",
|
|
33
|
+
span
|
|
34
|
+
};
|
|
35
|
+
case "date": return text("2000-01-01");
|
|
36
|
+
case "uuid": return text("00000000-0000-0000-0000-000000000000");
|
|
37
|
+
case "currency": return text("EUR");
|
|
38
|
+
case "country": return text("NL");
|
|
39
|
+
default: return text("x");
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const isSlot = (value) => typeof value === "object" && value !== null && value.kind === "string" && value.value === sentinel;
|
|
43
|
+
const fieldOf = (model, path) => path.length === 1 ? findField(model, path[0]) : void 0;
|
|
44
|
+
/** `ast` with each slot's stand-in replaced by a value its field takes in `model`; `related()` terms are left as they are. */
|
|
45
|
+
const fitSlots = (ast, model) => {
|
|
46
|
+
switch (ast.kind) {
|
|
47
|
+
case "and":
|
|
48
|
+
case "or": return {
|
|
49
|
+
...ast,
|
|
50
|
+
args: ast.args.map((arg) => fitSlots(arg, model))
|
|
51
|
+
};
|
|
52
|
+
case "not": return {
|
|
53
|
+
...ast,
|
|
54
|
+
arg: fitSlots(ast.arg, model)
|
|
55
|
+
};
|
|
56
|
+
case "compare": return isSlot(ast.value) && ast.field.kind === "field" ? {
|
|
57
|
+
...ast,
|
|
58
|
+
value: sampleFor(fieldOf(model, ast.field.path))
|
|
59
|
+
} : ast;
|
|
60
|
+
case "in": return ast.field.kind === "field" ? {
|
|
61
|
+
...ast,
|
|
62
|
+
values: ast.values.map((value) => isSlot(value) ? sampleFor(fieldOf(model, ast.field.path)) : value)
|
|
63
|
+
} : ast;
|
|
64
|
+
default: return ast;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/** A planned filter parsed with a stand-in for each slot, a duration after `+` or `-`; `undefined` when it has none. */
|
|
68
|
+
const parseSampled = (filter) => {
|
|
69
|
+
if (filter === void 0) return;
|
|
70
|
+
const sampled = filter.replace(/"(?:[^"\\]|\\.)*"/g, (value) => /^"\{\{[^{}]*\}\}"$/.test(value) ? `"${sentinel}"` : value.replace(/\{\{[^{}]*\}\}/g, "x")).replace(/([-+]\s*)\{\{[^{}]*\}\}/g, "$11d").replace(/\{\{[^{}]*\}\}/g, `"${sentinel}"`);
|
|
71
|
+
const result = parseFilter(sampled);
|
|
72
|
+
if (!result.ok) throw new Error(`The planned filter "${filter}" does not parse: ${result.errors[0].message}`);
|
|
73
|
+
return result.ast;
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
export { fitSlots, parseSampled };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.99.
|
|
3
|
+
"version": "0.99.5",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@better-auth/core": "^1.7.12",
|
|
45
45
|
"@hono/node-server": "^2.1.3",
|
|
46
|
-
"@protobase/config": "0.99.
|
|
47
|
-
"@protobase/layout": "0.99.
|
|
48
|
-
"@protobase/query": "0.99.
|
|
49
|
-
"@protobase/schema": "0.99.
|
|
50
|
-
"@protobase/server": "0.99.
|
|
51
|
-
"@protobase/ui": "0.99.
|
|
52
|
-
"@protobase/workflows": "0.99.
|
|
46
|
+
"@protobase/config": "0.99.5",
|
|
47
|
+
"@protobase/layout": "0.99.5",
|
|
48
|
+
"@protobase/query": "0.99.5",
|
|
49
|
+
"@protobase/schema": "0.99.5",
|
|
50
|
+
"@protobase/server": "0.99.5",
|
|
51
|
+
"@protobase/ui": "0.99.5",
|
|
52
|
+
"@protobase/workflows": "0.99.5",
|
|
53
53
|
"@react-email/render": "^2.1.0",
|
|
54
54
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
55
55
|
"@sentry/vite-plugin": "^11.10.0",
|
|
@@ -19,6 +19,7 @@ Pages of your own, built from blocks in a JSX layout.
|
|
|
19
19
|
- [Full-width pages](../reference/blocks.md#page): A page fills the whole area beside the sidebar instead of a centred column, for a dashboard with many blocks.
|
|
20
20
|
- [Data blocks](../reference/blocks.md#data): Counts, tables, single records, rows of cards, field values and progress bars.
|
|
21
21
|
- [Related records](../reference/layouts.md#related-records) (beta): Layouts repeat over a record's related records, the records a JSON list of keys points at and the items of a JSON list with `ReferenceManyField`, `ReferenceArrayField`, `ArrayField`, `RecordsIterator` and `ReferenceField`, nested as deep as needed, and a table draws such a list.
|
|
22
|
+
- [Read plans](../reference/layouts.md#how-a-page-reads-its-data) (beta): The build plans one read per data block ahead of time, derives the fields each read needs, and checks every nested read's indexes; custom components that fetch their own data are outside the plan.
|
|
22
23
|
- [Table columns on a phone](../reference/blocks.md#columns-on-a-phone): Table blocks choose which columns show at mobile width by priority, with a sensible default, and fall back to a Markdown table.
|
|
23
24
|
- [Row buttons](../reference/blocks.md#row-buttons) (experimental): A table's rows carry a button labelled by a field of the row, such as its next step; the row decides where it goes: a record tab a field names, a list with a filter built from its fields, or an outside https link a field holds. On mobile width the button sits under the row's first cell.
|
|
24
25
|
- [Record rows](../reference/blocks.md#recordrow): A sideways row of cards for a filter, favorites or saved searches, with a count and new-record marks.
|
|
@@ -110,7 +110,7 @@ The URL comes as for [`scaffold`](#scaffold): the connection string, the variabl
|
|
|
110
110
|
Imports `config/index.ts` and the views of `config/*/ui.ts`, as the config module has them by convention, and checks every exported resource against the database, with the resources Protobase adds by what `protobase.config.ts` (read with `command: 'tool'`) and the resources enable: the [webhook](webhooks.md#subscriptions-in-the-admin-app) ones of `webhooks`, Failed jobs, [Activity](history.md) and [Comments](comments.md). The tables [`migration`](#migration) writes for those have every index their reads need, so after the project's migrations they pass; a read of theirs that lacks an index, as on a table an earlier version wrote, is an error and doctor exits 1 until you run `migration` and apply what it writes:
|
|
111
111
|
|
|
112
112
|
- error: missing table or column, field type that does not fit the column (a file field needs a text column), table without a usable key, configured key not backed by a unique index, `.openExisting()` fields that are not the columns of a unique index, relation to a resource the config does not declare
|
|
113
|
-
- error: a read the [scan guard](api.md#safety) refuses, because no index serves a column it filters, sorts, searches or counts by. Doctor reads each table's indexes from the catalog, as the server does, and judges with the scan guard's own rule every read the API takes and the app makes: a filter on each `.filterable()` field, a sort by each `.sortable()` one, `search()` over the search fields, a role's own records (`.owner()`) and the names a relation shows; each view's list in its order and with the filter it opens with, its filter widgets and their counts, its chart, its map, board, calendar and timeline, its card photos, its order by hand, its related sections, galleries and charts, its named lists, its recent records and a saved search's new records; and each
|
|
113
|
+
- error: a read the [scan guard](api.md#safety) refuses, because no index serves a column it filters, sorts, searches or counts by. Doctor reads each table's indexes from the catalog, as the server does, and judges with the scan guard's own rule every read the API takes and the app makes: a filter on each `.filterable()` field, a sort by each `.sortable()` one, `search()` over the search fields, a role's own records (`.owner()`) and the names a relation shows; each view's list in its order and with the filter it opens with, its filter widgets and their counts, its chart, its map, board, calendar and timeline, its card photos, its order by hand, its related sections, galleries and charts, its named lists, its recent records and a saved search's new records; and each read of a [composed page's read plan](layouts.md#how-a-page-reads-its-data): its blocks' lists, counts and aggregates, a `Chart`'s grouped and split fields, a nested list's `limit_per`, and the outer read each `related()` term carries with its own filter and order. Each error is an index to add, with every request that needs it and where it comes from:
|
|
114
114
|
|
|
115
115
|
```
|
|
116
116
|
error agencies: no index serves the column "name", so the API refuses:
|
|
@@ -128,6 +128,8 @@ Imports `config/index.ts` and the views of `config/*/ui.ts`, as the config modul
|
|
|
128
128
|
```
|
|
129
129
|
- warning: enum values differ
|
|
130
130
|
|
|
131
|
+
Doctor also lists, as a note it does not count, each custom component of a page that declares no [reads](custom-components.md#reads), since it reads outside the page's plan.
|
|
132
|
+
|
|
131
133
|
Exit code is 1 when there are errors. Run it after the project's migrations and before a new version serves, as a platform's deploy does: a read it fails is one the API would refuse once the version runs. `protobase build` needs no database, so the indexes are doctor's to check.
|
|
132
134
|
|
|
133
135
|
## `eject`
|
|
@@ -90,6 +90,18 @@ A visual component, such as a map, falls back to the [picture](blocks.md#image)
|
|
|
90
90
|
|
|
91
91
|
The admin app draws the fallback too when the component is missing or throws while drawing; `protobase dev` also shows the warning above it. Without a fallback, the warning stands in the component's place, as above. A [record section](#record-sections) takes `fallback` in `l.custom`.
|
|
92
92
|
|
|
93
|
+
### Reads
|
|
94
|
+
|
|
95
|
+
A component that reads records says which of its props name what it reads, so its read is in the page's [read plan](layouts.md#how-a-page-reads-its-data) with the other blocks' and `protobase doctor` checks the indexes of that read. The plan describes the read; it does not make it, so the component still fetches its own data, through the hooks above:
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
export const TaskBoard = component<{ resource: string; filter?: string; limit?: number }>('TaskBoard', {
|
|
99
|
+
reads: { resource: 'resource', filter: 'filter', limit: 'limit' },
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`reads` takes `resource`, the one prop it needs, and `filter`, `sort`, `fields` (a prop holding a list of field names) and `limit`. A component without `reads` is outside the plan, which lists it as such (`page.reads.outside`), and doctor does not check its reads.
|
|
104
|
+
|
|
93
105
|
### Fields and resources in props
|
|
94
106
|
|
|
95
107
|
A prop that names a field is a field reference, `fieldRef('plan.name')` from `@protobase/layout`, read from the record around the element or the resource its `resource` prop names. The component gets the plain name (`'plan.name'`), and the names are checked when the server starts. A `resource` prop names a resource the way a block's does.
|
|
@@ -115,7 +115,7 @@ A `RecordCard` (its first match), a `ReferenceField`, each card of a `CardRow` a
|
|
|
115
115
|
## Related records
|
|
116
116
|
|
|
117
117
|
:::caution[Beta]
|
|
118
|
-
|
|
118
|
+
The build plans every list's read beside the page's other reads, as [how a page reads its data](#how-a-page-reads-its-data) says. Coming soon: the web starts those reads at page load; until then each list block reads its records once the record around it has drawn.
|
|
119
119
|
:::
|
|
120
120
|
|
|
121
121
|
A record's related records repeat under it as a list: a `ReferenceManyField` holds the records of another resource that point at it, a `ReferenceArrayField` the records whose keys a JSON field of it lists, and an `ArrayField` the items of a JSON list it holds. A `RecordsIterator` inside one draws its children once for each entry, with nothing around them, and a `Table` without `resource` draws the list as a table. A `ReferenceField` draws the one record a field points at. They take the names, props and defaults of the [react-admin](../features/compatibility.md#react-admin) components of the same names.
|
|
@@ -129,7 +129,7 @@ A record's related records repeat under it as a list: a `ReferenceManyField` hol
|
|
|
129
129
|
<RecordsIterator>
|
|
130
130
|
<Card title="Claim {{@index}}">
|
|
131
131
|
<ReferenceField source="worker_id" reference="workers" />
|
|
132
|
-
<ReferenceManyField reference="claim_steps" target="claim_id" empty="No steps yet">
|
|
132
|
+
<ReferenceManyField reference="claim_steps" target="claim_id" perPage={10} empty="No steps yet">
|
|
133
133
|
<RecordsIterator><Field source="name" /></RecordsIterator>
|
|
134
134
|
</ReferenceManyField>
|
|
135
135
|
</Card>
|
|
@@ -140,7 +140,26 @@ A record's related records repeat under it as a list: a `ReferenceManyField` hol
|
|
|
140
140
|
|
|
141
141
|
Each entry is a record of the stack while its children draw, so `Field`, `Show`, `{{path}}` placeholders and nested lists inside it read it, and `@index`, `@first` and `@last` count its place in the list. A text item of an `ArrayField`, such as a label, is read as `this`; an object item by its keys, which no model describes, so the build does not check them, and it warns when an item's key could shadow a field of a record further out. A list ties itself to the record around it by a field of that record, its `source`; a `ReferenceManyField`'s is `id` of the nearest record by default. A `ReferenceManyField`'s and a `ReferenceArrayField`'s `filter` and `sort` take AIP-160 and AIP-132 text, or the objects react-admin writes: `{ status: 'open' }` is `status = "open"`, its fields ANDed, and `{ field: 'claimed_at', order: 'DESC' }` is `claimed_at desc`. Without `sort`, such a list is in its key's order, descending, as react-admin's is. A `ReferenceManyField`'s `pagination="more"` adds a button that reads the next `perPage` records into the same list. The [blocks reference](blocks.md#related-records) lists every prop.
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
## How a page reads its data
|
|
144
|
+
|
|
145
|
+
:::caution[Beta]
|
|
146
|
+
The build plans a page's reads and `/api/meta` sends the plan; the web does not run it yet.
|
|
147
|
+
:::
|
|
148
|
+
|
|
149
|
+
The build compiles each page into a read plan: one ordinary API read per data block, each known before any record is read, so all of them can start together at page load and none waits for another's rows. There is no combined page query. `/api/meta` sends each user the plan of the page as they see it, so it holds no read of a resource they cannot read and no field they cannot read.
|
|
150
|
+
|
|
151
|
+
- **One read per block.** `Stat`, `RecordCard`, `Table`, `CardRow`, `RecordRow`, `RecordCards`, `Thread`, `Chart`, `ReferenceManyField`, `ReferenceArrayField` and `ReferenceField` each make one list, count or `:aggregate` read. A `Thread`'s tool calls, the row a `Table`'s `select` chose and each `<resource>.count` a condition names are reads of their own. A block under `Show` or `Match` is read anyway: the condition decides only what is drawn.
|
|
152
|
+
- **Fields are derived.** You never write `fields`. Each read asks for its key and every field drawn in its scope: by `Field`s, [field components](#field-values), `{{path}}`s, conditions and table columns, and the fields a nested list ties itself by. A block whose view decides what it draws, such as a `Table` without `columns` or a `RecordRow`, reads every field. So does a read whose records an `Action` or an edit form changes, since it asks for each record's ETag and an ETag covers the whole record. The other reads ask for no ETag.
|
|
153
|
+
- **A nested read carries the read around it.** A block inside the records of another ties itself to them by a field: a `ReferenceManyField`'s `target`, a `ReferenceField`'s or `ReferenceArrayField`'s `source`, or `task_id = {{id}}` as a top-level `AND` term of a filter. Its read takes that as a [`related()`](api.md#filtering-through-related-records) term holding the outer read's own filter, order and page size, such as `related(task_id, tasks.id, (status = "open"), "id desc", 10)`, never the values of records read first. A `ReferenceArrayField` ties itself by the JSON list of keys the same way, `related(id, tasks.waits, …)`. Terms nest for a list two levels deep.
|
|
154
|
+
- **Per record, in one read.** Inside a repeated record, such as a `CardRow` or a `RecordsIterator`, a list reads its `perPage` (or `limit`) for each record with [`limit_per`](api.md#first-rows-per-value), and a `Stat` count is one `:aggregate` grouped by the field it ties itself by. Each nested read holds at most 500 records in all, the API's largest page, and the build warns when the records around it times its page are more, as some records would show fewer. A `ReferenceArrayField` there reads one page for all the records around it, of up to `perPage` records for each of them, since one record can sit in several lists. A `RecordRow` there needs a `limit`.
|
|
155
|
+
- **The page's own record.** A field of it that the address does not hold, `{{@root.project_id}}`, becomes `related()` on that record by its key, so the page's record is not read first. Page parameters and the page record's key stay `{{…}}` slots in the plan, which the address fills.
|
|
156
|
+
- **Indexes.** [`protobase doctor`](cli.md#doctor) judges every planned read with the scan guard's rule, the outer reads its `related()` terms carry included, so it reports a nested read's missing index before the API refuses the read.
|
|
157
|
+
|
|
158
|
+
Each read in the plan (`page.reads.reads`) names its block by a JSON Pointer into the page's tree, such as `/children/0/children/2`, which `elementAt(tree, pointer)` finds. It gives the read's `resource`, its `kind` (`list`, `count` or `aggregate`), `filter`, `orderBy`, `pageSize`, `limitPer`, `groupBy`, `fields` (absent for every field) and `etag`. `compileReadPlan(tree, { models })` makes the same plan from a tree of your own.
|
|
159
|
+
|
|
160
|
+
A [custom component](custom-components.md#reads) is planned when it declares its reads. One that declares none is outside the plan (`page.reads.outside`): whatever it reads, it reads by itself once it draws, and `protobase doctor` lists it.
|
|
161
|
+
|
|
162
|
+
<gap>A page cannot start its planned reads at page load because running the read plan on the web is not implemented; each block still reads once it draws.</gap> <gap>A `Chart` cannot sit inside a repeated record because grouping a chart per record is not implemented.</gap>
|
|
144
163
|
|
|
145
164
|
## Field values
|
|
146
165
|
|
|
@@ -217,7 +236,7 @@ The build checks every path where it sits:
|
|
|
217
236
|
- **Reads that would wait.** A per-row value in a filter in any other place than a top-level `AND` equality (with `>=`, inside `OR` or `NOT`, a second one), `@index`, `@first`, `@last` or `this` in a filter, key or values, and any placeholder in `resource`, `sort`, `columns` or `limit` fail with "this would wait for the parent's rows": each block's records are one read of their own, and none may wait for another's rows.
|
|
218
237
|
- **Shadowing.** A bare name that more than one record around it has reads the nearest, and the build warns which it read and how to write the others: `"id" resolves to task_claims; write ../id for the one of tasks`. `protobase build --strict` fails on it instead. `this.`, `./`, `../` and `@` paths never warn.
|
|
219
238
|
|
|
220
|
-
<gap>A block inside a `CardRow` or `RecordCard` cannot read its records at page load, beside the records around it, because
|
|
239
|
+
<gap>A block inside a `CardRow` or `RecordCard` cannot read its records at page load, beside the records around it, because running the [read plan](#how-a-page-reads-its-data) on the web is not implemented; it reads once the card around it draws.</gap> <gap>A block under a `Show` or `Match` cannot read its records before its condition holds because running the read plan on the web is not implemented.</gap>
|
|
221
240
|
|
|
222
241
|
## Page parameters
|
|
223
242
|
|
|
@@ -243,7 +262,7 @@ A `Table` with `select` chooses one of its rows: each row links to the page with
|
|
|
243
262
|
|
|
244
263
|
In the [text form](#text-form), a `SelectInput` reads as its label and its choice's name, `**Window**: Last 30 days`, from the `params` you pass, and a `select` table's children as the row `records` gives for it.
|
|
245
264
|
|
|
246
|
-
<gap>A block reads a page parameter only once it draws, not at page load, because
|
|
265
|
+
<gap>A block reads a page parameter only once it draws, not at page load, because running the [read plan](#how-a-page-reads-its-data) on the web is not implemented.</gap> <gap>A data block other than a `Table`, or a list display such as a map, cannot take `select` because choosing a row there is not implemented.</gap>
|
|
247
266
|
|
|
248
267
|
## Conditions
|
|
249
268
|
|