@uptimizr/db 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/AGENTS.md +109 -0
  2. package/LICENSE +201 -0
  3. package/README.md +89 -0
  4. package/dist/cli/createProject.d.ts +3 -0
  5. package/dist/cli/createProject.d.ts.map +1 -0
  6. package/dist/cli/createProject.js +41 -0
  7. package/dist/cli/createProject.js.map +1 -0
  8. package/dist/cli/migrate.d.ts +3 -0
  9. package/dist/cli/migrate.d.ts.map +1 -0
  10. package/dist/cli/migrate.js +21 -0
  11. package/dist/cli/migrate.js.map +1 -0
  12. package/dist/cli/seed.d.ts +2 -0
  13. package/dist/cli/seed.d.ts.map +1 -0
  14. package/dist/cli/seed.js +175 -0
  15. package/dist/cli/seed.js.map +1 -0
  16. package/dist/duckdb/client.d.ts +42 -0
  17. package/dist/duckdb/client.d.ts.map +1 -0
  18. package/dist/duckdb/client.js +93 -0
  19. package/dist/duckdb/client.js.map +1 -0
  20. package/dist/duckdb/events.d.ts +44 -0
  21. package/dist/duckdb/events.d.ts.map +1 -0
  22. package/dist/duckdb/events.js +225 -0
  23. package/dist/duckdb/events.js.map +1 -0
  24. package/dist/duckdb/migrations.d.ts +20 -0
  25. package/dist/duckdb/migrations.d.ts.map +1 -0
  26. package/dist/duckdb/migrations.js +323 -0
  27. package/dist/duckdb/migrations.js.map +1 -0
  28. package/dist/duckdb/projects.d.ts +23 -0
  29. package/dist/duckdb/projects.d.ts.map +1 -0
  30. package/dist/duckdb/projects.js +55 -0
  31. package/dist/duckdb/projects.js.map +1 -0
  32. package/dist/duckdb/queries.d.ts +10 -0
  33. package/dist/duckdb/queries.d.ts.map +1 -0
  34. package/dist/duckdb/queries.js +10 -0
  35. package/dist/duckdb/queries.js.map +1 -0
  36. package/dist/duckdb/sceneRegistry.d.ts +15 -0
  37. package/dist/duckdb/sceneRegistry.d.ts.map +1 -0
  38. package/dist/duckdb/sceneRegistry.js +98 -0
  39. package/dist/duckdb/sceneRegistry.js.map +1 -0
  40. package/dist/env.d.ts +25 -0
  41. package/dist/env.d.ts.map +1 -0
  42. package/dist/env.js +55 -0
  43. package/dist/env.js.map +1 -0
  44. package/dist/events.d.ts +107 -0
  45. package/dist/events.d.ts.map +1 -0
  46. package/dist/events.js +153 -0
  47. package/dist/events.js.map +1 -0
  48. package/dist/index.d.ts +36 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +34 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/metadata.d.ts +84 -0
  53. package/dist/metadata.d.ts.map +1 -0
  54. package/dist/metadata.js +23 -0
  55. package/dist/metadata.js.map +1 -0
  56. package/dist/parity/cases.d.ts +31 -0
  57. package/dist/parity/cases.d.ts.map +1 -0
  58. package/dist/parity/cases.js +524 -0
  59. package/dist/parity/cases.js.map +1 -0
  60. package/dist/parity/compare.d.ts +47 -0
  61. package/dist/parity/compare.d.ts.map +1 -0
  62. package/dist/parity/compare.js +90 -0
  63. package/dist/parity/compare.js.map +1 -0
  64. package/dist/parity/fixtures.d.ts +45 -0
  65. package/dist/parity/fixtures.d.ts.map +1 -0
  66. package/dist/parity/fixtures.js +157 -0
  67. package/dist/parity/fixtures.js.map +1 -0
  68. package/dist/parity/index.d.ts +12 -0
  69. package/dist/parity/index.d.ts.map +1 -0
  70. package/dist/parity/index.js +12 -0
  71. package/dist/parity/index.js.map +1 -0
  72. package/dist/query/aggregations.d.ts +393 -0
  73. package/dist/query/aggregations.d.ts.map +1 -0
  74. package/dist/query/aggregations.js +1412 -0
  75. package/dist/query/aggregations.js.map +1 -0
  76. package/dist/query/dialect.d.ts +100 -0
  77. package/dist/query/dialect.d.ts.map +1 -0
  78. package/dist/query/dialect.js +88 -0
  79. package/dist/query/dialect.js.map +1 -0
  80. package/dist/query/duckdbDialect.d.ts +27 -0
  81. package/dist/query/duckdbDialect.d.ts.map +1 -0
  82. package/dist/query/duckdbDialect.js +83 -0
  83. package/dist/query/duckdbDialect.js.map +1 -0
  84. package/dist/query/index.d.ts +22 -0
  85. package/dist/query/index.d.ts.map +1 -0
  86. package/dist/query/index.js +30 -0
  87. package/dist/query/index.js.map +1 -0
  88. package/dist/query/types.d.ts +388 -0
  89. package/dist/query/types.d.ts.map +1 -0
  90. package/dist/query/types.js +11 -0
  91. package/dist/query/types.js.map +1 -0
  92. package/llms.txt +21 -0
  93. package/package.json +71 -0
package/AGENTS.md ADDED
@@ -0,0 +1,109 @@
1
+ # AGENTS.md — @uptimizr/db
2
+
3
+ > Packaged agent guide. For the human reference see [README.md](./README.md); for design
4
+ > rationale see the project ADRs at https://github.com/RaananW/Uptimizr/tree/main/docs/adr.
5
+
6
+ ## What this package is
7
+
8
+ The OSS storage contracts plus the single-file **DuckDB** store (ADR 0020):
9
+
10
+ - **DuckDB** — one persisted `.duckdb` file holding both events and metadata. A wide `events`
11
+ table (hot fields promoted to columns; the full event preserved as JSON in `payload` so reads
12
+ stay replay-complete) plus `projects` / `api_keys` (stored only as SHA-256 hashes).
13
+ - **Engine-neutral contracts** — the dialect-agnostic query layer (`buildX` + `Dialect`), the
14
+ neutral event-row mapper (`toEventRow`, `formatUtcTimestamp`), and the metadata types
15
+ (`Project`, `ApiKeyRecord`, `SceneRepresentation*`). An optional, separately-licensed
16
+ scale store composes these to drive its single-tenant ClickHouse + Postgres engines.
17
+
18
+ This package carries **no ClickHouse/Postgres dependency**. The optional scale engines live in a
19
+ separately-licensed scale store. Server/Node only — no DOM imports. Aggregations are
20
+ **query-time** in v1 (no materialized views).
21
+
22
+ ## Install
23
+
24
+ ```bash
25
+ pnpm add @uptimizr/db
26
+ ```
27
+
28
+ ## Canonical usage
29
+
30
+ ```ts
31
+ import {
32
+ createDuckdbClient,
33
+ migrateDuckdb,
34
+ duckdbInsertEvents,
35
+ duckdbGetSessionEvents,
36
+ duckdbResolveApiKey,
37
+ buildPointerHeatmap,
38
+ duckdbDialect,
39
+ runDuckdbQuery,
40
+ type HeatmapBinRow,
41
+ } from "@uptimizr/db";
42
+
43
+ const db = await createDuckdbClient("./data/uptimizr.duckdb");
44
+ await migrateDuckdb(db);
45
+
46
+ await duckdbInsertEvents(db, events); // events validated upstream at the collector boundary
47
+ const heat = await runDuckdbQuery<HeatmapBinRow>(
48
+ db,
49
+ buildPointerHeatmap("project-id", { bins: 50 }, duckdbDialect),
50
+ );
51
+ const timeline = await duckdbGetSessionEvents(db, "project-id", "session-id");
52
+ const projectId = await duckdbResolveApiKey(db, "utk_…");
53
+ ```
54
+
55
+ Connection settings come from the environment (`readDbSettings()`); when `DUCKDB_PATH` is unset
56
+ the store defaults to `<repo-root>/data/uptimizr.duckdb`, resolved against the monorepo root so
57
+ every tool (collector + CLIs) shares one canonical file regardless of cwd.
58
+
59
+ ## Rules for agents
60
+
61
+ - **Migrations are forward-only and additive.** Append to `DUCKDB_MIGRATIONS`; never edit a
62
+ shipped migration (ADR 0007). The ClickHouse/Postgres scale migrations live alongside the
63
+ scale store.
64
+ - A new aggregation = a pure `buildX(projectId, opts, dialect)` builder returning a `QuerySpec`
65
+ (`{ query, query_params }`), run with `runDuckdbQuery`. Keep builders pure, dialect-agnostic,
66
+ and unit-tested without a live database; add a `PARITY_CASES` entry so both engines stay equal.
67
+ - Validate events upstream at the collector boundary; this layer assumes valid input.
68
+ - API keys are only ever stored as SHA-256 hashes — never persist raw keys.
69
+ - **Single-writer store.** DuckDB allows only one read-write process per file; assume a single
70
+ collector per `.duckdb` file. Back up = copy the file. Multi-writer / horizontal scale is the
71
+ optional ClickHouse scale tier, not this package.
72
+
73
+ ## Cross-engine parity (ADR 0020)
74
+
75
+ The dialect-agnostic aggregations (`buildX(projectId, opts, dialect)`) are rendered per engine
76
+ (`duckdbDialect` for OSS, `clickhouseDialect` for the scale tier). A shared parity harness
77
+ proves the engines produce equal analytics:
78
+
79
+ - `PARITY_EVENTS` — one deterministic fixture event set exercising every aggregation (2D/3D
80
+ heatmaps, the camera-direction gaze heatmap, the ASOF click↔gaze ray and flow joins, quantile
81
+ perf, the daily rollups, and the scene/session dimensions).
82
+ - `PARITY_CASES` — each aggregation paired with its engine-independent **golden** output (authored
83
+ as truth, hand-verified from the fixtures).
84
+ - `diffParity(actual, golden, { sortKeys, ignoreColumns })` — compares under the tolerance rules
85
+ below and returns a list of differences (empty = parity).
86
+
87
+ OSS ships the **DuckDB-vs-golden** suite (`src/__tests__/duckdbParity.test.ts`, CI-runnable, no
88
+ service). The scale tier reuses the same exported `PARITY_CASES`/golden to run
89
+ **DuckDB-vs-ClickHouse**; two engines that both match the golden are in parity by transitivity.
90
+
91
+ **Tolerance rules** (see `src/parity/compare.ts`):
92
+
93
+ 1. **Order-insensitive** — rows are compared as a multiset, sorted by each case's `sortKeys`
94
+ (SQL guarantees no order beyond `ORDER BY`, and `ORDER BY count` ties are unstable).
95
+ 2. **Float tolerance** — continuous numeric columns (averages, quantiles, ASOF ray origins/hits)
96
+ match within `PARITY_ABS_TOLERANCE` (1e-6 absolute) or `PARITY_REL_TOLERANCE` (1e-9 relative).
97
+ 3. **Bin indices are integer-exact** — `floor(...)` heatmap bins are integers; fixtures avoid
98
+ exact bin boundaries where a sub-ulp difference could flip the floor across engines.
99
+ 4. **Temporal projections excluded** — wall-clock `TIMESTAMP` columns (`started_at`, `ended_at`,
100
+ `last_seen`) render differently per engine and are listed in `ignoreColumns`; date-granular
101
+ `day` strings (`YYYY-MM-DD`) render identically and are compared.
102
+
103
+ When adding an aggregation or event type, extend `PARITY_EVENTS`/`PARITY_CASES` with golden so both
104
+ engines stay covered.
105
+
106
+ ## More
107
+
108
+ - Package reference: [README.md](./README.md)
109
+ - Integration guide: https://github.com/RaananW/Uptimizr/blob/main/docs/integration.md
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions for
125
+ use, reproduction, or distribution of Your modifications, or for any
126
+ such Derivative Works as a whole, provided Your use, reproduction,
127
+ and distribution of the Work otherwise complies with the conditions
128
+ stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Uptimizr Contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # @uptimizr/db
2
+
3
+ The OSS storage contracts plus the single-file **DuckDB** store:
4
+
5
+ - **DuckDB (OSS default)** — one persisted `.duckdb` file holds **both events and metadata**, so
6
+ the collector self-hosts in a single process with no external database service. A wide `events`
7
+ table (hot fields like camera `position`/`direction`, pointer `screen`, `mesh`, `fps` promoted to
8
+ columns; the full event preserved as JSON in `payload` so reads stay replay-complete) plus
9
+ `projects` / `api_keys` (stored only as SHA-256 hashes).
10
+ - **Engine-neutral contracts** — the dialect-agnostic query layer (`buildX` + `Dialect`), the
11
+ neutral event-row mapper (`toEventRow`, `formatUtcTimestamp`), and the metadata types
12
+ (`Project`, `ApiKeyRecord`, `SceneRepresentation*`).
13
+
14
+ This package carries **no ClickHouse/Postgres dependency**. An optional, separately-licensed
15
+ scale store (single-tenant ClickHouse + Postgres + rollups) composes these contracts
16
+ behind the same interface. Server/Node only — no DOM imports. Aggregations are
17
+ **query-time** in v1; no materialized views.
18
+
19
+ > **Single-writer constraint.** DuckDB is an embedded, single-writer store: only one process may
20
+ > open the file read-write at a time. Run a single collector per file; for multi-writer /
21
+ > horizontal scale use the ClickHouse scale path. **Back up = copy the file.**
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ import {
27
+ createDuckdbClient,
28
+ migrateDuckdb,
29
+ duckdbInsertEvents,
30
+ duckdbGetSessionEvents,
31
+ duckdbResolveApiKey,
32
+ buildPointerHeatmap,
33
+ duckdbDialect,
34
+ runDuckdbQuery,
35
+ type HeatmapBinRow,
36
+ } from "@uptimizr/db";
37
+
38
+ const db = await createDuckdbClient("./data/uptimizr.duckdb");
39
+ await migrateDuckdb(db);
40
+
41
+ // ingest (events are validated upstream at the collector boundary)
42
+ await duckdbInsertEvents(db, events);
43
+
44
+ // query-time aggregation: one spec, rendered for the DuckDB dialect
45
+ const heat = await runDuckdbQuery<HeatmapBinRow>(
46
+ db,
47
+ buildPointerHeatmap("project-id", { bins: 50 }, duckdbDialect),
48
+ );
49
+
50
+ // ordered replay/timeline read
51
+ const timeline = await duckdbGetSessionEvents(db, "project-id", "session-id");
52
+
53
+ // authenticate ingestion
54
+ const projectId = await duckdbResolveApiKey(db, "utk_…");
55
+ ```
56
+
57
+ Connection settings come from the environment (see [`.env.example`](../../../.env.example));
58
+ `readDbSettings()` exposes them. When `DUCKDB_PATH` is unset the store defaults to
59
+ `<repo-root>/data/uptimizr.duckdb` — resolved against the monorepo root (the directory with
60
+ `pnpm-workspace.yaml`), not the process cwd, so the collector and the migrate/seed/new-project
61
+ CLIs all share one canonical file regardless of which package they run from.
62
+
63
+ ## Extending
64
+
65
+ - **New columns / tables:** append a migration to `DUCKDB_MIGRATIONS`. Forward-only and additive —
66
+ never edit a shipped migration. The ClickHouse/Postgres scale migrations live alongside the
67
+ scale store.
68
+ - **New aggregation — define once, emit per dialect:** add a pure
69
+ `buildX(projectId, opts, dialect)` builder in `src/query/aggregations.ts` that renders a
70
+ `QuerySpec` (`{ query, query_params }`) using the `Dialect` fragments (never hard-code
71
+ engine-specific SQL). Run it with `runDuckdbQuery(db, buildX(..., duckdbDialect))`; the scale
72
+ path runs the _same_ builder with `clickhouseDialect`. Add a `PARITY_CASES` entry so both engines
73
+ stay provably equal. Builders are pure and unit-tested without a live database.
74
+
75
+ ## Develop
76
+
77
+ ```bash
78
+ pnpm --filter @uptimizr/db build
79
+ pnpm --filter @uptimizr/db typecheck
80
+ pnpm --filter @uptimizr/db test
81
+ ```
82
+
83
+ Integration against real ClickHouse/Postgres lives with the scale store; the unit
84
+ tests here cover the pure mapping, the dialect-agnostic SQL builders, and the DuckDB store against
85
+ an in-memory (`:memory:`) database.
86
+
87
+ ## License
88
+
89
+ [Apache-2.0](./LICENSE) © Uptimizr.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=createProject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createProject.d.ts","sourceRoot":"","sources":["../../src/cli/createProject.ts"],"names":[],"mappings":""}
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env node
2
+ import { createApiKey as duckdbCreateApiKey, createProject as duckdbCreateProject, } from "../duckdb/projects.js";
3
+ import { createDuckdbClient } from "../duckdb/client.js";
4
+ import { migrateDuckdb } from "../duckdb/migrations.js";
5
+ import { readDbSettings } from "../env.js";
6
+ /**
7
+ * Create a project and issue an API key, then print the pair. Unlike `seed.ts`
8
+ * (which writes the single demo project into the root `.env`), this CLI is meant
9
+ * to be run repeatedly to mint distinct projects — one per playground/scene. It
10
+ * writes a machine-readable JSON line to **stdout** and human-readable progress
11
+ * to **stderr**, so callers can capture the result cleanly:
12
+ *
13
+ * ```bash
14
+ * pnpm --filter @uptimizr/db run new-project -- "My Scene"
15
+ * ```
16
+ *
17
+ * Targets the OSS DuckDB store.
18
+ */
19
+ async function main() {
20
+ const name = process.argv
21
+ .slice(2)
22
+ .filter((arg) => arg !== "--")
23
+ .join(" ")
24
+ .trim() || "Playground Project";
25
+ const db = await createDuckdbClient(readDbSettings().duckdb.path);
26
+ await migrateDuckdb(db);
27
+ const project = await duckdbCreateProject(db, name);
28
+ const { key } = await duckdbCreateApiKey(db, project.id);
29
+ await db.close();
30
+ printResult(project.id, project.name, key);
31
+ }
32
+ function printResult(projectId, name, apiKey) {
33
+ console.error(`✓ project created: ${projectId} (${name})`);
34
+ console.error(` API key (stored only as a hash; shown once): ${apiKey}`);
35
+ process.stdout.write(`${JSON.stringify({ projectId, name, apiKey })}\n`);
36
+ }
37
+ main().catch((err) => {
38
+ console.error(err);
39
+ process.exit(1);
40
+ });
41
+ //# sourceMappingURL=createProject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createProject.js","sourceRoot":"","sources":["../../src/cli/createProject.ts"],"names":[],"mappings":";AACA,OAAO,EACL,YAAY,IAAI,kBAAkB,EAClC,aAAa,IAAI,mBAAmB,GACrC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,IAAI;IACjB,MAAM,IAAI,GACR,OAAO,CAAC,IAAI;SACT,KAAK,CAAC,CAAC,CAAC;SACR,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC;SAC7B,IAAI,CAAC,GAAG,CAAC;SACT,IAAI,EAAE,IAAI,oBAAoB,CAAC;IAEpC,MAAM,EAAE,GAAG,MAAM,kBAAkB,CAAC,cAAc,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAClE,MAAM,aAAa,CAAC,EAAE,CAAC,CAAC;IACxB,MAAM,OAAO,GAAG,MAAM,mBAAmB,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACpD,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,kBAAkB,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IACzD,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC;IACjB,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,WAAW,CAAC,SAAiB,EAAE,IAAY,EAAE,MAAc;IAClE,OAAO,CAAC,KAAK,CAAC,sBAAsB,SAAS,KAAK,IAAI,GAAG,CAAC,CAAC;IAC3D,OAAO,CAAC,KAAK,CAAC,kDAAkD,MAAM,EAAE,CAAC,CAAC;IAC1E,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,CAAC;AAC3E,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/cli/migrate.ts"],"names":[],"mappings":""}
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+ import { createDuckdbClient } from "../duckdb/client.js";
3
+ import { migrateDuckdb } from "../duckdb/migrations.js";
4
+ import { readDbSettings } from "../env.js";
5
+ /**
6
+ * Apply migrations to the OSS single-file DuckDB store at `DUCKDB_PATH`.
7
+ *
8
+ * Run via `pnpm --filter @uptimizr/db migrate`.
9
+ */
10
+ async function main() {
11
+ const { path } = readDbSettings().duckdb;
12
+ const db = await createDuckdbClient(path);
13
+ await migrateDuckdb(db);
14
+ await db.close();
15
+ console.log(`✓ migrations applied (DuckDB: ${path})`);
16
+ }
17
+ main().catch((err) => {
18
+ console.error(err);
19
+ process.exit(1);
20
+ });
21
+ //# sourceMappingURL=migrate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../src/cli/migrate.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;GAIG;AACH,KAAK,UAAU,IAAI;IACjB,MAAM,EAAE,IAAI,EAAE,GAAG,cAAc,EAAE,CAAC,MAAM,CAAC;IACzC,MAAM,EAAE,GAAG,MAAM,kBAAkB,CAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,aAAa,CAAC,EAAE,CAAC,CAAC;IACxB,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC;IAEjB,OAAO,CAAC,GAAG,CAAC,iCAAiC,IAAI,GAAG,CAAC,CAAC;AACxD,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=seed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../../src/cli/seed.ts"],"names":[],"mappings":""}