spatial-views 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 +201 -19
- package/dist/app/assets/{abnfDiagram-S7VZAUFO-BVgtb0Hw.js → abnfDiagram-S7VZAUFO-CFbb_Sz2.js} +1 -1
- package/dist/app/assets/{architecture-WOLXFQ4H-DxylGHqT.js → architecture-WOLXFQ4H-CsBD__5a.js} +1 -1
- package/dist/app/assets/architectureDiagram-YGSQEFNW-C0ic4ib1.js +36 -0
- package/dist/app/assets/{blockDiagram-APKKGZFW-C7fb3MCE.js → blockDiagram-APKKGZFW-BZDPbzAG.js} +1 -1
- package/dist/app/assets/{c4Diagram-KJ2TOQU5-Bkb_0fbd.js → c4Diagram-KJ2TOQU5-BilBWuq5.js} +1 -1
- package/dist/app/assets/{chunk-6MUBBAK2-DU4vZ03M.js → chunk-6MUBBAK2-BBl-4tQr.js} +1 -1
- package/dist/app/assets/{chunk-AXFCVDMN-De96BVTB.js → chunk-AXFCVDMN-BhsttQ-k.js} +1 -1
- package/dist/app/assets/{chunk-BG4553SI-J33jsRLV.js → chunk-BG4553SI-mWcPcFZr.js} +2 -2
- package/dist/app/assets/{chunk-DQASFODM-Bh85SSGO.js → chunk-DQASFODM-7FKe2gLt.js} +1 -1
- package/dist/app/assets/{chunk-HFQBQIA3-CCklgplR.js → chunk-HFQBQIA3-nXrQ08BL.js} +1 -1
- package/dist/app/assets/{chunk-KQLB2F66-B04qR8ED.js → chunk-KQLB2F66-swukhFmZ.js} +1 -1
- package/dist/app/assets/{chunk-KS47KZWR-BsqDyyAt.js → chunk-KS47KZWR-DkDkwO82.js} +1 -1
- package/dist/app/assets/{chunk-LEVMRMFU-DwxrBz2u.js → chunk-LEVMRMFU-DXH6iXv3.js} +1 -1
- package/dist/app/assets/{chunk-M57EVK23-D9YIHjps.js → chunk-M57EVK23-B-kEyOJF.js} +1 -1
- package/dist/app/assets/{chunk-OY52VR5R-WB63DeTA.js → chunk-OY52VR5R-DweG7qYw.js} +1 -1
- package/dist/app/assets/{chunk-QB3YPZ4I-cHzzKdtb.js → chunk-QB3YPZ4I-DyozK3lV.js} +1 -1
- package/dist/app/assets/{chunk-R3KSYZAL-C7rDagaa.js → chunk-R3KSYZAL-BEXCUN6J.js} +1 -1
- package/dist/app/assets/{chunk-STOXU5M4-CG6pnJtH.js → chunk-STOXU5M4-BaTlty4O.js} +1 -1
- package/dist/app/assets/{chunk-TU62E6Q5-BbcGYfy_.js → chunk-TU62E6Q5-_rcUP1Fh.js} +1 -1
- package/dist/app/assets/chunk-X3CZISLH-NOpCnvgE.js +1 -0
- package/dist/app/assets/{chunk-X4PVCEUW-BUI1Oi3q.js → chunk-X4PVCEUW-DGxTL3kB.js} +1 -1
- package/dist/app/assets/{chunk-XXDRQBXY-DYhK5YW-.js → chunk-XXDRQBXY-C02Gpej2.js} +1 -1
- package/dist/app/assets/{classDiagram-v2-L25IHNXF-K-k5GTUn.js → classDiagram-v2-L25IHNXF-DLEBwpvk.js} +1 -1
- package/dist/app/assets/cose-bilkent-JH36ORCC-kNCXOm8k.js +1 -0
- package/dist/app/assets/cynefin-EF2NZ3EQ-6MWNLbuF.js +1 -0
- package/dist/app/assets/{cynefinDiagram-L36ESFBY-mkV33_Xx.js → cynefinDiagram-L36ESFBY-CLFstukD.js} +1 -1
- package/dist/app/assets/{d3-scale-ygdFCUH8.js → d3-scale-CaJQQYkF.js} +1 -1
- package/dist/app/assets/{dagre-SPEB5RXH-BNIymLsu.js → dagre-SPEB5RXH-BaSAny1T.js} +1 -1
- package/dist/app/assets/{diagram-3B7GL73J-CGOiolFW.js → diagram-3B7GL73J-DJNlt5Sg.js} +1 -1
- package/dist/app/assets/{diagram-DNEMPZML-DZFkHNly.js → diagram-DNEMPZML-DEHeuILB.js} +1 -1
- package/dist/app/assets/{diagram-L3MUGCDN-B42GAYLq.js → diagram-L3MUGCDN-ob5Ka2Xc.js} +1 -1
- package/dist/app/assets/{diagram-LA4NIPWU-BYuLRYId.js → diagram-LA4NIPWU-DNESzFAc.js} +1 -1
- package/dist/app/assets/{diagram-MHCOC4E2-DGzI6TJG.js → diagram-MHCOC4E2-CVi9zKjG.js} +1 -1
- package/dist/app/assets/{diagram-XPFHEONP-CTBEgDQ8.js → diagram-XPFHEONP-DInT4C7e.js} +1 -1
- package/dist/app/assets/dist-CjvHN74u.js +1 -0
- package/dist/app/assets/{ebnfDiagram-UZSYDL2S-GuMdOCvL.js → ebnfDiagram-UZSYDL2S-CRCZNsji.js} +1 -1
- package/dist/app/assets/{elk-XV6XMXQE-BpY3loT5.js → elk-XV6XMXQE-D4fsWI2l.js} +1 -1
- package/dist/app/assets/{erDiagram-EYBAPDEH-nDZ2PMGl.js → erDiagram-EYBAPDEH-YQNM5StV.js} +1 -1
- package/dist/app/assets/{eventmodeling-K75KTNOO-BN42Mgwh.js → eventmodeling-K75KTNOO-Bev08J2F.js} +1 -1
- package/dist/app/assets/flowDiagram-MALGAFMO-BhJ554DH.js +1 -0
- package/dist/app/assets/ganttDiagram-23REXITY-CkaBMYM4.js +292 -0
- package/dist/app/assets/gitGraph-VSP46ZUC-CtbZ0sIS.js +1 -0
- package/dist/app/assets/{gitGraphDiagram-STJD7YCW-CDkKLrvO.js → gitGraphDiagram-STJD7YCW-D15ZdQ-Z.js} +1 -1
- package/dist/app/assets/index-Bn6_wHEp.js +98 -0
- package/dist/app/assets/index-H-Z7MFhZ.css +1 -0
- package/dist/app/assets/info-OHQRW6UA-CfUW1XFR.js +1 -0
- package/dist/app/assets/{infoDiagram-KBXO4MBG-B92P8pjY.js → infoDiagram-KBXO4MBG-DT7LbBRQ.js} +1 -1
- package/dist/app/assets/{ishikawaDiagram-M35NCXHK-BRZsbCvK.js → ishikawaDiagram-M35NCXHK-CF51lhjB.js} +1 -1
- package/dist/app/assets/{journeyDiagram-OPLOXDXO-Bppwb1oF.js → journeyDiagram-OPLOXDXO-_36FonKk.js} +1 -1
- package/dist/app/assets/{kanban-definition-BZKZHSQY-ByD_x5cm.js → kanban-definition-BZKZHSQY-tLk_OlQ-.js} +1 -1
- package/dist/app/assets/{mermaid-parser.core-CKmnV0Md.js → mermaid-parser.core-_3Vo8x6b.js} +3 -3
- package/dist/app/assets/mermaid.core-y-x1CYxC.js +44 -0
- package/dist/app/assets/{mindmap-definition-I2QAS2VK-fRbUh23C.js → mindmap-definition-I2QAS2VK-BCI4HRX3.js} +1 -1
- package/dist/app/assets/packet-JDAUHWVQ-Cco3UoaI.js +1 -0
- package/dist/app/assets/{pegDiagram-FD4XPPHP-qIABf6H8.js → pegDiagram-FD4XPPHP-BOzSoNji.js} +1 -1
- package/dist/app/assets/pie-XZMESJXO-reOB2GXn.js +1 -0
- package/dist/app/assets/{pieDiagram-J4AKIIQI-Bb4RBdrc.js → pieDiagram-J4AKIIQI-PlHpwT_c.js} +1 -1
- package/dist/app/assets/{quadrantDiagram-XTYBLID2-B0CpcVWE.js → quadrantDiagram-XTYBLID2-CdxZ0ael.js} +1 -1
- package/dist/app/assets/radar-ABXABTNO-DfCl8uV2.js +1 -0
- package/dist/app/assets/railroad-I3PHUGI6-4s14UyW1.js +1 -0
- package/dist/app/assets/{railroad-abnf-LNEFI6M7-BUhw7Iuo.js → railroad-abnf-LNEFI6M7-C49jJE4Y.js} +1 -1
- package/dist/app/assets/{railroad-ebnf-DWYD2UWJ-nQNF56LJ.js → railroad-ebnf-DWYD2UWJ-Bx-pcPc7.js} +1 -1
- package/dist/app/assets/{railroad-peg-JR7BVNR7-CwvbVIo6.js → railroad-peg-JR7BVNR7-DjBPSCKV.js} +1 -1
- package/dist/app/assets/{railroadDiagram-MRQ6NCDL-Dwr-mPZL.js → railroadDiagram-MRQ6NCDL-wGcmIMqq.js} +1 -1
- package/dist/app/assets/{requirementDiagram-Y7LWUY3S-Ctv2DKJM.js → requirementDiagram-Y7LWUY3S-DprFI03z.js} +1 -1
- package/dist/app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/app/assets/{sankeyDiagram-WGJFYUXA-BvKX0eIe.js → sankeyDiagram-WGJFYUXA-DygYWLTR.js} +1 -1
- package/dist/app/assets/{sequenceDiagram-5NDRBG5M-Y4YqU2Xt.js → sequenceDiagram-5NDRBG5M-BzIr2DBO.js} +1 -1
- package/dist/app/assets/{stateDiagram-v2-FJ4FJNEA-DmjIjn4u.js → stateDiagram-v2-FJ4FJNEA-MGCqKnm_.js} +1 -1
- package/dist/app/assets/{swimlanes-UMQ2AFYF-B2O5JCjs.js → swimlanes-UMQ2AFYF-CIEH82Zt.js} +1 -1
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-YWNJjl0g.js +8 -0
- package/dist/app/assets/{timeline-definition-SEUHKTQE-DJJdixAI.js → timeline-definition-SEUHKTQE-BGYSqup2.js} +1 -1
- package/dist/app/assets/treeView-D4JQ5SDB-B39o1sJo.js +1 -0
- package/dist/app/assets/treemap-SAJKECNS-BoGxrUH4.js +1 -0
- package/dist/app/assets/{usecaseDiagram-AFA6YXN4-Bp8pXgvR.js → usecaseDiagram-AFA6YXN4-dy6oVOke.js} +1 -1
- package/dist/app/assets/{vennDiagram-NNMJR3RB-DYSfZMWT.js → vennDiagram-NNMJR3RB-CUyuhcAk.js} +1 -1
- package/dist/app/assets/wardley-7MLQ67FV-sYPAvQ06.js +1 -0
- package/dist/app/assets/{wardleyDiagram-S7YFUSEH-CGJ8qg76.js → wardleyDiagram-S7YFUSEH-DYtxYUHe.js} +1 -1
- package/dist/app/assets/{xychartDiagram-GGNKY7IX-CRSvraFc.js → xychartDiagram-GGNKY7IX-ClR06AlB.js} +1 -1
- package/dist/app/index.html +3 -2
- package/dist/cli/index.js +281 -53
- package/dist/core/api.js +262 -51
- package/dist/core/approvals.js +426 -0
- package/dist/core/context.js +68 -3
- package/dist/core/diff.js +156 -0
- package/dist/core/docs.js +9 -3
- package/dist/core/doctor.js +152 -0
- package/dist/core/doctypes.js +79 -0
- package/dist/core/edit.js +25 -0
- package/dist/core/flags.js +13 -0
- package/dist/core/gaps.js +7 -2
- package/dist/core/heading.js +4 -0
- package/dist/core/history.js +36 -5
- package/dist/core/home.js +345 -0
- package/dist/core/lanes.js +134 -0
- package/dist/core/launch.js +34 -0
- package/dist/core/layout.js +362 -0
- package/dist/core/newdoc.js +66 -0
- package/dist/core/open-folder.js +30 -0
- package/dist/core/openspec.js +6 -6
- package/dist/core/paths.js +71 -4
- package/dist/core/project.js +392 -0
- package/dist/core/route.js +420 -0
- package/dist/core/scan.js +514 -0
- package/dist/core/schema.js +170 -1
- package/dist/core/scope.js +126 -0
- package/dist/core/setup.js +134 -0
- package/dist/core/skills.js +92 -14
- package/dist/core/storage/config.js +74 -0
- package/dist/core/storage/events.js +54 -0
- package/dist/core/storage/folder-watch.js +147 -0
- package/dist/core/storage/folder.js +469 -0
- package/dist/core/storage/index.js +8 -0
- package/dist/core/storage/local.js +22 -0
- package/dist/core/storage/migrate.js +48 -0
- package/dist/core/storage/postgres.js +290 -0
- package/dist/core/storage/types.js +101 -0
- package/dist/core/storage.js +483 -77
- package/dist/core/suggest.js +283 -0
- package/dist/core/tree.js +254 -0
- package/dist/core/types.js +583 -0
- package/dist/core/version.js +1 -1
- package/dist/core/versions.js +245 -0
- package/dist/core/watch.js +2 -136
- package/dist/core/xml.js +23 -5
- package/dist/demo/args.js +58 -0
- package/dist/demo/browser.js +301 -0
- package/dist/demo/platform.js +147 -0
- package/dist/demo/run.js +198 -0
- package/dist/demo/steps.js +328 -0
- package/dist/mcp/project-tools.js +193 -0
- package/dist/mcp/server.js +493 -65
- package/examples/platform/README.md +33 -0
- package/examples/platform/api/package.json +8 -0
- package/examples/platform/api/src/auth-middleware.js +16 -0
- package/examples/platform/api/src/proxy.js +7 -0
- package/examples/platform/api/src/rate-limit.js +13 -0
- package/examples/platform/api/src/server.js +40 -0
- package/examples/platform/auth/package.json +9 -0
- package/examples/platform/auth/src/server.js +24 -0
- package/examples/platform/auth/src/tokens.js +19 -0
- package/examples/platform/auth/src/users.js +16 -0
- package/examples/platform/docs/api/PRD.md +10 -0
- package/examples/platform/docs/api/SDD.md +15 -0
- package/examples/platform/docs/auth/PRD.md +10 -0
- package/examples/platform/docs/auth/SDD.md +8 -0
- package/examples/platform/docs/frontend/PRD.md +18 -0
- package/examples/platform/docs/frontend/SDD.md +13 -0
- package/examples/platform/docs/pricing/PRD.md +9 -0
- package/examples/platform/docs/pricing/SDD.md +8 -0
- package/examples/platform/docs/products/PRD.md +9 -0
- package/examples/platform/docs/products/SDD.md +8 -0
- package/examples/platform/frontend/package.json +8 -0
- package/examples/platform/frontend/src/api-client.js +21 -0
- package/examples/platform/frontend/src/main.js +21 -0
- package/examples/platform/frontend/src/pages/products.js +12 -0
- package/examples/platform/frontend/src/pages/sign-in.js +14 -0
- package/examples/platform/frontend/src/session.js +11 -0
- package/examples/platform/pricing/package.json +9 -0
- package/examples/platform/pricing/src/cache.js +19 -0
- package/examples/platform/pricing/src/product-events.js +17 -0
- package/examples/platform/pricing/src/rules.js +19 -0
- package/examples/platform/pricing/src/server.js +32 -0
- package/examples/platform/products/package.json +9 -0
- package/examples/platform/products/src/catalog.js +20 -0
- package/examples/platform/products/src/events.js +11 -0
- package/examples/platform/products/src/search.js +9 -0
- package/examples/platform/products/src/server.js +31 -0
- package/migrations/postgres/001_init.sql +55 -0
- package/migrations/postgres/002_view_versions.sql +18 -0
- package/package.json +18 -2
- package/schema/approval.schema.json +173 -0
- package/schema/gap.schema.json +3 -0
- package/schema/project.schema.json +93 -0
- package/schema/settings.schema.json +86 -0
- package/schema/toolbox.schema.json +206 -0
- package/schema/type.schema.json +121 -0
- package/schema/view.schema.json +106 -0
- package/skills/claude/build-gap/SKILL.md +7 -0
- package/skills/claude/check-ui/SKILL.md +40 -0
- package/skills/claude/draw-system/SKILL.md +24 -9
- package/skills/claude/gap-from-view/SKILL.md +7 -0
- package/skills/claude/post-audit/SKILL.md +58 -0
- package/skills/claude/pre-audit/SKILL.md +82 -0
- package/skills/claude/update-state/SKILL.md +56 -0
- package/skills/cursor/check-ui.mdc +40 -0
- package/skills/cursor/draw-system.mdc +24 -9
- package/skills/cursor/post-audit.mdc +44 -0
- package/skills/cursor/pre-audit.mdc +49 -0
- package/skills/cursor/update-state.mdc +45 -0
- package/skills/windsurf/spatial-views.md +182 -36
- package/toolboxes/aws.yaml +43 -0
- package/toolboxes/azure.yaml +41 -0
- package/toolboxes/cloud.yaml +95 -0
- package/toolboxes/docs.yaml +111 -0
- package/toolboxes/software.yaml +114 -0
- package/toolboxes/types/docs/gap.yaml +15 -0
- package/toolboxes/types/docs/mdd.yaml +19 -0
- package/toolboxes/types/docs/plain.yaml +7 -0
- package/toolboxes/types/docs/prd.yaml +17 -0
- package/dist/app/assets/architectureDiagram-YGSQEFNW-BrBCHqUH.js +0 -36
- package/dist/app/assets/chunk-X3CZISLH-FMl4PpWt.js +0 -1
- package/dist/app/assets/cose-bilkent-JH36ORCC-BJmLLpaN.js +0 -1
- package/dist/app/assets/cynefin-EF2NZ3EQ-DrzwMKdm.js +0 -1
- package/dist/app/assets/dist-1Q8ng3tu.js +0 -1
- package/dist/app/assets/flowDiagram-MALGAFMO-BCwwiLzb.js +0 -1
- package/dist/app/assets/ganttDiagram-23REXITY-xMRHaU3y.js +0 -292
- package/dist/app/assets/gitGraph-VSP46ZUC-DyGq5mdd.js +0 -1
- package/dist/app/assets/index-BY66fc8Q.js +0 -95
- package/dist/app/assets/index-ByetEVbs.css +0 -1
- package/dist/app/assets/info-OHQRW6UA-DOBf-fQH.js +0 -1
- package/dist/app/assets/mermaid.core-BoRvk1Q-.js +0 -44
- package/dist/app/assets/packet-JDAUHWVQ-CgHq03Zj.js +0 -1
- package/dist/app/assets/pie-XZMESJXO-DtNh3cDE.js +0 -1
- package/dist/app/assets/radar-ABXABTNO-BS0FcsIX.js +0 -1
- package/dist/app/assets/railroad-I3PHUGI6-lO8vfGdI.js +0 -1
- package/dist/app/assets/swimlanesDiagram-OMPLPG4O-DhCl-Cjg.js +0 -8
- package/dist/app/assets/treeView-D4JQ5SDB-Dd1no4eE.js +0 -1
- package/dist/app/assets/treemap-SAJKECNS-t6wtsaki.js +0 -1
- package/dist/app/assets/wardley-7MLQ67FV-Bx60QTuu.js +0 -1
- package/skills/cursor/build-gap.mdc +0 -21
- package/skills/cursor/gap-from-view.mdc +0 -22
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import pg from "pg";
|
|
5
|
+
import { UserError } from "../errors.js";
|
|
6
|
+
import { checkId, ConflictError, KINDS, } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* PostgresStore: one row per object, for teams. Every table has id, project_id,
|
|
9
|
+
* a jsonb `data` column (exactly the folder file's JSON; a doc's or report's
|
|
10
|
+
* markdown as a JSON string), version, updated_at and updated_by.
|
|
11
|
+
* Live updates come from LISTEN/NOTIFY (a trigger on every table), so several
|
|
12
|
+
* canvases and MCP servers on one database stay in sync.
|
|
13
|
+
*
|
|
14
|
+
* The connection URL holds a password: it is never logged, and never put in an
|
|
15
|
+
* error message (see describeUrl).
|
|
16
|
+
*/
|
|
17
|
+
const TABLE = {
|
|
18
|
+
project: "projects",
|
|
19
|
+
view: "views",
|
|
20
|
+
view_version: "view_versions",
|
|
21
|
+
gap: "gaps",
|
|
22
|
+
doc: "docs",
|
|
23
|
+
doc_version: "doc_versions",
|
|
24
|
+
approval: "approvals",
|
|
25
|
+
pins: "pins",
|
|
26
|
+
settings: "settings",
|
|
27
|
+
toolbox: "toolboxes",
|
|
28
|
+
report: "reports",
|
|
29
|
+
};
|
|
30
|
+
const KIND_OF = Object.fromEntries(KINDS.map((k) => [TABLE[k], k]));
|
|
31
|
+
export const CHANNEL = "spatial_views";
|
|
32
|
+
/** The SQL migrations: `migrations/postgres/*.sql` in the package (three levels up from src/ or dist/core/storage/). */
|
|
33
|
+
export const MIGRATIONS_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "migrations", "postgres");
|
|
34
|
+
/** Where a database is, for messages: `host:port/db`, never the user or password. */
|
|
35
|
+
export function describeUrl(url) {
|
|
36
|
+
try {
|
|
37
|
+
const u = new URL(url);
|
|
38
|
+
const host = u.hostname || u.searchParams.get("host") || "localhost";
|
|
39
|
+
return `${host}${u.port ? `:${u.port}` : ""}${u.pathname && u.pathname !== "/" ? u.pathname : ""}`;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return "the configured database";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const item = (kind, r) => ({
|
|
46
|
+
kind,
|
|
47
|
+
project: r.project_id,
|
|
48
|
+
id: r.id,
|
|
49
|
+
data: r.data,
|
|
50
|
+
version: String(r.version),
|
|
51
|
+
updated_at: r.updated_at.toISOString(),
|
|
52
|
+
...(r.updated_by ? { updated_by: r.updated_by } : {}),
|
|
53
|
+
});
|
|
54
|
+
const expectedInt = (v) => (/^\d+$/.test(v) ? Number(v) : -1);
|
|
55
|
+
export class PostgresStore {
|
|
56
|
+
opts;
|
|
57
|
+
backend = "postgres";
|
|
58
|
+
where;
|
|
59
|
+
pool;
|
|
60
|
+
listener = null;
|
|
61
|
+
listening = null;
|
|
62
|
+
watchers = new Set();
|
|
63
|
+
closed = false;
|
|
64
|
+
retry = null;
|
|
65
|
+
constructor(opts) {
|
|
66
|
+
this.opts = opts;
|
|
67
|
+
this.where = describeUrl(opts.url);
|
|
68
|
+
this.pool = new pg.Pool({ connectionString: opts.url, max: opts.max ?? 5 });
|
|
69
|
+
// An idle client's error (the server restarted) must not crash the process.
|
|
70
|
+
this.pool.on("error", () => undefined);
|
|
71
|
+
}
|
|
72
|
+
/** Connect, and bring the schema up to date. */
|
|
73
|
+
static async connect(opts) {
|
|
74
|
+
const s = new PostgresStore(opts);
|
|
75
|
+
try {
|
|
76
|
+
await migrate(s.pool);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
await s.pool.end().catch(() => undefined);
|
|
80
|
+
throw new UserError(`could not open Postgres at ${s.where}: ${safeMessage(err)}`);
|
|
81
|
+
}
|
|
82
|
+
return s;
|
|
83
|
+
}
|
|
84
|
+
async get(kind, project, id) {
|
|
85
|
+
checkId(kind, id);
|
|
86
|
+
const r = await this.pool.query(`SELECT * FROM ${TABLE[kind]} WHERE project_id = $1 AND id = $2`, [project, id]);
|
|
87
|
+
return r.rows[0] ? item(kind, r.rows[0]) : null;
|
|
88
|
+
}
|
|
89
|
+
async list(kind, project, opts = {}) {
|
|
90
|
+
const args = [];
|
|
91
|
+
const where = [];
|
|
92
|
+
if (kind !== "project")
|
|
93
|
+
where.push(`project_id = $${args.push(project)}`);
|
|
94
|
+
if (opts.prefix)
|
|
95
|
+
where.push(`starts_with(id, $${args.push(opts.prefix)})`);
|
|
96
|
+
const r = await this.pool.query(`SELECT * FROM ${TABLE[kind]} ${where.length ? `WHERE ${where.join(" AND ")}` : ""} ORDER BY id COLLATE "C"`, args);
|
|
97
|
+
return r.rows.map((row) => item(kind, row));
|
|
98
|
+
}
|
|
99
|
+
async listIds(kind, project, opts = {}) {
|
|
100
|
+
const args = [];
|
|
101
|
+
const where = [];
|
|
102
|
+
if (kind !== "project")
|
|
103
|
+
where.push(`project_id = $${args.push(project)}`);
|
|
104
|
+
if (opts.prefix)
|
|
105
|
+
where.push(`starts_with(id, $${args.push(opts.prefix)})`);
|
|
106
|
+
const r = await this.pool.query(`SELECT id FROM ${TABLE[kind]} ${where.length ? `WHERE ${where.join(" AND ")}` : ""} ORDER BY id COLLATE "C"`, args);
|
|
107
|
+
return r.rows.map((row) => row.id);
|
|
108
|
+
}
|
|
109
|
+
async putOn(q, kind, project, id, data, expected, by) {
|
|
110
|
+
checkId(kind, id);
|
|
111
|
+
if (kind === "project" && id !== project)
|
|
112
|
+
throw new TypeError("a project's id is its slug");
|
|
113
|
+
const t = TABLE[kind];
|
|
114
|
+
const json = JSON.stringify(data);
|
|
115
|
+
const who = by ?? null;
|
|
116
|
+
let r;
|
|
117
|
+
if (expected === undefined)
|
|
118
|
+
r = await q.query(`INSERT INTO ${t} AS t (id, project_id, data, updated_by) VALUES ($1, $2, $3::jsonb, $4)
|
|
119
|
+
ON CONFLICT ${kind === "project" ? "(id)" : "(project_id, id)"} DO UPDATE
|
|
120
|
+
SET data = EXCLUDED.data, version = t.version + 1, updated_at = now(), updated_by = EXCLUDED.updated_by
|
|
121
|
+
RETURNING *`, [id, project, json, who]);
|
|
122
|
+
else if (expected === null)
|
|
123
|
+
r = await q.query(`INSERT INTO ${t} (id, project_id, data, updated_by) VALUES ($1, $2, $3::jsonb, $4) ON CONFLICT DO NOTHING RETURNING *`, [id, project, json, who]);
|
|
124
|
+
else
|
|
125
|
+
r = await q.query(`UPDATE ${t} SET data = $3::jsonb, version = version + 1, updated_at = now(), updated_by = $4
|
|
126
|
+
WHERE project_id = $2 AND id = $1 AND version = $5 RETURNING *`, [id, project, json, who, expectedInt(expected)]);
|
|
127
|
+
if (!r.rows[0])
|
|
128
|
+
throw new ConflictError(kind, project, id);
|
|
129
|
+
return item(kind, r.rows[0]);
|
|
130
|
+
}
|
|
131
|
+
async deleteOn(q, kind, project, id, expected) {
|
|
132
|
+
checkId(kind, id);
|
|
133
|
+
const t = TABLE[kind];
|
|
134
|
+
if (expected === null) {
|
|
135
|
+
const r = await q.query(`SELECT 1 FROM ${t} WHERE project_id = $1 AND id = $2`, [project, id]);
|
|
136
|
+
if (r.rowCount)
|
|
137
|
+
throw new ConflictError(kind, project, id);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
const r = expected === undefined
|
|
141
|
+
? await q.query(`DELETE FROM ${t} WHERE project_id = $1 AND id = $2`, [project, id])
|
|
142
|
+
: await q.query(`DELETE FROM ${t} WHERE project_id = $1 AND id = $2 AND version = $3`, [project, id, expectedInt(expected)]);
|
|
143
|
+
if (expected !== undefined && !r.rowCount)
|
|
144
|
+
throw new ConflictError(kind, project, id);
|
|
145
|
+
return (r.rowCount ?? 0) > 0;
|
|
146
|
+
}
|
|
147
|
+
put(kind, project, id, data, opts = {}) {
|
|
148
|
+
return this.putOn(this.pool, kind, project, id, data, opts.expected, opts.by);
|
|
149
|
+
}
|
|
150
|
+
delete(kind, project, id, opts = {}) {
|
|
151
|
+
return this.deleteOn(this.pool, kind, project, id, opts.expected);
|
|
152
|
+
}
|
|
153
|
+
async transact(ops, opts = {}) {
|
|
154
|
+
if (!ops.length)
|
|
155
|
+
return;
|
|
156
|
+
const c = await this.pool.connect();
|
|
157
|
+
try {
|
|
158
|
+
await c.query("BEGIN");
|
|
159
|
+
for (const o of ops) {
|
|
160
|
+
if (o.op === "put")
|
|
161
|
+
await this.putOn(c, o.kind, o.project, o.id, o.data, o.expected, opts.by);
|
|
162
|
+
else
|
|
163
|
+
await this.deleteOn(c, o.kind, o.project, o.id, o.expected);
|
|
164
|
+
}
|
|
165
|
+
await c.query("COMMIT");
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
await c.query("ROLLBACK").catch(() => undefined);
|
|
169
|
+
throw err;
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
c.release();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// ---- LISTEN / NOTIFY --------------------------------------------------------------
|
|
176
|
+
listen() {
|
|
177
|
+
if (this.listening)
|
|
178
|
+
return this.listening;
|
|
179
|
+
const client = new pg.Client({ connectionString: this.opts.url });
|
|
180
|
+
this.listener = client;
|
|
181
|
+
const again = () => {
|
|
182
|
+
if (this.listener !== client)
|
|
183
|
+
return;
|
|
184
|
+
this.listener = null;
|
|
185
|
+
this.listening = null;
|
|
186
|
+
client.end().catch(() => undefined);
|
|
187
|
+
if (this.closed || !this.watchers.size)
|
|
188
|
+
return;
|
|
189
|
+
// The server went away: reconnect, and keep the watches.
|
|
190
|
+
this.retry = setTimeout(() => void this.listen().catch(() => again()), 1000);
|
|
191
|
+
this.retry.unref?.();
|
|
192
|
+
};
|
|
193
|
+
client.on("error", again);
|
|
194
|
+
client.on("end", again);
|
|
195
|
+
client.on("notification", (n) => {
|
|
196
|
+
if (n.channel !== CHANNEL || !n.payload)
|
|
197
|
+
return;
|
|
198
|
+
let p;
|
|
199
|
+
try {
|
|
200
|
+
p = JSON.parse(n.payload);
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const kind = p.table ? KIND_OF[p.table] : undefined;
|
|
206
|
+
if (!kind || typeof p.project !== "string" || typeof p.id !== "string")
|
|
207
|
+
return;
|
|
208
|
+
const c = { kind, project: p.project, id: p.id, removed: p.removed === true };
|
|
209
|
+
for (const w of this.watchers)
|
|
210
|
+
if (w.project === null || w.project === c.project)
|
|
211
|
+
w.fn(c);
|
|
212
|
+
});
|
|
213
|
+
this.listening = client
|
|
214
|
+
.connect()
|
|
215
|
+
.then(() => client.query(`LISTEN ${CHANNEL}`))
|
|
216
|
+
.then(() => undefined)
|
|
217
|
+
.catch((err) => {
|
|
218
|
+
again();
|
|
219
|
+
throw new UserError(`could not listen on Postgres at ${this.where}: ${safeMessage(err)}`);
|
|
220
|
+
});
|
|
221
|
+
return this.listening;
|
|
222
|
+
}
|
|
223
|
+
watch(project, fn) {
|
|
224
|
+
const w = { project, fn };
|
|
225
|
+
this.watchers.add(w);
|
|
226
|
+
const ready = this.listen();
|
|
227
|
+
ready.catch(() => undefined);
|
|
228
|
+
return {
|
|
229
|
+
ready,
|
|
230
|
+
close: () => {
|
|
231
|
+
this.watchers.delete(w);
|
|
232
|
+
if (!this.watchers.size && this.listener) {
|
|
233
|
+
const l = this.listener;
|
|
234
|
+
this.listener = null;
|
|
235
|
+
this.listening = null;
|
|
236
|
+
l.end().catch(() => undefined);
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
async close() {
|
|
242
|
+
this.closed = true;
|
|
243
|
+
if (this.retry)
|
|
244
|
+
clearTimeout(this.retry);
|
|
245
|
+
this.watchers.clear();
|
|
246
|
+
const l = this.listener;
|
|
247
|
+
this.listener = null;
|
|
248
|
+
this.listening = null;
|
|
249
|
+
await l?.end().catch(() => undefined);
|
|
250
|
+
await this.pool.end().catch(() => undefined);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** A driver error's message, without anything that could hold the URL. */
|
|
254
|
+
function safeMessage(err) {
|
|
255
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
256
|
+
return msg.replace(/postgres(ql)?:\/\/\S+/gi, "<url>");
|
|
257
|
+
}
|
|
258
|
+
/** Apply every migration not yet applied, in name order, one transaction each. */
|
|
259
|
+
export async function migrate(pool, dir = MIGRATIONS_DIR) {
|
|
260
|
+
const files = (await readdir(dir)).filter((f) => f.endsWith(".sql")).sort();
|
|
261
|
+
const c = await pool.connect();
|
|
262
|
+
const applied = [];
|
|
263
|
+
try {
|
|
264
|
+
// One migrator at a time across every client of the database.
|
|
265
|
+
await c.query("SELECT pg_advisory_lock(hashtext('spatial_views_migrations'))");
|
|
266
|
+
await c.query("CREATE TABLE IF NOT EXISTS spatial_views_migrations (name text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())");
|
|
267
|
+
const done = new Set((await c.query("SELECT name FROM spatial_views_migrations")).rows.map((r) => r.name));
|
|
268
|
+
for (const f of files) {
|
|
269
|
+
if (done.has(f))
|
|
270
|
+
continue;
|
|
271
|
+
const sql = await readFile(join(dir, f), "utf8");
|
|
272
|
+
await c.query("BEGIN");
|
|
273
|
+
try {
|
|
274
|
+
await c.query(sql);
|
|
275
|
+
await c.query("INSERT INTO spatial_views_migrations (name) VALUES ($1)", [f]);
|
|
276
|
+
await c.query("COMMIT");
|
|
277
|
+
applied.push(f);
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
await c.query("ROLLBACK").catch(() => undefined);
|
|
281
|
+
throw err;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
await c.query("SELECT pg_advisory_unlock(hashtext('spatial_views_migrations'))").catch(() => undefined);
|
|
287
|
+
c.release();
|
|
288
|
+
}
|
|
289
|
+
return applied;
|
|
290
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { UserError } from "../errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* The Storage interface: every read and write of project data goes through it.
|
|
4
|
+
* Two backends implement it: FolderStore (the default: one file per object under
|
|
5
|
+
* the home folder) and PostgresStore (one row per object, for teams).
|
|
6
|
+
*
|
|
7
|
+
* An object is addressed by (kind, project, id). Its `data` is exactly what the
|
|
8
|
+
* folder file holds: parsed JSON for the JSON kinds, the markdown text for
|
|
9
|
+
* `doc` and `report`. Validation is the caller's (see ../storage.ts); a store
|
|
10
|
+
* keeps what it is given.
|
|
11
|
+
*/
|
|
12
|
+
export const KINDS = [
|
|
13
|
+
"project",
|
|
14
|
+
"view",
|
|
15
|
+
"view_version",
|
|
16
|
+
"gap",
|
|
17
|
+
"doc",
|
|
18
|
+
"doc_version",
|
|
19
|
+
"approval",
|
|
20
|
+
"pins",
|
|
21
|
+
"settings",
|
|
22
|
+
"toolbox",
|
|
23
|
+
"report",
|
|
24
|
+
];
|
|
25
|
+
/** Kinds whose data is markdown text, not JSON. */
|
|
26
|
+
export const TEXT_KINDS = new Set(["doc", "report"]);
|
|
27
|
+
/** The single object of a one-per-project kind. */
|
|
28
|
+
export const SINGLETON_ID = { pins: "pins", settings: "settings" };
|
|
29
|
+
/** An expected version missed: someone else wrote first. Re-read and try again. */
|
|
30
|
+
export class ConflictError extends UserError {
|
|
31
|
+
kind;
|
|
32
|
+
project;
|
|
33
|
+
id;
|
|
34
|
+
constructor(kind, project, id) {
|
|
35
|
+
super(`${kind} ${id} was changed by someone else; reload it and try again`);
|
|
36
|
+
this.kind = kind;
|
|
37
|
+
this.project = project;
|
|
38
|
+
this.id = id;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Read, change, write, retrying when another client wrote in between. */
|
|
42
|
+
export async function update(storage, kind, project, id, change, opts = {}) {
|
|
43
|
+
for (let n = 1;; n++) {
|
|
44
|
+
const cur = await storage.get(kind, project, id);
|
|
45
|
+
const next = await change(cur ? cur.data : null);
|
|
46
|
+
try {
|
|
47
|
+
return await storage.put(kind, project, id, next, { expected: cur ? cur.version : null, ...(opts.by ? { by: opts.by } : {}) });
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (!(err instanceof ConflictError) || n >= (opts.tries ?? 5))
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** A JSON value with object keys sorted, so equal data has one text (hashes, comparisons). */
|
|
56
|
+
export function canonical(v) {
|
|
57
|
+
return JSON.stringify(sortKeys(v));
|
|
58
|
+
}
|
|
59
|
+
function sortKeys(v) {
|
|
60
|
+
if (Array.isArray(v))
|
|
61
|
+
return v.map(sortKeys);
|
|
62
|
+
if (v && typeof v === "object") {
|
|
63
|
+
const out = {};
|
|
64
|
+
for (const k of Object.keys(v).sort())
|
|
65
|
+
out[k] = sortKeys(v[k]);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
return v;
|
|
69
|
+
}
|
|
70
|
+
const ID = /^[A-Za-z0-9_-]+$/;
|
|
71
|
+
/**
|
|
72
|
+
* Ids are letters, digits, - and _; a doc id is a project-relative .md path; a doc version is `<path>@<n>`;
|
|
73
|
+
* a View version is `<view id>@<n>`.
|
|
74
|
+
*/
|
|
75
|
+
export function checkId(kind, id) {
|
|
76
|
+
if (kind === "doc") {
|
|
77
|
+
if (!/\.md$/i.test(id) || id.startsWith("/") || id.split("/").some((p) => p === ".." || p === "" || p === ".") || id.includes("\\"))
|
|
78
|
+
throw new UserError(`${id}: a doc id is a relative .md path`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (kind === "doc_version") {
|
|
82
|
+
const at = id.lastIndexOf("@");
|
|
83
|
+
if (at < 1 || !/^\d+$/.test(id.slice(at + 1)))
|
|
84
|
+
throw new UserError(`${id}: a doc version id is <path>@<n>`);
|
|
85
|
+
checkId("doc", id.slice(0, at));
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (kind === "view_version") {
|
|
89
|
+
const at = id.lastIndexOf("@");
|
|
90
|
+
if (at < 1 || !/^\d+$/.test(id.slice(at + 1)) || !ID.test(id.slice(0, at)))
|
|
91
|
+
throw new UserError(`${id}: a View version id is <view id>@<n>`);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (kind === "report") {
|
|
95
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(id) || id.includes(".."))
|
|
96
|
+
throw new UserError(`${id}: a report name is letters, digits, ., - and _`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (!ID.test(id))
|
|
100
|
+
throw new UserError(`${id}: an id is letters, digits, - and _`);
|
|
101
|
+
}
|