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.
Files changed (301) hide show
  1. package/README.md +4 -3
  2. package/dist/adapter/sql-adapter.mjs +4 -4
  3. package/dist/body.mjs +11 -1
  4. package/dist/cache.d.mts +1 -1
  5. package/dist/cli/check.mjs +14 -1
  6. package/dist/cli/import/dbt-schema.mjs +2 -2
  7. package/dist/cli/import/index.mjs +2 -2
  8. package/dist/core/conformance/cases.d.mts +3 -0
  9. package/dist/core/conformance/cases.mjs +20 -4
  10. package/dist/core/conformance/check.mjs +6 -3
  11. package/dist/core/conformance/drill-cases.mjs +120 -0
  12. package/dist/core/document/view.d.mts +26 -7
  13. package/dist/core/document/view.mjs +42 -11
  14. package/dist/core/engine/index.d.mts +14 -2
  15. package/dist/core/engine/index.mjs +12 -1
  16. package/dist/core/engine/members.mjs +1 -1
  17. package/dist/core/engine/postgres.mjs +1 -1
  18. package/dist/core/engine/record-layout.d.mts +11 -0
  19. package/dist/core/engine/record-layout.mjs +7 -7
  20. package/dist/core/engine/records.d.mts +3 -2
  21. package/dist/core/engine/records.mjs +2 -1
  22. package/dist/core/engine/segmented.mjs +5 -2
  23. package/dist/core/engine/sql-records.mjs +2 -1
  24. package/dist/core/engine/statement.mjs +37 -3
  25. package/dist/core/engine/types.d.mts +3 -0
  26. package/dist/core/engine/version.d.mts +1 -1
  27. package/dist/core/engine/version.mjs +1 -1
  28. package/dist/core/error/codes.d.mts +31 -0
  29. package/dist/core/error/codes.mjs +28 -1
  30. package/dist/core/index.d.mts +9 -5
  31. package/dist/core/index.mjs +5 -3
  32. package/dist/core/model/catalog/check.mjs +1 -1
  33. package/dist/core/model/hints.d.mts +36 -0
  34. package/dist/core/model/member.d.mts +4 -2
  35. package/dist/core/model/member.mjs +16 -3
  36. package/dist/core/model/types.d.mts +8 -6
  37. package/dist/core/model/validate/drill.mjs +70 -0
  38. package/dist/core/model/validate/index.mjs +3 -0
  39. package/dist/core/model/validate/shape.mjs +150 -0
  40. package/dist/core/model/validate/source.mjs +5 -3
  41. package/dist/core/query/alias.mjs +7 -1
  42. package/dist/core/query/drill/compose.d.mts +22 -0
  43. package/dist/core/query/drill/compose.mjs +85 -0
  44. package/dist/core/query/drill/fields.d.mts +21 -0
  45. package/dist/core/query/drill/fields.mjs +91 -0
  46. package/dist/core/query/drill/internal.d.mts +11 -0
  47. package/dist/core/query/drill/items.d.mts +29 -0
  48. package/dist/core/query/drill/items.mjs +98 -0
  49. package/dist/core/query/drill/line.d.mts +31 -0
  50. package/dist/core/query/drill/line.mjs +183 -0
  51. package/dist/core/query/drill/links.d.mts +33 -0
  52. package/dist/core/query/drill/links.mjs +31 -0
  53. package/dist/core/query/drill/operand.mjs +7 -0
  54. package/dist/core/query/drill/scopes.d.mts +22 -0
  55. package/dist/core/query/drill/scopes.mjs +58 -0
  56. package/dist/core/query/drill/selection.d.mts +37 -0
  57. package/dist/core/query/drill/selection.mjs +144 -0
  58. package/dist/core/query/drill/steps.d.mts +14 -0
  59. package/dist/core/query/drill/steps.mjs +35 -0
  60. package/dist/core/query/drill/synthetic-expr.mjs +85 -0
  61. package/dist/core/query/drill/synthetic.d.mts +22 -0
  62. package/dist/core/query/drill/synthetic.mjs +165 -0
  63. package/dist/core/query/drill/time.d.mts +11 -0
  64. package/dist/core/query/drill/time.mjs +71 -0
  65. package/dist/core/query/drill/types.d.mts +155 -0
  66. package/dist/core/query/drill/types.mjs +4 -0
  67. package/dist/core/query/drill/views.d.mts +62 -0
  68. package/dist/core/query/drill/views.mjs +257 -0
  69. package/dist/core/query/segments.d.mts +12 -2
  70. package/dist/core/query/segments.mjs +3 -1
  71. package/dist/core/result/columns.mjs +17 -4
  72. package/dist/core/result/types.d.mts +5 -1
  73. package/dist/core/schema/answer-parts.mjs +117 -0
  74. package/dist/core/schema/answers.d.mts +2108 -128
  75. package/dist/core/schema/answers.mjs +10 -113
  76. package/dist/core/schema/commands.d.mts +2 -2
  77. package/dist/core/schema/document.d.mts +1 -1
  78. package/dist/core/schema/drill-answers.mjs +179 -0
  79. package/dist/core/schema/drill.d.mts +260 -0
  80. package/dist/core/schema/drill.mjs +107 -0
  81. package/dist/core/schema/index.d.mts +4 -2
  82. package/dist/core/schema/index.mjs +4 -2
  83. package/dist/core/schema/picks.d.mts +35 -0
  84. package/dist/core/schema/picks.mjs +23 -0
  85. package/dist/core/schema/query.d.mts +1 -1
  86. package/dist/core/schema/tools.mjs +1 -1
  87. package/dist/core/schema/view.d.mts +0 -35
  88. package/dist/core/schema/view.mjs +19 -24
  89. package/dist/core/store/schema.d.mts +5 -0
  90. package/dist/core/store/schema.mjs +10 -0
  91. package/dist/core/types/api.d.mts +15 -1
  92. package/dist/core/types/api.mjs +1 -1
  93. package/dist/core/types/drill-api.d.mts +210 -0
  94. package/dist/core/types/options.d.mts +21 -0
  95. package/dist/core/types/viewer.d.mts +5 -0
  96. package/dist/cursor-seal.d.mts +8 -0
  97. package/dist/cursor-seal.mjs +98 -0
  98. package/dist/dashboard-id.mjs +1 -1
  99. package/dist/drill-access.mjs +93 -0
  100. package/dist/drill-change.mjs +76 -0
  101. package/dist/drill-crumbs.mjs +113 -0
  102. package/dist/drill-distinct.mjs +130 -0
  103. package/dist/drill-explain.mjs +64 -0
  104. package/dist/drill-identity.mjs +35 -0
  105. package/dist/drill-item-id.mjs +35 -0
  106. package/dist/drill-items.mjs +132 -0
  107. package/dist/drill-open.mjs +169 -0
  108. package/dist/drill-seal.mjs +170 -0
  109. package/dist/drill-secret.mjs +14 -0
  110. package/dist/drill-share.mjs +53 -0
  111. package/dist/drill-summary.mjs +158 -0
  112. package/dist/drill-targets.mjs +53 -0
  113. package/dist/drill-totals.mjs +8 -0
  114. package/dist/drill-views.mjs +175 -0
  115. package/dist/drill.mjs +157 -0
  116. package/dist/editing.mjs +2 -2
  117. package/dist/endpoints/dashboards.mjs +11 -3
  118. package/dist/endpoints/drill.mjs +55 -0
  119. package/dist/endpoints/index.d.mts +233 -2
  120. package/dist/endpoints/index.mjs +5 -1
  121. package/dist/endpoints/query.mjs +26 -4
  122. package/dist/execute.d.mts +19 -1
  123. package/dist/execute.mjs +15 -17
  124. package/dist/index.d.mts +3 -1
  125. package/dist/index.mjs +29 -2
  126. package/dist/mcp/drill.mjs +61 -0
  127. package/dist/mcp/format.mjs +27 -3
  128. package/dist/mcp/index.d.mts +15 -4
  129. package/dist/mcp/index.mjs +13 -7
  130. package/dist/mcp/query.d.mts +7 -2
  131. package/dist/mcp/query.mjs +7 -3
  132. package/dist/node-postgres.d.mts +8 -4
  133. package/dist/node-postgres.mjs +29 -6
  134. package/dist/owner.mjs +3 -3
  135. package/dist/postgres-connection.d.mts +8 -2
  136. package/dist/postgres-connection.mjs +2 -1
  137. package/dist/react/Dashboard.d.mts +10 -5
  138. package/dist/react/Dashboard.mjs +51 -23
  139. package/dist/react/DashboardList.mjs +30 -2
  140. package/dist/react/Discard.mjs +7 -2
  141. package/dist/react/Grid.mjs +12 -5
  142. package/dist/react/Header.mjs +9 -0
  143. package/dist/react/Parts.mjs +6 -2
  144. package/dist/react/ReviewPanel.mjs +13 -2
  145. package/dist/react/RowBody.d.mts +19 -0
  146. package/dist/react/RowBody.mjs +55 -0
  147. package/dist/react/Tile.d.mts +1 -1
  148. package/dist/react/Tile.mjs +54 -30
  149. package/dist/react/builder/state.d.mts +1 -1
  150. package/dist/react/client.d.mts +2 -0
  151. package/dist/react/client.mjs +1 -0
  152. package/dist/react/context.d.mts +15 -1
  153. package/dist/react/context.mjs +19 -4
  154. package/dist/react/drill/Bars.mjs +179 -0
  155. package/dist/react/drill/Controls.mjs +173 -0
  156. package/dist/react/drill/DashboardTile.d.mts +18 -0
  157. package/dist/react/drill/DashboardTile.mjs +107 -0
  158. package/dist/react/drill/DrillPanel.d.mts +31 -0
  159. package/dist/react/drill/DrillPanel.mjs +104 -0
  160. package/dist/react/drill/DrillTabs.mjs +52 -0
  161. package/dist/react/drill/Header.mjs +131 -0
  162. package/dist/react/drill/Items.d.mts +22 -0
  163. package/dist/react/drill/Items.mjs +164 -0
  164. package/dist/react/drill/ItemsFoot.mjs +60 -0
  165. package/dist/react/drill/Notes.mjs +58 -0
  166. package/dist/react/drill/PanelBody.mjs +74 -0
  167. package/dist/react/drill/PanelStates.mjs +37 -0
  168. package/dist/react/drill/board.d.mts +18 -0
  169. package/dist/react/drill/board.mjs +91 -0
  170. package/dist/react/drill/counts.mjs +14 -0
  171. package/dist/react/drill/host.mjs +26 -0
  172. package/dist/react/drill/item-parts.d.mts +5 -0
  173. package/dist/react/drill/item-parts.mjs +76 -0
  174. package/dist/react/drill/item-type.d.mts +26 -0
  175. package/dist/react/drill/item-type.mjs +7 -0
  176. package/dist/react/drill/lazy.d.mts +6 -0
  177. package/dist/react/drill/lazy.mjs +17 -0
  178. package/dist/react/drill/pages.mjs +48 -0
  179. package/dist/react/drill/panel-focus.mjs +51 -0
  180. package/dist/react/drill/props.d.mts +25 -0
  181. package/dist/react/drill/props.mjs +72 -0
  182. package/dist/react/drill/state.d.mts +18 -0
  183. package/dist/react/drill/state.mjs +68 -0
  184. package/dist/react/drill/useDrill.d.mts +46 -0
  185. package/dist/react/drill/useDrill.mjs +194 -0
  186. package/dist/react/drill/words.d.mts +98 -0
  187. package/dist/react/drill/words.mjs +77 -0
  188. package/dist/react/english.mjs +9 -1
  189. package/dist/react/format.d.mts +3 -3
  190. package/dist/react/format.mjs +34 -12
  191. package/dist/react/index.d.mts +27 -4
  192. package/dist/react/index.mjs +10 -3
  193. package/dist/react/messages.d.mts +14 -1
  194. package/dist/react/renderers/index.d.mts +10 -2
  195. package/dist/react/renderers/index.mjs +54 -14
  196. package/dist/react/renderers/list.d.mts +22 -0
  197. package/dist/react/renderers/list.mjs +118 -0
  198. package/dist/react/review-words.mjs +1 -1
  199. package/dist/react/review.mjs +2 -2
  200. package/dist/react/scope.mjs +2 -7
  201. package/dist/react/series.d.mts +37 -0
  202. package/dist/react/series.mjs +95 -0
  203. package/dist/react/theme.css +1823 -1296
  204. package/dist/react/theme.css.d.ts +1 -0
  205. package/dist/react/tile-store.mjs +15 -3
  206. package/dist/react/useBoard.d.mts +5 -0
  207. package/dist/react/useBoard.mjs +4 -1
  208. package/dist/react/useDashboard.d.mts +1 -0
  209. package/dist/react/useDashboard.mjs +2 -1
  210. package/dist/react/useTiles.mjs +50 -15
  211. package/dist/react/view.d.mts +9 -0
  212. package/dist/react/view.mjs +22 -10
  213. package/dist/reading.d.mts +2 -0
  214. package/dist/reading.mjs +9 -1
  215. package/dist/recharts/charts.d.mts +0 -1
  216. package/dist/recharts/charts.mjs +128 -34
  217. package/dist/recharts/index.d.mts +2 -2
  218. package/dist/recharts/index.mjs +2 -2
  219. package/dist/recharts/marks.mjs +83 -0
  220. package/dist/recharts/series.d.mts +2 -22
  221. package/dist/recharts/series.mjs +1 -64
  222. package/dist/records-read.mjs +44 -0
  223. package/dist/store/apply.mjs +5 -5
  224. package/dist/store/commands.mjs +4 -4
  225. package/dist/store/discard.mjs +7 -7
  226. package/dist/store/drafts.mjs +5 -5
  227. package/dist/store/editor.mjs +1 -1
  228. package/dist/store/publish.mjs +12 -12
  229. package/dist/store/reader.mjs +41 -22
  230. package/dist/store/review.mjs +16 -16
  231. package/dist/store/rows.mjs +20 -20
  232. package/dist/store/undo.mjs +3 -3
  233. package/dist/store/unpublished.mjs +4 -4
  234. package/dist/store/versions.mjs +14 -14
  235. package/dist/testing/adapter-suite.mjs +4 -4
  236. package/dist/tiles.mjs +37 -3
  237. package/package.json +8 -10
  238. package/dist/react/Records.d.mts +0 -12
  239. package/dist/react/Records.mjs +0 -97
  240. package/src/adapter/sql-adapter.ts +0 -95
  241. package/src/adapter/statement.ts +0 -185
  242. package/src/authorize.ts +0 -73
  243. package/src/body.ts +0 -169
  244. package/src/bounded.ts +0 -43
  245. package/src/cache.ts +0 -192
  246. package/src/checks.ts +0 -170
  247. package/src/custom-source.ts +0 -59
  248. package/src/dashboard-id.ts +0 -36
  249. package/src/dashboards.ts +0 -132
  250. package/src/editing.ts +0 -148
  251. package/src/endpoints/access.ts +0 -41
  252. package/src/endpoints/dashboards.ts +0 -92
  253. package/src/endpoints/drafts.ts +0 -138
  254. package/src/endpoints/index.ts +0 -21
  255. package/src/endpoints/meta.ts +0 -137
  256. package/src/endpoints/query.ts +0 -86
  257. package/src/endpoints/read.ts +0 -92
  258. package/src/endpoints/review.ts +0 -89
  259. package/src/endpoints/values.ts +0 -62
  260. package/src/endpoints/versions.ts +0 -104
  261. package/src/execute.ts +0 -207
  262. package/src/handler.ts +0 -232
  263. package/src/http.ts +0 -66
  264. package/src/index.ts +0 -163
  265. package/src/introspect.ts +0 -205
  266. package/src/mcp/api.ts +0 -59
  267. package/src/mcp/format.ts +0 -67
  268. package/src/mcp/index.ts +0 -151
  269. package/src/mcp/query.ts +0 -153
  270. package/src/mcp/resolve.ts +0 -114
  271. package/src/node-postgres.ts +0 -81
  272. package/src/owner.ts +0 -36
  273. package/src/postgres-connection.ts +0 -143
  274. package/src/postgres-store.ts +0 -82
  275. package/src/principal.ts +0 -39
  276. package/src/reading.ts +0 -56
  277. package/src/request-rows.ts +0 -23
  278. package/src/request.ts +0 -22
  279. package/src/storage.ts +0 -63
  280. package/src/store/apply.ts +0 -129
  281. package/src/store/commands.ts +0 -75
  282. package/src/store/conflicts.ts +0 -98
  283. package/src/store/create-tables.ts +0 -104
  284. package/src/store/discard.ts +0 -42
  285. package/src/store/drafts.ts +0 -133
  286. package/src/store/editor.ts +0 -28
  287. package/src/store/log.ts +0 -28
  288. package/src/store/publish.ts +0 -199
  289. package/src/store/publisher.ts +0 -21
  290. package/src/store/reader.ts +0 -139
  291. package/src/store/review.ts +0 -239
  292. package/src/store/rows.ts +0 -250
  293. package/src/store/tenant.ts +0 -28
  294. package/src/store/undo.ts +0 -56
  295. package/src/store/unpublished.ts +0 -33
  296. package/src/store/versions.ts +0 -106
  297. package/src/store/writer.ts +0 -19
  298. package/src/testing/adapter-suite.ts +0 -239
  299. package/src/testing/index.ts +0 -2
  300. package/src/tile-doc.ts +0 -46
  301. package/src/tiles.ts +0 -120
@@ -1,199 +0,0 @@
1
- import {
2
- type Author,
3
- type DashAdapter,
4
- type Doc,
5
- type Published,
6
- type Row,
7
- replay,
8
- type Tenant,
9
- } from "@derive-to/dashboards-core";
10
- import { HttpError } from "../http";
11
- import { keptCommands } from "./commands";
12
- import { expectHead } from "./review";
13
- import {
14
- advanceDraft,
15
- dashboardRow,
16
- draftClosed,
17
- draftDoc,
18
- draftRow,
19
- expectRevision,
20
- headDoc,
21
- inTransaction,
22
- now,
23
- } from "./rows";
24
- import { tenantHash } from "./tenant";
25
-
26
- /**
27
- * The document to publish: what the person reviewed. When nobody published since the draft
28
- * started, the draft itself; otherwise its commands replayed on the newer version, which the
29
- * review showed. A change that no longer applies refuses the publish until it is decided.
30
- */
31
- async function publishedDoc(tx: DashAdapter, draft: Row, dashboard: Row, expected: number) {
32
- const head = await headDoc(tx, dashboard);
33
- expectHead(head.version, expected);
34
- if (head.version === Number(draft["base_version"]) || head.doc === undefined)
35
- return { doc: draftDoc(draft), head: head.version };
36
- const merged = replay(head.doc, await keptCommands(tx, draft["id"]));
37
- const refused = merged.outcomes.flatMap((o) => (o.applied ? [] : [o.issue]));
38
- const [issue] = refused;
39
- if (issue !== undefined)
40
- throw new HttpError(
41
- 409,
42
- "REPLAY_REFUSED",
43
- `${String(refused.length)} of your changes no longer apply to version ${String(head.version)}: ${issue.message}`,
44
- { issues: refused },
45
- );
46
- return { doc: merged.doc, head: head.version, replayedOn: head.version };
47
- }
48
-
49
- /** The version a retried publish already made, found by its request id. */
50
- /**
51
- * The version a retried publish or restore already made, by its author's request id: a request
52
- * id is the author's own, so another author's same id is never taken for theirs.
53
- */
54
- export function earlier(tx: DashAdapter, dashboardId: unknown, requestId: string, author: Author) {
55
- return tx.findOne({
56
- table: "dd_dashboard_version",
57
- where: [
58
- { field: "dashboard_id", value: dashboardId },
59
- { field: "request_id", value: requestId },
60
- { field: "author_kind", value: author.kind ?? "user" },
61
- { field: "author_id", value: author.id },
62
- ],
63
- });
64
- }
65
-
66
- export async function insertVersion(
67
- tx: DashAdapter,
68
- a: {
69
- tenant: Tenant;
70
- author: Author;
71
- requestId: string;
72
- dashboardId: unknown;
73
- n: number;
74
- doc: Doc;
75
- basedOn: number;
76
- restoredFrom?: number;
77
- },
78
- ) {
79
- await tx.create({
80
- table: "dd_dashboard_version",
81
- data: {
82
- id: crypto.randomUUID(),
83
- dashboard_id: a.dashboardId,
84
- tenant_hash: await tenantHash(a.tenant),
85
- n: a.n,
86
- spec: a.doc,
87
- spec_version: a.doc.specVersion,
88
- author_id: a.author.id,
89
- author_kind: a.author.kind ?? "user",
90
- request_id: a.requestId,
91
- based_on: a.basedOn === 0 ? null : a.basedOn,
92
- restored_from: a.restoredFrom ?? null,
93
- message: null,
94
- created_at: now(),
95
- },
96
- });
97
- }
98
-
99
- /**
100
- * Makes `head + 1` the dashboard's current version, only if `head` still is: the unique version
101
- * number already stops two publishes, and this keeps the head from ever moving backwards.
102
- */
103
- export async function advanceHead(tx: DashAdapter, dashboardId: unknown, head: number, doc: Doc) {
104
- const moved = await tx.updateMany({
105
- table: "dd_dashboard",
106
- where: [
107
- { field: "id", value: dashboardId },
108
- head === 0
109
- ? { field: "current_version", op: "isNull" }
110
- : { field: "current_version", value: head },
111
- ],
112
- set: {
113
- current_version: head + 1,
114
- title: doc.meta.title,
115
- description: doc.meta.description ?? null,
116
- updated_at: now(),
117
- },
118
- });
119
- if (moved !== 1)
120
- throw new HttpError(503, "RETRYABLE", "Someone published at the same moment. Try again.");
121
- }
122
-
123
- /**
124
- * What a publish already did, by the rows read once: the version a retry made (refusing new
125
- * edits sent with an old request id, which would otherwise publish nothing and say it worked),
126
- * or the current version when this request closed the draft without edits. Null for a new one.
127
- */
128
- async function answeredBefore(
129
- tx: DashAdapter,
130
- a: { id: string; author: Author; requestId: string },
131
- rows: { draft: Row | null; dashboard: Row },
132
- ): Promise<number | null> {
133
- const done = await earlier(tx, a.id, a.requestId, a.author);
134
- const { draft, dashboard } = rows;
135
- if (done !== null) {
136
- if (draft?.["state"] === "open")
137
- throw new HttpError(
138
- 409,
139
- "REQUEST_REUSED",
140
- `The request id "${a.requestId}" already published version ${String(done["n"])}. Send a new one for this publish.`,
141
- );
142
- return Number(done["n"]);
143
- }
144
- const quiet = draft?.["state"] === "closed" && draft["closed_by"] === a.requestId;
145
- return quiet && dashboard["current_version"] !== null
146
- ? Number(dashboard["current_version"])
147
- : null;
148
- }
149
-
150
- /**
151
- * Publishes the author's open draft as the dashboard's next version, in one transaction, and
152
- * closes the draft. A retry with the same request id returns the version it already made.
153
- */
154
- export function publish(
155
- db: DashAdapter,
156
- a: {
157
- tenant: Tenant;
158
- id: string;
159
- author: Author;
160
- expectedRevision: number;
161
- requestId: string;
162
- head: number;
163
- },
164
- ): Promise<Published> {
165
- return inTransaction(db, async (tx) => {
166
- // Each row read once: the dashboard, then the author's draft.
167
- const dashboard = await dashboardRow(tx, a.tenant, { id: a.id });
168
- if (dashboard === null)
169
- throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${a.id}".`);
170
- const draft = await draftRow(tx, a.id, a.author);
171
- const before = await answeredBefore(tx, a, { draft, dashboard });
172
- if (before !== null) return { dashboardId: a.id, version: before };
173
- if (draft?.["state"] !== "open") throw draftClosed();
174
- const row = draft;
175
- expectRevision(row, a.expectedRevision);
176
- const current = dashboard["current_version"];
177
- // A draft with no edits changes nothing: it closes, and no empty version is made.
178
- if (Number(row["revision"]) === 0 && current !== null) {
179
- await advanceDraft(tx, row, { state: "closed", closed_by: a.requestId, updated_at: now() });
180
- return { dashboardId: a.id, version: Number(current) };
181
- }
182
- const { doc, head, replayedOn } = await publishedDoc(tx, row, dashboard, a.head);
183
- const n = head + 1;
184
- await insertVersion(tx, {
185
- ...a,
186
- dashboardId: dashboard["id"],
187
- n,
188
- doc,
189
- basedOn: Number(row["base_version"]),
190
- });
191
- await advanceHead(tx, dashboard["id"], head, doc);
192
- await tx.deleteMany({
193
- table: "dd_dashboard_draft_command",
194
- where: [{ field: "draft_id", value: row["id"] }],
195
- });
196
- await advanceDraft(tx, row, { state: "closed", updated_at: now() });
197
- return { dashboardId: a.id, version: n, ...(replayedOn === undefined ? {} : { replayedOn }) };
198
- });
199
- }
@@ -1,21 +0,0 @@
1
- import type { DashAdapter, DashboardStore } from "@derive-to/dashboards-core";
2
- import { publish } from "./publish";
3
- import { draftDocument, preview, rebase } from "./review";
4
- import { restore, versions } from "./versions";
5
-
6
- type Publishing = Pick<
7
- DashboardStore,
8
- "preview" | "draftDocument" | "rebase" | "publish" | "versions" | "restore"
9
- >;
10
-
11
- /** What goes live and how: review, publish, history and restore. */
12
- export function publishMethods(db: DashAdapter): Publishing {
13
- return {
14
- preview: (a) => preview(db, a),
15
- draftDocument: (a) => draftDocument(db, a),
16
- rebase: (a) => rebase(db, a),
17
- publish: (a) => publish(db, a),
18
- versions: (a) => versions(db, a),
19
- restore: (a) => restore(db, a),
20
- };
21
- }
@@ -1,139 +0,0 @@
1
- import type {
2
- DashAdapter,
3
- DashboardHead,
4
- DashboardReader,
5
- DashboardSummary,
6
- Row,
7
- StoredVersion,
8
- Tenant,
9
- Where,
10
- } from "@derive-to/dashboards-core";
11
- import { documentOf } from "./rows";
12
- import { sameTenant, tenantHash, tenantOf } from "./tenant";
13
-
14
- const text = (value: unknown) => (typeof value === "string" ? value : undefined);
15
-
16
- function summaryOf(row: Row): DashboardSummary {
17
- const id = String(row["id"]);
18
- const optional = (key: string, value: unknown) => {
19
- const t = text(value);
20
- return t === undefined ? {} : { [key]: t };
21
- };
22
- return {
23
- id,
24
- ...optional("key", row["key"]),
25
- ...optional("defaultOf", row["default_of"]),
26
- tenant: tenantOf(row["tenant"]),
27
- title: String(row["title"]),
28
- ...optional("description", row["description"]),
29
- ownerId: String(row["owner_id"]),
30
- ownerKind: row["owner_kind"] === "agent" ? "agent" : "user",
31
- currentVersion: Number(row["current_version"]),
32
- token: `${id}@${String(row["current_version"])}`,
33
- ...optional("archivedAt", row["archived_at"]),
34
- };
35
- }
36
-
37
- /** A dashboard with its document at its current version. */
38
- function headOf(row: Row, spec: unknown): DashboardHead {
39
- return { ...summaryOf(row), doc: documentOf(spec, `dashboard "${String(row["id"])}"`) };
40
- }
41
-
42
- /**
43
- * The store's read path: a tenant's dashboards at their current versions, and any version by
44
- * number. Every lookup is by the tenant's hash and checks the stored tenant itself, so no read
45
- * can cross tenants.
46
- */
47
- class StoreReader implements DashboardReader {
48
- readonly #adapter: DashAdapter;
49
-
50
- constructor(adapter: DashAdapter) {
51
- this.#adapter = adapter;
52
- }
53
-
54
- async #live(tenant: Tenant): Promise<Where[]> {
55
- return [
56
- { field: "tenant_hash", value: await tenantHash(tenant) },
57
- { field: "archived_at", op: "isNull" },
58
- ];
59
- }
60
-
61
- #version(dashboardId: string, n: number): Promise<Row | null> {
62
- return this.#adapter.findOne({
63
- table: "dd_dashboard_version",
64
- where: [
65
- { field: "dashboard_id", value: dashboardId },
66
- { field: "n", value: n },
67
- ],
68
- });
69
- }
70
-
71
- /** A dashboard as viewers see it: none until its first publish. */
72
- #head(row: Row | null, tenant: Tenant): Promise<DashboardHead | null> {
73
- return storedHead(this.#adapter, row, tenant);
74
- }
75
-
76
- async get({ tenant, ref }: { tenant: Tenant; ref: { id: string } | { key: string } }) {
77
- const live = await this.#live(tenant);
78
- const where = "id" in ref ? { field: "id", value: ref.id } : { field: "key", value: ref.key };
79
- const row = await this.#adapter.findOne({ table: "dd_dashboard", where: [...live, where] });
80
- return this.#head(row, tenant);
81
- }
82
-
83
- async list({ tenant, cursor, limit }: { tenant: Tenant; cursor?: string; limit: number }) {
84
- const offset = cursor === undefined ? 0 : Number(cursor);
85
- const rows = await this.#adapter.findMany({
86
- table: "dd_dashboard",
87
- // Published ones only, so a page is never short.
88
- where: [...(await this.#live(tenant)), { field: "current_version", op: "notNull" }],
89
- orderBy: [
90
- { field: "title", direction: "asc" },
91
- { field: "id", direction: "asc" },
92
- ],
93
- limit: limit + 1,
94
- offset,
95
- });
96
- // A list reads rows, never documents: the title and version are on the row.
97
- const items = rows.slice(0, limit).filter((row) => sameTenant(row["tenant"], tenant));
98
- return {
99
- items: items.map((row) => summaryOf(row)),
100
- ...(rows.length > limit ? { next: String(offset + limit) } : {}),
101
- };
102
- }
103
-
104
- async getVersion({ tenant, id, version }: { tenant: Tenant; id: string; version: number }) {
105
- const owner = await this.get({ tenant, ref: { id } });
106
- const row = owner === null ? null : await this.#version(owner.id, version);
107
- if (owner === null || row === null) return null;
108
- const stored: StoredVersion = {
109
- dashboardId: owner.id,
110
- version,
111
- doc: documentOf(row["spec"], `version ${version} of "${owner.id}"`),
112
- author: { id: String(row["author_id"]) },
113
- createdAt: String(row["created_at"]),
114
- };
115
- return stored;
116
- }
117
- }
118
-
119
- /** A stored dashboard row as viewers see it, its current version read: none until its first publish. */
120
- export async function storedHead(
121
- adapter: DashAdapter,
122
- row: Row | null,
123
- tenant: Tenant,
124
- ): Promise<DashboardHead | null> {
125
- if (row === null || !sameTenant(row["tenant"], tenant)) return null;
126
- if (row["current_version"] === null) return null;
127
- const version = await adapter.findOne({
128
- table: "dd_dashboard_version",
129
- where: [
130
- { field: "dashboard_id", value: String(row["id"]) },
131
- { field: "n", value: Number(row["current_version"]) },
132
- ],
133
- });
134
- return headOf(row, version?.["spec"]);
135
- }
136
-
137
- export function createReader(adapter: DashAdapter): DashboardReader {
138
- return new StoreReader(adapter);
139
- }
@@ -1,239 +0,0 @@
1
- import {
2
- type Author,
3
- type CommandBody,
4
- canonical,
5
- type DashAdapter,
6
- type Doc,
7
- type Draft,
8
- diffDocs,
9
- markShifted,
10
- type Preview,
11
- placementOf,
12
- type Row,
13
- reduceAll,
14
- replay,
15
- type Tenant,
16
- } from "@derive-to/dashboards-core";
17
- import { sha256 } from "../cache";
18
- import { HttpError } from "../http";
19
- import { type Kept, keptColumns, keptCommands, rewriteCommands } from "./commands";
20
- import { overlapsOf, refusalsOf } from "./conflicts";
21
- import {
22
- advanceDraft,
23
- authorIn,
24
- baseDoc,
25
- dashboardRow,
26
- draftDoc,
27
- draftOf,
28
- draftRow,
29
- expectRevision,
30
- headDoc,
31
- inTransaction,
32
- namedOf,
33
- now,
34
- openDraft,
35
- originDoc,
36
- } from "./rows";
37
-
38
- type ByAuthor = { tenant: Tenant; id: string; author: Author };
39
-
40
- /** Who published a version and when. */
41
- async function publisher(tx: DashAdapter, dashboardId: unknown, n: number) {
42
- const v = await tx.findOne({
43
- table: "dd_dashboard_version",
44
- where: [
45
- { field: "dashboard_id", value: dashboardId },
46
- { field: "n", value: n },
47
- ],
48
- });
49
- const author = v === null ? undefined : authorIn(v, "author");
50
- return v === null
51
- ? null
52
- : { ...(author === undefined ? {} : { author }), at: String(v["created_at"]) };
53
- }
54
-
55
- /**
56
- * Once review moved a draft onto a newer version, the person's changes to widgets that version
57
- * also changed, against where the draft first started: they stay in view until publish. A draft
58
- * never moved reads nothing for this.
59
- */
60
- async function stillBoth(tx: DashAdapter, row: Row, head: Doc | undefined) {
61
- const origin = originDoc(row);
62
- if (origin === undefined || head === undefined) return [];
63
- const kept = await keptCommands(tx, row["id"]);
64
- return overlapsOf(kept, replay(head, kept), diffDocs(origin, head), head);
65
- }
66
-
67
- /** What publishing this draft row now would do, on the dashboard's current version. */
68
- async function previewOf(tx: DashAdapter, row: Row, dashboard: Row): Promise<Preview> {
69
- const doc = draftDoc(row);
70
- const base = baseDoc(row);
71
- const baseVersion = Number(row["base_version"]);
72
- const revision = Number(row["revision"]);
73
- const head = await headDoc(tx, dashboard);
74
- // The widgets the draft names are on its row: with nothing published since, no command is read.
75
- const named = namedOf(row);
76
- if (head.version === baseVersion || head.doc === undefined) {
77
- const changes = markShifted(diffDocs(head.doc ?? base, doc), named);
78
- const both = await stillBoth(tx, row, head.doc);
79
- const by = both.length === 0 ? null : await publisher(tx, dashboard["id"], head.version);
80
- return {
81
- head: head.version,
82
- revision,
83
- base: baseVersion,
84
- doc,
85
- changes,
86
- since: [],
87
- refused: [],
88
- overlaps: both,
89
- ...(by === null ? {} : { publishedBy: by }),
90
- };
91
- }
92
- const kept = await keptCommands(tx, row["id"]);
93
- const merged = replay(head.doc, kept);
94
- const since = diffDocs(base, head.doc);
95
- const by = await publisher(tx, dashboard["id"], head.version);
96
- return {
97
- head: head.version,
98
- revision,
99
- base: baseVersion,
100
- doc: merged.doc,
101
- changes: markShifted(diffDocs(head.doc, merged.doc), named),
102
- since,
103
- // Named as it was when the draft started: the name both sides last agreed on.
104
- refused: refusalsOf(kept, merged, [base, doc]),
105
- overlaps: overlapsOf(kept, merged, since, head.doc),
106
- ...(by === null ? {} : { publishedBy: by }),
107
- };
108
- }
109
-
110
- /** Refuses to drop a change the draft does not have, so a typo is not silently ignored. */
111
- function refuseUnknownDrops(drop: readonly string[], kept: readonly Kept[]): void {
112
- const unknown = drop.filter((id) => !kept.some((k) => k.commandId === id));
113
- if (unknown.length > 0)
114
- throw new HttpError(
115
- 400,
116
- "EDIT_REFUSED",
117
- `The draft has no change ${unknown.map((id) => `"${id}"`).join(", ")} to drop. The preview names each change by its commandId.`,
118
- );
119
- }
120
-
121
- /** The draft's document, or what publishing it would make now; null without an open draft. */
122
- export function draftDocument(
123
- db: DashAdapter,
124
- a: ByAuthor & { as: "draft" | "merged" },
125
- ): Promise<Doc | null> {
126
- return inTransaction(db, async (tx) => {
127
- const dashboard = await dashboardRow(tx, a.tenant, { id: a.id });
128
- const row = dashboard === null ? null : await draftRow(tx, a.id, a.author);
129
- if (dashboard === null || row === null || row["state"] !== "open") return null;
130
- const doc = draftDoc(row);
131
- if (a.as === "draft") return doc;
132
- const head = await headDoc(tx, dashboard);
133
- if (head.version === Number(row["base_version"]) || head.doc === undefined) return doc;
134
- return replay(head.doc, await keptCommands(tx, row["id"])).doc;
135
- });
136
- }
137
-
138
- /** What publishing the author's draft would do now, without writing anything. */
139
- export function preview(db: DashAdapter, a: ByAuthor): Promise<Preview> {
140
- return inTransaction(db, async (tx) => {
141
- const { row, dashboard } = await openDraft(tx, a);
142
- return previewOf(tx, row, dashboard);
143
- });
144
- }
145
-
146
- /** Refuses a review made on another version than the current one: the person reviews again. */
147
- export function expectHead(head: number, expected: number): void {
148
- if (head === expected) return;
149
- const why =
150
- expected > head
151
- ? `There is no version ${String(expected)}; the current version is ${String(head)}.`
152
- : `Someone published version ${String(head)} while you were reviewing.`;
153
- throw new HttpError(409, "HEAD_MOVED", `${why} Review the result on top of it.`);
154
- }
155
-
156
- /**
157
- * A widget put back as the person's draft has it, before the draft's own edits replay: those
158
- * edits are already in it, so replaying them changes nothing more. Only the draft's own
159
- * widgets come back, so the endpoint's check of them is a check of everything restored.
160
- */
161
- async function putBack(row: Row, head: Doc, widgetId: string, revision: number): Promise<Kept> {
162
- const source = draftDoc(row);
163
- const widget = source.widgets[widgetId];
164
- const place = placementOf(source, widgetId);
165
- const firstTab = head.tabs[0];
166
- if (widget === undefined || firstTab === undefined)
167
- throw new HttpError(
168
- 400,
169
- "EDIT_REFUSED",
170
- `Your draft has no widget "${widgetId}" to put back; drop the change instead.`,
171
- );
172
- const tab = head.tabs.some((t) => t.id === place?.tab)
173
- ? (place?.tab ?? firstTab.id)
174
- : firstTab.id;
175
- const at = place?.at ?? { x: 0, y: 0, w: 6, h: 4 };
176
- const body: CommandBody = { type: "widget.add", tab, id: widgetId, widget, at };
177
- const commandId = crypto.randomUUID();
178
- return { commandId, body, revision, digest: await sha256(canonical(body)) };
179
- }
180
-
181
- /**
182
- * Moves the author's draft onto the current version `head`, deciding each change that no
183
- * longer applies: the `drop` commands are left out, the `restore` widgets are put back as the
184
- * draft has them, and every other command replays on `head`. All must apply, or nothing changes. After
185
- * it, the draft starts from `head`, so its review and its publish show the same thing.
186
- */
187
- export function rebase(
188
- db: DashAdapter,
189
- a: ByAuthor & {
190
- expectedRevision: number;
191
- head: number;
192
- drop: readonly string[];
193
- restore: readonly string[];
194
- },
195
- ): Promise<Draft> {
196
- return inTransaction(db, async (tx) => {
197
- const { row, dashboard } = await openDraft(tx, a);
198
- expectRevision(row, a.expectedRevision);
199
- const head = await headDoc(tx, dashboard);
200
- expectHead(head.version, a.head);
201
- const kept = await keptCommands(tx, row["id"]);
202
- const onto = head.doc;
203
- if (onto === undefined)
204
- throw new HttpError(
205
- 400,
206
- "EDIT_REFUSED",
207
- "Nothing has been published yet, so there is nothing to move the draft onto.",
208
- );
209
- refuseUnknownDrops(a.drop, kept);
210
- // Put-backs join the draft's first edit, so undoing every edit takes them away too.
211
- const first = Math.min(Number(row["revision"]) + 1, ...kept.map((k) => k.revision));
212
- const restored = await Promise.all(a.restore.map((w) => putBack(row, onto, w, first)));
213
- const next = [...restored, ...kept.filter((k) => !a.drop.includes(k.commandId))];
214
- const outcome = reduceAll(
215
- onto,
216
- next.map((k) => k.body),
217
- );
218
- if (!outcome.ok)
219
- throw new HttpError(409, "REPLAY_REFUSED", outcome.issue.message, {
220
- issues: [outcome.issue],
221
- });
222
- const set = {
223
- spec: outcome.doc,
224
- base_spec: onto,
225
- base_version: head.version,
226
- // Where the draft first started, kept through every move until it is published.
227
- origin_version: row["origin_version"] ?? Number(row["base_version"]),
228
- origin_spec: row["origin_spec"] ?? row["base_spec"],
229
- revision: Number(row["revision"]) + 1,
230
- ...keptColumns(next),
231
- // The commands are written again from the first sequence number.
232
- next_seq: next.length,
233
- updated_at: now(),
234
- };
235
- await advanceDraft(tx, row, set);
236
- await rewriteCommands(tx, row["id"], next);
237
- return draftOf({ ...row, ...set });
238
- });
239
- }