derive-dashboards 0.1.0 → 0.1.1
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/README.md +4 -3
- package/dist/adapter/sql-adapter.mjs +4 -4
- package/dist/body.mjs +11 -1
- package/dist/cache.d.mts +1 -1
- package/dist/cli/check.mjs +14 -1
- package/dist/cli/import/dbt-schema.mjs +2 -2
- package/dist/cli/import/index.mjs +2 -2
- package/dist/core/conformance/cases.d.mts +3 -0
- package/dist/core/conformance/cases.mjs +20 -4
- package/dist/core/conformance/check.mjs +6 -3
- package/dist/core/conformance/drill-cases.mjs +120 -0
- package/dist/core/document/view.d.mts +26 -7
- package/dist/core/document/view.mjs +42 -11
- package/dist/core/engine/index.d.mts +14 -2
- package/dist/core/engine/index.mjs +12 -1
- package/dist/core/engine/members.mjs +1 -1
- package/dist/core/engine/postgres.mjs +1 -1
- package/dist/core/engine/record-layout.d.mts +11 -0
- package/dist/core/engine/record-layout.mjs +7 -7
- package/dist/core/engine/records.d.mts +3 -2
- package/dist/core/engine/records.mjs +2 -1
- package/dist/core/engine/segmented.mjs +5 -2
- package/dist/core/engine/sql-records.mjs +2 -1
- package/dist/core/engine/statement.mjs +37 -3
- package/dist/core/engine/types.d.mts +3 -0
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +31 -0
- package/dist/core/error/codes.mjs +28 -1
- package/dist/core/index.d.mts +9 -5
- package/dist/core/index.mjs +5 -3
- package/dist/core/model/catalog/check.mjs +1 -1
- package/dist/core/model/hints.d.mts +36 -0
- package/dist/core/model/member.d.mts +4 -2
- package/dist/core/model/member.mjs +16 -3
- package/dist/core/model/types.d.mts +8 -6
- package/dist/core/model/validate/drill.mjs +70 -0
- package/dist/core/model/validate/index.mjs +3 -0
- package/dist/core/model/validate/shape.mjs +150 -0
- package/dist/core/model/validate/source.mjs +5 -3
- package/dist/core/query/alias.mjs +7 -1
- package/dist/core/query/drill/compose.d.mts +22 -0
- package/dist/core/query/drill/compose.mjs +85 -0
- package/dist/core/query/drill/fields.d.mts +21 -0
- package/dist/core/query/drill/fields.mjs +91 -0
- package/dist/core/query/drill/internal.d.mts +11 -0
- package/dist/core/query/drill/items.d.mts +29 -0
- package/dist/core/query/drill/items.mjs +98 -0
- package/dist/core/query/drill/line.d.mts +31 -0
- package/dist/core/query/drill/line.mjs +183 -0
- package/dist/core/query/drill/links.d.mts +33 -0
- package/dist/core/query/drill/links.mjs +31 -0
- package/dist/core/query/drill/operand.mjs +7 -0
- package/dist/core/query/drill/scopes.d.mts +22 -0
- package/dist/core/query/drill/scopes.mjs +58 -0
- package/dist/core/query/drill/selection.d.mts +37 -0
- package/dist/core/query/drill/selection.mjs +144 -0
- package/dist/core/query/drill/steps.d.mts +14 -0
- package/dist/core/query/drill/steps.mjs +35 -0
- package/dist/core/query/drill/synthetic-expr.mjs +85 -0
- package/dist/core/query/drill/synthetic.d.mts +22 -0
- package/dist/core/query/drill/synthetic.mjs +165 -0
- package/dist/core/query/drill/time.d.mts +11 -0
- package/dist/core/query/drill/time.mjs +71 -0
- package/dist/core/query/drill/types.d.mts +155 -0
- package/dist/core/query/drill/types.mjs +4 -0
- package/dist/core/query/drill/views.d.mts +62 -0
- package/dist/core/query/drill/views.mjs +257 -0
- package/dist/core/query/segments.d.mts +12 -2
- package/dist/core/query/segments.mjs +3 -1
- package/dist/core/result/columns.mjs +17 -4
- package/dist/core/result/types.d.mts +5 -1
- package/dist/core/schema/answer-parts.mjs +117 -0
- package/dist/core/schema/answers.d.mts +2108 -128
- package/dist/core/schema/answers.mjs +10 -113
- package/dist/core/schema/commands.d.mts +2 -2
- package/dist/core/schema/document.d.mts +1 -1
- package/dist/core/schema/drill-answers.mjs +179 -0
- package/dist/core/schema/drill.d.mts +260 -0
- package/dist/core/schema/drill.mjs +107 -0
- package/dist/core/schema/index.d.mts +4 -2
- package/dist/core/schema/index.mjs +4 -2
- package/dist/core/schema/picks.d.mts +35 -0
- package/dist/core/schema/picks.mjs +23 -0
- package/dist/core/schema/query.d.mts +1 -1
- package/dist/core/schema/tools.mjs +1 -1
- package/dist/core/schema/view.d.mts +0 -35
- package/dist/core/schema/view.mjs +19 -24
- package/dist/core/store/schema.d.mts +5 -0
- package/dist/core/store/schema.mjs +10 -0
- package/dist/core/types/api.d.mts +15 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +210 -0
- package/dist/core/types/options.d.mts +21 -0
- package/dist/core/types/viewer.d.mts +5 -0
- package/dist/cursor-seal.d.mts +8 -0
- package/dist/cursor-seal.mjs +98 -0
- package/dist/dashboard-id.mjs +1 -1
- package/dist/drill-access.mjs +93 -0
- package/dist/drill-change.mjs +76 -0
- package/dist/drill-crumbs.mjs +113 -0
- package/dist/drill-distinct.mjs +130 -0
- package/dist/drill-explain.mjs +64 -0
- package/dist/drill-identity.mjs +35 -0
- package/dist/drill-item-id.mjs +35 -0
- package/dist/drill-items.mjs +132 -0
- package/dist/drill-open.mjs +169 -0
- package/dist/drill-seal.mjs +170 -0
- package/dist/drill-secret.mjs +14 -0
- package/dist/drill-share.mjs +53 -0
- package/dist/drill-summary.mjs +158 -0
- package/dist/drill-targets.mjs +53 -0
- package/dist/drill-totals.mjs +8 -0
- package/dist/drill-views.mjs +175 -0
- package/dist/drill.mjs +157 -0
- package/dist/editing.mjs +2 -2
- package/dist/endpoints/dashboards.mjs +11 -3
- package/dist/endpoints/drill.mjs +55 -0
- package/dist/endpoints/index.d.mts +233 -2
- package/dist/endpoints/index.mjs +5 -1
- package/dist/endpoints/query.mjs +26 -4
- package/dist/execute.d.mts +19 -1
- package/dist/execute.mjs +15 -17
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +29 -2
- package/dist/mcp/drill.mjs +61 -0
- package/dist/mcp/format.mjs +27 -3
- package/dist/mcp/index.d.mts +15 -4
- package/dist/mcp/index.mjs +13 -7
- package/dist/mcp/query.d.mts +7 -2
- package/dist/mcp/query.mjs +7 -3
- package/dist/node-postgres.d.mts +8 -4
- package/dist/node-postgres.mjs +29 -6
- package/dist/owner.mjs +3 -3
- package/dist/postgres-connection.d.mts +8 -2
- package/dist/postgres-connection.mjs +2 -1
- package/dist/react/Dashboard.d.mts +10 -5
- package/dist/react/Dashboard.mjs +51 -23
- package/dist/react/DashboardList.mjs +30 -2
- package/dist/react/Discard.mjs +7 -2
- package/dist/react/Grid.mjs +12 -5
- package/dist/react/Header.mjs +9 -0
- package/dist/react/Parts.mjs +6 -2
- package/dist/react/ReviewPanel.mjs +13 -2
- package/dist/react/RowBody.d.mts +19 -0
- package/dist/react/RowBody.mjs +55 -0
- package/dist/react/Tile.d.mts +1 -1
- package/dist/react/Tile.mjs +54 -30
- package/dist/react/builder/state.d.mts +1 -1
- package/dist/react/client.d.mts +2 -0
- package/dist/react/client.mjs +1 -0
- package/dist/react/context.d.mts +15 -1
- package/dist/react/context.mjs +19 -4
- package/dist/react/drill/Bars.mjs +179 -0
- package/dist/react/drill/Controls.mjs +173 -0
- package/dist/react/drill/DashboardTile.d.mts +18 -0
- package/dist/react/drill/DashboardTile.mjs +107 -0
- package/dist/react/drill/DrillPanel.d.mts +31 -0
- package/dist/react/drill/DrillPanel.mjs +104 -0
- package/dist/react/drill/DrillTabs.mjs +52 -0
- package/dist/react/drill/Header.mjs +131 -0
- package/dist/react/drill/Items.d.mts +22 -0
- package/dist/react/drill/Items.mjs +164 -0
- package/dist/react/drill/ItemsFoot.mjs +60 -0
- package/dist/react/drill/Notes.mjs +58 -0
- package/dist/react/drill/PanelBody.mjs +74 -0
- package/dist/react/drill/PanelStates.mjs +37 -0
- package/dist/react/drill/board.d.mts +18 -0
- package/dist/react/drill/board.mjs +91 -0
- package/dist/react/drill/counts.mjs +14 -0
- package/dist/react/drill/host.mjs +26 -0
- package/dist/react/drill/item-parts.d.mts +5 -0
- package/dist/react/drill/item-parts.mjs +76 -0
- package/dist/react/drill/item-type.d.mts +26 -0
- package/dist/react/drill/item-type.mjs +7 -0
- package/dist/react/drill/lazy.d.mts +6 -0
- package/dist/react/drill/lazy.mjs +17 -0
- package/dist/react/drill/pages.mjs +48 -0
- package/dist/react/drill/panel-focus.mjs +51 -0
- package/dist/react/drill/props.d.mts +25 -0
- package/dist/react/drill/props.mjs +72 -0
- package/dist/react/drill/state.d.mts +18 -0
- package/dist/react/drill/state.mjs +68 -0
- package/dist/react/drill/useDrill.d.mts +46 -0
- package/dist/react/drill/useDrill.mjs +194 -0
- package/dist/react/drill/words.d.mts +98 -0
- package/dist/react/drill/words.mjs +77 -0
- package/dist/react/english.mjs +9 -1
- package/dist/react/format.d.mts +3 -3
- package/dist/react/format.mjs +34 -12
- package/dist/react/index.d.mts +27 -4
- package/dist/react/index.mjs +10 -3
- package/dist/react/messages.d.mts +14 -1
- package/dist/react/renderers/index.d.mts +10 -2
- package/dist/react/renderers/index.mjs +54 -14
- package/dist/react/renderers/list.d.mts +22 -0
- package/dist/react/renderers/list.mjs +118 -0
- package/dist/react/review-words.mjs +1 -1
- package/dist/react/review.mjs +2 -2
- package/dist/react/scope.mjs +2 -7
- package/dist/react/series.d.mts +37 -0
- package/dist/react/series.mjs +95 -0
- package/dist/react/theme.css +1823 -1296
- package/dist/react/theme.css.d.ts +1 -0
- package/dist/react/tile-store.mjs +15 -3
- package/dist/react/useBoard.d.mts +5 -0
- package/dist/react/useBoard.mjs +4 -1
- package/dist/react/useDashboard.d.mts +1 -0
- package/dist/react/useDashboard.mjs +2 -1
- package/dist/react/useTiles.mjs +50 -15
- package/dist/react/view.d.mts +9 -0
- package/dist/react/view.mjs +22 -10
- package/dist/reading.d.mts +2 -0
- package/dist/reading.mjs +9 -1
- package/dist/recharts/charts.d.mts +0 -1
- package/dist/recharts/charts.mjs +128 -34
- package/dist/recharts/index.d.mts +2 -2
- package/dist/recharts/index.mjs +2 -2
- package/dist/recharts/marks.mjs +83 -0
- package/dist/recharts/series.d.mts +2 -22
- package/dist/recharts/series.mjs +1 -64
- package/dist/records-read.mjs +44 -0
- package/dist/store/apply.mjs +5 -5
- package/dist/store/commands.mjs +4 -4
- package/dist/store/discard.mjs +7 -7
- package/dist/store/drafts.mjs +5 -5
- package/dist/store/editor.mjs +1 -1
- package/dist/store/publish.mjs +12 -12
- package/dist/store/reader.mjs +41 -22
- package/dist/store/review.mjs +16 -16
- package/dist/store/rows.mjs +20 -20
- package/dist/store/undo.mjs +3 -3
- package/dist/store/unpublished.mjs +4 -4
- package/dist/store/versions.mjs +14 -14
- package/dist/testing/adapter-suite.mjs +4 -4
- package/dist/tiles.mjs +37 -3
- package/package.json +8 -10
- package/dist/react/Records.d.mts +0 -12
- package/dist/react/Records.mjs +0 -97
- package/src/adapter/sql-adapter.ts +0 -95
- package/src/adapter/statement.ts +0 -185
- package/src/authorize.ts +0 -73
- package/src/body.ts +0 -169
- package/src/bounded.ts +0 -43
- package/src/cache.ts +0 -192
- package/src/checks.ts +0 -170
- package/src/custom-source.ts +0 -59
- package/src/dashboard-id.ts +0 -36
- package/src/dashboards.ts +0 -132
- package/src/editing.ts +0 -148
- package/src/endpoints/access.ts +0 -41
- package/src/endpoints/dashboards.ts +0 -92
- package/src/endpoints/drafts.ts +0 -138
- package/src/endpoints/index.ts +0 -21
- package/src/endpoints/meta.ts +0 -137
- package/src/endpoints/query.ts +0 -86
- package/src/endpoints/read.ts +0 -92
- package/src/endpoints/review.ts +0 -89
- package/src/endpoints/values.ts +0 -62
- package/src/endpoints/versions.ts +0 -104
- package/src/execute.ts +0 -207
- package/src/handler.ts +0 -232
- package/src/http.ts +0 -66
- package/src/index.ts +0 -163
- package/src/introspect.ts +0 -205
- package/src/mcp/api.ts +0 -59
- package/src/mcp/format.ts +0 -67
- package/src/mcp/index.ts +0 -151
- package/src/mcp/query.ts +0 -153
- package/src/mcp/resolve.ts +0 -114
- package/src/node-postgres.ts +0 -81
- package/src/owner.ts +0 -36
- package/src/postgres-connection.ts +0 -143
- package/src/postgres-store.ts +0 -82
- package/src/principal.ts +0 -39
- package/src/reading.ts +0 -56
- package/src/request-rows.ts +0 -23
- package/src/request.ts +0 -22
- package/src/storage.ts +0 -63
- package/src/store/apply.ts +0 -129
- package/src/store/commands.ts +0 -75
- package/src/store/conflicts.ts +0 -98
- package/src/store/create-tables.ts +0 -104
- package/src/store/discard.ts +0 -42
- package/src/store/drafts.ts +0 -133
- package/src/store/editor.ts +0 -28
- package/src/store/log.ts +0 -28
- package/src/store/publish.ts +0 -199
- package/src/store/publisher.ts +0 -21
- package/src/store/reader.ts +0 -139
- package/src/store/review.ts +0 -239
- package/src/store/rows.ts +0 -250
- package/src/store/tenant.ts +0 -28
- package/src/store/undo.ts +0 -56
- package/src/store/unpublished.ts +0 -33
- package/src/store/versions.ts +0 -106
- package/src/store/writer.ts +0 -19
- package/src/testing/adapter-suite.ts +0 -239
- package/src/testing/index.ts +0 -2
- package/src/tile-doc.ts +0 -46
- package/src/tiles.ts +0 -120
package/src/store/rows.ts
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Author,
|
|
3
|
-
canonical,
|
|
4
|
-
type DashAdapter,
|
|
5
|
-
type Doc,
|
|
6
|
-
type Draft,
|
|
7
|
-
diffDocs,
|
|
8
|
-
markShifted,
|
|
9
|
-
type Row,
|
|
10
|
-
type Tenant,
|
|
11
|
-
} from "@derive-to/dashboards-core";
|
|
12
|
-
import { loadDocument } from "@derive-to/dashboards-core/schema";
|
|
13
|
-
import { sha256 } from "../cache";
|
|
14
|
-
import { HttpError } from "../http";
|
|
15
|
-
import { sameTenant, tenantHash } from "./tenant";
|
|
16
|
-
|
|
17
|
-
/** The store's own reads and writes of single rows, shared by drafts and publishing. */
|
|
18
|
-
|
|
19
|
-
export const now = () => new Date().toISOString();
|
|
20
|
-
|
|
21
|
-
/** A stored document as this version reads it; one that cannot be read is a server error that names why. */
|
|
22
|
-
export function documentOf(spec: unknown, where: string): Doc {
|
|
23
|
-
const loaded = loadDocument(spec);
|
|
24
|
-
if (!loaded.ok) {
|
|
25
|
-
const reason = loaded.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
|
|
26
|
-
// Not retryable: the same stored document fails the same way until someone repairs it.
|
|
27
|
-
throw new HttpError(
|
|
28
|
-
500,
|
|
29
|
-
"INTERNAL",
|
|
30
|
-
`The stored document of ${where} cannot be read: ${reason}`,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
return loaded.doc;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** A tenant's dashboard row by id or key, published or not, never archived; null when absent. */
|
|
37
|
-
export async function dashboardRow(
|
|
38
|
-
db: DashAdapter,
|
|
39
|
-
tenant: Tenant,
|
|
40
|
-
by: { id: string } | { key: string },
|
|
41
|
-
): Promise<Row | null> {
|
|
42
|
-
const field = "id" in by ? "id" : "key";
|
|
43
|
-
const row = await db.findOne({
|
|
44
|
-
table: "dd_dashboard",
|
|
45
|
-
where: [
|
|
46
|
-
{ field: "tenant_hash", value: await tenantHash(tenant) },
|
|
47
|
-
{ field: "archived_at", op: "isNull" },
|
|
48
|
-
{ field, value: "id" in by ? by.id : by.key },
|
|
49
|
-
],
|
|
50
|
-
});
|
|
51
|
-
return row !== null && sameTenant(row["tenant"], tenant) ? row : null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** A dashboard's document at its current version; a built-in or new one has none yet. */
|
|
55
|
-
export async function headDoc(db: DashAdapter, row: Row): Promise<{ version: number; doc?: Doc }> {
|
|
56
|
-
const version = row["current_version"] === null ? 0 : Number(row["current_version"]);
|
|
57
|
-
if (version === 0) return { version };
|
|
58
|
-
const stored = await db.findOne({
|
|
59
|
-
table: "dd_dashboard_version",
|
|
60
|
-
where: [
|
|
61
|
-
{ field: "dashboard_id", value: row["id"] },
|
|
62
|
-
{ field: "n", value: version },
|
|
63
|
-
],
|
|
64
|
-
});
|
|
65
|
-
return { version, doc: documentOf(stored?.["spec"], `dashboard "${String(row["id"])}"`) };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** A document's hash, by its canonical JSON. */
|
|
69
|
-
export const docDigest = (doc: Doc) => sha256(canonical(doc));
|
|
70
|
-
|
|
71
|
-
/** A new dashboard row, unpublished: it serves nothing until its first publish. */
|
|
72
|
-
export async function insertDashboard(
|
|
73
|
-
db: DashAdapter,
|
|
74
|
-
a: { tenant: Tenant; author: Author; doc: Doc; key?: string; requestId?: string },
|
|
75
|
-
): Promise<Row> {
|
|
76
|
-
const digest = a.requestId === undefined ? null : await docDigest(a.doc);
|
|
77
|
-
const at = now();
|
|
78
|
-
const data: Row = {
|
|
79
|
-
id: crypto.randomUUID(),
|
|
80
|
-
tenant: a.tenant,
|
|
81
|
-
tenant_hash: await tenantHash(a.tenant),
|
|
82
|
-
key: a.key ?? null,
|
|
83
|
-
title: a.doc.meta.title,
|
|
84
|
-
description: a.doc.meta.description ?? null,
|
|
85
|
-
owner_id: a.author.id,
|
|
86
|
-
owner_kind: a.author.kind ?? "user",
|
|
87
|
-
current_version: null,
|
|
88
|
-
default_of: a.key ?? null,
|
|
89
|
-
request_id: a.requestId ?? null,
|
|
90
|
-
request_digest: digest,
|
|
91
|
-
archived_at: null,
|
|
92
|
-
created_at: at,
|
|
93
|
-
updated_at: at,
|
|
94
|
-
};
|
|
95
|
-
await db.create({ table: "dd_dashboard", data });
|
|
96
|
-
return data;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The id a draft row is kept under. An agent keeps one draft per dashboard for each person it
|
|
101
|
-
* acts for, so its work for one person never mixes with its work for another. The draft's unique
|
|
102
|
-
* key is (dashboard, author kind, author id), so an agent's id carries the person, length
|
|
103
|
-
* first, which no two pairs of ids share. The person is also kept in `for_id`.
|
|
104
|
-
*/
|
|
105
|
-
export function draftAuthorId(author: Author): string {
|
|
106
|
-
return author.kind === "agent" && author.for !== undefined
|
|
107
|
-
? `${String(author.for.length)}:${author.for}:${author.id}`
|
|
108
|
-
: author.id;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** The id an author is known by, from the id their draft row is kept under. */
|
|
112
|
-
function authorIdOf(row: Row): string {
|
|
113
|
-
const key = String(row["author_id"]);
|
|
114
|
-
const person = row["for_id"];
|
|
115
|
-
if (row["author_kind"] !== "agent" || typeof person !== "string") return key;
|
|
116
|
-
return key.slice(`${String(person.length)}:${person}:`.length);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** The author's draft row of a dashboard, open or closed; null when there has never been one. */
|
|
120
|
-
export function draftRow(
|
|
121
|
-
db: DashAdapter,
|
|
122
|
-
dashboardId: string,
|
|
123
|
-
author: Author,
|
|
124
|
-
): Promise<Row | null> {
|
|
125
|
-
return db.findOne({
|
|
126
|
-
table: "dd_dashboard_draft",
|
|
127
|
-
where: [
|
|
128
|
-
{ field: "dashboard_id", value: dashboardId },
|
|
129
|
-
{ field: "author_kind", value: author.kind ?? "user" },
|
|
130
|
-
{ field: "author_id", value: draftAuthorId(author) },
|
|
131
|
-
],
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** The document a draft holds now. */
|
|
136
|
-
export const draftDoc = (row: Row): Doc =>
|
|
137
|
-
documentOf(row["spec"], `the draft of "${String(row["dashboard_id"])}"`);
|
|
138
|
-
|
|
139
|
-
/** The document a draft started from: the draft is this with its commands applied. */
|
|
140
|
-
export const baseDoc = (row: Row): Doc =>
|
|
141
|
-
documentOf(row["base_spec"], `the start of the draft of "${String(row["dashboard_id"])}"`);
|
|
142
|
-
|
|
143
|
-
/** The document a draft first started from, once review moved it onto a newer version; else none. */
|
|
144
|
-
export const originDoc = (row: Row): Doc | undefined =>
|
|
145
|
-
row["origin_spec"] === null || row["origin_spec"] === undefined
|
|
146
|
-
? undefined
|
|
147
|
-
: documentOf(
|
|
148
|
-
row["origin_spec"],
|
|
149
|
-
`the first start of the draft of "${String(row["dashboard_id"])}"`,
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
/** Who a draft row names in a column pair, if anyone. */
|
|
153
|
-
export function authorIn(row: Row, prefix: string): Author | undefined {
|
|
154
|
-
const id = row[`${prefix}_id`];
|
|
155
|
-
if (typeof id !== "string") return undefined;
|
|
156
|
-
return { id, kind: row[`${prefix}_kind`] === "agent" ? "agent" : "user" };
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** The widgets a draft row says its commands name. */
|
|
160
|
-
export const namedOf = (row: Row): Set<string> =>
|
|
161
|
-
new Set(Array.isArray(row["named"]) ? row["named"].map(String) : []);
|
|
162
|
-
|
|
163
|
-
/** A draft row as the client reads it: its document, changes and undo steps, from the row alone. */
|
|
164
|
-
export function draftOf(row: Row): Draft {
|
|
165
|
-
const doc = draftDoc(row);
|
|
166
|
-
return {
|
|
167
|
-
dashboardId: String(row["dashboard_id"]),
|
|
168
|
-
authorId: authorIdOf(row),
|
|
169
|
-
generation: Number(row["generation"]),
|
|
170
|
-
revision: Number(row["revision"]),
|
|
171
|
-
baseVersion: Number(row["base_version"]),
|
|
172
|
-
doc,
|
|
173
|
-
changes: markShifted(diffDocs(baseDoc(row), doc), namedOf(row)),
|
|
174
|
-
steps: Number(row["steps"]),
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** The refusal for an edit made against another revision of the draft: the one wording of it. */
|
|
179
|
-
const draftConflict = () =>
|
|
180
|
-
new HttpError(
|
|
181
|
-
409,
|
|
182
|
-
"DRAFT_CONFLICT",
|
|
183
|
-
"The draft changed since this edit was made. Reload the draft and try again.",
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
/** The refusal for an edit to a draft that is no longer open: the one wording of it. */
|
|
187
|
-
export const draftClosed = () =>
|
|
188
|
-
new HttpError(
|
|
189
|
-
409,
|
|
190
|
-
"DRAFT_CLOSED",
|
|
191
|
-
"There is no open draft of this dashboard. Start editing again.",
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
/** The author's open draft, or a refusal that says the edit is over. */
|
|
195
|
-
export async function openDraft(
|
|
196
|
-
db: DashAdapter,
|
|
197
|
-
a: { tenant: Tenant; id: string; author: Author },
|
|
198
|
-
): Promise<{ row: Row; dashboard: Row }> {
|
|
199
|
-
const dashboard = await dashboardRow(db, a.tenant, { id: a.id });
|
|
200
|
-
const row = dashboard === null ? null : await draftRow(db, a.id, a.author);
|
|
201
|
-
if (dashboard === null || row === null || row["state"] !== "open") throw draftClosed();
|
|
202
|
-
return { row, dashboard };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/** Refuses an edit made against another revision of the draft than the current one. */
|
|
206
|
-
export function expectRevision(row: Row, expected: number): void {
|
|
207
|
-
expectDraftRevision(Number(row["revision"]), expected);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/** Refuses an edit made against another revision than `current`, with the one message for it. */
|
|
211
|
-
export function expectDraftRevision(current: number | undefined, expected: number): void {
|
|
212
|
-
if (current !== expected) throw draftConflict();
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const ATTEMPTS = 3;
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Runs a write as one transaction, again when it lost a race: another writer took the same
|
|
219
|
-
* unique key (a version number, a draft, a command id) or the database could not serialize
|
|
220
|
-
* the two. The next attempt reads what the winner wrote, so it acknowledges, replays or
|
|
221
|
-
* refuses with a reason. Past the attempts, the request is answered as retryable.
|
|
222
|
-
*/
|
|
223
|
-
export async function inTransaction<T>(
|
|
224
|
-
db: DashAdapter,
|
|
225
|
-
work: (tx: DashAdapter) => Promise<T>,
|
|
226
|
-
): Promise<T> {
|
|
227
|
-
for (let attempt = 1; ; attempt += 1) {
|
|
228
|
-
try {
|
|
229
|
-
// oxlint-disable-next-line no-await-in-loop -- each attempt waits for the one before
|
|
230
|
-
return await db.transaction(work);
|
|
231
|
-
} catch (error) {
|
|
232
|
-
if (db.classifyError(error) === "other") throw error;
|
|
233
|
-
if (attempt === ATTEMPTS)
|
|
234
|
-
throw new HttpError(503, "RETRYABLE", "Others were editing at the same moment. Try again.");
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** Moves the draft to its next revision only if no other edit got there first. */
|
|
240
|
-
export async function advanceDraft(tx: DashAdapter, row: Row, set: Row): Promise<void> {
|
|
241
|
-
const moved = await tx.updateMany({
|
|
242
|
-
table: "dd_dashboard_draft",
|
|
243
|
-
where: [
|
|
244
|
-
{ field: "id", value: row["id"] },
|
|
245
|
-
{ field: "revision", value: row["revision"] },
|
|
246
|
-
],
|
|
247
|
-
set,
|
|
248
|
-
});
|
|
249
|
-
if (moved !== 1) throw draftConflict();
|
|
250
|
-
}
|
package/src/store/tenant.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { canonical } from "@derive-to/dashboards-core";
|
|
2
|
-
import { sha256 } from "../cache";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* One column for a composite tenant: SHA-256 over its canonical JSON (RFC 8785, keys sorted),
|
|
6
|
-
* so no two tenants share a hash by how their parts happen to concatenate. Lookups compare
|
|
7
|
-
* the stored tenant JSON as well, so a collision could never cross tenants.
|
|
8
|
-
*/
|
|
9
|
-
export function tenantHash(tenant: Record<string, string>): Promise<string> {
|
|
10
|
-
return sha256(canonical(tenant));
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Whether a stored tenant column is exactly this tenant: the same keys, the same values. */
|
|
14
|
-
export function sameTenant(stored: unknown, tenant: Record<string, string>): boolean {
|
|
15
|
-
if (typeof stored !== "object" || stored === null) return false;
|
|
16
|
-
const entries = Object.entries(stored);
|
|
17
|
-
return (
|
|
18
|
-
entries.length === Object.keys(tenant).length && entries.every(([k, v]) => tenant[k] === v)
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** A stored tenant column as a tenant: its string values only. */
|
|
23
|
-
export function tenantOf(stored: unknown): Record<string, string> {
|
|
24
|
-
if (typeof stored !== "object" || stored === null) return {};
|
|
25
|
-
return Object.fromEntries(
|
|
26
|
-
Object.entries(stored).filter((e): e is [string, string] => typeof e[1] === "string"),
|
|
27
|
-
);
|
|
28
|
-
}
|
package/src/store/undo.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { type Author, type DashAdapter, reduceAll, type Tenant } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "../http";
|
|
3
|
-
import { keptColumns, keptCommands } from "./commands";
|
|
4
|
-
import {
|
|
5
|
-
advanceDraft,
|
|
6
|
-
baseDoc,
|
|
7
|
-
draftOf,
|
|
8
|
-
expectRevision,
|
|
9
|
-
inTransaction,
|
|
10
|
-
now,
|
|
11
|
-
openDraft,
|
|
12
|
-
} from "./rows";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Steps the author's draft back by its last edit: the draft is the document it started from
|
|
16
|
-
* with its commands applied, so it is rebuilt without that edit's commands. They come back,
|
|
17
|
-
* so a client can apply them again as Redo. The draft's revision moves on, as for any edit.
|
|
18
|
-
*/
|
|
19
|
-
export function undo(
|
|
20
|
-
db: DashAdapter,
|
|
21
|
-
a: { tenant: Tenant; id: string; author: Author; expectedRevision: number },
|
|
22
|
-
) {
|
|
23
|
-
return inTransaction(db, async (tx) => {
|
|
24
|
-
const { row } = await openDraft(tx, a);
|
|
25
|
-
expectRevision(row, a.expectedRevision);
|
|
26
|
-
const base = baseDoc(row);
|
|
27
|
-
const kept = await keptCommands(tx, row["id"]);
|
|
28
|
-
if (kept.length === 0)
|
|
29
|
-
throw new HttpError(409, "NOTHING_TO_UNDO", "The draft has no edits left to undo.");
|
|
30
|
-
const last = Math.max(...kept.map((k) => k.revision));
|
|
31
|
-
const remaining = kept.filter((k) => k.revision !== last);
|
|
32
|
-
// The same commands on the same document applied before, in this order, so they apply again.
|
|
33
|
-
const outcome = reduceAll(
|
|
34
|
-
base,
|
|
35
|
-
remaining.map((k) => k.body),
|
|
36
|
-
);
|
|
37
|
-
if (!outcome.ok)
|
|
38
|
-
throw new HttpError(409, "EDIT_REFUSED", outcome.issue.message, {
|
|
39
|
-
issues: [outcome.issue],
|
|
40
|
-
});
|
|
41
|
-
const revision = Number(row["revision"]) + 1;
|
|
42
|
-
const set = { spec: outcome.doc, revision, ...keptColumns(remaining), updated_at: now() };
|
|
43
|
-
await advanceDraft(tx, row, set);
|
|
44
|
-
await tx.deleteMany({
|
|
45
|
-
table: "dd_dashboard_draft_command",
|
|
46
|
-
where: [
|
|
47
|
-
{ field: "draft_id", value: row["id"] },
|
|
48
|
-
{ field: "revision", value: last },
|
|
49
|
-
],
|
|
50
|
-
});
|
|
51
|
-
return {
|
|
52
|
-
draft: draftOf({ ...row, ...set }),
|
|
53
|
-
undone: kept.filter((k) => k.revision === last).map((k) => k.body),
|
|
54
|
-
};
|
|
55
|
-
});
|
|
56
|
-
}
|
package/src/store/unpublished.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Author, DashAdapter, Tenant, UnpublishedDraft } from "@derive-to/dashboards-core";
|
|
2
|
-
import { dashboardRow, draftAuthorId, draftDoc } from "./rows";
|
|
3
|
-
import { tenantHash } from "./tenant";
|
|
4
|
-
|
|
5
|
-
/** The author's open drafts of dashboards nobody has published yet, newest first. */
|
|
6
|
-
export async function unpublished(
|
|
7
|
-
db: DashAdapter,
|
|
8
|
-
a: { tenant: Tenant; author: Author },
|
|
9
|
-
): Promise<UnpublishedDraft[]> {
|
|
10
|
-
const drafts = await db.findMany({
|
|
11
|
-
table: "dd_dashboard_draft",
|
|
12
|
-
where: [
|
|
13
|
-
{ field: "tenant_hash", value: await tenantHash(a.tenant) },
|
|
14
|
-
{ field: "author_kind", value: a.author.kind ?? "user" },
|
|
15
|
-
{ field: "author_id", value: draftAuthorId(a.author) },
|
|
16
|
-
{ field: "state", value: "open" },
|
|
17
|
-
// A draft of a published dashboard started from a version; a new one's from none.
|
|
18
|
-
{ field: "base_version", value: 0 },
|
|
19
|
-
],
|
|
20
|
-
orderBy: [{ field: "updated_at", direction: "desc" }],
|
|
21
|
-
limit: 100,
|
|
22
|
-
});
|
|
23
|
-
const rows = await Promise.all(
|
|
24
|
-
drafts.map((d) => dashboardRow(db, a.tenant, { id: String(d["dashboard_id"]) })),
|
|
25
|
-
);
|
|
26
|
-
return drafts.flatMap((d, i) => {
|
|
27
|
-
const row = rows[i];
|
|
28
|
-
if (row === null || row === undefined || row["current_version"] !== null) return [];
|
|
29
|
-
// A built-in's copy keeps serving the built-in until published; it is listed as that.
|
|
30
|
-
if (row["default_of"] !== null) return [];
|
|
31
|
-
return [{ dashboardId: String(row["id"]), title: draftDoc(d).meta.title }];
|
|
32
|
-
});
|
|
33
|
-
}
|
package/src/store/versions.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Author,
|
|
3
|
-
DashAdapter,
|
|
4
|
-
Published,
|
|
5
|
-
Tenant,
|
|
6
|
-
VersionEntry,
|
|
7
|
-
} from "@derive-to/dashboards-core";
|
|
8
|
-
import { HttpError } from "../http";
|
|
9
|
-
import { advanceHead, earlier, insertVersion } from "./publish";
|
|
10
|
-
import { dashboardRow, documentOf, headDoc, inTransaction } from "./rows";
|
|
11
|
-
|
|
12
|
-
const PAGE = 50;
|
|
13
|
-
|
|
14
|
-
/** A tenant's dashboard row, or a refusal that names it. */
|
|
15
|
-
async function existing(db: DashAdapter, tenant: Tenant, id: string) {
|
|
16
|
-
const row = await dashboardRow(db, tenant, { id });
|
|
17
|
-
if (row === null) throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
|
|
18
|
-
return row;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const optional = (value: unknown) =>
|
|
22
|
-
value === null || value === undefined ? undefined : Number(value);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A page of the dashboard's published versions, newest first, before a version when given:
|
|
26
|
-
* who made each, when, and from what.
|
|
27
|
-
*/
|
|
28
|
-
export async function versions(
|
|
29
|
-
db: DashAdapter,
|
|
30
|
-
a: { tenant: Tenant; id: string; before?: number; limit?: number },
|
|
31
|
-
): Promise<VersionEntry[]> {
|
|
32
|
-
const row = await existing(db, a.tenant, a.id);
|
|
33
|
-
// A dashboard nobody has published has no history, and is only its maker's: to anyone
|
|
34
|
-
// asking for its history it does not exist, as it does not to /dashboards/get.
|
|
35
|
-
if (row["current_version"] === null)
|
|
36
|
-
throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${a.id}".`);
|
|
37
|
-
const rows = await db.findMany({
|
|
38
|
-
table: "dd_dashboard_version",
|
|
39
|
-
where: [
|
|
40
|
-
{ field: "dashboard_id", value: row["id"] },
|
|
41
|
-
...(a.before === undefined ? [] : [{ field: "n", op: "lt" as const, value: a.before }]),
|
|
42
|
-
],
|
|
43
|
-
orderBy: [{ field: "n", direction: "desc" }],
|
|
44
|
-
limit: Math.min(a.limit ?? PAGE, PAGE),
|
|
45
|
-
});
|
|
46
|
-
return rows.map((v) => {
|
|
47
|
-
const basedOn = optional(v["based_on"]);
|
|
48
|
-
const restoredFrom = optional(v["restored_from"]);
|
|
49
|
-
const kind = v["author_kind"] === "agent" ? "agent" : "user";
|
|
50
|
-
return {
|
|
51
|
-
version: Number(v["n"]),
|
|
52
|
-
author: { id: String(v["author_id"]), kind },
|
|
53
|
-
createdAt: String(v["created_at"]),
|
|
54
|
-
...(basedOn === undefined ? {} : { basedOn }),
|
|
55
|
-
...(restoredFrom === undefined ? {} : { restoredFrom }),
|
|
56
|
-
};
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Publishes an old version's document as the next version. The versions between stay in the
|
|
62
|
-
* history, and open drafts replay onto it when they publish, as onto any newer version.
|
|
63
|
-
*/
|
|
64
|
-
export function restore(
|
|
65
|
-
db: DashAdapter,
|
|
66
|
-
a: {
|
|
67
|
-
tenant: Tenant;
|
|
68
|
-
id: string;
|
|
69
|
-
author: Author;
|
|
70
|
-
version: number;
|
|
71
|
-
requestId: string;
|
|
72
|
-
/** The current version the caller checked the restore against; another one refuses it. */
|
|
73
|
-
expectedHead: number;
|
|
74
|
-
},
|
|
75
|
-
): Promise<Published> {
|
|
76
|
-
return inTransaction(db, async (tx) => {
|
|
77
|
-
const row = await existing(tx, a.tenant, a.id);
|
|
78
|
-
const done = await earlier(tx, row["id"], a.requestId, a.author);
|
|
79
|
-
if (done !== null) return { dashboardId: a.id, version: Number(done["n"]) };
|
|
80
|
-
const old = await tx.findOne({
|
|
81
|
-
table: "dd_dashboard_version",
|
|
82
|
-
where: [
|
|
83
|
-
{ field: "dashboard_id", value: row["id"] },
|
|
84
|
-
{ field: "n", value: a.version },
|
|
85
|
-
],
|
|
86
|
-
});
|
|
87
|
-
if (old === null)
|
|
88
|
-
throw new HttpError(404, "NOT_FOUND", `There is no version ${a.version} of "${a.id}".`);
|
|
89
|
-
const head = await headDoc(tx, row);
|
|
90
|
-
if (head.version !== a.expectedHead)
|
|
91
|
-
throw new HttpError(409, "VERSION_CONFLICT", "The dashboard changed. Reload its history.");
|
|
92
|
-
if (head.version === a.version)
|
|
93
|
-
throw new HttpError(409, "VERSION_CONFLICT", `Version ${a.version} is already current.`);
|
|
94
|
-
const doc = documentOf(old["spec"], `version ${a.version} of "${a.id}"`);
|
|
95
|
-
await insertVersion(tx, {
|
|
96
|
-
...a,
|
|
97
|
-
dashboardId: row["id"],
|
|
98
|
-
n: head.version + 1,
|
|
99
|
-
doc,
|
|
100
|
-
basedOn: head.version,
|
|
101
|
-
restoredFrom: a.version,
|
|
102
|
-
});
|
|
103
|
-
await advanceHead(tx, row["id"], head.version, doc);
|
|
104
|
-
return { dashboardId: a.id, version: head.version + 1 };
|
|
105
|
-
});
|
|
106
|
-
}
|
package/src/store/writer.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, DashboardStore } from "@derive-to/dashboards-core";
|
|
2
|
-
import { draftMethods } from "./editor";
|
|
3
|
-
import { publishMethods } from "./publisher";
|
|
4
|
-
import { createReader } from "./reader";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The store over any adapter: the read path, drafts, and publishing. Every write is one
|
|
8
|
-
* transaction on the adapter; every lookup is by the tenant's hash and checks the stored tenant.
|
|
9
|
-
*/
|
|
10
|
-
export function createStore(db: DashAdapter): DashboardStore {
|
|
11
|
-
const reader = createReader(db);
|
|
12
|
-
return {
|
|
13
|
-
get: (a) => reader.get(a),
|
|
14
|
-
list: (a) => reader.list(a),
|
|
15
|
-
getVersion: (a) => reader.getVersion(a),
|
|
16
|
-
...draftMethods(db),
|
|
17
|
-
...publishMethods(db),
|
|
18
|
-
};
|
|
19
|
-
}
|