derive-dashboards 0.1.0 → 0.2.0

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 (410) hide show
  1. package/README.md +8 -5
  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/add.mjs +130 -0
  6. package/dist/cli/check.mjs +14 -1
  7. package/dist/cli/cli.mjs +14 -12
  8. package/dist/cli/config.mjs +12 -1
  9. package/dist/cli/doctor.mjs +147 -0
  10. package/dist/cli/import/dbt-schema.mjs +2 -2
  11. package/dist/cli/import/index.mjs +2 -2
  12. package/dist/cli/init.mjs +3 -2
  13. package/dist/cli/json.mjs +21 -0
  14. package/dist/cli/layers.mjs +33 -0
  15. package/dist/core/conformance/cases.d.mts +3 -0
  16. package/dist/core/conformance/cases.mjs +20 -4
  17. package/dist/core/conformance/check.mjs +6 -3
  18. package/dist/core/conformance/drill-cases.mjs +120 -0
  19. package/dist/core/document/layout.mjs +1 -1
  20. package/dist/core/document/view-layout.d.mts +14 -0
  21. package/dist/core/document/view-layout.mjs +157 -0
  22. package/dist/core/document/view.d.mts +26 -7
  23. package/dist/core/document/view.mjs +42 -11
  24. package/dist/core/engine/filter.mjs +2 -2
  25. package/dist/core/engine/index.d.mts +14 -2
  26. package/dist/core/engine/index.mjs +12 -1
  27. package/dist/core/engine/leaf.mjs +9 -4
  28. package/dist/core/engine/members.mjs +2 -2
  29. package/dist/core/engine/postgres.mjs +1 -1
  30. package/dist/core/engine/record-layout.d.mts +11 -0
  31. package/dist/core/engine/record-layout.mjs +7 -7
  32. package/dist/core/engine/records.d.mts +3 -2
  33. package/dist/core/engine/records.mjs +2 -1
  34. package/dist/core/engine/segmented.mjs +5 -2
  35. package/dist/core/engine/sql-records.mjs +2 -1
  36. package/dist/core/engine/statement.mjs +37 -3
  37. package/dist/core/engine/types.d.mts +3 -0
  38. package/dist/core/engine/version.d.mts +1 -1
  39. package/dist/core/engine/version.mjs +1 -1
  40. package/dist/core/error/codes.d.mts +33 -1
  41. package/dist/core/error/codes.mjs +30 -2
  42. package/dist/core/index.d.mts +13 -7
  43. package/dist/core/index.mjs +9 -5
  44. package/dist/core/model/catalog/check.mjs +1 -1
  45. package/dist/core/model/hints.d.mts +36 -0
  46. package/dist/core/model/member.d.mts +4 -2
  47. package/dist/core/model/member.mjs +19 -3
  48. package/dist/core/model/types.d.mts +21 -19
  49. package/dist/core/model/validate/drill.mjs +70 -0
  50. package/dist/core/model/validate/index.mjs +3 -0
  51. package/dist/core/model/validate/members.mjs +7 -4
  52. package/dist/core/model/validate/shape.mjs +150 -0
  53. package/dist/core/model/validate/source.mjs +5 -3
  54. package/dist/core/policy/bind-query.d.mts +12 -0
  55. package/dist/core/policy/bind-query.mjs +53 -0
  56. package/dist/core/query/alias.mjs +7 -1
  57. package/dist/core/query/drill/compose.d.mts +22 -0
  58. package/dist/core/query/drill/compose.mjs +85 -0
  59. package/dist/core/query/drill/fields.d.mts +21 -0
  60. package/dist/core/query/drill/fields.mjs +91 -0
  61. package/dist/core/query/drill/internal.d.mts +11 -0
  62. package/dist/core/query/drill/items.d.mts +29 -0
  63. package/dist/core/query/drill/items.mjs +98 -0
  64. package/dist/core/query/drill/line.d.mts +31 -0
  65. package/dist/core/query/drill/line.mjs +183 -0
  66. package/dist/core/query/drill/links.d.mts +33 -0
  67. package/dist/core/query/drill/links.mjs +31 -0
  68. package/dist/core/query/drill/operand.mjs +7 -0
  69. package/dist/core/query/drill/scopes.d.mts +22 -0
  70. package/dist/core/query/drill/scopes.mjs +58 -0
  71. package/dist/core/query/drill/selection.d.mts +37 -0
  72. package/dist/core/query/drill/selection.mjs +144 -0
  73. package/dist/core/query/drill/steps.d.mts +14 -0
  74. package/dist/core/query/drill/steps.mjs +35 -0
  75. package/dist/core/query/drill/synthetic-expr.mjs +85 -0
  76. package/dist/core/query/drill/synthetic.d.mts +22 -0
  77. package/dist/core/query/drill/synthetic.mjs +165 -0
  78. package/dist/core/query/drill/time.d.mts +11 -0
  79. package/dist/core/query/drill/time.mjs +71 -0
  80. package/dist/core/query/drill/types.d.mts +155 -0
  81. package/dist/core/query/drill/types.mjs +4 -0
  82. package/dist/core/query/drill/views.d.mts +62 -0
  83. package/dist/core/query/drill/views.mjs +257 -0
  84. package/dist/core/query/filter.d.mts +6 -0
  85. package/dist/core/query/picks.d.mts +8 -2
  86. package/dist/core/query/picks.mjs +11 -4
  87. package/dist/core/query/segments.d.mts +12 -2
  88. package/dist/core/query/segments.mjs +5 -2
  89. package/dist/core/result/columns.mjs +20 -4
  90. package/dist/core/result/types.d.mts +5 -1
  91. package/dist/core/schema/aliases.d.mts +7 -0
  92. package/dist/core/schema/aliases.mjs +77 -0
  93. package/dist/core/schema/answer-parts.mjs +117 -0
  94. package/dist/core/schema/answers.d.mts +2109 -128
  95. package/dist/core/schema/answers.mjs +11 -113
  96. package/dist/core/schema/commands.d.mts +2 -2
  97. package/dist/core/schema/document.d.mts +1 -1
  98. package/dist/core/schema/drill-answers.mjs +179 -0
  99. package/dist/core/schema/drill.d.mts +260 -0
  100. package/dist/core/schema/drill.mjs +107 -0
  101. package/dist/core/schema/index.d.mts +6 -3
  102. package/dist/core/schema/index.mjs +6 -3
  103. package/dist/core/schema/picks.d.mts +35 -0
  104. package/dist/core/schema/picks.mjs +23 -0
  105. package/dist/core/schema/query.d.mts +82 -1
  106. package/dist/core/schema/query.mjs +9 -5
  107. package/dist/core/schema/tools.mjs +1 -1
  108. package/dist/core/schema/view.d.mts +0 -35
  109. package/dist/core/schema/view.mjs +19 -24
  110. package/dist/core/store/schema.d.mts +5 -0
  111. package/dist/core/store/schema.mjs +10 -0
  112. package/dist/core/types/api.d.mts +21 -1
  113. package/dist/core/types/api.mjs +1 -1
  114. package/dist/core/types/drill-api.d.mts +210 -0
  115. package/dist/core/types/options.d.mts +21 -0
  116. package/dist/core/types/viewer.d.mts +5 -0
  117. package/dist/cursor-seal.d.mts +8 -0
  118. package/dist/cursor-seal.mjs +98 -0
  119. package/dist/dashboard-id.mjs +1 -1
  120. package/dist/dashboards.mjs +4 -0
  121. package/dist/drill-access.mjs +93 -0
  122. package/dist/drill-change.mjs +76 -0
  123. package/dist/drill-crumbs.mjs +113 -0
  124. package/dist/drill-distinct.mjs +130 -0
  125. package/dist/drill-explain.mjs +64 -0
  126. package/dist/drill-identity.mjs +35 -0
  127. package/dist/drill-item-id.mjs +35 -0
  128. package/dist/drill-items.mjs +132 -0
  129. package/dist/drill-open.mjs +169 -0
  130. package/dist/drill-seal.mjs +170 -0
  131. package/dist/drill-secret.mjs +14 -0
  132. package/dist/drill-share.mjs +53 -0
  133. package/dist/drill-summary.mjs +158 -0
  134. package/dist/drill-targets.mjs +53 -0
  135. package/dist/drill-totals.mjs +8 -0
  136. package/dist/drill-views.mjs +175 -0
  137. package/dist/drill.mjs +157 -0
  138. package/dist/editing.mjs +3 -3
  139. package/dist/endpoints/dashboards.mjs +28 -5
  140. package/dist/endpoints/drill.mjs +55 -0
  141. package/dist/endpoints/index.d.mts +245 -4
  142. package/dist/endpoints/index.mjs +5 -1
  143. package/dist/endpoints/meta.mjs +10 -1
  144. package/dist/endpoints/query.mjs +26 -4
  145. package/dist/execute.d.mts +19 -1
  146. package/dist/execute.mjs +34 -27
  147. package/dist/handler.mjs +5 -1
  148. package/dist/index.d.mts +3 -1
  149. package/dist/index.mjs +29 -2
  150. package/dist/llms.txt +22 -0
  151. package/dist/mcp/drill.mjs +61 -0
  152. package/dist/mcp/format.mjs +69 -11
  153. package/dist/mcp/index.d.mts +15 -4
  154. package/dist/mcp/index.mjs +13 -7
  155. package/dist/mcp/query.d.mts +7 -2
  156. package/dist/mcp/query.mjs +40 -6
  157. package/dist/node-postgres.d.mts +8 -4
  158. package/dist/node-postgres.mjs +29 -6
  159. package/dist/owner.mjs +3 -3
  160. package/dist/postgres-connection.d.mts +8 -2
  161. package/dist/postgres-connection.mjs +2 -1
  162. package/dist/postgres-store.mjs +1 -0
  163. package/dist/react/Canvas.mjs +28 -5
  164. package/dist/react/Cell.mjs +2 -1
  165. package/dist/react/Dashboard.d.mts +10 -5
  166. package/dist/react/Dashboard.mjs +62 -22
  167. package/dist/react/DashboardList.mjs +52 -24
  168. package/dist/react/DashboardListRow.mjs +49 -0
  169. package/dist/react/Discard.mjs +12 -4
  170. package/dist/react/Editing.mjs +19 -5
  171. package/dist/react/FilterBar.d.mts +7 -9
  172. package/dist/react/Grid.mjs +19 -24
  173. package/dist/react/Header.mjs +10 -1
  174. package/dist/react/History.mjs +5 -0
  175. package/dist/react/Parts.mjs +6 -2
  176. package/dist/react/ReadFailed.mjs +23 -0
  177. package/dist/react/ReviewPanel.mjs +13 -2
  178. package/dist/react/RowBody.d.mts +19 -0
  179. package/dist/react/RowBody.mjs +55 -0
  180. package/dist/react/Spaces.mjs +15 -1
  181. package/dist/react/Tile.d.mts +3 -1
  182. package/dist/react/Tile.mjs +84 -32
  183. package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
  184. package/dist/react/arrange-steps.mjs +16 -13
  185. package/dist/react/arrange.mjs +10 -3
  186. package/dist/react/builder/Calcs.mjs +35 -11
  187. package/dist/react/builder/FilterEditor.mjs +196 -0
  188. package/dist/react/builder/Filters.mjs +172 -116
  189. package/dist/react/builder/Formula.mjs +82 -0
  190. package/dist/react/builder/Groups.mjs +1 -0
  191. package/dist/react/builder/Preview.mjs +1 -1
  192. package/dist/react/builder/RangeEditor.mjs +184 -0
  193. package/dist/react/builder/Sections.mjs +41 -6
  194. package/dist/react/builder/Time.mjs +138 -60
  195. package/dist/react/builder/Values.mjs +85 -0
  196. package/dist/react/builder/WidgetBuilder.mjs +50 -49
  197. package/dist/react/builder/base.d.mts +5 -0
  198. package/dist/react/builder/base.mjs +33 -8
  199. package/dist/react/builder/calc.mjs +15 -1
  200. package/dist/react/builder/escape.d.mts +1 -0
  201. package/dist/react/builder/escape.mjs +62 -0
  202. package/dist/react/builder/filter-tree.d.mts +9 -0
  203. package/dist/react/builder/filter-tree.mjs +186 -0
  204. package/dist/react/builder/formula-parse.d.mts +18 -0
  205. package/dist/react/builder/formula-parse.mjs +136 -0
  206. package/dist/react/builder/labels.d.mts +1 -1
  207. package/dist/react/builder/state.d.mts +2 -1
  208. package/dist/react/builder/state.mjs +23 -37
  209. package/dist/react/builder/useBuilder.d.mts +12 -1
  210. package/dist/react/builder/useBuilder.mjs +6 -3
  211. package/dist/react/builder/words.d.mts +59 -0
  212. package/dist/react/builder/words.mjs +73 -0
  213. package/dist/react/client.d.mts +2 -0
  214. package/dist/react/client.mjs +1 -0
  215. package/dist/react/context.d.mts +28 -1
  216. package/dist/react/context.mjs +25 -4
  217. package/dist/react/drill/Bars.mjs +179 -0
  218. package/dist/react/drill/Controls.mjs +173 -0
  219. package/dist/react/drill/DashboardTile.d.mts +18 -0
  220. package/dist/react/drill/DashboardTile.mjs +107 -0
  221. package/dist/react/drill/DrillPanel.d.mts +31 -0
  222. package/dist/react/drill/DrillPanel.mjs +104 -0
  223. package/dist/react/drill/DrillTabs.mjs +52 -0
  224. package/dist/react/drill/Header.mjs +131 -0
  225. package/dist/react/drill/Items.d.mts +22 -0
  226. package/dist/react/drill/Items.mjs +164 -0
  227. package/dist/react/drill/ItemsFoot.mjs +60 -0
  228. package/dist/react/drill/Notes.mjs +58 -0
  229. package/dist/react/drill/PanelBody.mjs +74 -0
  230. package/dist/react/drill/PanelStates.mjs +37 -0
  231. package/dist/react/drill/board.d.mts +18 -0
  232. package/dist/react/drill/board.mjs +91 -0
  233. package/dist/react/drill/counts.mjs +14 -0
  234. package/dist/react/drill/host.mjs +26 -0
  235. package/dist/react/drill/item-parts.d.mts +5 -0
  236. package/dist/react/drill/item-parts.mjs +76 -0
  237. package/dist/react/drill/item-type.d.mts +26 -0
  238. package/dist/react/drill/item-type.mjs +7 -0
  239. package/dist/react/drill/lazy.d.mts +6 -0
  240. package/dist/react/drill/lazy.mjs +17 -0
  241. package/dist/react/drill/pages.mjs +48 -0
  242. package/dist/react/drill/panel-focus.mjs +51 -0
  243. package/dist/react/drill/props.d.mts +25 -0
  244. package/dist/react/drill/props.mjs +72 -0
  245. package/dist/react/drill/state.d.mts +18 -0
  246. package/dist/react/drill/state.mjs +68 -0
  247. package/dist/react/drill/useDrill.d.mts +46 -0
  248. package/dist/react/drill/useDrill.mjs +194 -0
  249. package/dist/react/drill/words.d.mts +101 -0
  250. package/dist/react/drill/words.mjs +79 -0
  251. package/dist/react/english.mjs +29 -6
  252. package/dist/react/filters/bar.d.mts +12 -0
  253. package/dist/react/filters/bar.mjs +135 -0
  254. package/dist/react/filters/calendar.mjs +193 -0
  255. package/dist/react/filters/chip.mjs +174 -0
  256. package/dist/react/filters/context.d.mts +22 -0
  257. package/dist/react/filters/context.mjs +78 -0
  258. package/dist/react/filters/dates.mjs +89 -0
  259. package/dist/react/filters/days.d.mts +36 -0
  260. package/dist/react/filters/days.mjs +87 -0
  261. package/dist/react/filters/field-step.mjs +81 -0
  262. package/dist/react/filters/hooks.d.mts +34 -0
  263. package/dist/react/filters/hooks.mjs +94 -0
  264. package/dist/react/filters/listbox.mjs +131 -0
  265. package/dist/react/filters/logic.d.mts +59 -0
  266. package/dist/react/filters/logic.mjs +109 -0
  267. package/dist/react/filters/menu-search.mjs +211 -0
  268. package/dist/react/filters/menu.mjs +85 -0
  269. package/dist/react/filters/number.mjs +127 -0
  270. package/dist/react/filters/numbers.d.mts +47 -0
  271. package/dist/react/filters/numbers.mjs +151 -0
  272. package/dist/react/filters/operator.mjs +104 -0
  273. package/dist/react/filters/phone.mjs +104 -0
  274. package/dist/react/filters/popover.d.mts +19 -0
  275. package/dist/react/filters/popover.mjs +153 -0
  276. package/dist/react/filters/rules.d.mts +8 -0
  277. package/dist/react/filters/rules.mjs +55 -0
  278. package/dist/react/filters/sheet.mjs +196 -0
  279. package/dist/react/filters/state.mjs +132 -0
  280. package/dist/react/filters/suggest.d.mts +47 -0
  281. package/dist/react/filters/suggest.mjs +97 -0
  282. package/dist/react/filters/values.mjs +166 -0
  283. package/dist/react/filters/words.d.mts +81 -0
  284. package/dist/react/filters/words.mjs +73 -0
  285. package/dist/react/format.d.mts +3 -3
  286. package/dist/react/format.mjs +45 -16
  287. package/dist/react/index.d.mts +34 -5
  288. package/dist/react/index.mjs +15 -4
  289. package/dist/react/messages.d.mts +47 -2
  290. package/dist/react/place.mjs +7 -3
  291. package/dist/react/renderers/index.d.mts +10 -2
  292. package/dist/react/renderers/index.mjs +116 -14
  293. package/dist/react/renderers/list.d.mts +22 -0
  294. package/dist/react/renderers/list.mjs +118 -0
  295. package/dist/react/review-words.mjs +1 -1
  296. package/dist/react/review.mjs +2 -2
  297. package/dist/react/scope.mjs +2 -7
  298. package/dist/react/series.d.mts +54 -0
  299. package/dist/react/series.mjs +191 -0
  300. package/dist/react/theme.css +2734 -1296
  301. package/dist/react/theme.css.d.ts +1 -0
  302. package/dist/react/tile-store.mjs +15 -3
  303. package/dist/react/useBoard.d.mts +7 -0
  304. package/dist/react/useBoard.mjs +4 -1
  305. package/dist/react/useDashboard.d.mts +4 -0
  306. package/dist/react/useDashboard.mjs +8 -1
  307. package/dist/react/useTiles.mjs +50 -15
  308. package/dist/react/view-grid.mjs +16 -0
  309. package/dist/react/view.d.mts +9 -0
  310. package/dist/react/view.mjs +22 -10
  311. package/dist/reading.d.mts +2 -0
  312. package/dist/reading.mjs +9 -1
  313. package/dist/recharts/axes.mjs +28 -0
  314. package/dist/recharts/axis.mjs +84 -0
  315. package/dist/recharts/bar.d.mts +7 -0
  316. package/dist/recharts/bar.mjs +140 -0
  317. package/dist/recharts/charts.d.mts +4 -6
  318. package/dist/recharts/charts.mjs +2 -114
  319. package/dist/recharts/index.d.mts +5 -3
  320. package/dist/recharts/index.mjs +6 -3
  321. package/dist/recharts/line.d.mts +2 -0
  322. package/dist/recharts/line.mjs +111 -0
  323. package/dist/recharts/marks.mjs +112 -0
  324. package/dist/recharts/parts.mjs +88 -0
  325. package/dist/recharts/series.d.mts +2 -22
  326. package/dist/recharts/series.mjs +1 -64
  327. package/dist/recharts/table.mjs +71 -0
  328. package/dist/records-read.mjs +44 -0
  329. package/dist/registry/filter-bar.json +26 -0
  330. package/dist/skills/derive-dashboards/SKILL.md +69 -0
  331. package/dist/store/apply.mjs +5 -5
  332. package/dist/store/commands.mjs +4 -4
  333. package/dist/store/discard.mjs +7 -7
  334. package/dist/store/drafts.mjs +5 -5
  335. package/dist/store/editor.mjs +1 -1
  336. package/dist/store/publish.mjs +12 -12
  337. package/dist/store/reader.mjs +41 -22
  338. package/dist/store/review.mjs +16 -16
  339. package/dist/store/rows.mjs +20 -20
  340. package/dist/store/undo.mjs +3 -3
  341. package/dist/store/unpublished.mjs +4 -4
  342. package/dist/store/versions.mjs +14 -14
  343. package/dist/testing/adapter-suite.mjs +4 -4
  344. package/dist/tiles.mjs +37 -3
  345. package/package.json +12 -10
  346. package/dist/react/FilterBar.mjs +0 -119
  347. package/dist/react/Records.d.mts +0 -12
  348. package/dist/react/Records.mjs +0 -97
  349. package/src/adapter/sql-adapter.ts +0 -95
  350. package/src/adapter/statement.ts +0 -185
  351. package/src/authorize.ts +0 -73
  352. package/src/body.ts +0 -169
  353. package/src/bounded.ts +0 -43
  354. package/src/cache.ts +0 -192
  355. package/src/checks.ts +0 -170
  356. package/src/custom-source.ts +0 -59
  357. package/src/dashboard-id.ts +0 -36
  358. package/src/dashboards.ts +0 -132
  359. package/src/editing.ts +0 -148
  360. package/src/endpoints/access.ts +0 -41
  361. package/src/endpoints/dashboards.ts +0 -92
  362. package/src/endpoints/drafts.ts +0 -138
  363. package/src/endpoints/index.ts +0 -21
  364. package/src/endpoints/meta.ts +0 -137
  365. package/src/endpoints/query.ts +0 -86
  366. package/src/endpoints/read.ts +0 -92
  367. package/src/endpoints/review.ts +0 -89
  368. package/src/endpoints/values.ts +0 -62
  369. package/src/endpoints/versions.ts +0 -104
  370. package/src/execute.ts +0 -207
  371. package/src/handler.ts +0 -232
  372. package/src/http.ts +0 -66
  373. package/src/index.ts +0 -163
  374. package/src/introspect.ts +0 -205
  375. package/src/mcp/api.ts +0 -59
  376. package/src/mcp/format.ts +0 -67
  377. package/src/mcp/index.ts +0 -151
  378. package/src/mcp/query.ts +0 -153
  379. package/src/mcp/resolve.ts +0 -114
  380. package/src/node-postgres.ts +0 -81
  381. package/src/owner.ts +0 -36
  382. package/src/postgres-connection.ts +0 -143
  383. package/src/postgres-store.ts +0 -82
  384. package/src/principal.ts +0 -39
  385. package/src/reading.ts +0 -56
  386. package/src/request-rows.ts +0 -23
  387. package/src/request.ts +0 -22
  388. package/src/storage.ts +0 -63
  389. package/src/store/apply.ts +0 -129
  390. package/src/store/commands.ts +0 -75
  391. package/src/store/conflicts.ts +0 -98
  392. package/src/store/create-tables.ts +0 -104
  393. package/src/store/discard.ts +0 -42
  394. package/src/store/drafts.ts +0 -133
  395. package/src/store/editor.ts +0 -28
  396. package/src/store/log.ts +0 -28
  397. package/src/store/publish.ts +0 -199
  398. package/src/store/publisher.ts +0 -21
  399. package/src/store/reader.ts +0 -139
  400. package/src/store/review.ts +0 -239
  401. package/src/store/rows.ts +0 -250
  402. package/src/store/tenant.ts +0 -28
  403. package/src/store/undo.ts +0 -56
  404. package/src/store/unpublished.ts +0 -33
  405. package/src/store/versions.ts +0 -106
  406. package/src/store/writer.ts +0 -19
  407. package/src/testing/adapter-suite.ts +0 -239
  408. package/src/testing/index.ts +0 -2
  409. package/src/tile-doc.ts +0 -46
  410. package/src/tiles.ts +0 -120
@@ -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
- }
package/src/store/rows.ts DELETED
@@ -1,250 +0,0 @@
1
- import {
2
- type Author,
3
- canonical,
4
- type DashAdapter,
5
- type Doc,
6
- type Draft,
7
- diffDocs,
8
- markShifted,
9
- type Row,
10
- type Tenant,
11
- } from "@derive-to/dashboards-core";
12
- import { loadDocument } from "@derive-to/dashboards-core/schema";
13
- import { sha256 } from "../cache";
14
- import { HttpError } from "../http";
15
- import { sameTenant, tenantHash } from "./tenant";
16
-
17
- /** The store's own reads and writes of single rows, shared by drafts and publishing. */
18
-
19
- export const now = () => new Date().toISOString();
20
-
21
- /** A stored document as this version reads it; one that cannot be read is a server error that names why. */
22
- export function documentOf(spec: unknown, where: string): Doc {
23
- const loaded = loadDocument(spec);
24
- if (!loaded.ok) {
25
- const reason = loaded.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
26
- // Not retryable: the same stored document fails the same way until someone repairs it.
27
- throw new HttpError(
28
- 500,
29
- "INTERNAL",
30
- `The stored document of ${where} cannot be read: ${reason}`,
31
- );
32
- }
33
- return loaded.doc;
34
- }
35
-
36
- /** A tenant's dashboard row by id or key, published or not, never archived; null when absent. */
37
- export async function dashboardRow(
38
- db: DashAdapter,
39
- tenant: Tenant,
40
- by: { id: string } | { key: string },
41
- ): Promise<Row | null> {
42
- const field = "id" in by ? "id" : "key";
43
- const row = await db.findOne({
44
- table: "dd_dashboard",
45
- where: [
46
- { field: "tenant_hash", value: await tenantHash(tenant) },
47
- { field: "archived_at", op: "isNull" },
48
- { field, value: "id" in by ? by.id : by.key },
49
- ],
50
- });
51
- return row !== null && sameTenant(row["tenant"], tenant) ? row : null;
52
- }
53
-
54
- /** A dashboard's document at its current version; a built-in or new one has none yet. */
55
- export async function headDoc(db: DashAdapter, row: Row): Promise<{ version: number; doc?: Doc }> {
56
- const version = row["current_version"] === null ? 0 : Number(row["current_version"]);
57
- if (version === 0) return { version };
58
- const stored = await db.findOne({
59
- table: "dd_dashboard_version",
60
- where: [
61
- { field: "dashboard_id", value: row["id"] },
62
- { field: "n", value: version },
63
- ],
64
- });
65
- return { version, doc: documentOf(stored?.["spec"], `dashboard "${String(row["id"])}"`) };
66
- }
67
-
68
- /** A document's hash, by its canonical JSON. */
69
- export const docDigest = (doc: Doc) => sha256(canonical(doc));
70
-
71
- /** A new dashboard row, unpublished: it serves nothing until its first publish. */
72
- export async function insertDashboard(
73
- db: DashAdapter,
74
- a: { tenant: Tenant; author: Author; doc: Doc; key?: string; requestId?: string },
75
- ): Promise<Row> {
76
- const digest = a.requestId === undefined ? null : await docDigest(a.doc);
77
- const at = now();
78
- const data: Row = {
79
- id: crypto.randomUUID(),
80
- tenant: a.tenant,
81
- tenant_hash: await tenantHash(a.tenant),
82
- key: a.key ?? null,
83
- title: a.doc.meta.title,
84
- description: a.doc.meta.description ?? null,
85
- owner_id: a.author.id,
86
- owner_kind: a.author.kind ?? "user",
87
- current_version: null,
88
- default_of: a.key ?? null,
89
- request_id: a.requestId ?? null,
90
- request_digest: digest,
91
- archived_at: null,
92
- created_at: at,
93
- updated_at: at,
94
- };
95
- await db.create({ table: "dd_dashboard", data });
96
- return data;
97
- }
98
-
99
- /**
100
- * The id a draft row is kept under. An agent keeps one draft per dashboard for each person it
101
- * acts for, so its work for one person never mixes with its work for another. The draft's unique
102
- * key is (dashboard, author kind, author id), so an agent's id carries the person, length
103
- * first, which no two pairs of ids share. The person is also kept in `for_id`.
104
- */
105
- export function draftAuthorId(author: Author): string {
106
- return author.kind === "agent" && author.for !== undefined
107
- ? `${String(author.for.length)}:${author.for}:${author.id}`
108
- : author.id;
109
- }
110
-
111
- /** The id an author is known by, from the id their draft row is kept under. */
112
- function authorIdOf(row: Row): string {
113
- const key = String(row["author_id"]);
114
- const person = row["for_id"];
115
- if (row["author_kind"] !== "agent" || typeof person !== "string") return key;
116
- return key.slice(`${String(person.length)}:${person}:`.length);
117
- }
118
-
119
- /** The author's draft row of a dashboard, open or closed; null when there has never been one. */
120
- export function draftRow(
121
- db: DashAdapter,
122
- dashboardId: string,
123
- author: Author,
124
- ): Promise<Row | null> {
125
- return db.findOne({
126
- table: "dd_dashboard_draft",
127
- where: [
128
- { field: "dashboard_id", value: dashboardId },
129
- { field: "author_kind", value: author.kind ?? "user" },
130
- { field: "author_id", value: draftAuthorId(author) },
131
- ],
132
- });
133
- }
134
-
135
- /** The document a draft holds now. */
136
- export const draftDoc = (row: Row): Doc =>
137
- documentOf(row["spec"], `the draft of "${String(row["dashboard_id"])}"`);
138
-
139
- /** The document a draft started from: the draft is this with its commands applied. */
140
- export const baseDoc = (row: Row): Doc =>
141
- documentOf(row["base_spec"], `the start of the draft of "${String(row["dashboard_id"])}"`);
142
-
143
- /** The document a draft first started from, once review moved it onto a newer version; else none. */
144
- export const originDoc = (row: Row): Doc | undefined =>
145
- row["origin_spec"] === null || row["origin_spec"] === undefined
146
- ? undefined
147
- : documentOf(
148
- row["origin_spec"],
149
- `the first start of the draft of "${String(row["dashboard_id"])}"`,
150
- );
151
-
152
- /** Who a draft row names in a column pair, if anyone. */
153
- export function authorIn(row: Row, prefix: string): Author | undefined {
154
- const id = row[`${prefix}_id`];
155
- if (typeof id !== "string") return undefined;
156
- return { id, kind: row[`${prefix}_kind`] === "agent" ? "agent" : "user" };
157
- }
158
-
159
- /** The widgets a draft row says its commands name. */
160
- export const namedOf = (row: Row): Set<string> =>
161
- new Set(Array.isArray(row["named"]) ? row["named"].map(String) : []);
162
-
163
- /** A draft row as the client reads it: its document, changes and undo steps, from the row alone. */
164
- export function draftOf(row: Row): Draft {
165
- const doc = draftDoc(row);
166
- return {
167
- dashboardId: String(row["dashboard_id"]),
168
- authorId: authorIdOf(row),
169
- generation: Number(row["generation"]),
170
- revision: Number(row["revision"]),
171
- baseVersion: Number(row["base_version"]),
172
- doc,
173
- changes: markShifted(diffDocs(baseDoc(row), doc), namedOf(row)),
174
- steps: Number(row["steps"]),
175
- };
176
- }
177
-
178
- /** The refusal for an edit made against another revision of the draft: the one wording of it. */
179
- const draftConflict = () =>
180
- new HttpError(
181
- 409,
182
- "DRAFT_CONFLICT",
183
- "The draft changed since this edit was made. Reload the draft and try again.",
184
- );
185
-
186
- /** The refusal for an edit to a draft that is no longer open: the one wording of it. */
187
- export const draftClosed = () =>
188
- new HttpError(
189
- 409,
190
- "DRAFT_CLOSED",
191
- "There is no open draft of this dashboard. Start editing again.",
192
- );
193
-
194
- /** The author's open draft, or a refusal that says the edit is over. */
195
- export async function openDraft(
196
- db: DashAdapter,
197
- a: { tenant: Tenant; id: string; author: Author },
198
- ): Promise<{ row: Row; dashboard: Row }> {
199
- const dashboard = await dashboardRow(db, a.tenant, { id: a.id });
200
- const row = dashboard === null ? null : await draftRow(db, a.id, a.author);
201
- if (dashboard === null || row === null || row["state"] !== "open") throw draftClosed();
202
- return { row, dashboard };
203
- }
204
-
205
- /** Refuses an edit made against another revision of the draft than the current one. */
206
- export function expectRevision(row: Row, expected: number): void {
207
- expectDraftRevision(Number(row["revision"]), expected);
208
- }
209
-
210
- /** Refuses an edit made against another revision than `current`, with the one message for it. */
211
- export function expectDraftRevision(current: number | undefined, expected: number): void {
212
- if (current !== expected) throw draftConflict();
213
- }
214
-
215
- const ATTEMPTS = 3;
216
-
217
- /**
218
- * Runs a write as one transaction, again when it lost a race: another writer took the same
219
- * unique key (a version number, a draft, a command id) or the database could not serialize
220
- * the two. The next attempt reads what the winner wrote, so it acknowledges, replays or
221
- * refuses with a reason. Past the attempts, the request is answered as retryable.
222
- */
223
- export async function inTransaction<T>(
224
- db: DashAdapter,
225
- work: (tx: DashAdapter) => Promise<T>,
226
- ): Promise<T> {
227
- for (let attempt = 1; ; attempt += 1) {
228
- try {
229
- // oxlint-disable-next-line no-await-in-loop -- each attempt waits for the one before
230
- return await db.transaction(work);
231
- } catch (error) {
232
- if (db.classifyError(error) === "other") throw error;
233
- if (attempt === ATTEMPTS)
234
- throw new HttpError(503, "RETRYABLE", "Others were editing at the same moment. Try again.");
235
- }
236
- }
237
- }
238
-
239
- /** Moves the draft to its next revision only if no other edit got there first. */
240
- export async function advanceDraft(tx: DashAdapter, row: Row, set: Row): Promise<void> {
241
- const moved = await tx.updateMany({
242
- table: "dd_dashboard_draft",
243
- where: [
244
- { field: "id", value: row["id"] },
245
- { field: "revision", value: row["revision"] },
246
- ],
247
- set,
248
- });
249
- if (moved !== 1) throw draftConflict();
250
- }
@@ -1,28 +0,0 @@
1
- import { canonical } from "@derive-to/dashboards-core";
2
- import { sha256 } from "../cache";
3
-
4
- /**
5
- * One column for a composite tenant: SHA-256 over its canonical JSON (RFC 8785, keys sorted),
6
- * so no two tenants share a hash by how their parts happen to concatenate. Lookups compare
7
- * the stored tenant JSON as well, so a collision could never cross tenants.
8
- */
9
- export function tenantHash(tenant: Record<string, string>): Promise<string> {
10
- return sha256(canonical(tenant));
11
- }
12
-
13
- /** Whether a stored tenant column is exactly this tenant: the same keys, the same values. */
14
- export function sameTenant(stored: unknown, tenant: Record<string, string>): boolean {
15
- if (typeof stored !== "object" || stored === null) return false;
16
- const entries = Object.entries(stored);
17
- return (
18
- entries.length === Object.keys(tenant).length && entries.every(([k, v]) => tenant[k] === v)
19
- );
20
- }
21
-
22
- /** A stored tenant column as a tenant: its string values only. */
23
- export function tenantOf(stored: unknown): Record<string, string> {
24
- if (typeof stored !== "object" || stored === null) return {};
25
- return Object.fromEntries(
26
- Object.entries(stored).filter((e): e is [string, string] => typeof e[1] === "string"),
27
- );
28
- }