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.
- package/README.md +8 -5
- package/dist/adapter/sql-adapter.mjs +4 -4
- package/dist/body.mjs +11 -1
- package/dist/cache.d.mts +1 -1
- package/dist/cli/add.mjs +130 -0
- package/dist/cli/check.mjs +14 -1
- package/dist/cli/cli.mjs +14 -12
- package/dist/cli/config.mjs +12 -1
- package/dist/cli/doctor.mjs +147 -0
- package/dist/cli/import/dbt-schema.mjs +2 -2
- package/dist/cli/import/index.mjs +2 -2
- package/dist/cli/init.mjs +3 -2
- package/dist/cli/json.mjs +21 -0
- package/dist/cli/layers.mjs +33 -0
- package/dist/core/conformance/cases.d.mts +3 -0
- package/dist/core/conformance/cases.mjs +20 -4
- package/dist/core/conformance/check.mjs +6 -3
- package/dist/core/conformance/drill-cases.mjs +120 -0
- package/dist/core/document/layout.mjs +1 -1
- package/dist/core/document/view-layout.d.mts +14 -0
- package/dist/core/document/view-layout.mjs +157 -0
- package/dist/core/document/view.d.mts +26 -7
- package/dist/core/document/view.mjs +42 -11
- package/dist/core/engine/filter.mjs +2 -2
- package/dist/core/engine/index.d.mts +14 -2
- package/dist/core/engine/index.mjs +12 -1
- package/dist/core/engine/leaf.mjs +9 -4
- package/dist/core/engine/members.mjs +2 -2
- package/dist/core/engine/postgres.mjs +1 -1
- package/dist/core/engine/record-layout.d.mts +11 -0
- package/dist/core/engine/record-layout.mjs +7 -7
- package/dist/core/engine/records.d.mts +3 -2
- package/dist/core/engine/records.mjs +2 -1
- package/dist/core/engine/segmented.mjs +5 -2
- package/dist/core/engine/sql-records.mjs +2 -1
- package/dist/core/engine/statement.mjs +37 -3
- package/dist/core/engine/types.d.mts +3 -0
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +33 -1
- package/dist/core/error/codes.mjs +30 -2
- package/dist/core/index.d.mts +13 -7
- package/dist/core/index.mjs +9 -5
- package/dist/core/model/catalog/check.mjs +1 -1
- package/dist/core/model/hints.d.mts +36 -0
- package/dist/core/model/member.d.mts +4 -2
- package/dist/core/model/member.mjs +19 -3
- package/dist/core/model/types.d.mts +21 -19
- package/dist/core/model/validate/drill.mjs +70 -0
- package/dist/core/model/validate/index.mjs +3 -0
- package/dist/core/model/validate/members.mjs +7 -4
- package/dist/core/model/validate/shape.mjs +150 -0
- package/dist/core/model/validate/source.mjs +5 -3
- package/dist/core/policy/bind-query.d.mts +12 -0
- package/dist/core/policy/bind-query.mjs +53 -0
- package/dist/core/query/alias.mjs +7 -1
- package/dist/core/query/drill/compose.d.mts +22 -0
- package/dist/core/query/drill/compose.mjs +85 -0
- package/dist/core/query/drill/fields.d.mts +21 -0
- package/dist/core/query/drill/fields.mjs +91 -0
- package/dist/core/query/drill/internal.d.mts +11 -0
- package/dist/core/query/drill/items.d.mts +29 -0
- package/dist/core/query/drill/items.mjs +98 -0
- package/dist/core/query/drill/line.d.mts +31 -0
- package/dist/core/query/drill/line.mjs +183 -0
- package/dist/core/query/drill/links.d.mts +33 -0
- package/dist/core/query/drill/links.mjs +31 -0
- package/dist/core/query/drill/operand.mjs +7 -0
- package/dist/core/query/drill/scopes.d.mts +22 -0
- package/dist/core/query/drill/scopes.mjs +58 -0
- package/dist/core/query/drill/selection.d.mts +37 -0
- package/dist/core/query/drill/selection.mjs +144 -0
- package/dist/core/query/drill/steps.d.mts +14 -0
- package/dist/core/query/drill/steps.mjs +35 -0
- package/dist/core/query/drill/synthetic-expr.mjs +85 -0
- package/dist/core/query/drill/synthetic.d.mts +22 -0
- package/dist/core/query/drill/synthetic.mjs +165 -0
- package/dist/core/query/drill/time.d.mts +11 -0
- package/dist/core/query/drill/time.mjs +71 -0
- package/dist/core/query/drill/types.d.mts +155 -0
- package/dist/core/query/drill/types.mjs +4 -0
- package/dist/core/query/drill/views.d.mts +62 -0
- package/dist/core/query/drill/views.mjs +257 -0
- package/dist/core/query/filter.d.mts +6 -0
- package/dist/core/query/picks.d.mts +8 -2
- package/dist/core/query/picks.mjs +11 -4
- package/dist/core/query/segments.d.mts +12 -2
- package/dist/core/query/segments.mjs +5 -2
- package/dist/core/result/columns.mjs +20 -4
- package/dist/core/result/types.d.mts +5 -1
- package/dist/core/schema/aliases.d.mts +7 -0
- package/dist/core/schema/aliases.mjs +77 -0
- package/dist/core/schema/answer-parts.mjs +117 -0
- package/dist/core/schema/answers.d.mts +2109 -128
- package/dist/core/schema/answers.mjs +11 -113
- package/dist/core/schema/commands.d.mts +2 -2
- package/dist/core/schema/document.d.mts +1 -1
- package/dist/core/schema/drill-answers.mjs +179 -0
- package/dist/core/schema/drill.d.mts +260 -0
- package/dist/core/schema/drill.mjs +107 -0
- package/dist/core/schema/index.d.mts +6 -3
- package/dist/core/schema/index.mjs +6 -3
- package/dist/core/schema/picks.d.mts +35 -0
- package/dist/core/schema/picks.mjs +23 -0
- package/dist/core/schema/query.d.mts +82 -1
- package/dist/core/schema/query.mjs +9 -5
- package/dist/core/schema/tools.mjs +1 -1
- package/dist/core/schema/view.d.mts +0 -35
- package/dist/core/schema/view.mjs +19 -24
- package/dist/core/store/schema.d.mts +5 -0
- package/dist/core/store/schema.mjs +10 -0
- package/dist/core/types/api.d.mts +21 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +210 -0
- package/dist/core/types/options.d.mts +21 -0
- package/dist/core/types/viewer.d.mts +5 -0
- package/dist/cursor-seal.d.mts +8 -0
- package/dist/cursor-seal.mjs +98 -0
- package/dist/dashboard-id.mjs +1 -1
- package/dist/dashboards.mjs +4 -0
- package/dist/drill-access.mjs +93 -0
- package/dist/drill-change.mjs +76 -0
- package/dist/drill-crumbs.mjs +113 -0
- package/dist/drill-distinct.mjs +130 -0
- package/dist/drill-explain.mjs +64 -0
- package/dist/drill-identity.mjs +35 -0
- package/dist/drill-item-id.mjs +35 -0
- package/dist/drill-items.mjs +132 -0
- package/dist/drill-open.mjs +169 -0
- package/dist/drill-seal.mjs +170 -0
- package/dist/drill-secret.mjs +14 -0
- package/dist/drill-share.mjs +53 -0
- package/dist/drill-summary.mjs +158 -0
- package/dist/drill-targets.mjs +53 -0
- package/dist/drill-totals.mjs +8 -0
- package/dist/drill-views.mjs +175 -0
- package/dist/drill.mjs +157 -0
- package/dist/editing.mjs +3 -3
- package/dist/endpoints/dashboards.mjs +28 -5
- package/dist/endpoints/drill.mjs +55 -0
- package/dist/endpoints/index.d.mts +245 -4
- package/dist/endpoints/index.mjs +5 -1
- package/dist/endpoints/meta.mjs +10 -1
- package/dist/endpoints/query.mjs +26 -4
- package/dist/execute.d.mts +19 -1
- package/dist/execute.mjs +34 -27
- package/dist/handler.mjs +5 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +29 -2
- package/dist/llms.txt +22 -0
- package/dist/mcp/drill.mjs +61 -0
- package/dist/mcp/format.mjs +69 -11
- package/dist/mcp/index.d.mts +15 -4
- package/dist/mcp/index.mjs +13 -7
- package/dist/mcp/query.d.mts +7 -2
- package/dist/mcp/query.mjs +40 -6
- package/dist/node-postgres.d.mts +8 -4
- package/dist/node-postgres.mjs +29 -6
- package/dist/owner.mjs +3 -3
- package/dist/postgres-connection.d.mts +8 -2
- package/dist/postgres-connection.mjs +2 -1
- package/dist/postgres-store.mjs +1 -0
- package/dist/react/Canvas.mjs +28 -5
- package/dist/react/Cell.mjs +2 -1
- package/dist/react/Dashboard.d.mts +10 -5
- package/dist/react/Dashboard.mjs +62 -22
- package/dist/react/DashboardList.mjs +52 -24
- package/dist/react/DashboardListRow.mjs +49 -0
- package/dist/react/Discard.mjs +12 -4
- package/dist/react/Editing.mjs +19 -5
- package/dist/react/FilterBar.d.mts +7 -9
- package/dist/react/Grid.mjs +19 -24
- package/dist/react/Header.mjs +10 -1
- package/dist/react/History.mjs +5 -0
- package/dist/react/Parts.mjs +6 -2
- package/dist/react/ReadFailed.mjs +23 -0
- package/dist/react/ReviewPanel.mjs +13 -2
- package/dist/react/RowBody.d.mts +19 -0
- package/dist/react/RowBody.mjs +55 -0
- package/dist/react/Spaces.mjs +15 -1
- package/dist/react/Tile.d.mts +3 -1
- package/dist/react/Tile.mjs +84 -32
- package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
- package/dist/react/arrange-steps.mjs +16 -13
- package/dist/react/arrange.mjs +10 -3
- package/dist/react/builder/Calcs.mjs +35 -11
- package/dist/react/builder/FilterEditor.mjs +196 -0
- package/dist/react/builder/Filters.mjs +172 -116
- package/dist/react/builder/Formula.mjs +82 -0
- package/dist/react/builder/Groups.mjs +1 -0
- package/dist/react/builder/Preview.mjs +1 -1
- package/dist/react/builder/RangeEditor.mjs +184 -0
- package/dist/react/builder/Sections.mjs +41 -6
- package/dist/react/builder/Time.mjs +138 -60
- package/dist/react/builder/Values.mjs +85 -0
- package/dist/react/builder/WidgetBuilder.mjs +50 -49
- package/dist/react/builder/base.d.mts +5 -0
- package/dist/react/builder/base.mjs +33 -8
- package/dist/react/builder/calc.mjs +15 -1
- package/dist/react/builder/escape.d.mts +1 -0
- package/dist/react/builder/escape.mjs +62 -0
- package/dist/react/builder/filter-tree.d.mts +9 -0
- package/dist/react/builder/filter-tree.mjs +186 -0
- package/dist/react/builder/formula-parse.d.mts +18 -0
- package/dist/react/builder/formula-parse.mjs +136 -0
- package/dist/react/builder/labels.d.mts +1 -1
- package/dist/react/builder/state.d.mts +2 -1
- package/dist/react/builder/state.mjs +23 -37
- package/dist/react/builder/useBuilder.d.mts +12 -1
- package/dist/react/builder/useBuilder.mjs +6 -3
- package/dist/react/builder/words.d.mts +59 -0
- package/dist/react/builder/words.mjs +73 -0
- package/dist/react/client.d.mts +2 -0
- package/dist/react/client.mjs +1 -0
- package/dist/react/context.d.mts +28 -1
- package/dist/react/context.mjs +25 -4
- package/dist/react/drill/Bars.mjs +179 -0
- package/dist/react/drill/Controls.mjs +173 -0
- package/dist/react/drill/DashboardTile.d.mts +18 -0
- package/dist/react/drill/DashboardTile.mjs +107 -0
- package/dist/react/drill/DrillPanel.d.mts +31 -0
- package/dist/react/drill/DrillPanel.mjs +104 -0
- package/dist/react/drill/DrillTabs.mjs +52 -0
- package/dist/react/drill/Header.mjs +131 -0
- package/dist/react/drill/Items.d.mts +22 -0
- package/dist/react/drill/Items.mjs +164 -0
- package/dist/react/drill/ItemsFoot.mjs +60 -0
- package/dist/react/drill/Notes.mjs +58 -0
- package/dist/react/drill/PanelBody.mjs +74 -0
- package/dist/react/drill/PanelStates.mjs +37 -0
- package/dist/react/drill/board.d.mts +18 -0
- package/dist/react/drill/board.mjs +91 -0
- package/dist/react/drill/counts.mjs +14 -0
- package/dist/react/drill/host.mjs +26 -0
- package/dist/react/drill/item-parts.d.mts +5 -0
- package/dist/react/drill/item-parts.mjs +76 -0
- package/dist/react/drill/item-type.d.mts +26 -0
- package/dist/react/drill/item-type.mjs +7 -0
- package/dist/react/drill/lazy.d.mts +6 -0
- package/dist/react/drill/lazy.mjs +17 -0
- package/dist/react/drill/pages.mjs +48 -0
- package/dist/react/drill/panel-focus.mjs +51 -0
- package/dist/react/drill/props.d.mts +25 -0
- package/dist/react/drill/props.mjs +72 -0
- package/dist/react/drill/state.d.mts +18 -0
- package/dist/react/drill/state.mjs +68 -0
- package/dist/react/drill/useDrill.d.mts +46 -0
- package/dist/react/drill/useDrill.mjs +194 -0
- package/dist/react/drill/words.d.mts +101 -0
- package/dist/react/drill/words.mjs +79 -0
- package/dist/react/english.mjs +29 -6
- package/dist/react/filters/bar.d.mts +12 -0
- package/dist/react/filters/bar.mjs +135 -0
- package/dist/react/filters/calendar.mjs +193 -0
- package/dist/react/filters/chip.mjs +174 -0
- package/dist/react/filters/context.d.mts +22 -0
- package/dist/react/filters/context.mjs +78 -0
- package/dist/react/filters/dates.mjs +89 -0
- package/dist/react/filters/days.d.mts +36 -0
- package/dist/react/filters/days.mjs +87 -0
- package/dist/react/filters/field-step.mjs +81 -0
- package/dist/react/filters/hooks.d.mts +34 -0
- package/dist/react/filters/hooks.mjs +94 -0
- package/dist/react/filters/listbox.mjs +131 -0
- package/dist/react/filters/logic.d.mts +59 -0
- package/dist/react/filters/logic.mjs +109 -0
- package/dist/react/filters/menu-search.mjs +211 -0
- package/dist/react/filters/menu.mjs +85 -0
- package/dist/react/filters/number.mjs +127 -0
- package/dist/react/filters/numbers.d.mts +47 -0
- package/dist/react/filters/numbers.mjs +151 -0
- package/dist/react/filters/operator.mjs +104 -0
- package/dist/react/filters/phone.mjs +104 -0
- package/dist/react/filters/popover.d.mts +19 -0
- package/dist/react/filters/popover.mjs +153 -0
- package/dist/react/filters/rules.d.mts +8 -0
- package/dist/react/filters/rules.mjs +55 -0
- package/dist/react/filters/sheet.mjs +196 -0
- package/dist/react/filters/state.mjs +132 -0
- package/dist/react/filters/suggest.d.mts +47 -0
- package/dist/react/filters/suggest.mjs +97 -0
- package/dist/react/filters/values.mjs +166 -0
- package/dist/react/filters/words.d.mts +81 -0
- package/dist/react/filters/words.mjs +73 -0
- package/dist/react/format.d.mts +3 -3
- package/dist/react/format.mjs +45 -16
- package/dist/react/index.d.mts +34 -5
- package/dist/react/index.mjs +15 -4
- package/dist/react/messages.d.mts +47 -2
- package/dist/react/place.mjs +7 -3
- package/dist/react/renderers/index.d.mts +10 -2
- package/dist/react/renderers/index.mjs +116 -14
- package/dist/react/renderers/list.d.mts +22 -0
- package/dist/react/renderers/list.mjs +118 -0
- package/dist/react/review-words.mjs +1 -1
- package/dist/react/review.mjs +2 -2
- package/dist/react/scope.mjs +2 -7
- package/dist/react/series.d.mts +54 -0
- package/dist/react/series.mjs +191 -0
- package/dist/react/theme.css +2734 -1296
- package/dist/react/theme.css.d.ts +1 -0
- package/dist/react/tile-store.mjs +15 -3
- package/dist/react/useBoard.d.mts +7 -0
- package/dist/react/useBoard.mjs +4 -1
- package/dist/react/useDashboard.d.mts +4 -0
- package/dist/react/useDashboard.mjs +8 -1
- package/dist/react/useTiles.mjs +50 -15
- package/dist/react/view-grid.mjs +16 -0
- package/dist/react/view.d.mts +9 -0
- package/dist/react/view.mjs +22 -10
- package/dist/reading.d.mts +2 -0
- package/dist/reading.mjs +9 -1
- package/dist/recharts/axes.mjs +28 -0
- package/dist/recharts/axis.mjs +84 -0
- package/dist/recharts/bar.d.mts +7 -0
- package/dist/recharts/bar.mjs +140 -0
- package/dist/recharts/charts.d.mts +4 -6
- package/dist/recharts/charts.mjs +2 -114
- package/dist/recharts/index.d.mts +5 -3
- package/dist/recharts/index.mjs +6 -3
- package/dist/recharts/line.d.mts +2 -0
- package/dist/recharts/line.mjs +111 -0
- package/dist/recharts/marks.mjs +112 -0
- package/dist/recharts/parts.mjs +88 -0
- package/dist/recharts/series.d.mts +2 -22
- package/dist/recharts/series.mjs +1 -64
- package/dist/recharts/table.mjs +71 -0
- package/dist/records-read.mjs +44 -0
- package/dist/registry/filter-bar.json +26 -0
- package/dist/skills/derive-dashboards/SKILL.md +69 -0
- package/dist/store/apply.mjs +5 -5
- package/dist/store/commands.mjs +4 -4
- package/dist/store/discard.mjs +7 -7
- package/dist/store/drafts.mjs +5 -5
- package/dist/store/editor.mjs +1 -1
- package/dist/store/publish.mjs +12 -12
- package/dist/store/reader.mjs +41 -22
- package/dist/store/review.mjs +16 -16
- package/dist/store/rows.mjs +20 -20
- package/dist/store/undo.mjs +3 -3
- package/dist/store/unpublished.mjs +4 -4
- package/dist/store/versions.mjs +14 -14
- package/dist/testing/adapter-suite.mjs +4 -4
- package/dist/tiles.mjs +37 -3
- package/package.json +12 -10
- package/dist/react/FilterBar.mjs +0 -119
- package/dist/react/Records.d.mts +0 -12
- package/dist/react/Records.mjs +0 -97
- package/src/adapter/sql-adapter.ts +0 -95
- package/src/adapter/statement.ts +0 -185
- package/src/authorize.ts +0 -73
- package/src/body.ts +0 -169
- package/src/bounded.ts +0 -43
- package/src/cache.ts +0 -192
- package/src/checks.ts +0 -170
- package/src/custom-source.ts +0 -59
- package/src/dashboard-id.ts +0 -36
- package/src/dashboards.ts +0 -132
- package/src/editing.ts +0 -148
- package/src/endpoints/access.ts +0 -41
- package/src/endpoints/dashboards.ts +0 -92
- package/src/endpoints/drafts.ts +0 -138
- package/src/endpoints/index.ts +0 -21
- package/src/endpoints/meta.ts +0 -137
- package/src/endpoints/query.ts +0 -86
- package/src/endpoints/read.ts +0 -92
- package/src/endpoints/review.ts +0 -89
- package/src/endpoints/values.ts +0 -62
- package/src/endpoints/versions.ts +0 -104
- package/src/execute.ts +0 -207
- package/src/handler.ts +0 -232
- package/src/http.ts +0 -66
- package/src/index.ts +0 -163
- package/src/introspect.ts +0 -205
- package/src/mcp/api.ts +0 -59
- package/src/mcp/format.ts +0 -67
- package/src/mcp/index.ts +0 -151
- package/src/mcp/query.ts +0 -153
- package/src/mcp/resolve.ts +0 -114
- package/src/node-postgres.ts +0 -81
- package/src/owner.ts +0 -36
- package/src/postgres-connection.ts +0 -143
- package/src/postgres-store.ts +0 -82
- package/src/principal.ts +0 -39
- package/src/reading.ts +0 -56
- package/src/request-rows.ts +0 -23
- package/src/request.ts +0 -22
- package/src/storage.ts +0 -63
- package/src/store/apply.ts +0 -129
- package/src/store/commands.ts +0 -75
- package/src/store/conflicts.ts +0 -98
- package/src/store/create-tables.ts +0 -104
- package/src/store/discard.ts +0 -42
- package/src/store/drafts.ts +0 -133
- package/src/store/editor.ts +0 -28
- package/src/store/log.ts +0 -28
- package/src/store/publish.ts +0 -199
- package/src/store/publisher.ts +0 -21
- package/src/store/reader.ts +0 -139
- package/src/store/review.ts +0 -239
- package/src/store/rows.ts +0 -250
- package/src/store/tenant.ts +0 -28
- package/src/store/undo.ts +0 -56
- package/src/store/unpublished.ts +0 -33
- package/src/store/versions.ts +0 -106
- package/src/store/writer.ts +0 -19
- package/src/testing/adapter-suite.ts +0 -239
- package/src/testing/index.ts +0 -2
- package/src/tile-doc.ts +0 -46
- package/src/tiles.ts +0 -120
package/src/store/conflicts.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Change,
|
|
3
|
-
commandWidget,
|
|
4
|
-
type Doc,
|
|
5
|
-
type OverlapPart,
|
|
6
|
-
type Preview,
|
|
7
|
-
type Refusal,
|
|
8
|
-
reduceAll,
|
|
9
|
-
type replay,
|
|
10
|
-
widgetDiff,
|
|
11
|
-
} from "@derive-to/dashboards-core";
|
|
12
|
-
import type { Kept } from "./commands";
|
|
13
|
-
|
|
14
|
-
/** What a review says about a draft replayed on a newer version: what no longer applies, and what both changed. */
|
|
15
|
-
|
|
16
|
-
/** The changes that no longer apply, each with its reason and the widget it needs by its title. */
|
|
17
|
-
export function refusalsOf(
|
|
18
|
-
kept: readonly Kept[],
|
|
19
|
-
merged: ReturnType<typeof replay>,
|
|
20
|
-
titles: Doc[],
|
|
21
|
-
) {
|
|
22
|
-
return merged.outcomes.flatMap((o, i): Refusal[] => {
|
|
23
|
-
const k = kept[i];
|
|
24
|
-
if (o.applied || k === undefined) return [];
|
|
25
|
-
const title = titles.map((d) => d.widgets[o.widget ?? ""]?.title).find((t) => t !== undefined);
|
|
26
|
-
return [
|
|
27
|
-
{
|
|
28
|
-
commandId: o.commandId,
|
|
29
|
-
body: k.body,
|
|
30
|
-
issue: { code: o.issue.code, message: o.issue.message },
|
|
31
|
-
...(o.widget === undefined ? {} : { widget: { id: o.widget, title: title ?? o.widget } }),
|
|
32
|
-
},
|
|
33
|
-
];
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** What changes touched, as "widget part": each part of it edited, its place, or all of it. */
|
|
38
|
-
const touchedBy = (changes: readonly Change[]) =>
|
|
39
|
-
changes.flatMap((c) => {
|
|
40
|
-
if (c.kind === "widget.edited") return c.parts.map((part) => `${c.widget} ${part}`);
|
|
41
|
-
if (c.kind === "widget.moved") return [`${c.widget} place`];
|
|
42
|
-
return c.kind === "widget.removed" ? [`${c.widget} removed`] : [];
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
/** The parts of a widget a change of the person's touched that the newer version's changes did too. */
|
|
46
|
-
function shared(
|
|
47
|
-
mine: readonly string[],
|
|
48
|
-
theirs: ReadonlySet<string>,
|
|
49
|
-
widget: string,
|
|
50
|
-
): OverlapPart[] {
|
|
51
|
-
const parts = mine.flatMap((t): OverlapPart[] => {
|
|
52
|
-
const part = t.slice(widget.length + 1);
|
|
53
|
-
if (part === "removed")
|
|
54
|
-
return [...theirs].some((x) => x.startsWith(`${widget} `)) ? ["removed"] : [];
|
|
55
|
-
return theirs.has(t) && isOverlapPart(part) ? [part] : [];
|
|
56
|
-
});
|
|
57
|
-
return [...new Set(parts)];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const OVERLAP_PARTS: ReadonlySet<string> = new Set([
|
|
61
|
-
"title",
|
|
62
|
-
"kind",
|
|
63
|
-
"display",
|
|
64
|
-
"query",
|
|
65
|
-
"place",
|
|
66
|
-
"removed",
|
|
67
|
-
]);
|
|
68
|
-
const isOverlapPart = (part: string): part is OverlapPart => OVERLAP_PARTS.has(part);
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The person's applied changes to a part of a widget (its title, kind, display, query or
|
|
72
|
-
* place) that someone else changed since the draft started, or that remove a widget they
|
|
73
|
-
* changed: nothing refuses them, so without this the other person's edit would go quietly.
|
|
74
|
-
* Each change is read by what it did to its own widget on top of the newer version, so a
|
|
75
|
-
* change to another part of the same widget does not count.
|
|
76
|
-
*/
|
|
77
|
-
export function overlapsOf(
|
|
78
|
-
kept: readonly Kept[],
|
|
79
|
-
merged: ReturnType<typeof replay>,
|
|
80
|
-
since: readonly Change[],
|
|
81
|
-
head: Doc,
|
|
82
|
-
): Preview["overlaps"] {
|
|
83
|
-
const theirs = new Set(touchedBy(since));
|
|
84
|
-
if (theirs.size === 0) return [];
|
|
85
|
-
let current = head;
|
|
86
|
-
return kept.flatMap((k, i) => {
|
|
87
|
-
const widget = commandWidget(k.body);
|
|
88
|
-
const step = merged.outcomes[i]?.applied === true ? reduceAll(current, [k.body]) : undefined;
|
|
89
|
-
if (step === undefined || !step.ok) return [];
|
|
90
|
-
const before = current;
|
|
91
|
-
current = step.doc;
|
|
92
|
-
if (widget === undefined) return [];
|
|
93
|
-
const parts = shared(touchedBy(widgetDiff(before, step.doc, widget)), theirs, widget);
|
|
94
|
-
if (parts.length === 0) return [];
|
|
95
|
-
const title = head.widgets[widget]?.title ?? widget;
|
|
96
|
-
return [{ commandId: k.commandId, widget: { id: widget, title }, parts }];
|
|
97
|
-
});
|
|
98
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { type DashAdapter, schemaChecks } from "@derive-to/dashboards-core";
|
|
2
|
-
import { inTransaction } from "./rows";
|
|
3
|
-
|
|
4
|
-
/** Thrown when a `dd_` table lacks a required column with no default: rows there would have no value. */
|
|
5
|
-
export class SchemaMismatchError extends Error {
|
|
6
|
-
constructor(table: string, column: string, cause?: unknown) {
|
|
7
|
-
super(
|
|
8
|
-
`The table ${table} lacks the required column ${column}, which has no default to give rows already there. It was made by an early pre-release: drop the dd_ tables (they hold only dashboards) and run createTables again.`,
|
|
9
|
-
{ cause },
|
|
10
|
-
);
|
|
11
|
-
this.name = "SchemaMismatchError";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** What it takes to bring a database to this schema, in the order to run it. */
|
|
16
|
-
export type SchemaDiff = { tables: string[]; columns: string[]; indexes: string[] };
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* The columns each `dd_` table has, read from the database's catalog, and the schema they are
|
|
20
|
-
* all in, if they share one: a table a newer release adds is made beside them, so a host's grants
|
|
21
|
-
* and backups on that schema cover it.
|
|
22
|
-
*/
|
|
23
|
-
async function present(
|
|
24
|
-
adapter: DashAdapter,
|
|
25
|
-
): Promise<{ have: Map<string, Set<string>>; home?: string }> {
|
|
26
|
-
const have = new Map<string, Set<string>>();
|
|
27
|
-
const schemas = new Set<string | undefined>();
|
|
28
|
-
for (const { table, column, schema } of await adapter.schema.columns()) {
|
|
29
|
-
const columns = have.get(table) ?? new Set<string>();
|
|
30
|
-
columns.add(column);
|
|
31
|
-
have.set(table, columns);
|
|
32
|
-
schemas.add(schema);
|
|
33
|
-
}
|
|
34
|
-
const [home] = schemas;
|
|
35
|
-
return schemas.size === 1 && home !== undefined ? { have, home } : { have };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The difference between a database and this schema, read from its catalog: the tables it
|
|
40
|
-
* lacks, the columns an older table lacks (nullable, or with a default, so rows there are kept),
|
|
41
|
-
* and every key and index (made only if missing). It only reads. `createTables` runs it;
|
|
42
|
-
* `generate --diff` writes it for a host's own migration tool.
|
|
43
|
-
*/
|
|
44
|
-
export async function schemaDiff(adapter: DashAdapter): Promise<SchemaDiff> {
|
|
45
|
-
const { have, home } = await present(adapter);
|
|
46
|
-
const diff: SchemaDiff = { tables: [], columns: [], indexes: [] };
|
|
47
|
-
for (const check of schemaChecks(adapter.schema.dialect, home)) {
|
|
48
|
-
const [table = "", ...keys] = check.create;
|
|
49
|
-
diff.indexes.push(...keys);
|
|
50
|
-
const columns = have.get(check.table);
|
|
51
|
-
if (columns === undefined) {
|
|
52
|
-
diff.tables.push(table);
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
for (const column of check.columns) {
|
|
56
|
-
if (columns.has(column.name)) continue;
|
|
57
|
-
if (column.add === undefined) throw new SchemaMismatchError(check.table, column.name);
|
|
58
|
-
diff.columns.push(column.add);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return diff;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Runs a diff: new tables in one transaction, columns one by one, then keys and indexes. */
|
|
65
|
-
async function apply(adapter: DashAdapter, diff: SchemaDiff): Promise<void> {
|
|
66
|
-
const run = async (db: DashAdapter, statements: readonly string[]) => {
|
|
67
|
-
for (const statement of statements) {
|
|
68
|
-
// oxlint-disable-next-line no-await-in-loop -- DDL runs in order, one statement at a time
|
|
69
|
-
await db.schema.execute(statement);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
await inTransaction(adapter, (tx) => run(tx, diff.tables));
|
|
73
|
-
await run(adapter, diff.columns);
|
|
74
|
-
await inTransaction(adapter, (tx) => run(tx, diff.indexes));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Makes the library's `dd_` tables, keys and indexes, and brings older ones up to this
|
|
79
|
-
* schema: there is one schema, no migration history. Tables that are missing are made; a
|
|
80
|
-
* table an older release made gets the columns this release added (a release only adds
|
|
81
|
-
* columns that are nullable or have a default, so rows are kept), then any keys and indexes
|
|
82
|
-
* it lacks. Safe to run on every start, from every instance: when another instance makes the
|
|
83
|
-
* same thing first, the database is read again and only what is still missing is run. For a
|
|
84
|
-
* host with its own schema tool, `generate` writes the same tables, and `generate --diff` only
|
|
85
|
-
* what a database lacks.
|
|
86
|
-
*/
|
|
87
|
-
export async function createTables(adapter: DashAdapter): Promise<void> {
|
|
88
|
-
const diff = await schemaDiff(adapter);
|
|
89
|
-
try {
|
|
90
|
-
await apply(adapter, diff);
|
|
91
|
-
} catch (first) {
|
|
92
|
-
// Another instance may have made a table or index between the read and the write; read
|
|
93
|
-
// again. A failure that is not that one fails again here, and is thrown with the first.
|
|
94
|
-
try {
|
|
95
|
-
await apply(adapter, await schemaDiff(adapter));
|
|
96
|
-
} catch (second) {
|
|
97
|
-
throw new AggregateError(
|
|
98
|
-
[first, second],
|
|
99
|
-
`createTables failed, and again after reading the database once more: ${second instanceof Error ? second.message : String(second)}`,
|
|
100
|
-
{ cause: second },
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
package/src/store/discard.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { Author, DashAdapter, Tenant } from "@derive-to/dashboards-core";
|
|
2
|
-
import { advanceDraft, expectRevision, inTransaction, now, openDraft } from "./rows";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Drops the author's open draft. A dashboard that was never published, and that nobody else
|
|
6
|
-
* is editing, goes with it: a new one disappears, and a built-in serves its code document.
|
|
7
|
-
*/
|
|
8
|
-
export function discardDraft(
|
|
9
|
-
db: DashAdapter,
|
|
10
|
-
a: { tenant: Tenant; id: string; author: Author; expectedRevision: number },
|
|
11
|
-
): Promise<{ removed: boolean }> {
|
|
12
|
-
return inTransaction(db, async (tx) => {
|
|
13
|
-
const { row, dashboard } = await openDraft(tx, a);
|
|
14
|
-
expectRevision(row, a.expectedRevision);
|
|
15
|
-
await tx.deleteMany({
|
|
16
|
-
table: "dd_dashboard_draft_command",
|
|
17
|
-
where: [{ field: "draft_id", value: row["id"] }],
|
|
18
|
-
});
|
|
19
|
-
const others = await tx.count({
|
|
20
|
-
table: "dd_dashboard_draft",
|
|
21
|
-
where: [
|
|
22
|
-
{ field: "dashboard_id", value: dashboard["id"] },
|
|
23
|
-
{ field: "state", value: "open" },
|
|
24
|
-
{ field: "id", op: "ne", value: row["id"] },
|
|
25
|
-
],
|
|
26
|
-
});
|
|
27
|
-
if (dashboard["current_version"] === null && others === 0) {
|
|
28
|
-
await tx.deleteMany({
|
|
29
|
-
table: "dd_dashboard_draft",
|
|
30
|
-
where: [{ field: "dashboard_id", value: dashboard["id"] }],
|
|
31
|
-
});
|
|
32
|
-
await tx.deleteMany({
|
|
33
|
-
table: "dd_dashboard",
|
|
34
|
-
where: [{ field: "id", value: dashboard["id"] }],
|
|
35
|
-
});
|
|
36
|
-
// A built-in still serves its code document: only a new dashboard is gone.
|
|
37
|
-
return { removed: dashboard["default_of"] === null };
|
|
38
|
-
}
|
|
39
|
-
await advanceDraft(tx, row, { state: "closed", updated_at: now() });
|
|
40
|
-
return { removed: false };
|
|
41
|
-
});
|
|
42
|
-
}
|
package/src/store/drafts.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Author,
|
|
3
|
-
DashAdapter,
|
|
4
|
-
Doc,
|
|
5
|
-
Draft,
|
|
6
|
-
DraftTarget,
|
|
7
|
-
Row,
|
|
8
|
-
Tenant,
|
|
9
|
-
} from "@derive-to/dashboards-core";
|
|
10
|
-
import { HttpError } from "../http";
|
|
11
|
-
import {
|
|
12
|
-
dashboardRow,
|
|
13
|
-
docDigest,
|
|
14
|
-
draftAuthorId,
|
|
15
|
-
draftOf,
|
|
16
|
-
draftRow,
|
|
17
|
-
headDoc,
|
|
18
|
-
insertDashboard,
|
|
19
|
-
inTransaction,
|
|
20
|
-
now,
|
|
21
|
-
} from "./rows";
|
|
22
|
-
import { tenantHash } from "./tenant";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The new dashboard a retried create already made, by the maker's request id; the same id
|
|
26
|
-
* sent for a different dashboard is refused rather than answered with the first one.
|
|
27
|
-
*/
|
|
28
|
-
async function madeBefore(
|
|
29
|
-
db: DashAdapter,
|
|
30
|
-
a: { tenant: Tenant; author: Author; target: { create: Doc; requestId?: string } },
|
|
31
|
-
): Promise<Row | null> {
|
|
32
|
-
const { requestId } = a.target;
|
|
33
|
-
if (requestId === undefined) return null;
|
|
34
|
-
const made = await db.findOne({
|
|
35
|
-
table: "dd_dashboard",
|
|
36
|
-
where: [
|
|
37
|
-
{ field: "tenant_hash", value: await tenantHash(a.tenant) },
|
|
38
|
-
{ field: "owner_kind", value: a.author.kind ?? "user" },
|
|
39
|
-
{ field: "owner_id", value: a.author.id },
|
|
40
|
-
{ field: "request_id", value: requestId },
|
|
41
|
-
{ field: "archived_at", op: "isNull" },
|
|
42
|
-
],
|
|
43
|
-
});
|
|
44
|
-
if (made !== null && made["request_digest"] !== (await docDigest(a.target.create)))
|
|
45
|
-
throw new HttpError(
|
|
46
|
-
409,
|
|
47
|
-
"REQUEST_REUSED",
|
|
48
|
-
`The request id "${requestId}" already made "${String(made["title"])}". Send a new one for another dashboard.`,
|
|
49
|
-
);
|
|
50
|
-
return made;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** The dashboard row a draft edits, made for a built-in or a new dashboard the first time. */
|
|
54
|
-
async function targetRow(
|
|
55
|
-
db: DashAdapter,
|
|
56
|
-
a: { tenant: Tenant; author: Author; target: DraftTarget },
|
|
57
|
-
): Promise<{ row: Row; base: Doc | undefined }> {
|
|
58
|
-
const { target } = a;
|
|
59
|
-
if ("id" in target) {
|
|
60
|
-
const row = await dashboardRow(db, a.tenant, { id: target.id });
|
|
61
|
-
if (row === null)
|
|
62
|
-
throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${target.id}".`);
|
|
63
|
-
return { row, base: undefined };
|
|
64
|
-
}
|
|
65
|
-
if ("builtIn" in target) {
|
|
66
|
-
const found = await dashboardRow(db, a.tenant, { key: target.builtIn });
|
|
67
|
-
const row =
|
|
68
|
-
found ?? (await insertDashboard(db, { ...a, doc: target.doc, key: target.builtIn }));
|
|
69
|
-
return { row, base: target.doc };
|
|
70
|
-
}
|
|
71
|
-
const made = await madeBefore(db, { ...a, target });
|
|
72
|
-
const row =
|
|
73
|
-
made ??
|
|
74
|
-
(await insertDashboard(db, {
|
|
75
|
-
...a,
|
|
76
|
-
doc: target.create,
|
|
77
|
-
...(target.requestId === undefined ? {} : { requestId: target.requestId }),
|
|
78
|
-
}));
|
|
79
|
-
return { row, base: target.create };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* The author's open draft of the target, or a new one from its current document: the latest
|
|
84
|
-
* published version, else the built-in's code document or the new dashboard's first one.
|
|
85
|
-
*/
|
|
86
|
-
export function startDraft(
|
|
87
|
-
db: DashAdapter,
|
|
88
|
-
a: { tenant: Tenant; author: Author; target: DraftTarget },
|
|
89
|
-
): Promise<Draft> {
|
|
90
|
-
return inTransaction(db, async (tx) => {
|
|
91
|
-
const { row, base } = await targetRow(tx, a);
|
|
92
|
-
const id = String(row["id"]);
|
|
93
|
-
const existing = await draftRow(tx, id, a.author);
|
|
94
|
-
if (existing?.["state"] === "open") return draftOf(existing);
|
|
95
|
-
const head = await headDoc(tx, row);
|
|
96
|
-
const doc = head.doc ?? base;
|
|
97
|
-
if (doc === undefined)
|
|
98
|
-
throw new HttpError(404, "NOT_FOUND", `The dashboard "${id}" has no document to edit.`);
|
|
99
|
-
const fresh = {
|
|
100
|
-
state: "open",
|
|
101
|
-
closed_by: null,
|
|
102
|
-
revision: 0,
|
|
103
|
-
base_version: head.version,
|
|
104
|
-
spec: doc,
|
|
105
|
-
base_spec: doc,
|
|
106
|
-
for_id: a.author.kind === "agent" ? (a.author.for ?? null) : null,
|
|
107
|
-
steps: 0,
|
|
108
|
-
named: [],
|
|
109
|
-
next_seq: 0,
|
|
110
|
-
updated_at: now(),
|
|
111
|
-
};
|
|
112
|
-
if (existing === null) {
|
|
113
|
-
const data = {
|
|
114
|
-
id: crypto.randomUUID(),
|
|
115
|
-
dashboard_id: id,
|
|
116
|
-
tenant_hash: await tenantHash(a.tenant),
|
|
117
|
-
author_id: draftAuthorId(a.author),
|
|
118
|
-
author_kind: a.author.kind ?? "user",
|
|
119
|
-
generation: 1,
|
|
120
|
-
...fresh,
|
|
121
|
-
};
|
|
122
|
-
await tx.create({ table: "dd_dashboard_draft", data });
|
|
123
|
-
return draftOf(data);
|
|
124
|
-
}
|
|
125
|
-
const generation = Number(existing["generation"]) + 1;
|
|
126
|
-
await tx.updateMany({
|
|
127
|
-
table: "dd_dashboard_draft",
|
|
128
|
-
where: [{ field: "id", value: existing["id"] }],
|
|
129
|
-
set: { ...fresh, generation },
|
|
130
|
-
});
|
|
131
|
-
return draftOf({ ...existing, ...fresh, generation });
|
|
132
|
-
});
|
|
133
|
-
}
|
package/src/store/editor.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, DashboardStore } from "@derive-to/dashboards-core";
|
|
2
|
-
import { applyDraft } from "./apply";
|
|
3
|
-
import { discardDraft } from "./discard";
|
|
4
|
-
import { startDraft } from "./drafts";
|
|
5
|
-
import { dashboardRow, draftOf, draftRow } from "./rows";
|
|
6
|
-
import { undo } from "./undo";
|
|
7
|
-
import { unpublished } from "./unpublished";
|
|
8
|
-
|
|
9
|
-
type Editing = Pick<
|
|
10
|
-
DashboardStore,
|
|
11
|
-
"getDraft" | "startDraft" | "applyDraft" | "discardDraft" | "unpublished" | "undo"
|
|
12
|
-
>;
|
|
13
|
-
|
|
14
|
-
/** A person's or an agent's own draft: opened, edited, stepped back, dropped. */
|
|
15
|
-
export function draftMethods(db: DashAdapter): Editing {
|
|
16
|
-
return {
|
|
17
|
-
getDraft: async (a) => {
|
|
18
|
-
const dashboard = await dashboardRow(db, a.tenant, { id: a.id });
|
|
19
|
-
const row = dashboard === null ? null : await draftRow(db, a.id, a.author);
|
|
20
|
-
return row === null || row["state"] !== "open" ? null : draftOf(row);
|
|
21
|
-
},
|
|
22
|
-
startDraft: (a) => startDraft(db, a),
|
|
23
|
-
applyDraft: (a) => applyDraft(db, a),
|
|
24
|
-
discardDraft: (a) => discardDraft(db, a),
|
|
25
|
-
unpublished: (a) => unpublished(db, a),
|
|
26
|
-
undo: (a) => undo(db, a),
|
|
27
|
-
};
|
|
28
|
-
}
|
package/src/store/log.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, Principal } from "@derive-to/dashboards-core";
|
|
2
|
-
import { principalTenant } from "@derive-to/dashboards-core";
|
|
3
|
-
import type { Answered } from "../execute";
|
|
4
|
-
import { tenantHash } from "./tenant";
|
|
5
|
-
|
|
6
|
-
/** One `dd_query_log` row for an answered query: who, what (by hash), how long, how many rows, from where. */
|
|
7
|
-
export async function logQuery(
|
|
8
|
-
adapter: DashAdapter,
|
|
9
|
-
principal: Principal,
|
|
10
|
-
source: string,
|
|
11
|
-
answer: Answered,
|
|
12
|
-
): Promise<void> {
|
|
13
|
-
const { receipt } = answer;
|
|
14
|
-
await adapter.create({
|
|
15
|
-
table: "dd_query_log",
|
|
16
|
-
data: {
|
|
17
|
-
id: crypto.randomUUID(),
|
|
18
|
-
tenant_hash: await tenantHash(principalTenant(principal)),
|
|
19
|
-
viewer_id: principal.channel === "user" ? principal.viewer.id : null,
|
|
20
|
-
query_hash: receipt.queryHash,
|
|
21
|
-
source,
|
|
22
|
-
duration_ms: receipt.durationMs,
|
|
23
|
-
rows: receipt.rowsReturned,
|
|
24
|
-
cache: receipt.cache,
|
|
25
|
-
at: receipt.computedAt,
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
}
|
package/src/store/publish.ts
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Author,
|
|
3
|
-
type DashAdapter,
|
|
4
|
-
type Doc,
|
|
5
|
-
type Published,
|
|
6
|
-
type Row,
|
|
7
|
-
replay,
|
|
8
|
-
type Tenant,
|
|
9
|
-
} from "@derive-to/dashboards-core";
|
|
10
|
-
import { HttpError } from "../http";
|
|
11
|
-
import { keptCommands } from "./commands";
|
|
12
|
-
import { expectHead } from "./review";
|
|
13
|
-
import {
|
|
14
|
-
advanceDraft,
|
|
15
|
-
dashboardRow,
|
|
16
|
-
draftClosed,
|
|
17
|
-
draftDoc,
|
|
18
|
-
draftRow,
|
|
19
|
-
expectRevision,
|
|
20
|
-
headDoc,
|
|
21
|
-
inTransaction,
|
|
22
|
-
now,
|
|
23
|
-
} from "./rows";
|
|
24
|
-
import { tenantHash } from "./tenant";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The document to publish: what the person reviewed. When nobody published since the draft
|
|
28
|
-
* started, the draft itself; otherwise its commands replayed on the newer version, which the
|
|
29
|
-
* review showed. A change that no longer applies refuses the publish until it is decided.
|
|
30
|
-
*/
|
|
31
|
-
async function publishedDoc(tx: DashAdapter, draft: Row, dashboard: Row, expected: number) {
|
|
32
|
-
const head = await headDoc(tx, dashboard);
|
|
33
|
-
expectHead(head.version, expected);
|
|
34
|
-
if (head.version === Number(draft["base_version"]) || head.doc === undefined)
|
|
35
|
-
return { doc: draftDoc(draft), head: head.version };
|
|
36
|
-
const merged = replay(head.doc, await keptCommands(tx, draft["id"]));
|
|
37
|
-
const refused = merged.outcomes.flatMap((o) => (o.applied ? [] : [o.issue]));
|
|
38
|
-
const [issue] = refused;
|
|
39
|
-
if (issue !== undefined)
|
|
40
|
-
throw new HttpError(
|
|
41
|
-
409,
|
|
42
|
-
"REPLAY_REFUSED",
|
|
43
|
-
`${String(refused.length)} of your changes no longer apply to version ${String(head.version)}: ${issue.message}`,
|
|
44
|
-
{ issues: refused },
|
|
45
|
-
);
|
|
46
|
-
return { doc: merged.doc, head: head.version, replayedOn: head.version };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The version a retried publish already made, found by its request id. */
|
|
50
|
-
/**
|
|
51
|
-
* The version a retried publish or restore already made, by its author's request id: a request
|
|
52
|
-
* id is the author's own, so another author's same id is never taken for theirs.
|
|
53
|
-
*/
|
|
54
|
-
export function earlier(tx: DashAdapter, dashboardId: unknown, requestId: string, author: Author) {
|
|
55
|
-
return tx.findOne({
|
|
56
|
-
table: "dd_dashboard_version",
|
|
57
|
-
where: [
|
|
58
|
-
{ field: "dashboard_id", value: dashboardId },
|
|
59
|
-
{ field: "request_id", value: requestId },
|
|
60
|
-
{ field: "author_kind", value: author.kind ?? "user" },
|
|
61
|
-
{ field: "author_id", value: author.id },
|
|
62
|
-
],
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export async function insertVersion(
|
|
67
|
-
tx: DashAdapter,
|
|
68
|
-
a: {
|
|
69
|
-
tenant: Tenant;
|
|
70
|
-
author: Author;
|
|
71
|
-
requestId: string;
|
|
72
|
-
dashboardId: unknown;
|
|
73
|
-
n: number;
|
|
74
|
-
doc: Doc;
|
|
75
|
-
basedOn: number;
|
|
76
|
-
restoredFrom?: number;
|
|
77
|
-
},
|
|
78
|
-
) {
|
|
79
|
-
await tx.create({
|
|
80
|
-
table: "dd_dashboard_version",
|
|
81
|
-
data: {
|
|
82
|
-
id: crypto.randomUUID(),
|
|
83
|
-
dashboard_id: a.dashboardId,
|
|
84
|
-
tenant_hash: await tenantHash(a.tenant),
|
|
85
|
-
n: a.n,
|
|
86
|
-
spec: a.doc,
|
|
87
|
-
spec_version: a.doc.specVersion,
|
|
88
|
-
author_id: a.author.id,
|
|
89
|
-
author_kind: a.author.kind ?? "user",
|
|
90
|
-
request_id: a.requestId,
|
|
91
|
-
based_on: a.basedOn === 0 ? null : a.basedOn,
|
|
92
|
-
restored_from: a.restoredFrom ?? null,
|
|
93
|
-
message: null,
|
|
94
|
-
created_at: now(),
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Makes `head + 1` the dashboard's current version, only if `head` still is: the unique version
|
|
101
|
-
* number already stops two publishes, and this keeps the head from ever moving backwards.
|
|
102
|
-
*/
|
|
103
|
-
export async function advanceHead(tx: DashAdapter, dashboardId: unknown, head: number, doc: Doc) {
|
|
104
|
-
const moved = await tx.updateMany({
|
|
105
|
-
table: "dd_dashboard",
|
|
106
|
-
where: [
|
|
107
|
-
{ field: "id", value: dashboardId },
|
|
108
|
-
head === 0
|
|
109
|
-
? { field: "current_version", op: "isNull" }
|
|
110
|
-
: { field: "current_version", value: head },
|
|
111
|
-
],
|
|
112
|
-
set: {
|
|
113
|
-
current_version: head + 1,
|
|
114
|
-
title: doc.meta.title,
|
|
115
|
-
description: doc.meta.description ?? null,
|
|
116
|
-
updated_at: now(),
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
if (moved !== 1)
|
|
120
|
-
throw new HttpError(503, "RETRYABLE", "Someone published at the same moment. Try again.");
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* What a publish already did, by the rows read once: the version a retry made (refusing new
|
|
125
|
-
* edits sent with an old request id, which would otherwise publish nothing and say it worked),
|
|
126
|
-
* or the current version when this request closed the draft without edits. Null for a new one.
|
|
127
|
-
*/
|
|
128
|
-
async function answeredBefore(
|
|
129
|
-
tx: DashAdapter,
|
|
130
|
-
a: { id: string; author: Author; requestId: string },
|
|
131
|
-
rows: { draft: Row | null; dashboard: Row },
|
|
132
|
-
): Promise<number | null> {
|
|
133
|
-
const done = await earlier(tx, a.id, a.requestId, a.author);
|
|
134
|
-
const { draft, dashboard } = rows;
|
|
135
|
-
if (done !== null) {
|
|
136
|
-
if (draft?.["state"] === "open")
|
|
137
|
-
throw new HttpError(
|
|
138
|
-
409,
|
|
139
|
-
"REQUEST_REUSED",
|
|
140
|
-
`The request id "${a.requestId}" already published version ${String(done["n"])}. Send a new one for this publish.`,
|
|
141
|
-
);
|
|
142
|
-
return Number(done["n"]);
|
|
143
|
-
}
|
|
144
|
-
const quiet = draft?.["state"] === "closed" && draft["closed_by"] === a.requestId;
|
|
145
|
-
return quiet && dashboard["current_version"] !== null
|
|
146
|
-
? Number(dashboard["current_version"])
|
|
147
|
-
: null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Publishes the author's open draft as the dashboard's next version, in one transaction, and
|
|
152
|
-
* closes the draft. A retry with the same request id returns the version it already made.
|
|
153
|
-
*/
|
|
154
|
-
export function publish(
|
|
155
|
-
db: DashAdapter,
|
|
156
|
-
a: {
|
|
157
|
-
tenant: Tenant;
|
|
158
|
-
id: string;
|
|
159
|
-
author: Author;
|
|
160
|
-
expectedRevision: number;
|
|
161
|
-
requestId: string;
|
|
162
|
-
head: number;
|
|
163
|
-
},
|
|
164
|
-
): Promise<Published> {
|
|
165
|
-
return inTransaction(db, async (tx) => {
|
|
166
|
-
// Each row read once: the dashboard, then the author's draft.
|
|
167
|
-
const dashboard = await dashboardRow(tx, a.tenant, { id: a.id });
|
|
168
|
-
if (dashboard === null)
|
|
169
|
-
throw new HttpError(404, "NOT_FOUND", `There is no dashboard "${a.id}".`);
|
|
170
|
-
const draft = await draftRow(tx, a.id, a.author);
|
|
171
|
-
const before = await answeredBefore(tx, a, { draft, dashboard });
|
|
172
|
-
if (before !== null) return { dashboardId: a.id, version: before };
|
|
173
|
-
if (draft?.["state"] !== "open") throw draftClosed();
|
|
174
|
-
const row = draft;
|
|
175
|
-
expectRevision(row, a.expectedRevision);
|
|
176
|
-
const current = dashboard["current_version"];
|
|
177
|
-
// A draft with no edits changes nothing: it closes, and no empty version is made.
|
|
178
|
-
if (Number(row["revision"]) === 0 && current !== null) {
|
|
179
|
-
await advanceDraft(tx, row, { state: "closed", closed_by: a.requestId, updated_at: now() });
|
|
180
|
-
return { dashboardId: a.id, version: Number(current) };
|
|
181
|
-
}
|
|
182
|
-
const { doc, head, replayedOn } = await publishedDoc(tx, row, dashboard, a.head);
|
|
183
|
-
const n = head + 1;
|
|
184
|
-
await insertVersion(tx, {
|
|
185
|
-
...a,
|
|
186
|
-
dashboardId: dashboard["id"],
|
|
187
|
-
n,
|
|
188
|
-
doc,
|
|
189
|
-
basedOn: Number(row["base_version"]),
|
|
190
|
-
});
|
|
191
|
-
await advanceHead(tx, dashboard["id"], head, doc);
|
|
192
|
-
await tx.deleteMany({
|
|
193
|
-
table: "dd_dashboard_draft_command",
|
|
194
|
-
where: [{ field: "draft_id", value: row["id"] }],
|
|
195
|
-
});
|
|
196
|
-
await advanceDraft(tx, row, { state: "closed", updated_at: now() });
|
|
197
|
-
return { dashboardId: a.id, version: n, ...(replayedOn === undefined ? {} : { replayedOn }) };
|
|
198
|
-
});
|
|
199
|
-
}
|
package/src/store/publisher.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, DashboardStore } from "@derive-to/dashboards-core";
|
|
2
|
-
import { publish } from "./publish";
|
|
3
|
-
import { draftDocument, preview, rebase } from "./review";
|
|
4
|
-
import { restore, versions } from "./versions";
|
|
5
|
-
|
|
6
|
-
type Publishing = Pick<
|
|
7
|
-
DashboardStore,
|
|
8
|
-
"preview" | "draftDocument" | "rebase" | "publish" | "versions" | "restore"
|
|
9
|
-
>;
|
|
10
|
-
|
|
11
|
-
/** What goes live and how: review, publish, history and restore. */
|
|
12
|
-
export function publishMethods(db: DashAdapter): Publishing {
|
|
13
|
-
return {
|
|
14
|
-
preview: (a) => preview(db, a),
|
|
15
|
-
draftDocument: (a) => draftDocument(db, a),
|
|
16
|
-
rebase: (a) => rebase(db, a),
|
|
17
|
-
publish: (a) => publish(db, a),
|
|
18
|
-
versions: (a) => versions(db, a),
|
|
19
|
-
restore: (a) => restore(db, a),
|
|
20
|
-
};
|
|
21
|
-
}
|