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,95 +0,0 @@
1
- import {
2
- CATALOG,
3
- type DashAdapter,
4
- type Row,
5
- type StoreDialect,
6
- type TableName,
7
- } from "@derive-to/dashboards-core";
8
- import {
9
- count,
10
- decodeRow,
11
- insert,
12
- insertAll,
13
- remove,
14
- type Statement,
15
- select,
16
- update,
17
- } from "./statement";
18
-
19
- /** What a database store supplies: how to run a statement, a transaction and DDL. */
20
- export type SqlRunner = {
21
- readonly dialect: StoreDialect;
22
- /** Rows as objects keyed by column name. */
23
- run(statement: Statement): Promise<Row[]>;
24
- /** Runs `fn` in one transaction with rollback, on a connection nothing else uses meanwhile. */
25
- transaction<T>(fn: (runner: SqlRunner) => Promise<T>): Promise<T>;
26
- execute(ddl: string): Promise<void>;
27
- };
28
-
29
- /** Postgres's codes for a unique violation and a failed serializable or deadlocked transaction. */
30
- const UNIQUE = new Set(["23505", "SQLITE_CONSTRAINT_UNIQUE", "SQLITE_CONSTRAINT_PRIMARYKEY"]);
31
- const SERIALIZATION = new Set(["40001", "40P01", "SQLITE_BUSY"]);
32
-
33
- /** A catalog name, which a driver returns as text. */
34
- const text = (value: unknown): string => (typeof value === "string" ? value : "");
35
-
36
- function codeOf(error: unknown): string | undefined {
37
- if (typeof error !== "object" || error === null) return undefined;
38
- if ("code" in error && typeof error.code === "string") return error.code;
39
- return "cause" in error ? codeOf(error.cause) : undefined;
40
- }
41
-
42
- /**
43
- * The store adapter over any SQL database: the library's reads and writes on its own tables as
44
- * parameterized statements. A database's store only says how to run a statement, a transaction,
45
- * and DDL.
46
- */
47
- export function sqlAdapter(id: string, runner: SqlRunner): DashAdapter {
48
- return adapterOver(id, runner);
49
- }
50
-
51
- function adapterOver(id: string, runner: SqlRunner): DashAdapter {
52
- const { dialect } = runner;
53
- const rows = async (table: TableName, statement: Statement) =>
54
- (await runner.run(statement)).map((row) => decodeRow(table, row));
55
- return {
56
- id,
57
- async createMany({ table, rows: data }) {
58
- if (data.length > 0) await runner.run(insertAll(dialect, table, data));
59
- },
60
- async create({ table, data }) {
61
- await runner.run(insert(dialect, table, data));
62
- },
63
- async findOne({ table, where }) {
64
- const [first] = await rows(table, select(dialect, { table, where, limit: 1 }));
65
- return first ?? null;
66
- },
67
- findMany: (a) => rows(a.table, select(dialect, a)),
68
- async updateMany({ table, where, set }) {
69
- return (await runner.run(update(dialect, table, where, set))).length;
70
- },
71
- async deleteMany({ table, where }) {
72
- return (await runner.run(remove(dialect, table, where))).length;
73
- },
74
- async count({ table, where }) {
75
- const [row] = await runner.run(count(dialect, table, where ?? []));
76
- return Number(row?.["n"] ?? 0);
77
- },
78
- transaction: (fn) => runner.transaction((tx) => fn(adapterOver(id, tx))),
79
- classifyError(error) {
80
- const code = codeOf(error) ?? "";
81
- if (UNIQUE.has(code)) return "unique";
82
- return SERIALIZATION.has(code) ? "serialization" : "other";
83
- },
84
- schema: {
85
- dialect,
86
- execute: (ddl) => runner.execute(ddl),
87
- columns: async () =>
88
- (await runner.run({ strings: [CATALOG[dialect]], values: [] })).map((row) => ({
89
- table: text(row["table"]),
90
- column: text(row["column"]),
91
- ...(row["schema"] === undefined ? {} : { schema: text(row["schema"]) }),
92
- })),
93
- },
94
- };
95
- }
@@ -1,185 +0,0 @@
1
- import {
2
- type FieldType,
3
- type Row,
4
- type StoreDialect,
5
- TABLES,
6
- type TableName,
7
- type Where,
8
- } from "@derive-to/dashboards-core";
9
-
10
- /**
11
- * A statement: the text around the values, and the values, which are always parameters. Table
12
- * and column names come only from `TABLES` and are checked against it. Updates and deletes
13
- * return one row per row they changed, so the count needs nothing from the driver.
14
- */
15
- export type Statement = { strings: readonly string[]; values: readonly unknown[] };
16
-
17
- /** Builds a statement piece by piece: text is appended, values become parameters. */
18
- class Builder {
19
- readonly #strings: string[] = [""];
20
- readonly #values: unknown[] = [];
21
- text(part: string): this {
22
- this.#strings[this.#strings.length - 1] += part;
23
- return this;
24
- }
25
- value(v: unknown): this {
26
- this.#values.push(v);
27
- this.#strings.push("");
28
- return this;
29
- }
30
- build(): Statement {
31
- return { strings: [...this.#strings], values: [...this.#values] };
32
- }
33
- }
34
-
35
- /** The table a statement is about, and the dialect it is written in. */
36
- type On = { dialect: StoreDialect; table: TableName };
37
-
38
- const quote = (name: string) => `"${name.replaceAll('"', '""')}"`;
39
-
40
- function typeOf(table: TableName, field: string): FieldType | undefined {
41
- const fields: Record<string, { type: FieldType }> = TABLES[table].fields;
42
- return fields[field]?.type;
43
- }
44
-
45
- /** A column of the table, quoted; any other name is refused before it reaches SQL text. */
46
- function column(on: On, field: string): string {
47
- if (typeOf(on.table, field) === undefined)
48
- throw new Error(`"${on.table}" has no field "${field}".`);
49
- return quote(field);
50
- }
51
-
52
- /** A value as the column stores it: JSON as text, cast by the statement on Postgres. */
53
- function param(b: Builder, on: On, field: string, value: unknown): Builder {
54
- const type = typeOf(on.table, field);
55
- if (value === null || value === undefined) return b.value(null);
56
- if (type === "boolean") return b.value(value === true);
57
- if (type !== "json") return b.value(value);
58
- const text = JSON.stringify(value);
59
- return on.dialect === "postgres" ? b.text("CAST(").value(text).text(" AS jsonb)") : b.value(text);
60
- }
61
-
62
- const OPS = { eq: "=", ne: "<>", lt: "<", lte: "<=", gt: ">", gte: ">=" } as const;
63
-
64
- function inList(b: Builder, on: On, c: Where): void {
65
- const list: readonly unknown[] = Array.isArray(c.value) ? c.value : [];
66
- if (list.length === 0) {
67
- b.text("FALSE");
68
- return;
69
- }
70
- b.text(`${column(on, c.field)} IN (`);
71
- for (const [i, v] of list.entries()) param(i === 0 ? b : b.text(", "), on, c.field, v);
72
- b.text(")");
73
- }
74
-
75
- function condition(b: Builder, on: On, c: Where): void {
76
- const op = c.op ?? "eq";
77
- const col = column(on, c.field);
78
- if (op === "isNull") b.text(`${col} IS NULL`);
79
- else if (op === "notNull") b.text(`${col} IS NOT NULL`);
80
- else if (op === "in") inList(b, on, c);
81
- else param(b.text(`${col} ${OPS[op]} `), on, c.field, c.value);
82
- }
83
-
84
- function where(b: Builder, on: On, conditions: readonly Where[]): Builder {
85
- for (const [i, c] of conditions.entries())
86
- condition(b.text(i === 0 ? " WHERE " : " AND "), on, c);
87
- return b;
88
- }
89
-
90
- export function insert(dialect: StoreDialect, table: TableName, data: Row): Statement {
91
- return insertAll(dialect, table, [data]);
92
- }
93
-
94
- /** One INSERT of every row, each with the first row's fields: one round trip for a batch. */
95
- export function insertAll(
96
- dialect: StoreDialect,
97
- table: TableName,
98
- rows: readonly Row[],
99
- ): Statement {
100
- const on = { dialect, table };
101
- const fields = Object.keys(rows[0] ?? {});
102
- const b = new Builder().text(
103
- `INSERT INTO ${quote(table)} (${fields.map((f) => column(on, f)).join(", ")}) VALUES `,
104
- );
105
- for (const [r, data] of rows.entries()) {
106
- b.text(r === 0 ? "(" : ", (");
107
- for (const [i, f] of fields.entries()) param(i === 0 ? b : b.text(", "), on, f, data[f]);
108
- b.text(")");
109
- }
110
- return b.build();
111
- }
112
-
113
- export type Select = {
114
- table: TableName;
115
- where?: readonly Where[];
116
- orderBy?: readonly { field: string; direction: "asc" | "desc" }[];
117
- limit?: number;
118
- offset?: number;
119
- };
120
-
121
- export function select(dialect: StoreDialect, s: Select): Statement {
122
- const on = { dialect, table: s.table };
123
- const b = where(new Builder().text(`SELECT * FROM ${quote(s.table)}`), on, s.where ?? []);
124
- const order = (s.orderBy ?? []).map(
125
- (o) => `${column(on, o.field)} ${o.direction === "asc" ? "ASC" : "DESC"}`,
126
- );
127
- if (order.length > 0) b.text(` ORDER BY ${order.join(", ")}`);
128
- if (s.limit !== undefined) b.text(" LIMIT ").value(s.limit);
129
- if (s.offset !== undefined) b.text(" OFFSET ").value(s.offset);
130
- return b.build();
131
- }
132
-
133
- export function update(
134
- dialect: StoreDialect,
135
- table: TableName,
136
- conditions: readonly Where[],
137
- set: Row,
138
- ): Statement {
139
- const on = { dialect, table };
140
- const b = new Builder().text(`UPDATE ${quote(table)} SET `);
141
- for (const [i, f] of Object.keys(set).entries())
142
- param(b.text(`${i === 0 ? "" : ", "}${column(on, f)} = `), on, f, set[f]);
143
- return where(b, on, conditions).text(" RETURNING 1").build();
144
- }
145
-
146
- export function remove(
147
- dialect: StoreDialect,
148
- table: TableName,
149
- conditions: readonly Where[],
150
- ): Statement {
151
- const on = { dialect, table };
152
- const b = new Builder().text(`DELETE FROM ${quote(table)}`);
153
- return where(b, on, conditions).text(" RETURNING 1").build();
154
- }
155
-
156
- export function count(
157
- dialect: StoreDialect,
158
- table: TableName,
159
- conditions: readonly Where[],
160
- ): Statement {
161
- const on = { dialect, table };
162
- const b = new Builder().text(`SELECT count(*) AS n FROM ${quote(table)}`);
163
- return where(b, on, conditions).build();
164
- }
165
-
166
- function timestamp(value: unknown): string {
167
- if (value instanceof Date) return value.toISOString();
168
- if (typeof value === "string" || typeof value === "number") return new Date(value).toISOString();
169
- throw new TypeError("A timestamp column returned something that is not a time.");
170
- }
171
-
172
- function decodeValue(type: FieldType | undefined, value: unknown): unknown {
173
- if (value === null || value === undefined) return null;
174
- if (type === "integer") return Number(value);
175
- if (type === "boolean") return value === true || value === 1;
176
- if (type === "json") return typeof value === "string" ? (JSON.parse(value) as unknown) : value;
177
- return type === "timestamp" ? timestamp(value) : value;
178
- }
179
-
180
- /** A row as the store reads it: integers as numbers, JSON parsed, timestamps as ISO text. */
181
- export function decodeRow(table: TableName, raw: Row): Row {
182
- return Object.fromEntries(
183
- Object.entries(raw).map(([name, value]) => [name, decodeValue(typeOf(table, name), value)]),
184
- );
185
- }
package/src/authorize.ts DELETED
@@ -1,73 +0,0 @@
1
- import {
2
- type DashOptions,
3
- type Principal,
4
- parseMemberId,
5
- queryClosure,
6
- type Viewer,
7
- type WireQuery,
8
- } from "@derive-to/dashboards-core";
9
- import { HttpError } from "./http";
10
-
11
- /** Whether the model marks a closure member (`source.name`) sensitive. */
12
- function isSensitive(options: DashOptions, id: string): boolean {
13
- const ref = parseMemberId(id);
14
- if (ref === undefined) return false;
15
- const source = options.model.sources[ref.source];
16
- return (source?.dimensions[ref.name] ?? source?.measures[ref.name])?.sensitive === true;
17
- }
18
-
19
- /** A member the model marks sensitive is refused unless the host's `sensitive` says yes. */
20
- function sensitiveAllowed(options: DashOptions, viewer: Viewer | undefined, id: string): boolean {
21
- if (!isSensitive(options, id)) return true;
22
- return viewer !== undefined && options.access.sensitive?.(viewer, id) === true;
23
- }
24
-
25
- /**
26
- * The closure of a query, refused with every problem when it is not valid. The message is the
27
- * first problem itself, so a person or an agent reads the fix without opening the issues.
28
- */
29
- function closureOf(options: DashOptions, query: WireQuery) {
30
- const closure = queryClosure(options.model, query);
31
- const [first, ...rest] = closure.issues;
32
- if (first !== undefined) {
33
- const more = rest.length === 0 ? "" : ` (and ${rest.length} more)`;
34
- throw new HttpError(400, "QUERY_INVALID", `${first.message}${more}`, {
35
- issues: closure.issues,
36
- });
37
- }
38
- return closure;
39
- }
40
-
41
- /**
42
- * Every form of query, by reference or free: no sensitive member unless the host allows it for
43
- * this viewer. A principal with no viewer (a public link) never reads one.
44
- */
45
- export function checkSensitive(options: DashOptions, principal: Principal, query: WireQuery): void {
46
- const viewer = principal.channel === "user" ? principal.viewer : undefined;
47
- const refused = closureOf(options, query).members.find(
48
- (id) => !sensitiveAllowed(options, viewer, id),
49
- );
50
- if (refused !== undefined) throw new HttpError(403, "FORBIDDEN", `"${refused}" is sensitive.`);
51
- }
52
-
53
- /**
54
- * A free query (the widget builder's preview, an agent's question) is a different grant from viewing a dashboard: the
55
- * viewer needs `view` on every source and every member it reads.
56
- */
57
- export async function authorizeFree(
58
- options: DashOptions,
59
- viewer: Viewer,
60
- query: WireQuery,
61
- ): Promise<void> {
62
- const closure = closureOf(options, query);
63
- const checks = [
64
- ...closure.sources.map((id) => ({ kind: "source" as const, id })),
65
- ...closure.members.map((id) => ({ kind: "member" as const, id })),
66
- ];
67
- const answers = await Promise.all(
68
- checks.map((r) => Promise.resolve(options.access.can(viewer, "view", r))),
69
- );
70
- const refused = checks.find((_r, i) => answers[i] !== true);
71
- if (refused !== undefined)
72
- throw new HttpError(403, "FORBIDDEN", `You cannot read the ${refused.kind} "${refused.id}".`);
73
- }
package/src/body.ts DELETED
@@ -1,169 +0,0 @@
1
- import type { Command } from "@derive-to/dashboards-core";
2
- import {
3
- commandSchema,
4
- type JsonSchema,
5
- jsonSchema,
6
- picksSchema,
7
- wireQuerySchema,
8
- } from "@derive-to/dashboards-core/schema";
9
- import { z } from "zod";
10
- import { HttpError } from "./http";
11
-
12
- /** The request bodies every endpoint checks before anything reads them. Unknown keys are refused. */
13
-
14
- const id = z.string().min(1).max(200);
15
- const picks = picksSchema;
16
-
17
- /** One edit command, typed as the contract's `Command`, which the schema satisfies. */
18
- const commands: z.ZodType<Command> = commandSchema;
19
-
20
- /** A page of rows: after which row it starts (the `next` of the page before) and how many. */
21
- const page = {
22
- after: z.string().min(1).max(4000).exactOptional(),
23
- limit: z.int().min(1).max(1000).exactOptional(),
24
- };
25
-
26
- /** A tile by reference: the dashboard, the widget, which of its queries, and the viewer's picks. */
27
- const tileRef = {
28
- dashboardId: id,
29
- widgetId: id,
30
- query: z.string().max(100).exactOptional(),
31
- picks: picks.exactOptional(),
32
- /** The version the client is showing; a different current version is a 409. */
33
- version: z.int().min(0).exactOptional(),
34
- /** Run against the viewer's own open draft instead: needs `edit`. */
35
- draft: z.literal(true).exactOptional(),
36
- /** Run against what publishing the viewer's draft would make now: needs `edit`. */
37
- merged: z.literal(true).exactOptional(),
38
- /** Run against an earlier published version, to preview it: needs `edit`. */
39
- at: z.int().min(1).exactOptional(),
40
- };
41
-
42
- export const bodies = {
43
- dashboard: z.strictObject({ dashboardId: id }),
44
- tile: z.strictObject(tileRef),
45
- tiles: z.strictObject({
46
- dashboardId: id,
47
- version: z.int().min(0).exactOptional(),
48
- tiles: z
49
- .array(z.strictObject({ widgetId: id, query: z.string().max(100).exactOptional() }))
50
- .min(1)
51
- .max(50),
52
- picks: picks.exactOptional(),
53
- draft: z.literal(true).exactOptional(),
54
- merged: z.literal(true).exactOptional(),
55
- at: z.int().min(1).exactOptional(),
56
- }),
57
- records: z.strictObject({ ...tileRef, ...page }),
58
- controlValues: z.strictObject({
59
- dashboardId: id,
60
- controlId: id,
61
- search: z.string().max(200).exactOptional(),
62
- limit: page.limit,
63
- draft: z.literal(true).exactOptional(),
64
- }),
65
- memberValues: z.strictObject({
66
- member: id,
67
- search: z.string().max(200).exactOptional(),
68
- limit: page.limit,
69
- }),
70
- query: z.strictObject({ query: wireQuerySchema }),
71
- queryBatch: z.strictObject({ queries: z.array(wireQuerySchema).min(1).max(50) }),
72
- queryRecords: z.strictObject({ query: wireQuerySchema, ...page }),
73
- list: z.strictObject({}),
74
- newDashboard: z.strictObject({
75
- title: z.string().min(1).max(500),
76
- /** The client's id for this create, so a retry makes one dashboard. */
77
- requestId: z.string().min(1).max(200).exactOptional(),
78
- /** Start from a published version of another dashboard, as a copy. */
79
- from: z.strictObject({ dashboardId: id, version: z.int().min(1) }).exactOptional(),
80
- }),
81
- applyDraft: z.strictObject({
82
- dashboardId: id,
83
- expectedRevision: z.int().min(0),
84
- commands: z.array(commands).min(1).max(100).readonly(),
85
- }),
86
- publishDraft: z.strictObject({
87
- dashboardId: id,
88
- expectedRevision: z.int().min(0),
89
- requestId: id,
90
- /** The version the review was on top of: another one published since refuses it. */
91
- head: z.int().min(0),
92
- }),
93
- undo: z.strictObject({ dashboardId: id, expectedRevision: z.int().min(0) }),
94
- rebase: z.strictObject({
95
- dashboardId: id,
96
- expectedRevision: z.int().min(0),
97
- head: z.int().min(0),
98
- drop: z.array(id).max(100).readonly().exactOptional(),
99
- restore: z.array(id).max(100).readonly().exactOptional(),
100
- }),
101
- version: z.strictObject({ dashboardId: id, version: z.int().min(1) }),
102
- versions: z.strictObject({ dashboardId: id, before: z.int().min(1).exactOptional() }),
103
- restore: z.strictObject({
104
- dashboardId: id,
105
- version: z.number().int().min(1),
106
- requestId: z.string().min(1).max(200),
107
- }),
108
- discardDraft: z.strictObject({ dashboardId: id, expectedRevision: z.int().min(0) }),
109
- };
110
-
111
- /** Every endpoint that takes a body, by path, and the body it takes; a test holds it complete. */
112
- const BODIES_BY_PATH = {
113
- "/dashboards/get": bodies.dashboard,
114
- "/dashboards/tile": bodies.tile,
115
- "/dashboards/tiles": bodies.tiles,
116
- "/dashboards/records": bodies.records,
117
- "/dashboards/control-values": bodies.controlValues,
118
- "/dashboards/new": bodies.newDashboard,
119
- "/dashboards/draft/start": bodies.dashboard,
120
- "/dashboards/draft/apply": bodies.applyDraft,
121
- "/dashboards/draft/undo": bodies.undo,
122
- "/dashboards/draft/preview": bodies.dashboard,
123
- "/dashboards/draft/rebase": bodies.rebase,
124
- "/dashboards/draft/publish": bodies.publishDraft,
125
- "/dashboards/draft/discard": bodies.discardDraft,
126
- "/dashboards/versions": bodies.versions,
127
- "/dashboards/version": bodies.version,
128
- "/dashboards/restore": bodies.restore,
129
- "/query/run": bodies.query,
130
- "/query/validate": bodies.query,
131
- "/query/batch": bodies.queryBatch,
132
- "/query/records": bodies.queryRecords,
133
- "/meta/values": bodies.memberValues,
134
- } as const;
135
-
136
- /** Each endpoint's body as JSON Schema, from the same schemas the endpoints check with. */
137
- export function bodySchemas(): Record<string, JsonSchema> {
138
- return Object.fromEntries(
139
- Object.entries(BODIES_BY_PATH).map(([path, schema]): [string, JsonSchema] => [
140
- path,
141
- jsonSchema(schema),
142
- ]),
143
- );
144
- }
145
-
146
- /** Each problem with a body, as one line a person can act on: where it is, and what is wrong. */
147
- function described(error: z.ZodError): string {
148
- return error.issues
149
- .map((issue) => {
150
- const where = issue.path.map(String).join(".");
151
- const message = /[.!?]$/u.test(issue.message) ? issue.message : `${issue.message}.`;
152
- return where === "" ? message : `${where}: ${message}`;
153
- })
154
- .join(" ");
155
- }
156
-
157
- /**
158
- * Checks a body against its schema; a body it does not accept is a 400 that says where and why.
159
- * A free query's body is refused as `QUERY_INVALID`, any other as `BODY_INVALID`.
160
- */
161
- export function parseWith<T>(
162
- schema: z.ZodType<T>,
163
- input: unknown,
164
- code: "BODY_INVALID" | "QUERY_INVALID" = "BODY_INVALID",
165
- ): T {
166
- const parsed = schema.safeParse(input);
167
- if (!parsed.success) throw new HttpError(400, code, described(parsed.error));
168
- return parsed.data;
169
- }
package/src/bounded.ts DELETED
@@ -1,43 +0,0 @@
1
- import type { RunOptions } from "@derive-to/dashboards-core";
2
- import { HttpError } from "./http";
3
-
4
- export const pastDeadline = () =>
5
- new HttpError(504, "QUERY_TIMEOUT", "The query ran past its deadline.");
6
-
7
- /** Work's result, or QUERY_TIMEOUT at the deadline and the abort's reason on abort. */
8
- export async function bounded<T>(
9
- work: Promise<T>,
10
- options: Pick<RunOptions, "signal" | "deadline">,
11
- ): Promise<T> {
12
- let timer: ReturnType<typeof setTimeout> | undefined;
13
- let onAbort: (() => void) | undefined;
14
- const stop = new Promise<never>((_, reject) => {
15
- timer = setTimeout(
16
- () => {
17
- reject(pastDeadline());
18
- },
19
- Math.max(0, options.deadline - Date.now()),
20
- );
21
- onAbort = () => {
22
- const reason: unknown = options.signal.reason;
23
- reject(reason instanceof Error ? reason : new Error("The request was aborted."));
24
- };
25
- options.signal.addEventListener("abort", onAbort, { once: true });
26
- });
27
- try {
28
- return await Promise.race([work, stop]);
29
- } finally {
30
- clearTimeout(timer);
31
- if (onAbort !== undefined) options.signal.removeEventListener("abort", onAbort);
32
- }
33
- }
34
-
35
- /** Starts `work` only when the request is still wanted and inside its deadline, and bounds it. */
36
- export function boundedCall<T>(
37
- options: Pick<RunOptions, "signal" | "deadline">,
38
- work: () => Promise<T>,
39
- ): Promise<T> {
40
- options.signal.throwIfAborted();
41
- if (Date.now() >= options.deadline) throw pastDeadline();
42
- return bounded(work(), options);
43
- }