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,59 +0,0 @@
1
- import type {
2
- CustomSource,
3
- GroupedRead,
4
- HostRows,
5
- ReadContext,
6
- RecordsRead,
7
- } from "@derive-to/dashboards-core";
8
- import { boundedCall } from "./bounded";
9
-
10
- type Rows = Promise<HostRows>;
11
-
12
- /** What a host writes for its own data layer: the grouped read and, optionally, records. */
13
- export type CustomSourceOptions = {
14
- /**
15
- * One grouped read: the rows of `request.source` passing `request.where`, grouped by the
16
- * bucket and dimensions, each measure aggregated. Every access rule is already in `where`:
17
- * apply it exactly, or the viewer reads rows they may not.
18
- */
19
- answer: (request: GroupedRead, context: ReadContext) => Rows;
20
- /** The rows behind a tile. Without it, records of these sources are refused with a reason. */
21
- records?: (request: RecordsRead, context: ReadContext) => Rows;
22
- };
23
-
24
- /**
25
- * A connection that is the host's own data layer (its SQL, ClickHouse, an API, a cache): the
26
- * library still does dashboards, editing, caching, the UI, agents, totals, ranking, Other,
27
- * comparisons and formulas, and asks it only grouped reads and records. The host owns safety
28
- * on this path: run `derive-dashboards check --conformance` against it.
29
- */
30
- export function customSource(options: CustomSourceOptions): CustomSource {
31
- const { answer, records } = options;
32
- return { kind: "custom", answer, ...(records === undefined ? {} : { records }) };
33
- }
34
-
35
- /** Whether a value is a custom source. */
36
- export function isCustomSource(handle: unknown): handle is CustomSource {
37
- return (
38
- typeof handle === "object" &&
39
- handle !== null &&
40
- "kind" in handle &&
41
- handle.kind === "custom" &&
42
- "answer" in handle &&
43
- typeof handle.answer === "function"
44
- );
45
- }
46
-
47
- /** A custom source whose every call starts inside the deadline and ends at it or on abort. */
48
- export function boundedSource(source: CustomSource): CustomSource {
49
- const { records } = source;
50
- return {
51
- kind: "custom",
52
- answer: (request, context) => boundedCall(context, () => source.answer(request, context)),
53
- ...(records === undefined
54
- ? {}
55
- : {
56
- records: (request, context) => boundedCall(context, () => records(request, context)),
57
- }),
58
- };
59
- }
@@ -1,36 +0,0 @@
1
- import type { Row } from "@derive-to/dashboards-core";
2
- import { HttpError } from "./http";
3
-
4
- /**
5
- * What a client's dashboard id means: the one owner of that rule. A built-in is named by
6
- * `default:<key>`, and so is its tenant's stored copy once someone customized it; any other
7
- * id is a stored dashboard's own id. A dashboard answers to exactly one id, so the host's
8
- * `can`, which is asked about the id a client sent, is always asked about the dashboard it gets.
9
- */
10
-
11
- const BUILT_IN = "default:";
12
-
13
- export type DashboardRef = { key: string } | { id: string };
14
-
15
- /** A client's id as a reference to a built-in (by key) or a stored dashboard (by id). */
16
- export function parseDashboardId(id: string): DashboardRef {
17
- return id.startsWith(BUILT_IN) ? { key: id.slice(BUILT_IN.length) } : { id };
18
- }
19
-
20
- /** The id clients use for a built-in, customized or not. */
21
- export const builtInId = (key: string) => `${BUILT_IN}${key}`;
22
-
23
- /**
24
- * Refuses a stored row reached by an id it does not answer to: a built-in's copy by its
25
- * internal id. It is not found, as for any id that names nothing.
26
- */
27
- export function expectServedAs(row: Row | null, ref: DashboardRef, id: string): void {
28
- if (row === null || "key" in ref) return;
29
- if (row["default_of"] !== null && row["default_of"] !== undefined)
30
- throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
31
- }
32
-
33
- /** An answer that names a dashboard, naming it by the id the client used. */
34
- export function servedAs<T extends { dashboardId: string }>(answer: T, id: string): T {
35
- return { ...answer, dashboardId: id };
36
- }
package/src/dashboards.ts DELETED
@@ -1,132 +0,0 @@
1
- import {
2
- type DashAdapter,
3
- type DashboardSummary,
4
- type DashOptions,
5
- type Doc,
6
- type Principal,
7
- principalTenant,
8
- } from "@derive-to/dashboards-core";
9
- import { builtInId, parseDashboardId } from "./dashboard-id";
10
- import { HttpError } from "./http";
11
- import { requestDashboardRow } from "./request-rows";
12
- import { createReader, storedHead } from "./store/reader";
13
-
14
- /** A dashboard as the runtime serves it: its id, the version pinned for this request, its document. */
15
- export type Loaded = { id: string; version: number; document: Doc };
16
-
17
- const PAGE = 500;
18
- const MOST = 5000;
19
-
20
- /**
21
- * A tenant's published dashboards, every page of them up to a bound, so access is checked on
22
- * all of them rather than on the first page only. The list reads rows, never documents.
23
- */
24
- async function allStored(adapter: DashAdapter, tenant: Record<string, string>) {
25
- const reader = createReader(adapter);
26
- const items: DashboardSummary[] = [];
27
- let cursor: string | undefined;
28
- do {
29
- // oxlint-disable-next-line no-await-in-loop -- each page starts where the last ended
30
- const page = await reader.list({
31
- tenant,
32
- limit: PAGE,
33
- ...(cursor === undefined ? {} : { cursor }),
34
- });
35
- items.push(...page.items);
36
- cursor = page.next;
37
- } while (cursor !== undefined && items.length < MOST);
38
- return items;
39
- }
40
-
41
- /** The store adapter for this request, when the host gave one. */
42
- export function adapterFor(options: DashOptions, request: Request): DashAdapter | undefined {
43
- const { database } = options;
44
- return typeof database === "function" ? database(request) : database;
45
- }
46
-
47
- /**
48
- * Every dashboard a tenant sees: its stored ones at their current versions, and each code-defined
49
- * one it has not customized, at version 0.
50
- */
51
- /** A dashboard in the list, with who made it when it is stored, for `can` to read. */
52
- type Listed = {
53
- id: string;
54
- title: string;
55
- version: number;
56
- owner?: { id: string; kind: "user" | "agent" };
57
- };
58
-
59
- export async function listDashboards(
60
- options: DashOptions,
61
- request: Request,
62
- principal: Principal,
63
- ): Promise<Listed[]> {
64
- const adapter = adapterFor(options, request);
65
- const stored = adapter === undefined ? [] : await allStored(adapter, principalTenant(principal));
66
- const customized = new Set(
67
- stored.flatMap((d) => (d.defaultOf === undefined ? [] : [d.defaultOf])),
68
- );
69
- const defaults = (options.dashboards ?? [])
70
- .filter((d) => !customized.has(d.key))
71
- .map((d) => ({ id: builtInId(d.key), title: d.document.meta.title, version: 0 }));
72
- return [
73
- // A customized built-in keeps the id clients know it by.
74
- ...stored.map((d) => ({
75
- id: d.defaultOf === undefined ? d.id : builtInId(d.defaultOf),
76
- title: d.title,
77
- version: d.currentVersion,
78
- // A built-in's copy belongs to nobody, whoever first pressed Edit.
79
- ...(d.defaultOf === undefined ? { owner: { id: d.ownerId, kind: d.ownerKind } } : {}),
80
- })),
81
- ...defaults,
82
- ];
83
- }
84
-
85
- async function find(
86
- options: DashOptions,
87
- request: Request,
88
- principal: Principal,
89
- id: string,
90
- ): Promise<Loaded> {
91
- const adapter = adapterFor(options, request);
92
- const ref = parseDashboardId(id);
93
- // The row the request's access check already read, then its current version.
94
- const tenant = principalTenant(principal);
95
- const stored =
96
- adapter === undefined
97
- ? null
98
- : await storedHead(adapter, await requestDashboardRow(request, adapter, tenant, ref), tenant);
99
- // A built-in's copy answers only to its built-in id, never to its stored one.
100
- if (stored !== null && ("key" in ref || stored.defaultOf === undefined))
101
- return { id, version: stored.currentVersion, document: stored.doc };
102
- const key = "key" in ref ? ref.key : undefined;
103
- const code =
104
- key === undefined ? undefined : (options.dashboards ?? []).find((d) => d.key === key);
105
- if (code === undefined)
106
- throw new HttpError(
107
- 404,
108
- "NOT_FOUND",
109
- `There is no dashboard "${id}". A built-in one's id is "default:" and its key; /dashboards/list gives every id.`,
110
- );
111
- return { id, version: 0, document: code.document };
112
- }
113
-
114
- /**
115
- * A dashboard by id, pinned at its current version for the rest of the request: the tenant's
116
- * stored row, or for `default:<key>` the tenant's customized copy if it has one, else the
117
- * code-defined document at version 0. A `version` the client names is an expectation: a
118
- * different current version is a 409, so a client never runs tiles against a dashboard that
119
- * changed under it.
120
- */
121
- export async function loadDashboard(
122
- options: DashOptions,
123
- request: Request,
124
- principal: Principal,
125
- ref: { id: string; version?: number },
126
- ): Promise<Loaded> {
127
- const loaded = await find(options, request, principal, ref.id);
128
- const expected = ref.version;
129
- if (expected !== undefined && expected !== loaded.version)
130
- throw new HttpError(409, "VERSION_CONFLICT", "The dashboard changed. Reload it.");
131
- return loaded;
132
- }
package/src/editing.ts DELETED
@@ -1,148 +0,0 @@
1
- import {
2
- type Author,
3
- type DashboardStore,
4
- type Doc,
5
- type Draft,
6
- type DraftTarget,
7
- type Principal,
8
- principalTenant,
9
- type Viewer,
10
- } from "@derive-to/dashboards-core";
11
- import { expectServedAs, parseDashboardId } from "./dashboard-id";
12
- import { adapterFor } from "./dashboards";
13
- import type { Runtime } from "./execute";
14
- import { HttpError } from "./http";
15
- import { dashboardResource } from "./owner";
16
- import { requestDashboardRow } from "./request-rows";
17
- import { draftClosed, draftRow } from "./store/rows";
18
- import { createStore } from "./store/writer";
19
-
20
- /** What the draft endpoints share: the store, how a client's id maps to a row, and who edits. */
21
-
22
- type User = Principal & { viewer: Viewer };
23
-
24
- /** The store for this request; editing without one is refused with what to configure. */
25
- export function storeFor(rt: Runtime, request: Request): DashboardStore {
26
- const adapter = adapterFor(rt.options, request);
27
- if (adapter === undefined)
28
- throw new HttpError(
29
- 400,
30
- "STORE_REQUIRED",
31
- "Editing dashboards needs a store: pass `database` to deriveDashboards.",
32
- );
33
- return createStore(adapter);
34
- }
35
-
36
- /**
37
- * What a draft of `id` starts from: a built-in by its key, or a stored dashboard by the one id
38
- * it answers to (a built-in's copy is reached only by its built-in id).
39
- */
40
- export async function targetOf(
41
- rt: Runtime,
42
- request: Request,
43
- user: User,
44
- id: string,
45
- ): Promise<DraftTarget> {
46
- const ref = parseDashboardId(id);
47
- if ("id" in ref) {
48
- await storedId(rt, request, user, id);
49
- return ref;
50
- }
51
- const { key } = ref;
52
- const code = (rt.options.dashboards ?? []).find((d) => d.key === key);
53
- if (code === undefined) throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${id}".`);
54
- return { builtIn: key, doc: code.document };
55
- }
56
-
57
- /** Who edits, and in which tenant, from the request's principal: a person, or an agent and whom it acts for. */
58
- export function editorOf(user: User): { tenant: Record<string, string>; author: Author } {
59
- const { agent } = user.viewer;
60
- const author: Author =
61
- agent === undefined
62
- ? { id: user.viewer.id, kind: "user" }
63
- : { id: user.viewer.id, kind: "agent", for: agent.for };
64
- return { tenant: principalTenant(user), author };
65
- }
66
-
67
- /** The stored row id behind the id a client uses: a built-in's copy is found by its key. */
68
- export async function rowId(rt: Runtime, request: Request, user: User, id: string) {
69
- const stored = await storedId(rt, request, user, id);
70
- if (stored === null) throw draftClosed();
71
- return stored;
72
- }
73
-
74
- /**
75
- * The stored row id behind a client's id, or null for a built-in nobody has customized. A
76
- * built-in's copy named by its stored id is refused: it answers only to its built-in id.
77
- */
78
- export async function storedId(rt: Runtime, request: Request, user: User, id: string) {
79
- const ref = parseDashboardId(id);
80
- const adapter = adapterFor(rt.options, request);
81
- const row =
82
- adapter === undefined
83
- ? null
84
- : await requestDashboardRow(request, adapter, principalTenant(user), ref);
85
- expectServedAs(row, ref, id);
86
- if ("id" in ref) return id;
87
- return row === null ? null : String(row["id"]);
88
- }
89
-
90
- /**
91
- * The document tiles run against for the viewer's open draft: the draft, or what publishing it
92
- * would make now. Refused without `edit`.
93
- */
94
- export async function draftDoc(
95
- rt: Runtime,
96
- request: Request,
97
- user: User,
98
- a: { id: string; as: "draft" | "merged" },
99
- ): Promise<Doc> {
100
- const resource = await dashboardResource(rt.options, request, user, a.id);
101
- const can = await rt.options.access.can(user.viewer, "edit", resource);
102
- if (!can) throw new HttpError(403, "FORBIDDEN", "Running a draft needs edit on its dashboard.");
103
- const doc = await storeFor(rt, request).draftDocument({
104
- ...editorOf(user),
105
- id: await rowId(rt, request, user, a.id),
106
- as: a.as,
107
- });
108
- if (doc === null) throw draftClosed();
109
- return doc;
110
- }
111
-
112
- /**
113
- * A dashboard the caller made and has not published: served to them alone, from their draft.
114
- * Null when there is none, so the caller's refusal stands.
115
- */
116
- export function unpublishedFor(
117
- rt: Runtime,
118
- request: Request,
119
- user: User,
120
- id: string,
121
- ): Promise<Draft | null> {
122
- const adapter = adapterFor(rt.options, request);
123
- if (adapter === undefined || "key" in parseDashboardId(id)) return Promise.resolve(null);
124
- return createStore(adapter).getDraft({ ...editorOf(user), id });
125
- }
126
-
127
- /** The caller's own new dashboards, for the list: only they see them until they publish. */
128
- export async function ownUnpublished(rt: Runtime, request: Request, user: User) {
129
- const adapter = adapterFor(rt.options, request);
130
- if (adapter === undefined) return [];
131
- const drafts = await createStore(adapter).unpublished(editorOf(user));
132
- return drafts.map((d) => ({
133
- id: d.dashboardId,
134
- title: d.title,
135
- version: 0,
136
- unpublished: true as const,
137
- }));
138
- }
139
-
140
- /** Whether the caller has an open draft of this dashboard, so a client offers to resume it. */
141
- export async function hasOpenDraft(rt: Runtime, request: Request, user: User, id: string) {
142
- const adapter = adapterFor(rt.options, request);
143
- const stored = adapter === undefined ? null : await storedId(rt, request, user, id);
144
- if (adapter === undefined || stored === null) return false;
145
- // `storedId` found the row in the viewer's tenant (read once per request): only the draft is left.
146
- const row = await draftRow(adapter, stored, editorOf(user).author);
147
- return row !== null && row["state"] === "open";
148
- }
@@ -1,41 +0,0 @@
1
- import type { EndpointAccess, Resource } from "@derive-to/dashboards-core";
2
- import { HttpError } from "../http";
3
-
4
- /**
5
- * The id a body names in a field, before its schema checks it. A body without one is refused
6
- * here as malformed, so the host's `can` is never asked about an empty id.
7
- */
8
- function idIn(body: unknown, field: string): string {
9
- const value: unknown =
10
- typeof body === "object" && body !== null
11
- ? Object.getOwnPropertyDescriptor(body, field)?.value
12
- : undefined;
13
- if (typeof value === "string" && value !== "") return value;
14
- throw new HttpError(400, "BODY_INVALID", `${field}: a non-empty id is required.`);
15
- }
16
-
17
- /** The dashboard a body names. */
18
- const dashboardIn = (body: unknown): Resource => ({
19
- kind: "dashboard",
20
- id: idIn(body, "dashboardId"),
21
- });
22
-
23
- /** `view` on the dashboard the body names. The body is checked by the endpoint's own schema after. */
24
- export const viewDashboard: EndpointAccess = { kind: "can", verb: "view", resource: dashboardIn };
25
-
26
- /** `edit` on the dashboard the body names: drafts, and tiles run against one. */
27
- export const editDashboard: EndpointAccess = { kind: "can", verb: "edit", resource: dashboardIn };
28
-
29
- /** `edit` on the tenant: making a new dashboard. */
30
- export const editTenant: EndpointAccess = {
31
- kind: "can",
32
- verb: "edit",
33
- resource: () => ({ kind: "tenant" }),
34
- };
35
-
36
- /** `view` on the member the body names. */
37
- export const viewMember: EndpointAccess = {
38
- kind: "can",
39
- verb: "view",
40
- resource: (body: unknown): Resource => ({ kind: "member", id: idIn(body, "member") }),
41
- };
@@ -1,92 +0,0 @@
1
- import { createDashEndpoint, documentLock, type ValuesResponse } from "@derive-to/dashboards-core";
2
- import { bodies, parseWith } from "../body";
3
- import { executeRecords, type Runtime } from "../execute";
4
- import { HttpError } from "../http";
5
- import { nowOf, userOf } from "../request";
6
- import { docFor, type TileCtx } from "../tile-doc";
7
- import { type Ctx, runTile, streamTiles, tileExecution } from "../tiles";
8
- import { viewDashboard } from "./access";
9
- import { memberValues } from "./values";
10
-
11
- /** What every tile of one request shares: the dashboard pinned at its version (or the viewer's draft), the picks, one `now`. */
12
- async function tileContext(rt: Runtime, a: TileCtx): Promise<Ctx> {
13
- const principal = userOf(a.principal);
14
- const loaded = await docFor(rt, a, principal);
15
- return {
16
- rt,
17
- request: a.request,
18
- principal,
19
- dashboardId: loaded.id,
20
- doc: loaded.document,
21
- picks: a.picks ?? {},
22
- now: nowOf(rt.options),
23
- };
24
- }
25
-
26
- const RECORDS_LIMIT = 50;
27
-
28
- /** A dashboard filter control's values: its own list, or the values of its member the viewer can see under the lock. */
29
- function controlValues(
30
- rt: Runtime,
31
- ctx: Ctx,
32
- a: { controlId: string; search?: string; limit?: number },
33
- ): Promise<ValuesResponse> {
34
- const control = ctx.doc.controls.find((c) => c.id === a.controlId);
35
- if (control?.kind !== "filter")
36
- throw new HttpError(404, "NOT_FOUND", `There is no filter control "${a.controlId}".`);
37
- const own = control.values;
38
- if (typeof own === "object") return Promise.resolve({ values: [...own], more: false });
39
- const x = {
40
- rt,
41
- request: ctx.request,
42
- principal: ctx.principal,
43
- now: ctx.now,
44
- lock: documentLock(ctx.doc),
45
- coverage: { applied: [], dropped: [] },
46
- dashboardId: ctx.dashboardId,
47
- };
48
- return memberValues(x, { ...a, member: control.member });
49
- }
50
-
51
- /** Running a dashboard's tiles, records and control values by reference. */
52
- function runEndpoints(rt: Runtime) {
53
- return {
54
- tile: createDashEndpoint("/dashboards/tile", {
55
- method: "POST",
56
- access: viewDashboard,
57
- parse: (input) => parseWith(bodies.tile, input),
58
- handler: async ({ body, principal, request }) =>
59
- runTile(await tileContext(rt, { ...body, principal, request }), body),
60
- }),
61
- tiles: createDashEndpoint("/dashboards/tiles", {
62
- method: "POST",
63
- access: viewDashboard,
64
- respond: "ndjson",
65
- parse: (input) => parseWith(bodies.tiles, input),
66
- handler: async ({ body, principal, request }) =>
67
- streamTiles(await tileContext(rt, { ...body, principal, request }), body.tiles),
68
- }),
69
- records: createDashEndpoint("/dashboards/records", {
70
- method: "POST",
71
- access: viewDashboard,
72
- parse: (input) => parseWith(bodies.records, input),
73
- handler: async ({ body, principal, request }) => {
74
- const x = tileExecution(await tileContext(rt, { ...body, principal, request }), body);
75
- const after = body.after === undefined ? {} : { after: body.after };
76
- return executeRecords(x, { limit: body.limit ?? RECORDS_LIMIT, ...after });
77
- },
78
- }),
79
- controlValues: createDashEndpoint("/dashboards/control-values", {
80
- method: "POST",
81
- access: viewDashboard,
82
- parse: (input) => parseWith(bodies.controlValues, input),
83
- handler: async ({ body, principal, request }) =>
84
- controlValues(rt, await tileContext(rt, { ...body, principal, request }), body),
85
- }),
86
- };
87
- }
88
-
89
- /** Dashboards by reference: listed, read, and their tiles, records and control values run. */
90
- export function dashboardEndpoints(rt: Runtime) {
91
- return runEndpoints(rt);
92
- }
@@ -1,138 +0,0 @@
1
- import { createDashEndpoint, type Doc, emptyDoc } from "@derive-to/dashboards-core";
2
- import { bodies, parseWith } from "../body";
3
- import { checkCommands, checkDocument, versionDoc } from "../checks";
4
- import { servedAs } from "../dashboard-id";
5
- import { editorOf, rowId, storeFor, targetOf } from "../editing";
6
- import type { Runtime } from "../execute";
7
- import { HttpError } from "../http";
8
- import { dashboardResource } from "../owner";
9
- import { userOf } from "../request";
10
- import { editDashboard, editTenant } from "./access";
11
-
12
- /**
13
- * A new dashboard's first document: empty, or a copy of a published version of another one,
14
- * read by the one rule for earlier versions (`versionDoc`) and with every query one the caller
15
- * may read, titled as they asked.
16
- */
17
- async function firstDoc(
18
- rt: Runtime,
19
- request: Request,
20
- user: ReturnType<typeof userOf>,
21
- body: { title: string; from?: { dashboardId: string; version: number } },
22
- ): Promise<Doc> {
23
- if (body.from === undefined) return emptyDoc(body.title);
24
- const old = await versionDoc(rt, request, user, body.from);
25
- await checkDocument(
26
- rt,
27
- user.viewer,
28
- { kind: "tenant" },
29
- { restored: old.doc, current: undefined },
30
- );
31
- return { ...old.doc, meta: { ...old.doc.meta, title: body.title } };
32
- }
33
-
34
- /** Opening a draft of a dashboard, or of a new one. */
35
- function startEndpoints(rt: Runtime) {
36
- return {
37
- startDraft: createDashEndpoint("/dashboards/draft/start", {
38
- method: "POST",
39
- access: editDashboard,
40
- parse: (input) => parseWith(bodies.dashboard, input),
41
- handler: async ({ body, principal, request }) => {
42
- const user = userOf(principal);
43
- const draft = await storeFor(rt, request).startDraft({
44
- ...editorOf(user),
45
- target: await targetOf(rt, request, user, body.dashboardId),
46
- });
47
- return servedAs(draft, body.dashboardId);
48
- },
49
- }),
50
- newDashboard: createDashEndpoint("/dashboards/new", {
51
- method: "POST",
52
- access: editTenant,
53
- parse: (input) => parseWith(bodies.newDashboard, input),
54
- handler: async ({ body, principal, request }) => {
55
- const user = userOf(principal);
56
- const doc = await firstDoc(rt, request, user, body);
57
- return storeFor(rt, request).startDraft({
58
- ...editorOf(user),
59
- target: {
60
- create: doc,
61
- ...(body.requestId === undefined ? {} : { requestId: body.requestId }),
62
- },
63
- });
64
- },
65
- }),
66
- };
67
- }
68
-
69
- /** Applying a batch of commands to the editor's open draft. */
70
- function applyEndpoint(rt: Runtime) {
71
- return createDashEndpoint("/dashboards/draft/apply", {
72
- method: "POST",
73
- access: editDashboard,
74
- parse: (input) => parseWith(bodies.applyDraft, input),
75
- handler: async ({ body, principal, request }) => {
76
- const user = userOf(principal);
77
- const ids = body.commands.map((c) => c.commandId);
78
- const repeated = ids.find((cid, i) => ids.indexOf(cid) !== i);
79
- if (repeated !== undefined)
80
- throw new HttpError(
81
- 400,
82
- "DUPLICATE_COMMAND_ID",
83
- `The command id "${repeated}" is used twice in one batch; give each command its own.`,
84
- );
85
- const resource = await dashboardResource(rt.options, request, user, body.dashboardId);
86
- await checkCommands(rt, user.viewer, resource, body.commands);
87
- const applied = await storeFor(rt, request).applyDraft({
88
- ...editorOf(user),
89
- id: await rowId(rt, request, user, body.dashboardId),
90
- expectedRevision: body.expectedRevision,
91
- commands: body.commands,
92
- });
93
- return { ...applied, draft: servedAs(applied.draft, body.dashboardId) };
94
- },
95
- });
96
- }
97
-
98
- /** Publishing the draft as the next version, or dropping it. */
99
- function closeEndpoints(rt: Runtime) {
100
- return {
101
- publishDraft: createDashEndpoint("/dashboards/draft/publish", {
102
- method: "POST",
103
- access: editDashboard,
104
- parse: (input) => parseWith(bodies.publishDraft, input),
105
- handler: async ({ body, principal, request }) => {
106
- const user = userOf(principal);
107
- const editor = editorOf(user);
108
- const published = await storeFor(rt, request).publish({
109
- ...editor,
110
- id: await rowId(rt, request, user, body.dashboardId),
111
- expectedRevision: body.expectedRevision,
112
- requestId: body.requestId,
113
- head: body.head,
114
- });
115
- return servedAs(published, body.dashboardId);
116
- },
117
- }),
118
- discardDraft: createDashEndpoint("/dashboards/draft/discard", {
119
- method: "POST",
120
- access: editDashboard,
121
- parse: (input) => parseWith(bodies.discardDraft, input),
122
- handler: async ({ body, principal, request }) => {
123
- const user = userOf(principal);
124
- const { removed } = await storeFor(rt, request).discardDraft({
125
- ...editorOf(user),
126
- id: await rowId(rt, request, user, body.dashboardId),
127
- expectedRevision: body.expectedRevision,
128
- });
129
- return { discarded: true as const, removed };
130
- },
131
- }),
132
- };
133
- }
134
-
135
- /** Starting, editing, publishing and discarding a draft; and making a new dashboard. */
136
- export function draftEndpoints(rt: Runtime) {
137
- return { ...startEndpoints(rt), applyDraft: applyEndpoint(rt), ...closeEndpoints(rt) };
138
- }
@@ -1,21 +0,0 @@
1
- import type { Runtime } from "../execute";
2
- import { dashboardEndpoints } from "./dashboards";
3
- import { draftEndpoints } from "./drafts";
4
- import { metaEndpoints } from "./meta";
5
- import { queryEndpoints } from "./query";
6
- import { readEndpoints } from "./read";
7
- import { reviewEndpoints } from "./review";
8
- import { versionEndpoints } from "./versions";
9
-
10
- /** The endpoints every install has, by name. Each declares who may call it. */
11
- export function coreEndpoints(rt: Runtime) {
12
- const meta = metaEndpoints(rt);
13
- const dashboards = { ...readEndpoints(rt), ...dashboardEndpoints(rt) };
14
- const drafts = { ...draftEndpoints(rt), ...reviewEndpoints(rt) };
15
- const query = queryEndpoints(rt);
16
- const history = versionEndpoints(rt);
17
- const names = [meta, dashboards, drafts, query, history].flatMap((g) => Object.keys(g));
18
- const repeated = names.find((name, i) => names.indexOf(name) !== i);
19
- if (repeated !== undefined) throw new Error(`Two core endpoints are named "${repeated}".`);
20
- return { ...meta, ...dashboards, ...drafts, ...query, ...history };
21
- }