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,239 +0,0 @@
1
- import {
2
- type DashAdapter,
3
- emptyDoc,
4
- type Row,
5
- TABLES,
6
- type TableName,
7
- } from "@derive-to/dashboards-core";
8
- import { afterAll, beforeAll, describe, expect, it } from "vitest";
9
- import { createTables } from "../store/create-tables";
10
- import { createReader } from "../store/reader";
11
- import { tenantHash } from "../store/tenant";
12
-
13
- /** A fresh, empty database behind the adapter under test. */
14
- export type OpenAdapter = () => Promise<{ adapter: DashAdapter; close: () => Promise<void> }>;
15
-
16
- const ACME = { org: "acme" };
17
- const GLOBEX = { org: "globex" };
18
- const NOW = "2026-11-12T10:00:00.000Z";
19
-
20
- async function seedDashboard(
21
- adapter: DashAdapter,
22
- id: string,
23
- tenant: Record<string, string>,
24
- extra: Record<string, unknown> = {},
25
- ) {
26
- const hash = await tenantHash(tenant);
27
- await adapter.create({
28
- table: "dd_dashboard",
29
- data: {
30
- id,
31
- tenant,
32
- tenant_hash: hash,
33
- key: null,
34
- title: `Title ${id}`,
35
- description: null,
36
- owner_id: "u1",
37
- owner_kind: "user",
38
- current_version: 1,
39
- default_of: null,
40
- archived_at: null,
41
- created_at: NOW,
42
- updated_at: NOW,
43
- ...extra,
44
- },
45
- });
46
- await adapter.create({
47
- table: "dd_dashboard_version",
48
- data: {
49
- id: `${id}-v1`,
50
- dashboard_id: id,
51
- tenant_hash: hash,
52
- n: 1,
53
- spec: emptyDoc(`Title ${id}`),
54
- spec_version: 1,
55
- author_id: "u1",
56
- author_kind: "user",
57
- request_id: `${id}-r1`,
58
- message: null,
59
- created_at: NOW,
60
- },
61
- });
62
- }
63
-
64
- type Get = () => DashAdapter;
65
-
66
- /** The schema: every table made, and making it again changes nothing. */
67
- function schemaCases(get: Get): void {
68
- it("makes every table, and is safe to run twice at once", async () => {
69
- await Promise.all([createTables(get()), createTables(get())]);
70
- // The table names are the keys of TABLES.
71
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion
72
- const names = Object.keys(TABLES) as (keyof typeof TABLES)[];
73
- const counts = await Promise.all(names.map((table) => get().count({ table })));
74
- expect(counts.every((n) => n === 0)).toBe(true);
75
- });
76
-
77
- it("reads every column of the dd_ tables from the catalog", async () => {
78
- const read = await get().schema.columns();
79
- const want = Object.entries(TABLES).flatMap(([table, def]) =>
80
- Object.keys(def.fields).map((column) => `${table}.${column}`),
81
- );
82
- expect(read.map((c) => `${c.table}.${c.column}`).toSorted()).toEqual(want.toSorted());
83
- });
84
- }
85
-
86
- /** Rows: typed reads and writes, and every operator. */
87
- function rowCases(get: Get): void {
88
- it("writes and reads rows with their types: integers, JSON, timestamps and nulls", async () => {
89
- await seedDashboard(get(), "d1", ACME, { key: "ops" });
90
- const row = await get().findOne({
91
- table: "dd_dashboard",
92
- where: [{ field: "id", value: "d1" }],
93
- });
94
- expect(row).toMatchObject({
95
- id: "d1",
96
- tenant: ACME,
97
- current_version: 1,
98
- created_at: NOW,
99
- archived_at: null,
100
- key: "ops",
101
- });
102
- const changed = await get().updateMany({
103
- table: "dd_dashboard",
104
- where: [{ field: "id", value: "d1" }],
105
- set: { title: "Ops" },
106
- });
107
- expect(changed).toBe(1);
108
- expect(
109
- await get().count({ table: "dd_dashboard", where: [{ field: "title", value: "Ops" }] }),
110
- ).toBe(1);
111
- });
112
-
113
- it("orders, limits and filters with every operator", async () => {
114
- await seedDashboard(get(), "d2", ACME);
115
- await seedDashboard(get(), "d3", ACME, { archived_at: NOW });
116
- const ids = await get().findMany({
117
- table: "dd_dashboard",
118
- where: [
119
- { field: "id", op: "in", value: ["d1", "d2", "d3"] },
120
- { field: "archived_at", op: "isNull" },
121
- ],
122
- orderBy: [{ field: "id", direction: "desc" }],
123
- limit: 1,
124
- offset: 1,
125
- });
126
- expect(ids.map((r) => r["id"])).toEqual(["d1"]);
127
- expect(
128
- await get().count({
129
- table: "dd_dashboard",
130
- where: [{ field: "id", op: "in", value: [] }],
131
- }),
132
- ).toBe(0);
133
- expect(
134
- await get().deleteMany({ table: "dd_dashboard", where: [{ field: "id", value: "d3" }] }),
135
- ).toBe(1);
136
- });
137
- }
138
-
139
- /** Transactions roll back whole, and a unique violation is told apart from anything else. */
140
- /** An adapter's createMany, or create row by row where it has none: either writes the same rows. */
141
- const createAll =
142
- (adapter: DashAdapter) =>
143
- async (a: { table: TableName; rows: readonly Row[] }): Promise<void> => {
144
- if (adapter.createMany !== undefined) return adapter.createMany(a);
145
- // oxlint-disable-next-line no-await-in-loop -- one row at a time, as create writes
146
- for (const data of a.rows) await adapter.create({ table: a.table, data });
147
- };
148
-
149
- function batchCases(get: Get): void {
150
- it("writes many rows in one call, when it can, each as create would", async () => {
151
- const rows = [1, 2, 3].map((n) => ({
152
- draft_id: "dr",
153
- command_id: `c${String(n)}`,
154
- digest: "x",
155
- body: { type: "meta.set", title: `T${String(n)}` },
156
- revision: 1,
157
- seq: n,
158
- created_at: NOW,
159
- }));
160
- const adapter = get();
161
- await createAll(adapter)({ table: "dd_dashboard_draft_command", rows });
162
- const read = await adapter.findMany({
163
- table: "dd_dashboard_draft_command",
164
- where: [{ field: "draft_id", value: "dr" }],
165
- orderBy: [{ field: "seq", direction: "asc" }],
166
- });
167
- expect(read.map((r) => [r["command_id"], r["body"], r["seq"]])).toEqual(
168
- rows.map((r) => [r.command_id, r.body, r.seq]),
169
- );
170
- });
171
- }
172
-
173
- function transactionCases(get: Get): void {
174
- it("rolls a transaction back whole and tells a unique violation apart", async () => {
175
- const failing = get().transaction(async (tx) => {
176
- await seedDashboard(tx, "d9", ACME);
177
- throw new Error("stop");
178
- });
179
- await expect(failing).rejects.toThrow("stop");
180
- expect(
181
- await get().findOne({ table: "dd_dashboard", where: [{ field: "id", value: "d9" }] }),
182
- ).toBeNull();
183
- const duplicate = await seedDashboard(get(), "d1", ACME).catch((error: unknown) => error);
184
- expect(get().classifyError(duplicate)).toBe("unique");
185
- expect(get().classifyError(new Error("other"))).toBe("other");
186
- });
187
- }
188
-
189
- /** The read path: a tenant's dashboards and versions, never another tenant's; then an upgrade of the populated database. */
190
- function readerCases(get: Get): void {
191
- it("reads a tenant's dashboards and versions, and never another tenant's", async () => {
192
- await seedDashboard(get(), "g1", GLOBEX, { key: "ops" });
193
- const reader = createReader(get());
194
- expect(await reader.get({ tenant: ACME, ref: { key: "ops" } })).toMatchObject({
195
- id: "d1",
196
- title: "Ops",
197
- currentVersion: 1,
198
- });
199
- expect(await reader.get({ tenant: GLOBEX, ref: { id: "d1" } })).toBeNull();
200
- expect(await reader.get({ tenant: GLOBEX, ref: { key: "ops" } })).toMatchObject({ id: "g1" });
201
- const page = await reader.list({ tenant: ACME, limit: 1 });
202
- expect(page.items.map((d) => d.id)).toEqual(["d1"]);
203
- expect(page.next).toBe("1");
204
- expect(await reader.getVersion({ tenant: ACME, id: "d1", version: 1 })).toMatchObject({
205
- version: 1,
206
- doc: { meta: { title: "Title d1" } },
207
- });
208
- expect(await reader.getVersion({ tenant: GLOBEX, id: "d1", version: 1 })).toBeNull();
209
- });
210
-
211
- it("keeps every row when the tables are made again", async () => {
212
- const before = await get().count({ table: "dd_dashboard" });
213
- await createTables(get());
214
- expect(await get().count({ table: "dd_dashboard" })).toBe(before);
215
- });
216
- }
217
-
218
- /**
219
- * The contract every store adapter must meet, run against a real database: the schema made
220
- * from one definition, typed reads and writes, atomic transactions, error classes, and the dashboard read
221
- * path with tenant isolation. A new adapter calls this from its own tests.
222
- */
223
- export function adapterSuite(name: string, open: OpenAdapter): void {
224
- // DDL on an embedded database is slow on a shared CI runner.
225
- describe(`store adapter: ${name}`, { timeout: 30_000 }, () => {
226
- let adapter: DashAdapter;
227
- let close: () => Promise<void>;
228
- beforeAll(async () => {
229
- ({ adapter, close } = await open());
230
- }, 30_000);
231
- afterAll(() => close());
232
- const get = () => adapter;
233
- schemaCases(get);
234
- rowCases(get);
235
- batchCases(get);
236
- transactionCases(get);
237
- readerCases(get);
238
- });
239
- }
@@ -1,2 +0,0 @@
1
- // For adapter authors: the contract suite a store adapter must pass. Needs vitest.
2
- export { adapterSuite, type OpenAdapter } from "./adapter-suite";
package/src/tile-doc.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { Picks, Principal } from "@derive-to/dashboards-core";
2
- import { versionDoc } from "./checks";
3
- import { loadDashboard } from "./dashboards";
4
- import { draftDoc } from "./editing";
5
- import type { Runtime } from "./execute";
6
- import { HttpError } from "./http";
7
- import type { userOf } from "./request";
8
-
9
- /** Which document a tile request runs against, and who may ask for each. */
10
-
11
- export type TileCtx = {
12
- dashboardId: string;
13
- version?: number;
14
- picks?: Picks;
15
- draft?: true;
16
- merged?: true;
17
- at?: number;
18
- principal: Principal | null;
19
- request: Request;
20
- };
21
-
22
- /**
23
- * The document tiles run against: the viewer's open draft, what publishing it would make now,
24
- * an earlier version being previewed, or else the pinned current version. All but the last
25
- * need `edit`.
26
- */
27
- export async function docFor(rt: Runtime, a: TileCtx, principal: ReturnType<typeof userOf>) {
28
- const sources = [a.draft, a.merged, a.at].filter((s) => s !== undefined).length;
29
- if (sources > 1)
30
- throw new HttpError(
31
- 400,
32
- "BODY_INVALID",
33
- "A tile runs against one document: send at most one of draft, merged and at.",
34
- );
35
- if (a.draft === true || a.merged === true) {
36
- const as = a.draft === true ? "draft" : "merged";
37
- const document = await draftDoc(rt, a.request, principal, { id: a.dashboardId, as });
38
- return { id: a.dashboardId, document };
39
- }
40
- if (a.at !== undefined) {
41
- const old = await versionDoc(rt, a.request, principal, { ...a, version: a.at });
42
- return { id: a.dashboardId, document: old.doc };
43
- }
44
- const ref = { id: a.dashboardId, ...(a.version === undefined ? {} : { version: a.version }) };
45
- return loadDashboard(rt.options, a.request, principal, ref);
46
- }
package/src/tiles.ts DELETED
@@ -1,120 +0,0 @@
1
- import {
2
- composeTile,
3
- type Doc,
4
- type Picks,
5
- type Principal,
6
- type TileAnswer,
7
- type TileLine,
8
- type TileRequest,
9
- } from "@derive-to/dashboards-core";
10
- import { EngineError } from "@derive-to/dashboards-core/engine";
11
- import { type Execution, execute, type Runtime } from "./execute";
12
- import { HttpError } from "./http";
13
- import { calendarOf } from "./request";
14
-
15
- /** What every tile of one request shares: the dashboard as pinned, the picks and one `now`. */
16
- export type Ctx = {
17
- rt: Runtime;
18
- request: Request;
19
- principal: Principal;
20
- dashboardId: string;
21
- doc: Doc;
22
- picks: Picks;
23
- now: Date;
24
- };
25
-
26
- /** One tile composed from the document by the written rules, ready to run. */
27
- export function tileExecution(ctx: Ctx, tile: TileRequest): Execution {
28
- const { options } = ctx.rt;
29
- const composed = composeTile({
30
- model: options.model,
31
- doc: ctx.doc,
32
- widgetId: tile.widgetId,
33
- picks: ctx.picks,
34
- ...(tile.query === undefined ? {} : { queryName: tile.query }),
35
- ...(options.compose === undefined ? {} : { policy: options.compose }),
36
- });
37
- return {
38
- rt: ctx.rt,
39
- request: ctx.request,
40
- principal: ctx.principal,
41
- now: ctx.now,
42
- calendar: calendarOf(options, ctx.doc),
43
- query: composed.query,
44
- lock: composed.lock,
45
- coverage: composed.coverage,
46
- dashboardId: ctx.dashboardId,
47
- };
48
- }
49
-
50
- /** One tile by reference: composed from the document, planned under the viewer's access, run. */
51
- export async function runTile(ctx: Ctx, tile: TileRequest): Promise<TileAnswer> {
52
- return { widgetId: tile.widgetId, ...(await execute(tileExecution(ctx, tile))) };
53
- }
54
-
55
- /** An error a client can act on, as a line; anything else ends the stream. */
56
- function refusal(error: unknown): { code: string; message: string } | undefined {
57
- if (error instanceof EngineError || error instanceof HttpError)
58
- return { code: error.code, message: error.message };
59
- return undefined;
60
- }
61
-
62
- type Settled<T> =
63
- | { i: number; value: T }
64
- | { i: number; error: { code: string; message: string } }
65
- | { i: number; failure: unknown };
66
-
67
- async function settle<T>(i: number, run: () => Promise<T>): Promise<Settled<T>> {
68
- try {
69
- return { i, value: await run() };
70
- } catch (error) {
71
- const refused = refusal(error);
72
- return refused === undefined ? { i, failure: error } : { i, error: refused };
73
- }
74
- }
75
-
76
- /** How many jobs of one request run at once: `query.concurrency`, default 6. */
77
- export function concurrencyOf(rt: Runtime): number {
78
- const wanted = rt.options.query?.concurrency ?? 6;
79
- return Number.isInteger(wanted) && wanted >= 1 ? wanted : 6;
80
- }
81
-
82
- /**
83
- * Runs the jobs, at most `width` at once (each next one starts as one finishes), and yields
84
- * each answer as it finishes, as an NDJSON line, then `{ done: true }`. A refused job is a line
85
- * with its error and the others still answer. A reader that stops reading starts no more jobs.
86
- */
87
- export async function* streamLines<T>(
88
- jobs: readonly (() => Promise<T>)[],
89
- width: number,
90
- ): AsyncGenerator<{ i: number; ok: true; result: T } | Exclude<TileLine, { ok: true }>> {
91
- const running = new Map<number, Promise<Settled<T>>>();
92
- let started = 0;
93
- const fill = () => {
94
- for (; started < jobs.length && running.size < width; started++) {
95
- const job = jobs[started];
96
- if (job !== undefined) running.set(started, settle(started, job));
97
- }
98
- };
99
- fill();
100
- while (running.size > 0) {
101
- // The running jobs are already running; this waits for whichever finishes next.
102
- // oxlint-disable-next-line eslint/no-await-in-loop
103
- const settled = await Promise.race(running.values());
104
- running.delete(settled.i);
105
- fill();
106
- if ("failure" in settled) throw settled.failure;
107
- yield "error" in settled
108
- ? { i: settled.i, ok: false, error: settled.error }
109
- : { i: settled.i, ok: true, result: settled.value };
110
- }
111
- yield { done: true };
112
- }
113
-
114
- /** Each tile by reference, streamed as it finishes. */
115
- export function streamTiles(ctx: Ctx, tiles: readonly TileRequest[]) {
116
- return streamLines(
117
- tiles.map((tile) => () => runTile(ctx, tile)),
118
- concurrencyOf(ctx.rt),
119
- );
120
- }