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
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import type { IntrospectingConnection, ReadSession, RunOptions } from "@derive-to/dashboards-core";
|
|
2
|
-
import { postgres } from "@derive-to/dashboards-core/engine";
|
|
3
|
-
import { bounded, pastDeadline } from "./bounded";
|
|
4
|
-
import { HttpError } from "./http";
|
|
5
|
-
import { describePostgres, introspectPostgres } from "./introspect";
|
|
6
|
-
|
|
7
|
-
/** Runs one statement with its parameters and returns its rows as arrays, in select order. */
|
|
8
|
-
export type RunSql = (
|
|
9
|
-
text: string,
|
|
10
|
-
params: readonly unknown[],
|
|
11
|
-
) => Promise<readonly (readonly unknown[])[]>;
|
|
12
|
-
|
|
13
|
-
export type PostgresConnectionOptions = {
|
|
14
|
-
/** Defaults to "main". */
|
|
15
|
-
id?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Runs a statement on any connection. Without `transaction`, a statement past the request's
|
|
18
|
-
* deadline is answered with QUERY_TIMEOUT on time, but keeps running in the database until
|
|
19
|
-
* it ends: only `transaction` sets a statement timeout that stops it there.
|
|
20
|
-
*/
|
|
21
|
-
run: RunSql;
|
|
22
|
-
/**
|
|
23
|
-
* Runs `fn` inside one transaction on one connection. With it, every statement of a request
|
|
24
|
-
* reads one snapshot; without it, each statement reads its own. `begin` starts that
|
|
25
|
-
* transaction in one round trip: BEGIN with a read-only REPEATABLE READ snapshot, then the
|
|
26
|
-
* request's statement timeout, two statements without parameters.
|
|
27
|
-
*/
|
|
28
|
-
transaction?: <T>(fn: (run: RunSql) => Promise<T>, begin: string) => Promise<T>;
|
|
29
|
-
/**
|
|
30
|
-
* True when `transaction` sends the `begin` it is given in place of its own BEGIN
|
|
31
|
-
* (node-postgres: `await client.query(begin)`). Otherwise it begins its own way (PGlite's
|
|
32
|
-
* `db.transaction`) and the snapshot and the timeout are set after, one round trip each.
|
|
33
|
-
*/
|
|
34
|
-
sendsBegin?: boolean;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const SNAPSHOT = "ISOLATION LEVEL REPEATABLE READ, READ ONLY";
|
|
38
|
-
|
|
39
|
-
/** The statements that make a transaction a request's: its snapshot, then its timeout. */
|
|
40
|
-
function settings(deadline: number) {
|
|
41
|
-
const timeout = `SET LOCAL statement_timeout = ${Math.max(1, Math.ceil(deadline - Date.now()))}`;
|
|
42
|
-
return {
|
|
43
|
-
begin: `BEGIN ${SNAPSHOT}; ${timeout}`,
|
|
44
|
-
snapshot: `SET TRANSACTION ${SNAPSHOT}`,
|
|
45
|
-
timeout,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Postgres cancelled the statement: its statement_timeout passed. */
|
|
50
|
-
const QUERY_CANCELED = "57014";
|
|
51
|
-
|
|
52
|
-
function timedOut(error: unknown): boolean {
|
|
53
|
-
return (
|
|
54
|
-
typeof error === "object" && error !== null && "code" in error && error.code === QUERY_CANCELED
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Postgres could not read a given value as its column's type: invalid text for a number or a
|
|
60
|
-
* uuid (22P02), or for a time (22007). The engine's own arithmetic raises neither.
|
|
61
|
-
*/
|
|
62
|
-
const BAD_VALUE = new Set(["22P02", "22007"]);
|
|
63
|
-
|
|
64
|
-
const badValue = (error: unknown): boolean =>
|
|
65
|
-
typeof error === "object" &&
|
|
66
|
-
error !== null &&
|
|
67
|
-
"code" in error &&
|
|
68
|
-
typeof error.code === "string" &&
|
|
69
|
-
BAD_VALUE.has(error.code);
|
|
70
|
-
|
|
71
|
-
/** The engine reads columns by position; rows as objects mean `rowMode: "array"` was left out. */
|
|
72
|
-
function arrayRows<R>(rows: readonly R[]): readonly R[] {
|
|
73
|
-
if (rows.length > 0 && !Array.isArray(rows[0]))
|
|
74
|
-
throw new Error(
|
|
75
|
-
'postgresConnection\'s run must return rows as arrays, in select order (node-postgres: rowMode: "array").',
|
|
76
|
-
);
|
|
77
|
-
return rows;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Each statement checks the request is still wanted and inside its deadline before it runs,
|
|
82
|
-
* and is answered no later than the deadline, whether or not the database stops it.
|
|
83
|
-
*/
|
|
84
|
-
function sessionOf(run: RunSql, options: RunOptions, concurrent: boolean): ReadSession {
|
|
85
|
-
return {
|
|
86
|
-
...(concurrent ? { concurrent: true } : {}),
|
|
87
|
-
run: async (sql) => {
|
|
88
|
-
options.signal.throwIfAborted();
|
|
89
|
-
if (Date.now() >= options.deadline) throw pastDeadline();
|
|
90
|
-
try {
|
|
91
|
-
return { rows: arrayRows(await bounded(run(sql.text, sql.params), options)) };
|
|
92
|
-
} catch (error) {
|
|
93
|
-
if (timedOut(error)) throw pastDeadline();
|
|
94
|
-
// Fails closed with a reason, never as a server error: most often the viewer's tenant
|
|
95
|
-
// value does not fit the tenant column's type (an id "acme" on an integer column).
|
|
96
|
-
// Postgres's own message can echo stored values, so it goes to the host only.
|
|
97
|
-
if (badValue(error))
|
|
98
|
-
throw new HttpError(
|
|
99
|
-
400,
|
|
100
|
-
"QUERY_INVALID",
|
|
101
|
-
"A value does not fit its column's type in the database. Check the viewer's tenant values against the tenant columns, and the query's filter values.",
|
|
102
|
-
{ cause: error },
|
|
103
|
-
);
|
|
104
|
-
throw error;
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* A Postgres connection for the engine, from two small functions the host writes for its
|
|
112
|
-
* driver: one that runs a statement (rows as arrays, in select order), and one that runs a
|
|
113
|
-
* function inside a transaction on one client, sending `begin` as its BEGIN (`sendsBegin`).
|
|
114
|
-
* With node-postgres, `nodePostgres(pool)` writes both, and the store's, the safe way.
|
|
115
|
-
*/
|
|
116
|
-
export function postgresConnection(options: PostgresConnectionOptions): IntrospectingConnection {
|
|
117
|
-
return {
|
|
118
|
-
id: options.id ?? "main",
|
|
119
|
-
dialect: postgres,
|
|
120
|
-
introspect: () => introspectPostgres(options.run),
|
|
121
|
-
describe: (query, columns) => describePostgres(options.run, query, columns),
|
|
122
|
-
|
|
123
|
-
async session(runOptions, fn) {
|
|
124
|
-
const { transaction } = options;
|
|
125
|
-
if (transaction === undefined) {
|
|
126
|
-
// No transaction: every statement is its own, so a tile's independent ones go out at once.
|
|
127
|
-
return {
|
|
128
|
-
value: await fn(sessionOf(options.run, runOptions, true)),
|
|
129
|
-
consistency: "statement",
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
const { begin, snapshot, timeout } = settings(runOptions.deadline);
|
|
133
|
-
const value = await transaction(async (run) => {
|
|
134
|
-
if (options.sendsBegin !== true) {
|
|
135
|
-
await run(snapshot, []);
|
|
136
|
-
await run(timeout, []);
|
|
137
|
-
}
|
|
138
|
-
return fn(sessionOf(run, runOptions, false));
|
|
139
|
-
}, begin);
|
|
140
|
-
return { value, consistency: "snapshot" };
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
}
|
package/src/postgres-store.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import type { DashAdapter, Row } from "@derive-to/dashboards-core";
|
|
2
|
-
import { type SqlRunner, sqlAdapter } from "./adapter/sql-adapter";
|
|
3
|
-
import type { Statement } from "./adapter/statement";
|
|
4
|
-
|
|
5
|
-
/** Runs one statement with its parameters and returns its rows as objects keyed by column. */
|
|
6
|
-
export type RunRows = (text: string, params: readonly unknown[]) => Promise<readonly Row[]>;
|
|
7
|
-
|
|
8
|
-
export type PostgresStoreOptions = {
|
|
9
|
-
/** Runs a statement on any connection. */
|
|
10
|
-
run: RunRows;
|
|
11
|
-
/**
|
|
12
|
-
* Runs `fn` inside one transaction on one connection, committing when it resolves and
|
|
13
|
-
* rolling back when it throws. Every write of the store is one transaction.
|
|
14
|
-
*/
|
|
15
|
-
transaction: <T>(fn: (run: RunRows) => Promise<T>) => Promise<T>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/** A statement's text with Postgres's numbered placeholders. */
|
|
19
|
-
const textOf = (s: Statement) => s.strings.reduce((all, part, i) => `${all}$${i}${part}`);
|
|
20
|
-
|
|
21
|
-
/** The store reads rows by column name; rows as arrays mean the driver was set up for queries. */
|
|
22
|
-
function objectRows(rows: readonly Row[]): Row[] {
|
|
23
|
-
if (rows.length > 0 && Array.isArray(rows[0]))
|
|
24
|
-
throw new Error(
|
|
25
|
-
'postgresStore\'s run must return rows as objects by column name, not arrays: give it a run without rowMode: "array".',
|
|
26
|
-
);
|
|
27
|
-
return [...rows];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function runner(options: PostgresStoreOptions, run: RunRows, depth: number): SqlRunner {
|
|
31
|
-
const self: SqlRunner = {
|
|
32
|
-
dialect: "postgres",
|
|
33
|
-
run: async (statement) => objectRows(await run(textOf(statement), statement.values)),
|
|
34
|
-
async execute(ddl) {
|
|
35
|
-
await run(ddl, []);
|
|
36
|
-
},
|
|
37
|
-
// Outside a transaction, a new one; inside one, a savepoint, so a step can fail alone.
|
|
38
|
-
transaction: async (fn) => {
|
|
39
|
-
if (depth === 0) return options.transaction((tx) => fn(runner(options, tx, 1)));
|
|
40
|
-
const savepoint = `dd_step_${depth}`;
|
|
41
|
-
await run(`SAVEPOINT ${savepoint}`, []);
|
|
42
|
-
try {
|
|
43
|
-
const value = await fn(runner(options, run, depth + 1));
|
|
44
|
-
await run(`RELEASE SAVEPOINT ${savepoint}`, []);
|
|
45
|
-
return value;
|
|
46
|
-
} catch (error) {
|
|
47
|
-
await run(`ROLLBACK TO SAVEPOINT ${savepoint}`, []);
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
return self;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Where dashboards are stored, in the host's own Postgres database, through whatever driver it
|
|
57
|
-
* already uses: give it a function that runs a statement and returns rows as objects, and one
|
|
58
|
-
* that runs a transaction. With node-postgres:
|
|
59
|
-
*
|
|
60
|
-
* postgresStore({
|
|
61
|
-
* run: async (text, params) => (await pool.query(text, [...params])).rows,
|
|
62
|
-
* transaction: async (fn) => {
|
|
63
|
-
* const client = await pool.connect();
|
|
64
|
-
* try {
|
|
65
|
-
* await client.query("BEGIN");
|
|
66
|
-
* const value = await fn(async (text, params) => (await client.query(text, [...params])).rows);
|
|
67
|
-
* await client.query("COMMIT");
|
|
68
|
-
* return value;
|
|
69
|
-
* } catch (error) {
|
|
70
|
-
* await client.query("ROLLBACK");
|
|
71
|
-
* throw error;
|
|
72
|
-
* } finally {
|
|
73
|
-
* client.release();
|
|
74
|
-
* }
|
|
75
|
-
* },
|
|
76
|
-
* })
|
|
77
|
-
*
|
|
78
|
-
* Its `dd_` tables come from `generate` in your own schema, or from `createTables`.
|
|
79
|
-
*/
|
|
80
|
-
export function postgresStore(options: PostgresStoreOptions): DashAdapter {
|
|
81
|
-
return sqlAdapter("postgres", runner(options, options.run, 0));
|
|
82
|
-
}
|
package/src/principal.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { DashOptions, Principal, Viewer } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "./http";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The signed-in viewer, its tenant narrowed to the model's keys: what the store and the cache
|
|
6
|
-
* scope by is exactly what queries are scoped by, and `{}` in an app of one tenant.
|
|
7
|
-
*/
|
|
8
|
-
export async function principalOf(
|
|
9
|
-
options: DashOptions,
|
|
10
|
-
request: Request,
|
|
11
|
-
given?: Viewer,
|
|
12
|
-
): Promise<Principal | null> {
|
|
13
|
-
const viewer = given ?? (await options.viewer(request));
|
|
14
|
-
if (viewer === null) return null;
|
|
15
|
-
const values = viewer.tenant ?? {};
|
|
16
|
-
const tenant = Object.fromEntries(
|
|
17
|
-
(options.model.tenant?.keys ?? []).flatMap((k) => {
|
|
18
|
-
const value = values[k] ?? "";
|
|
19
|
-
return value === "" ? [] : [[k, value] as const];
|
|
20
|
-
}),
|
|
21
|
-
);
|
|
22
|
-
return { channel: "user", viewer: { ...viewer, tenant } };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** With tenant keys, a viewer without a value for each reads and writes nothing, stored or not. */
|
|
26
|
-
export function checkTenant(
|
|
27
|
-
options: DashOptions,
|
|
28
|
-
principal: Principal & { channel: "user" },
|
|
29
|
-
): void {
|
|
30
|
-
const missing = (options.model.tenant?.keys ?? []).find(
|
|
31
|
-
(k) => (principal.viewer.tenant?.[k] ?? "") === "",
|
|
32
|
-
);
|
|
33
|
-
if (missing !== undefined)
|
|
34
|
-
throw new HttpError(
|
|
35
|
-
403,
|
|
36
|
-
"TENANT_MISSING",
|
|
37
|
-
`The viewer has no value for the tenant key "${missing}": viewer() must return it in the viewer's tenant.`,
|
|
38
|
-
);
|
|
39
|
-
}
|
package/src/reading.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { CustomSource, DashOptions, QueryConnection } from "@derive-to/dashboards-core";
|
|
2
|
-
import { customReader, type Reader, sqlReader } from "@derive-to/dashboards-core/engine";
|
|
3
|
-
import { boundedSource, isCustomSource } from "./custom-source";
|
|
4
|
-
|
|
5
|
-
/** Whether a value is a connection the engine can read through. */
|
|
6
|
-
function isConnection(handle: unknown): handle is QueryConnection {
|
|
7
|
-
return (
|
|
8
|
-
typeof handle === "object" && handle !== null && "session" in handle && "dialect" in handle
|
|
9
|
-
);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** Whether a value is anything `connections` may hold: a database or a host's data layer. */
|
|
13
|
-
export const isSource = (handle: unknown): handle is QueryConnection | CustomSource =>
|
|
14
|
-
isConnection(handle) || isCustomSource(handle);
|
|
15
|
-
|
|
16
|
-
/** What one read needs from its request: the options, the request, the source and the cap. */
|
|
17
|
-
export type ReadAt = { options: DashOptions; request: Request; source: string; maxRows: number };
|
|
18
|
-
|
|
19
|
-
/** The connection a source reads from; per request where the host gives a function. */
|
|
20
|
-
function connectionOf(at: ReadAt): QueryConnection | CustomSource {
|
|
21
|
-
const connection = at.options.model.sources[at.source]?.in ?? "";
|
|
22
|
-
const entry = at.options.connections[connection];
|
|
23
|
-
const handle: unknown = typeof entry === "function" ? entry(at.request) : entry;
|
|
24
|
-
if (!isSource(handle))
|
|
25
|
-
throw new Error(
|
|
26
|
-
`\`connections.${connection}\` gave no connection for this request. It must return postgresConnection({ run, transaction }) or customSource({ answer }).`,
|
|
27
|
-
);
|
|
28
|
-
return handle;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type Reading<T> = {
|
|
32
|
-
value: T;
|
|
33
|
-
consistency: "snapshot" | "statement";
|
|
34
|
-
/** Whether the host's own data layer answered, not the library's SQL. */
|
|
35
|
-
custom: boolean;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Runs `fn` with a reader for the source's connection, under the request's deadline and abort:
|
|
40
|
-
* in one read-only session of a database, or through the host's custom source, each of its
|
|
41
|
-
* calls bounded by the same deadline.
|
|
42
|
-
*/
|
|
43
|
-
export async function withReader<T>(
|
|
44
|
-
at: ReadAt,
|
|
45
|
-
fn: (reader: Reader) => Promise<T>,
|
|
46
|
-
): Promise<Reading<T>> {
|
|
47
|
-
const source = connectionOf(at);
|
|
48
|
-
const deadline = Date.now() + (at.options.query?.deadlineMs ?? 15_000);
|
|
49
|
-
const run = { signal: at.request.signal, deadline, maxRows: at.maxRows };
|
|
50
|
-
if (isCustomSource(source)) {
|
|
51
|
-
const value = await fn(customReader(boundedSource(source), run));
|
|
52
|
-
return { value, consistency: "statement", custom: true };
|
|
53
|
-
}
|
|
54
|
-
const { value, consistency } = await source.session(run, (s) => fn(sqlReader(s, source.dialect)));
|
|
55
|
-
return { value, consistency, custom: false };
|
|
56
|
-
}
|
package/src/request-rows.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { canonical, type DashAdapter, type Row, type Tenant } from "@derive-to/dashboards-core";
|
|
2
|
-
import { dashboardRow } from "./store/rows";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* The dashboard rows one request's checks read (who made it, for `can`; which stored row an id
|
|
6
|
-
* names), read once per request. A request's writes read their rows again in their own
|
|
7
|
-
* transaction, so nothing here decides a write.
|
|
8
|
-
*/
|
|
9
|
-
const read = new WeakMap<Request, Map<string, Promise<Row | null>>>();
|
|
10
|
-
|
|
11
|
-
export function requestDashboardRow(
|
|
12
|
-
request: Request,
|
|
13
|
-
adapter: DashAdapter,
|
|
14
|
-
tenant: Tenant,
|
|
15
|
-
by: { id: string } | { key: string },
|
|
16
|
-
): Promise<Row | null> {
|
|
17
|
-
const rows = read.get(request) ?? new Map<string, Promise<Row | null>>();
|
|
18
|
-
read.set(request, rows);
|
|
19
|
-
const key = canonical({ tenant, by });
|
|
20
|
-
const found = rows.get(key) ?? dashboardRow(adapter, tenant, by);
|
|
21
|
-
rows.set(key, found);
|
|
22
|
-
return found;
|
|
23
|
-
}
|
package/src/request.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Calendar, DashOptions, Doc, Principal, Viewer } from "@derive-to/dashboards-core";
|
|
2
|
-
import { HttpError } from "./http";
|
|
3
|
-
|
|
4
|
-
/** The signed-in viewer a request runs as; anything else is a 401. */
|
|
5
|
-
export function userOf(principal: Principal | null): Principal & { viewer: Viewer } {
|
|
6
|
-
if (principal?.channel !== "user") throw new HttpError(401, "UNAUTHORIZED", "Sign in first.");
|
|
7
|
-
return principal;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** The request's one `now`: the host's clock, which tests and samples pin. */
|
|
11
|
-
export function nowOf(options: DashOptions): Date {
|
|
12
|
-
return options.now?.() ?? new Date();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** The request's calendar: the dashboard's zone, then the host's, then UTC. */
|
|
16
|
-
export function calendarOf(options: DashOptions, doc?: Doc): Calendar {
|
|
17
|
-
return {
|
|
18
|
-
timezone: doc?.scope.timezone ?? options.time?.timezone ?? "UTC",
|
|
19
|
-
weekStart: options.time?.weekStart ?? 1,
|
|
20
|
-
fiscalYearStartMonth: options.time?.fiscalYearStartMonth ?? 1,
|
|
21
|
-
};
|
|
22
|
-
}
|
package/src/storage.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { SecondaryStorage } from "@derive-to/dashboards-core";
|
|
2
|
-
|
|
3
|
-
const MEMORY_ENTRIES = 1000;
|
|
4
|
-
|
|
5
|
-
/** The process's own storage when the host gives none: a bounded map, oldest entry out first. */
|
|
6
|
-
export function memoryStorage(limit = MEMORY_ENTRIES): SecondaryStorage {
|
|
7
|
-
return memoryMap<string>(limit);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** Values kept in this process, the oldest dropped past `limit`, each until its ttl (seconds). */
|
|
11
|
-
function memoryMap<T>(limit: number) {
|
|
12
|
-
const entries = new Map<string, { value: T; expires: number }>();
|
|
13
|
-
return {
|
|
14
|
-
get(key: string): T | null {
|
|
15
|
-
const entry = entries.get(key);
|
|
16
|
-
if (entry === undefined || entry.expires <= Date.now()) {
|
|
17
|
-
entries.delete(key);
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
return entry.value;
|
|
21
|
-
},
|
|
22
|
-
set(key: string, value: T, ttl?: number): void {
|
|
23
|
-
entries.delete(key);
|
|
24
|
-
entries.set(key, { value, expires: ttl === undefined ? Infinity : Date.now() + ttl * 1000 });
|
|
25
|
-
const oldest = entries.keys().next();
|
|
26
|
-
if (entries.size > limit && oldest.done !== true) entries.delete(oldest.value);
|
|
27
|
-
},
|
|
28
|
-
delete(key: string): void {
|
|
29
|
-
entries.delete(key);
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Where answers are kept: as JSON in the host's storage, or as they are in this process's memory. */
|
|
35
|
-
export type Entries<T> = {
|
|
36
|
-
get(key: string): Promise<T | null>;
|
|
37
|
-
set(key: string, entry: T, ttl: number): Promise<void>;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export function entriesIn<T>(storage: SecondaryStorage | undefined): Entries<T> {
|
|
41
|
-
if (storage === undefined) {
|
|
42
|
-
// In memory there is nothing to parse: an answer read back is the object stored.
|
|
43
|
-
const map = memoryMap<T>(MEMORY_ENTRIES);
|
|
44
|
-
return {
|
|
45
|
-
get: (key) => Promise.resolve(map.get(key)),
|
|
46
|
-
set: (key, entry, ttl) => {
|
|
47
|
-
map.set(key, entry, ttl);
|
|
48
|
-
return Promise.resolve();
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
async get(key) {
|
|
54
|
-
const stored = await storage.get(key);
|
|
55
|
-
// The storage holds only what this class wrote.
|
|
56
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
57
|
-
return stored === null ? null : (JSON.parse(stored) as T);
|
|
58
|
-
},
|
|
59
|
-
set: async (key, entry, ttl) => {
|
|
60
|
-
await storage.set(key, JSON.stringify(entry), ttl);
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
}
|
package/src/store/apply.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Author,
|
|
3
|
-
type Command,
|
|
4
|
-
type CommandBody,
|
|
5
|
-
type CommandId,
|
|
6
|
-
canonical,
|
|
7
|
-
type DashAdapter,
|
|
8
|
-
type Draft,
|
|
9
|
-
type Row,
|
|
10
|
-
reduceAll,
|
|
11
|
-
type Tenant,
|
|
12
|
-
} from "@derive-to/dashboards-core";
|
|
13
|
-
import { sha256 } from "../cache";
|
|
14
|
-
import { HttpError } from "../http";
|
|
15
|
-
import { writeCommands } from "./commands";
|
|
16
|
-
import {
|
|
17
|
-
advanceDraft,
|
|
18
|
-
draftOf,
|
|
19
|
-
expectRevision,
|
|
20
|
-
inTransaction,
|
|
21
|
-
namedOf,
|
|
22
|
-
now,
|
|
23
|
-
openDraft,
|
|
24
|
-
} from "./rows";
|
|
25
|
-
|
|
26
|
-
/** Applying edits to a draft: each batch all or nothing, each command once. */
|
|
27
|
-
|
|
28
|
-
const bodyOf = ({ commandId: _id, ...body }: Command): CommandBody => body;
|
|
29
|
-
const digestOf = (command: Command) => sha256(canonical(bodyOf(command)));
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Commands this draft already applied, found in one read: by id, a retry with the same body is
|
|
33
|
-
* acknowledged; the same id with another body is refused.
|
|
34
|
-
*/
|
|
35
|
-
async function newCommands(
|
|
36
|
-
tx: DashAdapter,
|
|
37
|
-
draftId: unknown,
|
|
38
|
-
commands: readonly Command[],
|
|
39
|
-
): Promise<{ fresh: Command[]; acknowledged: CommandId[] }> {
|
|
40
|
-
// Only this batch's ids: an edit reads none of the draft's other commands.
|
|
41
|
-
const seen = await tx.findMany({
|
|
42
|
-
table: "dd_dashboard_draft_command",
|
|
43
|
-
where: [
|
|
44
|
-
{ field: "draft_id", value: draftId },
|
|
45
|
-
{ field: "command_id", op: "in", value: commands.map((c) => c.commandId) },
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
const digests = new Map(seen.map((r) => [String(r["command_id"]), String(r["digest"])]));
|
|
49
|
-
const fresh: Command[] = [];
|
|
50
|
-
const acknowledged: CommandId[] = [];
|
|
51
|
-
for (const command of commands) {
|
|
52
|
-
const stored = digests.get(command.commandId);
|
|
53
|
-
// oxlint-disable-next-line no-await-in-loop -- a digest is a hash of this one command
|
|
54
|
-
const same = stored !== undefined && stored === (await digestOf(command));
|
|
55
|
-
if (stored === undefined) fresh.push(command);
|
|
56
|
-
else if (same) acknowledged.push(command.commandId);
|
|
57
|
-
else
|
|
58
|
-
throw new HttpError(
|
|
59
|
-
409,
|
|
60
|
-
"DUPLICATE_COMMAND_ID",
|
|
61
|
-
`The command id "${command.commandId}" was used for another edit.`,
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
return { fresh, acknowledged };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** The draft row's summary after one more edit: a step more, the widgets it names, the next sequence. */
|
|
68
|
-
function summed(row: Row, current: Draft, fresh: readonly Command[]) {
|
|
69
|
-
const moved = fresh.flatMap((c) => {
|
|
70
|
-
const body = bodyOf(c);
|
|
71
|
-
return body.type === "widget.move" ? [body.id] : [];
|
|
72
|
-
});
|
|
73
|
-
const named = new Set([...namedOf(row), ...moved]);
|
|
74
|
-
return {
|
|
75
|
-
steps: current.steps + 1,
|
|
76
|
-
named: [...named].toSorted(),
|
|
77
|
-
next_seq: Number(row["next_seq"]) + fresh.length,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Applies a batch of commands to the author's open draft, made against `expectedRevision`: all or
|
|
83
|
-
* nothing, a refused command refuses the batch with its reason. The commands are kept, in
|
|
84
|
-
* order, so a publish on top of someone else's version can replay them.
|
|
85
|
-
*/
|
|
86
|
-
export function applyDraft(
|
|
87
|
-
db: DashAdapter,
|
|
88
|
-
a: {
|
|
89
|
-
tenant: Tenant;
|
|
90
|
-
id: string;
|
|
91
|
-
author: Author;
|
|
92
|
-
expectedRevision: number;
|
|
93
|
-
commands: readonly Command[];
|
|
94
|
-
},
|
|
95
|
-
): Promise<{ draft: Draft; acknowledged: readonly CommandId[] }> {
|
|
96
|
-
return inTransaction(db, async (tx) => {
|
|
97
|
-
const { row } = await openDraft(tx, a);
|
|
98
|
-
const { fresh, acknowledged } = await newCommands(tx, row["id"], a.commands);
|
|
99
|
-
const current = draftOf(row);
|
|
100
|
-
// A retry of a batch already applied is acknowledged whatever revision it names: the
|
|
101
|
-
// client that lost the answer still holds the revision it sent the batch against.
|
|
102
|
-
if (fresh.length === 0) return { draft: current, acknowledged };
|
|
103
|
-
expectRevision(row, a.expectedRevision);
|
|
104
|
-
const outcome = reduceAll(
|
|
105
|
-
current.doc,
|
|
106
|
-
fresh.map((c) => bodyOf(c)),
|
|
107
|
-
);
|
|
108
|
-
if (!outcome.ok)
|
|
109
|
-
throw new HttpError(400, "EDIT_REFUSED", outcome.issue.message, {
|
|
110
|
-
issues: [outcome.issue],
|
|
111
|
-
});
|
|
112
|
-
const revision = current.revision + 1;
|
|
113
|
-
const at = now();
|
|
114
|
-
const first = Number(row["next_seq"]);
|
|
115
|
-
const set = { spec: outcome.doc, revision, ...summed(row, current, fresh), updated_at: at };
|
|
116
|
-
await advanceDraft(tx, row, set);
|
|
117
|
-
const added = await Promise.all(
|
|
118
|
-
fresh.map(async (c) => ({
|
|
119
|
-
commandId: c.commandId,
|
|
120
|
-
body: bodyOf(c),
|
|
121
|
-
revision,
|
|
122
|
-
digest: await digestOf(c),
|
|
123
|
-
})),
|
|
124
|
-
);
|
|
125
|
-
await writeCommands(tx, { draftId: row["id"], commands: added, first, at });
|
|
126
|
-
const draft = draftOf({ ...row, ...set });
|
|
127
|
-
return { draft, acknowledged };
|
|
128
|
-
});
|
|
129
|
-
}
|
package/src/store/commands.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { CommandBody, DashAdapter, Recorded } from "@derive-to/dashboards-core";
|
|
2
|
-
import { now } from "./rows";
|
|
3
|
-
|
|
4
|
-
/** A draft's commands as it keeps them: each with the edit (revision) it came in. */
|
|
5
|
-
|
|
6
|
-
export type Kept = Recorded & { revision: number; digest: string };
|
|
7
|
-
|
|
8
|
-
/** The draft's commands in the order they were applied. */
|
|
9
|
-
export async function keptCommands(tx: DashAdapter, draftId: unknown): Promise<Kept[]> {
|
|
10
|
-
const rows = await tx.findMany({
|
|
11
|
-
table: "dd_dashboard_draft_command",
|
|
12
|
-
where: [{ field: "draft_id", value: draftId }],
|
|
13
|
-
orderBy: [{ field: "seq", direction: "asc" }],
|
|
14
|
-
});
|
|
15
|
-
return rows.map((r) => ({
|
|
16
|
-
commandId: String(r["command_id"]),
|
|
17
|
-
// The bodies were checked by the command schema when they were applied.
|
|
18
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
19
|
-
body: r["body"] as CommandBody,
|
|
20
|
-
revision: Number(r["revision"]),
|
|
21
|
-
digest: String(r["digest"]),
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** A draft's commands as rows, numbered from `first`; written in one statement where the store can. */
|
|
26
|
-
export async function writeCommands(
|
|
27
|
-
tx: DashAdapter,
|
|
28
|
-
a: { draftId: unknown; commands: readonly Kept[]; first: number; at: string },
|
|
29
|
-
): Promise<void> {
|
|
30
|
-
const rows = a.commands.map((k, i) => ({
|
|
31
|
-
draft_id: a.draftId,
|
|
32
|
-
command_id: k.commandId,
|
|
33
|
-
digest: k.digest,
|
|
34
|
-
body: k.body,
|
|
35
|
-
revision: k.revision,
|
|
36
|
-
seq: a.first + i,
|
|
37
|
-
created_at: a.at,
|
|
38
|
-
}));
|
|
39
|
-
if (rows.length === 0) return;
|
|
40
|
-
if (tx.createMany !== undefined) {
|
|
41
|
-
await tx.createMany({ table: "dd_dashboard_draft_command", rows });
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
// oxlint-disable-next-line no-await-in-loop -- a store without createMany, one row at a time
|
|
45
|
-
for (const data of rows) await tx.create({ table: "dd_dashboard_draft_command", data });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Replaces a draft's kept commands with these, in order. */
|
|
49
|
-
export async function rewriteCommands(
|
|
50
|
-
tx: DashAdapter,
|
|
51
|
-
draftId: unknown,
|
|
52
|
-
kept: readonly Kept[],
|
|
53
|
-
): Promise<void> {
|
|
54
|
-
await tx.deleteMany({
|
|
55
|
-
table: "dd_dashboard_draft_command",
|
|
56
|
-
where: [{ field: "draft_id", value: draftId }],
|
|
57
|
-
});
|
|
58
|
-
await writeCommands(tx, { draftId, commands: kept, first: 0, at: now() });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** How many edits Undo can step back through: one per revision that applied commands. */
|
|
62
|
-
const stepsIn = (kept: readonly Kept[]) => new Set(kept.map((k) => k.revision)).size;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The widgets a draft's commands move, so a move of any other is read as room made: a title
|
|
66
|
-
* edit names a widget, but a push from another tile's resize is still not its own move.
|
|
67
|
-
*/
|
|
68
|
-
const namedBy = (kept: readonly Recorded[]) =>
|
|
69
|
-
new Set(kept.flatMap((k) => (k.body.type === "widget.move" ? [k.body.id] : [])));
|
|
70
|
-
|
|
71
|
-
/** The draft row's columns that sum up its kept commands, for a write that has them all. */
|
|
72
|
-
export const keptColumns = (kept: readonly Kept[]) => ({
|
|
73
|
-
steps: stepsIn(kept),
|
|
74
|
-
named: [...namedBy(kept)].toSorted(),
|
|
75
|
-
});
|