@nanobpm/urban-pr-review-codefirst 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.
- package/README.md +141 -0
- package/components/review-round.json +69 -0
- package/db/migrations/001_init.sql +46 -0
- package/db/migrations/002_transcript.sql +7 -0
- package/db/migrations/003_open_escalation.sql +8 -0
- package/deno.json +22 -0
- package/deno.lock +77 -0
- package/main.ts +352 -0
- package/nano-generated/app-pages.ts +676 -0
- package/nano-generated/data-cli.ts +552 -0
- package/nano-generated/data-sdk.ts +536 -0
- package/nano-generated/domain-rows.d.ts +87 -0
- package/nano-generated/domain-types.ts +1418 -0
- package/nano-generated/domain.ts +36 -0
- package/nano-generated/llm-worker.ts +403 -0
- package/nano-generated/message-io.d.ts +17 -0
- package/nano-generated/messages.ts +26 -0
- package/nano-generated/meta.ts +18 -0
- package/nano-generated/node-deno-shim.mjs +250 -0
- package/nano-generated/node-loader.mjs +60 -0
- package/nano-generated/node-register.mjs +11 -0
- package/nano-generated/worker-io.d.ts +28 -0
- package/nano-generated/worker-sdk.ts +468 -0
- package/nano-generated/workers.ts +29 -0
- package/nano.app.json +17 -0
- package/nanobpm.project.json +22 -0
- package/package.json +28 -0
- package/pages/home.page.json +127 -0
- package/prompts/review-round.md +119 -0
- package/scripts/answer.ts +20 -0
- package/scripts/purge-db.ts +70 -0
- package/scripts/review-ready.ts +20 -0
- package/scripts/submit.ts +32 -0
- package/tsconfig.json +14 -0
- package/workflows/convergence-loop.ts +299 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// nanobpmn embedded datasource SDK (Deno-preferred, Node-capable) — ADR 0024.
|
|
2
|
+
//
|
|
3
|
+
// Materialised verbatim to <workspace>/<project>/nano-generated/data-sdk.ts and
|
|
4
|
+
// imported as `@nanobpm/data` (or `./data-sdk.ts` from the sibling worker SDK).
|
|
5
|
+
// It is the runtime half of Urban's "BDE alias": a named, swappable data
|
|
6
|
+
// connection. Consumers bind to a datasource BY NAME (`data.app`), never by
|
|
7
|
+
// driver, so the same App bundle runs on embedded SQLite in the IDE and — once
|
|
8
|
+
// a `nano-ide-data-*` driver pack is installed — on a server database in
|
|
9
|
+
// production by flipping `NANO_APP_DB_*` env, with no source change.
|
|
10
|
+
//
|
|
11
|
+
// import { openDataSource } from "@nanobpm/data";
|
|
12
|
+
// const db = await openDataSource(); // the manifest's default source
|
|
13
|
+
// await db.exec("INSERT INTO orders(id) VALUES (?)", [id]);
|
|
14
|
+
// const rows = await db.query("SELECT * FROM orders");
|
|
15
|
+
//
|
|
16
|
+
// Or, inside a worker handler, via the injected context:
|
|
17
|
+
//
|
|
18
|
+
// defineWorker({ type: "save", async handle(job, ctx) {
|
|
19
|
+
// const db = await ctx.data("app");
|
|
20
|
+
// await db.exec("INSERT INTO orders(id) VALUES (?)", [job.variables.id]);
|
|
21
|
+
// }});
|
|
22
|
+
//
|
|
23
|
+
// Core ships SQLite only (the `node:sqlite` built-in — embedded, single-file —
|
|
24
|
+
// present in both Deno and Node >= 22.5). Other drivers arrive as ADR 0007 packs
|
|
25
|
+
// on the `nano-ide-data-*` axis; an unknown driver throws a pack-install hint.
|
|
26
|
+
|
|
27
|
+
// Runtime adapter: the few host calls that differ between Deno (native `Deno.*`)
|
|
28
|
+
// and Node (`process` / `node:fs`). Detected once at load; see ADR 0036.
|
|
29
|
+
interface DataRuntime {
|
|
30
|
+
cwd(): string;
|
|
31
|
+
env(key: string): string | undefined;
|
|
32
|
+
readTextFile(path: string): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
const RT: DataRuntime = ((): DataRuntime => {
|
|
35
|
+
const g = globalThis as unknown as {
|
|
36
|
+
Deno?: {
|
|
37
|
+
cwd(): string;
|
|
38
|
+
env: { get(k: string): string | undefined };
|
|
39
|
+
readTextFile(p: string): Promise<string>;
|
|
40
|
+
};
|
|
41
|
+
process?: { cwd(): string; env: Record<string, string | undefined> };
|
|
42
|
+
};
|
|
43
|
+
if (g.Deno) {
|
|
44
|
+
const d = g.Deno;
|
|
45
|
+
return { cwd: () => d.cwd(), env: (k) => d.env.get(k), readTextFile: (p) => d.readTextFile(p) };
|
|
46
|
+
}
|
|
47
|
+
const p = g.process!;
|
|
48
|
+
return {
|
|
49
|
+
cwd: () => p.cwd(),
|
|
50
|
+
env: (k) => p.env[k],
|
|
51
|
+
readTextFile: async (path) => (await import("node:fs/promises")).readFile(path, "utf8"),
|
|
52
|
+
};
|
|
53
|
+
})();
|
|
54
|
+
|
|
55
|
+
/** One column of a table, from the datasource's introspected schema. */
|
|
56
|
+
export interface ColumnMeta {
|
|
57
|
+
name: string;
|
|
58
|
+
type: string;
|
|
59
|
+
notNull: boolean;
|
|
60
|
+
primaryKey: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One foreign-key constraint: `column` in this table references
|
|
64
|
+
* `refTable(refColumn)`. `refColumn` is empty when the FK targets the parent's
|
|
65
|
+
* primary key without naming a column. `onDelete` is the referential action
|
|
66
|
+
* (e.g. `CASCADE`), empty when none was declared. */
|
|
67
|
+
export interface ForeignKeyMeta {
|
|
68
|
+
column: string;
|
|
69
|
+
refTable: string;
|
|
70
|
+
refColumn: string;
|
|
71
|
+
onDelete: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** One table: its columns, the names of its indexes, and its foreign keys.
|
|
75
|
+
* Powers the DB Manager, form data-binding, and the ADR 0029 domain-type ↔ table
|
|
76
|
+
* projection. */
|
|
77
|
+
export interface TableMeta {
|
|
78
|
+
name: string;
|
|
79
|
+
columns: ColumnMeta[];
|
|
80
|
+
indexes: string[];
|
|
81
|
+
foreignKeys: ForeignKeyMeta[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type Row = Record<string, unknown>;
|
|
85
|
+
|
|
86
|
+
export interface ExecResult {
|
|
87
|
+
/** Rows changed by an INSERT/UPDATE/DELETE. */
|
|
88
|
+
changed: number;
|
|
89
|
+
/** Rowid of the last inserted row, when the driver reports one. */
|
|
90
|
+
lastInsertId?: number | bigint;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// The one thin, uniform interface behind every driver (ADR 0024 §2) — the
|
|
94
|
+
/// `TDataSet` equivalent. The driver underneath is interchangeable because every
|
|
95
|
+
/// consumer shares exactly this surface.
|
|
96
|
+
export interface DataSource {
|
|
97
|
+
/** Run a SELECT (or any row-returning statement) and collect the rows. */
|
|
98
|
+
query(sql: string, params?: unknown[]): Promise<Row[]>;
|
|
99
|
+
/** Run a non-row statement (INSERT/UPDATE/DELETE/DDL). */
|
|
100
|
+
exec(sql: string, params?: unknown[]): Promise<ExecResult>;
|
|
101
|
+
/** Run `fn` inside a transaction, committing on success and rolling back on
|
|
102
|
+
* throw. The handle passed to `fn` targets the same connection. */
|
|
103
|
+
tx<T>(fn: (t: DataSource) => Promise<T>): Promise<T>;
|
|
104
|
+
/** Introspect the datasource's tables/columns/indexes. */
|
|
105
|
+
schema(): Promise<TableMeta[]>;
|
|
106
|
+
/** A typed gateway over one table — the RAD "TTable": manipulate rows as typed
|
|
107
|
+
* records instead of hand-writing SQL. The row type comes from the generated
|
|
108
|
+
* `domain-rows.d.ts` (ADR 0029); `pk` is the primary-key column (default "id"). */
|
|
109
|
+
table<T extends object = Row>(name: string, pk?: string): Table<T>;
|
|
110
|
+
/** Close the underlying connection. */
|
|
111
|
+
close(): void;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// --- env-template resolution (the alias flip) ------------------------------
|
|
115
|
+
|
|
116
|
+
/// Expand `${VAR}` and `${VAR:-default}` against `env`. An unset or empty var
|
|
117
|
+
/// falls back to the `:-default` (or "" when none is given). This is what lets a
|
|
118
|
+
/// manifest `driver`/`url` read `${NANO_APP_DB_DRIVER:-sqlite}` and become
|
|
119
|
+
/// Postgres in production purely from the environment (ADR 0024 §1).
|
|
120
|
+
export function resolveEnvTemplate(
|
|
121
|
+
tpl: string,
|
|
122
|
+
env: (key: string) => string | undefined,
|
|
123
|
+
): string {
|
|
124
|
+
return tpl.replace(
|
|
125
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g,
|
|
126
|
+
(_m, name: string, dflt: string | undefined) => {
|
|
127
|
+
const v = env(name);
|
|
128
|
+
if (v !== undefined && v !== "") return v;
|
|
129
|
+
return dflt ?? "";
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface RawSource {
|
|
135
|
+
driver: unknown;
|
|
136
|
+
url: string;
|
|
137
|
+
migrations?: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface ManifestData {
|
|
141
|
+
default?: string;
|
|
142
|
+
sources: Record<string, RawSource>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface ResolvedSource {
|
|
146
|
+
name: string;
|
|
147
|
+
driver: string;
|
|
148
|
+
url: string;
|
|
149
|
+
migrations?: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/// Resolve one raw manifest source (env-templated `driver`/`url`) to concrete
|
|
153
|
+
/// values against `env`.
|
|
154
|
+
export function resolveSource(
|
|
155
|
+
name: string,
|
|
156
|
+
raw: RawSource,
|
|
157
|
+
env: (key: string) => string | undefined,
|
|
158
|
+
): ResolvedSource {
|
|
159
|
+
const driver = typeof raw.driver === "string"
|
|
160
|
+
? resolveEnvTemplate(raw.driver, env)
|
|
161
|
+
: String(raw.driver);
|
|
162
|
+
return {
|
|
163
|
+
name,
|
|
164
|
+
driver,
|
|
165
|
+
url: resolveEnvTemplate(raw.url, env),
|
|
166
|
+
migrations: raw.migrations,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// List every datasource the manifest declares (resolved against the
|
|
171
|
+
/// environment), plus the `default` source name. Powers the DB Manager's
|
|
172
|
+
/// datasource picker — it enumerates the aliases without opening a connection.
|
|
173
|
+
export async function listSources(
|
|
174
|
+
cwd?: string,
|
|
175
|
+
): Promise<{ default?: string; sources: ResolvedSource[] }> {
|
|
176
|
+
const { data } = await findManifest(cwd ?? RT.cwd());
|
|
177
|
+
const env = (k: string) => RT.env(k);
|
|
178
|
+
const sources = Object.entries(data.sources).map(([name, raw]) =>
|
|
179
|
+
resolveSource(name, raw, env)
|
|
180
|
+
);
|
|
181
|
+
return { default: data.default, sources };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// --- manifest discovery ----------------------------------------------------
|
|
185
|
+
|
|
186
|
+
interface ManifestLocation {
|
|
187
|
+
root: string;
|
|
188
|
+
data: ManifestData;
|
|
189
|
+
/** The raw `types` registry block (ADR 0029 §4.2), or `{}` when absent. */
|
|
190
|
+
types: Record<string, unknown>;
|
|
191
|
+
/** The raw `workers` array (ADR 0022 §E / ADR 0033 §3), or `[]` when absent. */
|
|
192
|
+
workers: WorkerDecl[];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** One `workers[]` entry, narrowed to the fields the typed-worker codegen reads
|
|
196
|
+
* (ADR 0033 §3). `inputType`/`outputType` name declared domain types. */
|
|
197
|
+
export interface WorkerDecl {
|
|
198
|
+
taskType: string;
|
|
199
|
+
inputType?: string;
|
|
200
|
+
outputType?: string;
|
|
201
|
+
/** The `zeebe:header` keys declared on the task (ADR 0033 §3). Model-derived;
|
|
202
|
+
* reified into a typed `job.customHeaders` shape (known keys, `string` values). */
|
|
203
|
+
headerKeys?: string[];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/// Walk up from `startDir` to the first directory containing `nano.app.json` and
|
|
207
|
+
/// return that directory (the project root) plus its `data` block. Workers run
|
|
208
|
+
/// with their cwd inside `workers/<name>/`, so the manifest sits above them.
|
|
209
|
+
async function findManifest(startDir: string): Promise<ManifestLocation> {
|
|
210
|
+
let dir = startDir.replace(/\/+$/, "");
|
|
211
|
+
for (let i = 0; i < 12; i++) {
|
|
212
|
+
try {
|
|
213
|
+
const text = await RT.readTextFile(`${dir}/nano.app.json`);
|
|
214
|
+
const json = JSON.parse(text) as {
|
|
215
|
+
data?: ManifestData;
|
|
216
|
+
types?: Record<string, unknown>;
|
|
217
|
+
workers?: WorkerDecl[];
|
|
218
|
+
};
|
|
219
|
+
const data = json.data ?? { sources: {} };
|
|
220
|
+
return {
|
|
221
|
+
root: dir,
|
|
222
|
+
data: { default: data.default, sources: data.sources ?? {} },
|
|
223
|
+
types: json.types ?? {},
|
|
224
|
+
workers: Array.isArray(json.workers) ? json.workers : [],
|
|
225
|
+
};
|
|
226
|
+
} catch {
|
|
227
|
+
// not here — keep walking up
|
|
228
|
+
}
|
|
229
|
+
const slash = dir.lastIndexOf("/");
|
|
230
|
+
if (slash <= 0) break;
|
|
231
|
+
const parent = dir.slice(0, slash);
|
|
232
|
+
if (parent === dir) break;
|
|
233
|
+
dir = parent;
|
|
234
|
+
}
|
|
235
|
+
throw new Error(
|
|
236
|
+
`nano.app.json not found at or above ${startDir}; datasources require an Urban manifest`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/// The manifest's domain-type registry (ADR 0029 §4.2): the transient/declared
|
|
241
|
+
/// shapes with no backing table. Returns `{}` when the manifest declares none.
|
|
242
|
+
/// The domain-type reifier folds these in alongside the datasource table spine.
|
|
243
|
+
export async function manifestTypes(
|
|
244
|
+
cwd?: string,
|
|
245
|
+
): Promise<Record<string, unknown>> {
|
|
246
|
+
return (await findManifest(cwd ?? RT.cwd())).types;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/// The manifest's `workers` declarations (ADR 0033 §3), narrowed to the fields
|
|
250
|
+
/// the typed-worker codegen reads (`taskType` + `inputType`/`outputType`).
|
|
251
|
+
/// Returns `[]` when the manifest declares no workers.
|
|
252
|
+
export async function manifestWorkers(
|
|
253
|
+
cwd?: string,
|
|
254
|
+
): Promise<WorkerDecl[]> {
|
|
255
|
+
return (await findManifest(cwd ?? RT.cwd())).workers;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/// Turn a datasource `url` into a filesystem path for file-backed drivers.
|
|
259
|
+
/// Accepts `file:./app.db`, `file:app.db`, `file:/abs/app.db`, a bare relative
|
|
260
|
+
/// or absolute path, or `:memory:`. Relative paths resolve against the project
|
|
261
|
+
/// root so `file:./app.db` is the same file wherever the consumer's cwd is.
|
|
262
|
+
export function sqlitePath(url: string, root: string): string {
|
|
263
|
+
let p = url.startsWith("file:") ? url.slice("file:".length) : url;
|
|
264
|
+
if (p === "" || p === ":memory:") return ":memory:";
|
|
265
|
+
while (p.startsWith("./")) p = p.slice(2);
|
|
266
|
+
if (!p.startsWith("/")) p = `${root}/${p}`;
|
|
267
|
+
return p;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --- SQLite driver (core) --------------------------------------------------
|
|
271
|
+
|
|
272
|
+
import { DatabaseSync } from "node:sqlite";
|
|
273
|
+
|
|
274
|
+
function quoteIdent(name: string): string {
|
|
275
|
+
return `"${name.replaceAll('"', '""')}"`;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// --- typed table gateway (the RAD "TTable") --------------------------------
|
|
279
|
+
|
|
280
|
+
/** Build a parameterised ` WHERE a = ? AND b = ?` clause from an equality map;
|
|
281
|
+
* an empty map yields an empty clause (matches all rows). Takes `object` (not
|
|
282
|
+
* `Row`) so a `Partial<T>` for a generated `interface` row type (which lacks a
|
|
283
|
+
* string index signature) is accepted; keys/values are read via a `Row` cast. */
|
|
284
|
+
function whereClause(where: object): { clause: string; params: unknown[] } {
|
|
285
|
+
const w = where as Row;
|
|
286
|
+
const keys = Object.keys(w);
|
|
287
|
+
if (keys.length === 0) return { clause: "", params: [] };
|
|
288
|
+
const clause = " WHERE " +
|
|
289
|
+
keys.map((k) => `${quoteIdent(k)} = ?`).join(" AND ");
|
|
290
|
+
return { clause, params: keys.map((k) => w[k]) };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/// A typed gateway over a single table — the record-oriented data object a RAD
|
|
294
|
+
/// worker binds to instead of hand-writing SQL (the Delphi `TTable`/data-module
|
|
295
|
+
/// idea, ADR 0029 §6). It builds parameterised SQL from a typed row object's own
|
|
296
|
+
/// keys, so callers manipulate rows as records. `T` comes from the generated
|
|
297
|
+
/// `domain-rows.d.ts`; this class is generic *runtime* and knows nothing about any
|
|
298
|
+
/// specific schema, so it stays a plain dual-runtime (Node + Deno) module — no
|
|
299
|
+
/// codegen, no Deno-only APIs. `pk` is the primary-key column (default `id`).
|
|
300
|
+
export class Table<T extends object = Row> {
|
|
301
|
+
readonly name: string;
|
|
302
|
+
readonly pk: string;
|
|
303
|
+
#src: DataSource;
|
|
304
|
+
|
|
305
|
+
constructor(src: DataSource, name: string, pk = "id") {
|
|
306
|
+
this.#src = src;
|
|
307
|
+
this.name = name;
|
|
308
|
+
this.pk = pk;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Insert one row (only the present keys are written); returns the new
|
|
312
|
+
* primary-key value (the inserted rowid for an INTEGER PRIMARY KEY). */
|
|
313
|
+
async insert(row: Partial<T>): Promise<number | bigint> {
|
|
314
|
+
const keys = Object.keys(row);
|
|
315
|
+
if (keys.length === 0) {
|
|
316
|
+
throw new Error(`Table(${this.name}).insert: no columns to insert`);
|
|
317
|
+
}
|
|
318
|
+
const cols = keys.map(quoteIdent).join(", ");
|
|
319
|
+
const ph = keys.map(() => "?").join(", ");
|
|
320
|
+
const r = await this.#src.exec(
|
|
321
|
+
`INSERT INTO ${quoteIdent(this.name)} (${cols}) VALUES (${ph})`,
|
|
322
|
+
keys.map((k) => (row as Row)[k]),
|
|
323
|
+
);
|
|
324
|
+
return r.lastInsertId ?? 0;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Fetch the row with the given primary key, or `undefined`. */
|
|
328
|
+
async get(id: unknown): Promise<T | undefined> {
|
|
329
|
+
const rows = await this.#src.query(
|
|
330
|
+
`SELECT * FROM ${quoteIdent(this.name)} WHERE ${quoteIdent(this.pk)} = ? LIMIT 1`,
|
|
331
|
+
[id],
|
|
332
|
+
);
|
|
333
|
+
return rows[0] as T | undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Every row (optionally capped at `limit`). */
|
|
337
|
+
async all(limit?: number): Promise<T[]> {
|
|
338
|
+
const lim = typeof limit === "number"
|
|
339
|
+
? ` LIMIT ${Math.max(0, Math.floor(limit))}`
|
|
340
|
+
: "";
|
|
341
|
+
return (await this.#src.query(
|
|
342
|
+
`SELECT * FROM ${quoteIdent(this.name)}${lim}`,
|
|
343
|
+
)) as T[];
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Rows matching an equality filter (keys ANDed). An empty filter matches
|
|
347
|
+
* all rows. */
|
|
348
|
+
async find(where: Partial<T> = {}): Promise<T[]> {
|
|
349
|
+
const { clause, params } = whereClause(where as Row);
|
|
350
|
+
return (await this.#src.query(
|
|
351
|
+
`SELECT * FROM ${quoteIdent(this.name)}${clause}`,
|
|
352
|
+
params,
|
|
353
|
+
)) as T[];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The first row matching an equality filter, or `undefined`. */
|
|
357
|
+
async findOne(where: Partial<T> = {}): Promise<T | undefined> {
|
|
358
|
+
const { clause, params } = whereClause(where as Row);
|
|
359
|
+
const rows = await this.#src.query(
|
|
360
|
+
`SELECT * FROM ${quoteIdent(this.name)}${clause} LIMIT 1`,
|
|
361
|
+
params,
|
|
362
|
+
);
|
|
363
|
+
return rows[0] as T | undefined;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Patch the row with the given primary key; returns rows changed. */
|
|
367
|
+
async update(id: unknown, patch: Partial<T>): Promise<number> {
|
|
368
|
+
const keys = Object.keys(patch);
|
|
369
|
+
if (keys.length === 0) return 0;
|
|
370
|
+
const set = keys.map((k) => `${quoteIdent(k)} = ?`).join(", ");
|
|
371
|
+
const r = await this.#src.exec(
|
|
372
|
+
`UPDATE ${quoteIdent(this.name)} SET ${set} WHERE ${quoteIdent(this.pk)} = ?`,
|
|
373
|
+
[...keys.map((k) => (patch as Row)[k]), id],
|
|
374
|
+
);
|
|
375
|
+
return r.changed;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** Delete the row with the given primary key; returns rows changed. */
|
|
379
|
+
async delete(id: unknown): Promise<number> {
|
|
380
|
+
const r = await this.#src.exec(
|
|
381
|
+
`DELETE FROM ${quoteIdent(this.name)} WHERE ${quoteIdent(this.pk)} = ?`,
|
|
382
|
+
[id],
|
|
383
|
+
);
|
|
384
|
+
return r.changed;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Count rows matching an equality filter (all rows when omitted). */
|
|
388
|
+
async count(where: Partial<T> = {}): Promise<number> {
|
|
389
|
+
const { clause, params } = whereClause(where as Row);
|
|
390
|
+
const rows = await this.#src.query(
|
|
391
|
+
`SELECT COUNT(*) AS n FROM ${quoteIdent(this.name)}${clause}`,
|
|
392
|
+
params,
|
|
393
|
+
);
|
|
394
|
+
return Number((rows[0] as Row)?.n ?? 0);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
class SqliteDataSource implements DataSource {
|
|
399
|
+
#db: DatabaseSync;
|
|
400
|
+
#onClose?: () => void;
|
|
401
|
+
|
|
402
|
+
constructor(path: string, onClose?: () => void) {
|
|
403
|
+
this.#db = new DatabaseSync(path);
|
|
404
|
+
if (path !== ":memory:") this.#db.exec("PRAGMA journal_mode = WAL;");
|
|
405
|
+
this.#db.exec("PRAGMA foreign_keys = ON;");
|
|
406
|
+
this.#onClose = onClose;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
query(sql: string, params: unknown[] = []): Promise<Row[]> {
|
|
410
|
+
const rows = this.#db.prepare(sql).all(...(params as never[]));
|
|
411
|
+
return Promise.resolve(rows as Row[]);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
exec(sql: string, params: unknown[] = []): Promise<ExecResult> {
|
|
415
|
+
const r = this.#db.prepare(sql).run(...(params as never[]));
|
|
416
|
+
return Promise.resolve({
|
|
417
|
+
changed: Number(r.changes),
|
|
418
|
+
lastInsertId: r.lastInsertRowid,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async tx<T>(fn: (t: DataSource) => Promise<T>): Promise<T> {
|
|
423
|
+
this.#db.exec("BEGIN");
|
|
424
|
+
try {
|
|
425
|
+
const out = await fn(this);
|
|
426
|
+
this.#db.exec("COMMIT");
|
|
427
|
+
return out;
|
|
428
|
+
} catch (e) {
|
|
429
|
+
this.#db.exec("ROLLBACK");
|
|
430
|
+
throw e;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
schema(): Promise<TableMeta[]> {
|
|
435
|
+
// Exclude SQLite internals (`sqlite_%`) and Nano's own bookkeeping tables
|
|
436
|
+
// (`_nano_%`, e.g. the `_nano_migrations` ledger): neither is a user/domain
|
|
437
|
+
// table, so they must never surface in the domain model, DB Manager, or forms.
|
|
438
|
+
const tables = this.#db
|
|
439
|
+
.prepare(
|
|
440
|
+
"SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '\\_nano\\_%' ESCAPE '\\' ORDER BY name",
|
|
441
|
+
)
|
|
442
|
+
.all() as Array<{ name: string }>;
|
|
443
|
+
const out: TableMeta[] = [];
|
|
444
|
+
for (const t of tables) {
|
|
445
|
+
const cols = this.#db
|
|
446
|
+
.prepare(`PRAGMA table_info(${quoteIdent(t.name)})`)
|
|
447
|
+
.all() as Array<{ name: string; type: string; notnull: number; pk: number }>;
|
|
448
|
+
const idx = this.#db
|
|
449
|
+
.prepare(`PRAGMA index_list(${quoteIdent(t.name)})`)
|
|
450
|
+
.all() as Array<{ name: string }>;
|
|
451
|
+
const fks = this.#db
|
|
452
|
+
.prepare(`PRAGMA foreign_key_list(${quoteIdent(t.name)})`)
|
|
453
|
+
.all() as Array<
|
|
454
|
+
{ from: string; table: string; to: string | null; on_delete?: string }
|
|
455
|
+
>;
|
|
456
|
+
out.push({
|
|
457
|
+
name: t.name,
|
|
458
|
+
columns: cols.map((c) => ({
|
|
459
|
+
name: c.name,
|
|
460
|
+
type: c.type,
|
|
461
|
+
notNull: !!c.notnull,
|
|
462
|
+
primaryKey: !!c.pk,
|
|
463
|
+
})),
|
|
464
|
+
indexes: idx.map((i) => String(i.name)),
|
|
465
|
+
foreignKeys: fks.map((f) => ({
|
|
466
|
+
column: f.from,
|
|
467
|
+
refTable: f.table,
|
|
468
|
+
refColumn: f.to ?? "",
|
|
469
|
+
onDelete: f.on_delete && f.on_delete.toUpperCase() !== "NO ACTION"
|
|
470
|
+
? f.on_delete.toUpperCase()
|
|
471
|
+
: "",
|
|
472
|
+
})),
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
return Promise.resolve(out);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
close(): void {
|
|
479
|
+
this.#db.close();
|
|
480
|
+
this.#onClose?.();
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
table<T extends object = Row>(name: string, pk = "id"): Table<T> {
|
|
484
|
+
return new Table<T>(this, name, pk);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// --- open (the named-alias entrypoint) -------------------------------------
|
|
489
|
+
|
|
490
|
+
// One handle per resolved (driver,url), so repeated opens in a process share a
|
|
491
|
+
// connection rather than reopening the file.
|
|
492
|
+
const CACHE = new Map<string, DataSource>();
|
|
493
|
+
|
|
494
|
+
export interface OpenOptions {
|
|
495
|
+
/** Directory to begin the manifest search from. Defaults to the runtime cwd. */
|
|
496
|
+
cwd?: string;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/// Open the named datasource (or the manifest's `default` when `name` is
|
|
500
|
+
/// omitted), resolving its driver/url from the environment. Bind by NAME — this
|
|
501
|
+
/// is the seam the SQLite→server flip happens behind (ADR 0024 §1).
|
|
502
|
+
export async function openDataSource(
|
|
503
|
+
name?: string,
|
|
504
|
+
opts?: OpenOptions,
|
|
505
|
+
): Promise<DataSource> {
|
|
506
|
+
const cwd = opts?.cwd ?? RT.cwd();
|
|
507
|
+
const { root, data } = await findManifest(cwd);
|
|
508
|
+
const srcName = name ?? data.default ?? Object.keys(data.sources)[0];
|
|
509
|
+
if (!srcName) {
|
|
510
|
+
throw new Error("no datasource declared in nano.app.json (data.sources is empty)");
|
|
511
|
+
}
|
|
512
|
+
const raw = data.sources[srcName];
|
|
513
|
+
if (!raw) {
|
|
514
|
+
throw new Error(
|
|
515
|
+
`datasource "${srcName}" is not declared in nano.app.json data.sources`,
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
const resolved = resolveSource(srcName, raw, (k) => RT.env(k));
|
|
519
|
+
const key = `${resolved.driver}::${resolved.url}`;
|
|
520
|
+
const hit = CACHE.get(key);
|
|
521
|
+
if (hit) return hit;
|
|
522
|
+
|
|
523
|
+
let ds: DataSource;
|
|
524
|
+
if (resolved.driver === "sqlite") {
|
|
525
|
+
ds = new SqliteDataSource(sqlitePath(resolved.url, root), () => CACHE.delete(key));
|
|
526
|
+
} else {
|
|
527
|
+
throw new Error(
|
|
528
|
+
`datasource driver "${resolved.driver}" is not bundled; install a nano-ide-data-${resolved.driver} pack (ADR 0024 §3)`,
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
CACHE.set(key, ds);
|
|
532
|
+
return ds;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/// Alias reading like the ADR's `ctx.data(...)`: `data("app")`.
|
|
536
|
+
export const data = openDataSource;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// AUTO-GENERATED by nanobpmn from the datasource schema (ADR 0029 §4.1).
|
|
2
|
+
// Do not edit — regenerated from the live tables. Erased at `deno compile`.
|
|
3
|
+
// eslint-disable
|
|
4
|
+
|
|
5
|
+
export interface Escalations {
|
|
6
|
+
/** INTEGER (primary key) */
|
|
7
|
+
id: number;
|
|
8
|
+
/** TEXT */
|
|
9
|
+
pr_key: string;
|
|
10
|
+
/** INTEGER */
|
|
11
|
+
round_no: number;
|
|
12
|
+
/** TEXT */
|
|
13
|
+
kind: string;
|
|
14
|
+
/** TEXT */
|
|
15
|
+
question: string;
|
|
16
|
+
/** TEXT */
|
|
17
|
+
answer: string | null;
|
|
18
|
+
/** TEXT */
|
|
19
|
+
status: string;
|
|
20
|
+
/** TEXT */
|
|
21
|
+
asked_at: string;
|
|
22
|
+
/** TEXT */
|
|
23
|
+
answered_at: string | null;
|
|
24
|
+
/** TEXT */
|
|
25
|
+
transcript: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PullRequests {
|
|
29
|
+
/** TEXT (primary key) */
|
|
30
|
+
pr_key: string;
|
|
31
|
+
/** TEXT */
|
|
32
|
+
repo: string;
|
|
33
|
+
/** INTEGER */
|
|
34
|
+
number: number;
|
|
35
|
+
/** TEXT */
|
|
36
|
+
url: string;
|
|
37
|
+
/** TEXT */
|
|
38
|
+
title: string | null;
|
|
39
|
+
/** TEXT */
|
|
40
|
+
status: string;
|
|
41
|
+
/** INTEGER */
|
|
42
|
+
current_round: number;
|
|
43
|
+
/** TEXT */
|
|
44
|
+
process_key: string | null;
|
|
45
|
+
/** TEXT */
|
|
46
|
+
waiting_since: string | null;
|
|
47
|
+
/** INTEGER */
|
|
48
|
+
last_review_id: number | null;
|
|
49
|
+
/** TEXT */
|
|
50
|
+
outcome: string | null;
|
|
51
|
+
/** TEXT */
|
|
52
|
+
created_at: string;
|
|
53
|
+
/** TEXT */
|
|
54
|
+
updated_at: string;
|
|
55
|
+
/** TEXT */
|
|
56
|
+
converged_at: string | null;
|
|
57
|
+
/** INTEGER */
|
|
58
|
+
open_escalation_id: number | null;
|
|
59
|
+
/** TEXT */
|
|
60
|
+
open_escalation_question: string | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface Rounds {
|
|
64
|
+
/** INTEGER (primary key) */
|
|
65
|
+
id: number;
|
|
66
|
+
/** TEXT */
|
|
67
|
+
pr_key: string;
|
|
68
|
+
/** INTEGER */
|
|
69
|
+
round_no: number;
|
|
70
|
+
/** TEXT */
|
|
71
|
+
status: string | null;
|
|
72
|
+
/** TEXT */
|
|
73
|
+
summary: string | null;
|
|
74
|
+
/** TEXT */
|
|
75
|
+
started_at: string;
|
|
76
|
+
/** TEXT */
|
|
77
|
+
ended_at: string | null;
|
|
78
|
+
/** TEXT */
|
|
79
|
+
transcript: string | null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Every table keyed by its wire name — index it for a row type. */
|
|
83
|
+
export interface DomainTables {
|
|
84
|
+
"escalations": Escalations;
|
|
85
|
+
"pull_requests": PullRequests;
|
|
86
|
+
"rounds": Rounds;
|
|
87
|
+
}
|