pgkiln 0.49.0 → 0.51.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 (70) hide show
  1. package/SECURITY.md +3 -0
  2. package/db/migrations/119_workspace_home.sql +30 -0
  3. package/db/migrations/120_datasets.sql +87 -0
  4. package/docs/apex-feature-parity.md +7 -6
  5. package/docs/guide/01-installation.md +5 -0
  6. package/docs/guide/03-builder.md +140 -11
  7. package/docs/guide/04-pages-and-regions.md +25 -1
  8. package/docs/guide/12-development.md +47 -11
  9. package/examples/hr/hr_63_datasets.sql +29 -0
  10. package/package.json +1 -1
  11. package/public/app.css +6 -0
  12. package/public/builder.css +95 -3
  13. package/public/code-editor.css +32 -79
  14. package/public/code-editor.js +58 -3
  15. package/public/data-modeler.css +17 -14
  16. package/public/worksheet.css +14 -7
  17. package/public/worksheet.js +119 -3
  18. package/src/app.ts +5 -1
  19. package/src/builder/changelog.ts +1 -1
  20. package/src/builder/data-modeler.ts +1 -1
  21. package/src/builder/datasets.ts +472 -0
  22. package/src/builder/db-users.ts +2 -2
  23. package/src/builder/home.ts +12 -7
  24. package/src/builder/instance.ts +2 -0
  25. package/src/builder/object-browser.ts +4 -4
  26. package/src/builder/querybuilder.ts +1 -1
  27. package/src/builder/quicksql.ts +1 -1
  28. package/src/builder/reporter.ts +52 -3
  29. package/src/builder/routes.ts +3 -3
  30. package/src/builder/scheduling.ts +2 -2
  31. package/src/builder/scripts.ts +1 -1
  32. package/src/builder/sql-charts.ts +1 -1
  33. package/src/builder/sql-monitor.ts +1 -1
  34. package/src/builder/subscriptions.ts +1 -0
  35. package/src/builder/table-editor.ts +2 -2
  36. package/src/builder/ui.ts +7 -5
  37. package/src/builder/worksheet.ts +171 -23
  38. package/src/builder/workspace-home.ts +283 -0
  39. package/src/builder/workspaces.ts +7 -4
  40. package/src/datasets.ts +367 -0
  41. package/src/i18n/ar.ts +16 -0
  42. package/src/i18n/cs.ts +16 -0
  43. package/src/i18n/da.ts +16 -0
  44. package/src/i18n/de.ts +16 -0
  45. package/src/i18n/el.ts +16 -0
  46. package/src/i18n/es.ts +16 -0
  47. package/src/i18n/fi.ts +16 -0
  48. package/src/i18n/fr.ts +16 -0
  49. package/src/i18n/he.ts +16 -0
  50. package/src/i18n/it.ts +16 -0
  51. package/src/i18n/ja.ts +16 -0
  52. package/src/i18n/ko.ts +16 -0
  53. package/src/i18n/nb.ts +16 -0
  54. package/src/i18n/pl.ts +16 -0
  55. package/src/i18n/pt.ts +16 -0
  56. package/src/i18n/ru.ts +16 -0
  57. package/src/i18n/sv.ts +16 -0
  58. package/src/i18n/tr.ts +16 -0
  59. package/src/i18n/uk.ts +16 -0
  60. package/src/i18n/zh.ts +16 -0
  61. package/src/i18n.ts +32 -0
  62. package/src/instance.ts +1 -0
  63. package/src/release-check.ts +139 -0
  64. package/src/reporting-app.ts +101 -0
  65. package/src/runtime/data-reporter.ts +199 -32
  66. package/src/session.ts +7 -0
  67. package/src/sqlworksheet.ts +9 -6
  68. package/src/subscriptions.ts +2 -1
  69. package/src/worksheet-txn.ts +270 -0
  70. package/src/workspace-export.ts +24 -4
@@ -1,7 +1,9 @@
1
1
  import pg from 'pg';
2
2
  import type { FastifyInstance, FastifyReply } from 'fastify';
3
- import { appTx, savepoint } from '../db.ts';
3
+ import { appDatasets, DATASET_NAME, datasetSql } from '../datasets.ts';
4
+ import { appTx, owner, runtime, savepoint } from '../db.ts';
4
5
  import { html, raw, type Raw } from '../html.ts';
6
+ import { english } from '../i18n.ts';
5
7
  import type { Region } from '../metadata.ts';
6
8
  import { logActivity, saveState } from '../session.ts';
7
9
  import { checkPageAccess, computeVisibility, Forbidden, isAuthorized } from './authz.ts';
@@ -21,7 +23,13 @@ import { loadContext, simplePage, txContext, type Req } from './routes.ts';
21
23
  // "columns": [{"name": "ename", "label": "Name"}, {"name": "sal", "label": "Salary", "format": "FML999G990D00"}]}],
22
24
  // "sharing": true, // users may share reports with everyone (default true)
23
25
  // "share_authz": "SCHEME", // …but only users that pass this scheme (default: every signed-in user)
24
- // "page_size": 25
26
+ // "page_size": 25,
27
+ // // (sprint 110) a workspace dataset as a source (the builder's Data Reporter): its joined tables, columns,
28
+ // // labels, masks and row filter come from meta.dataset; label and description may be given here too
29
+ // // "sources": [{"id": "hr", "dataset": "hr"}]
30
+ // "edit_authz": "SCHEME", // who may build, change and save reports; others only run shared ones (default: everyone)
31
+ // "admin_authz": "SCHEME", // who may unpublish and delete anyone's reports of the region
32
+ // "publishing": true // shared reports are "published": a stable URL (/a/<alias>/reports/<id>), pinning
25
33
  // }
26
34
  //
27
35
  // A report is a definition (Def): the source, the columns, filters, group by
@@ -39,6 +47,7 @@ export interface SourceColumn {
39
47
  name: string;
40
48
  label?: string;
41
49
  format?: string;
50
+ description?: string;
42
51
  }
43
52
  export interface Source {
44
53
  id: string;
@@ -47,6 +56,10 @@ export interface Source {
47
56
  schema: string;
48
57
  table: string;
49
58
  columns: SourceColumn[];
59
+ /** (sprint 110) the name of a workspace dataset: schema and table are empty until resolveSources() */
60
+ dataset?: string;
61
+ /** (sprint 110) the dataset's SELECT (built by datasetSql from the checked definition) */
62
+ sql?: string;
50
63
  }
51
64
  export interface Def {
52
65
  source: string;
@@ -73,6 +86,10 @@ export function sourcesOf(r: { config: Record<string, any> }): Source[] {
73
86
  const list = Array.isArray(r.config.sources) ? r.config.sources : [];
74
87
  const out: Source[] = [];
75
88
  for (const s of list) {
89
+ if (s && typeof s === 'object' && SOURCE_ID.test(str(s.id)) && DATASET_NAME.test(str(s.dataset)) && !out.some((x) => x.id === s.id)) {
90
+ out.push({ id: s.id, label: str(s.label) || undefined, description: str(s.description) || undefined, schema: '', table: '', columns: [], dataset: s.dataset });
91
+ continue;
92
+ }
76
93
  if (!s || typeof s !== 'object' || !SOURCE_ID.test(str(s.id)) || !str(s.schema) || !str(s.table) || out.some((x) => x.id === s.id)) continue;
77
94
  // never pgkiln's own or the system's tables
78
95
  if (RESERVED_SCHEMAS.test(s.schema)) continue;
@@ -82,15 +99,41 @@ export function sourcesOf(r: { config: Record<string, any> }): Source[] {
82
99
  return out;
83
100
  }
84
101
 
102
+ /**
103
+ * (sprint 110) The region's sources with its datasets filled in (schema, columns with labels, masks and descriptions,
104
+ * the dataset's SELECT); a dataset source whose dataset is gone is left out.
105
+ */
106
+ export async function resolveSources(r: { config: Record<string, any> }, appId: number): Promise<Source[]> {
107
+ const list = sourcesOf(r);
108
+ const names = [...new Set(list.map((s) => s.dataset).filter((x): x is string => !!x))];
109
+ if (!names.length) return list;
110
+ const sets = await appDatasets(runtime, appId, names);
111
+ const out: Source[] = [];
112
+ for (const s of list) {
113
+ if (!s.dataset) {
114
+ out.push(s);
115
+ continue;
116
+ }
117
+ const d = sets.get(s.dataset);
118
+ if (!d || !d.columns.length) continue;
119
+ out.push({
120
+ ...s, label: s.label ?? d.label, description: s.description ?? d.description, schema: d.schema, table: d.name, sql: datasetSql(d),
121
+ columns: d.columns.map((c) => ({ name: c.name, ...(c.label ? { label: c.label } : {}), ...(c.format ? { format: c.format } : {}), ...(c.description ? { description: c.description } : {}) })),
122
+ });
123
+ }
124
+ return out;
125
+ }
126
+
85
127
  /** A column a user may use: offered by the developer and readable now. */
86
128
  export interface Col {
87
129
  name: string;
88
130
  label: string;
89
131
  format?: string;
132
+ description?: string;
90
133
  type: number;
91
134
  }
92
135
 
93
- const objectSql = (s: Source) => `${pg.escapeIdentifier(s.schema)}.${pg.escapeIdentifier(s.table)}`;
136
+ const objectSql = (s: Source) => (s.sql ? `(${s.sql})` : `${pg.escapeIdentifier(s.schema)}.${pg.escapeIdentifier(s.table)}`);
94
137
  const q = (col: string) => `"__q".${pg.escapeIdentifier(col)}`;
95
138
 
96
139
  /**
@@ -103,7 +146,7 @@ export async function availableColumns(c: pg.PoolClient, s: Source, tr: (x: stri
103
146
  const out = new Map<string, Col>();
104
147
  for (const sc of s.columns)
105
148
  if (types.has(sc.name) && !out.has(sc.name))
106
- out.set(sc.name, { name: sc.name, label: tr(sc.label || heading(sc.name)), format: sc.format || undefined, type: types.get(sc.name)! });
149
+ out.set(sc.name, { name: sc.name, label: tr(sc.label || heading(sc.name)), format: sc.format || undefined, description: sc.description ? tr(sc.description) : undefined, type: types.get(sc.name)! });
107
150
  return out;
108
151
  }
109
152
 
@@ -239,6 +282,35 @@ export async function mayShare(ctx: PageContext, r: Region) {
239
282
  return typeof r.config.share_authz === 'string' && r.config.share_authz ? isAuthorized(ctx, r.config.share_authz) : true;
240
283
  }
241
284
 
285
+ /** (sprint 110) May the current user build, change and save reports of this region (config.edit_authz)? */
286
+ export async function mayEdit(ctx: PageContext, r: Region) {
287
+ const a = r.config.edit_authz;
288
+ return typeof a === 'string' && a ? isAuthorized(ctx, a) : true;
289
+ }
290
+
291
+ /** (sprint 110) May the current user unpublish and delete anyone's reports of this region (config.admin_authz)? */
292
+ export async function mayModerate(ctx: PageContext, r: Region) {
293
+ const a = r.config.admin_authz;
294
+ return ctx.user !== 'nobody' && typeof a === 'string' && a ? isAuthorized(ctx, a) : false;
295
+ }
296
+
297
+ /** (sprint 110) Shared reports are called published (a stable URL, pins). */
298
+ const publishing = (r: Region) => r.config.publishing === true;
299
+
300
+ /** (sprint 110) A report's stable URL: /a/<alias>/reports/<id> (it opens the report on its page). */
301
+ export const reportLink = (ctx: PageContext, id: number) => `${ctx.base}/reports/${id}`;
302
+
303
+ const MAX_PINS = 20;
304
+ const pinsName = (r: Region) => `reporter.pins.${r.id}`;
305
+
306
+ /** (sprint 110) The published reports the current user pinned in this region (a user preference; with config.publishing). */
307
+ async function pinsOf(ctx: PageContext, r: Region): Promise<number[]> {
308
+ if (ctx.user === 'nobody') return [];
309
+ const c = ctx.client!;
310
+ const v = (await savepoint(c, () => c.query<{ v: string | null }>('select meta.get_preference($1) as v', [pinsName(r)]))).rows[0]?.v ?? '';
311
+ return v.split(',').filter((x) => /^\d{1,9}$/.test(x)).map(Number).slice(0, MAX_PINS);
312
+ }
313
+
242
314
  const pageSize = (r: Region) => {
243
315
  const n = Number(r.config.page_size);
244
316
  return Number.isInteger(n) && n >= 5 && n <= 200 ? n : DEFAULT_PAGE_SIZE;
@@ -261,27 +333,29 @@ export async function renderDataReporter(ctx: PageContext, r: Region): Promise<R
261
333
  const t = ctx.locale.t;
262
334
  const c = ctx.client!;
263
335
  const prefix = prefixOf(r);
264
- const sources = sourcesOf(r);
336
+ const sources = await resolveSources(r, ctx.app.id);
265
337
  if (!sources.length) return html`<p class="empty">${t('reporter.no_sources')}</p>`;
338
+ // (sprint 110) users who may not edit only run saved reports: no editor, no reports of their own
339
+ const edit = await mayEdit(ctx, r);
266
340
  const openId = Number(ctx.params.get(`${prefix}open`));
267
341
  let saved: Saved | undefined;
268
342
  if (Number.isInteger(openId) && openId > 0) {
269
343
  saved = (await savepoint(c, () => c.query<Saved>('select * from meta.data_reports where id = $1 and region_id = $2', [openId, r.id]))).rows[0];
270
- if (!saved) return html`<div class="alert alert-error" role="alert">${t('reporter.not_found')}</div>${await home(ctx, r, sources)}`;
344
+ if (!saved) return html`<div class="alert alert-error" role="alert">${t('reporter.not_found')}</div>${await home(ctx, r, sources, edit)}`;
271
345
  }
272
- const edited = ctx.params.has(`${prefix}src`);
346
+ const edited = edit && ctx.params.has(`${prefix}src`);
273
347
  const sourceId = edited ? ctx.params.get(`${prefix}src`)! : saved ? str(saved.definition?.source) : '';
274
- if (!sourceId) return home(ctx, r, sources);
348
+ if (!sourceId) return home(ctx, r, sources, edit);
275
349
  const source = sources.find((s) => s.id === sourceId);
276
- if (!source) return html`<div class="alert alert-error" role="alert">${t('reporter.unknown_source')}</div>${await home(ctx, r, sources)}`;
350
+ if (!source) return html`<div class="alert alert-error" role="alert">${t('reporter.unknown_source')}</div>${await home(ctx, r, sources, edit)}`;
277
351
  let cols: Map<string, Col>;
278
352
  try {
279
353
  cols = await availableColumns(c, source, ctx.locale.tr);
280
354
  } catch (e) {
281
- return html`<div class="alert alert-error" role="alert">${await publicError(ctx, e, `data reporter "${r.title ?? r.id}" source ${source.id}`)}</div>${await home(ctx, r, sources)}`;
355
+ return html`<div class="alert alert-error" role="alert">${await publicError(ctx, e, `data reporter "${r.title ?? r.id}" source ${source.id}`)}</div>${await home(ctx, r, sources, edit)}`;
282
356
  }
283
357
  const def = checkDef(edited ? defFromParams(ctx.params, prefix) : saved!.definition, source.id, cols);
284
- return reportView(ctx, r, source, cols, def, saved, edited);
358
+ return reportView(ctx, r, source, cols, def, saved, edited, edit);
285
359
  }
286
360
 
287
361
  /**
@@ -291,12 +365,12 @@ export async function renderDataReporter(ctx: PageContext, r: Region): Promise<R
291
365
  * keeps the compact form (a list of sources and a Start button); welcome_title
292
366
  * and welcome_text replace the greeting.
293
367
  */
294
- async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw> {
368
+ async function home(ctx: PageContext, r: Region, sources: Source[], edit = true): Promise<Raw> {
295
369
  const t = ctx.locale.t;
296
370
  const c = ctx.client!;
297
371
  const prefix = prefixOf(r);
298
372
  const q = (ctx.params.get(`${prefix}q`) ?? '').trim().slice(0, 100);
299
- const reports = (
373
+ const all = (
300
374
  await savepoint(c, () =>
301
375
  c.query<Saved & { updated_at: string }>(
302
376
  `select id, name, description, username, shared, own, definition, updated_at from meta.data_reports
@@ -304,6 +378,11 @@ async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw
304
378
  order by own desc, case when own then updated_at end desc, lower(name), id`, [r.id, q]),
305
379
  )
306
380
  ).rows;
381
+ // (sprint 110) users who may not edit see the shared (published) reports only
382
+ const reports = edit ? all : all.filter((x) => x.shared);
383
+ const pub = publishing(r);
384
+ const pins = pub ? await pinsOf(ctx, r) : [];
385
+ const sharedTag = pub ? t('reporter.published_tag') : t('reporter.shared_tag');
307
386
  const labelOf = (id: string) => {
308
387
  const s = sources.find((x) => x.id === id);
309
388
  return s ? ctx.locale.tr(s.label || heading(s.table)) : id;
@@ -311,51 +390,54 @@ async function home(ctx: PageContext, r: Region, sources: Source[]): Promise<Raw
311
390
  const list = (rows: (Saved & { updated_at: string })[]) =>
312
391
  html`<ul class="reporter-list">${rows.map((x) => html`<li>
313
392
  <a href="${url(ctx, r, new URLSearchParams([[`${prefix}open`, String(x.id)]]))}">${x.name}</a>
314
- <span class="muted">${labelOf(str(x.definition?.source))}${x.own ? html` · ${t('reporter.changed', { date: String(x.updated_at).slice(0, 10) })}` : html` · ${t('reporter.by', { user: x.username })}`}</span>${x.definition?.chart ? html` <span class="tag">${t('reporter.chart_tag')}</span>` : ''}${x.own && x.shared ? html` <span class="tag">${t('reporter.shared_tag')}</span>` : ''}
393
+ <span class="muted">${labelOf(str(x.definition?.source))}${x.own ? html` · ${t('reporter.changed', { date: String(x.updated_at).slice(0, 10) })}` : html` · ${t('reporter.by', { user: x.username })}`}</span>${x.definition?.chart ? html` <span class="tag">${t('reporter.chart_tag')}</span>` : ''}${x.own && x.shared ? html` <span class="tag">${sharedTag}</span>` : ''}
315
394
  ${x.description ? html`<p class="muted reporter-desc">${x.description}</p>` : ''}
316
395
  </li>`)}</ul>`;
317
- const mine = reports.filter((x) => x.own);
318
- const others = reports.filter((x) => !x.own);
396
+ const pinned = pins.map((id) => reports.find((x) => x.id === id)).filter((x): x is (typeof reports)[number] => !!x);
397
+ const mine = edit ? reports.filter((x) => x.own) : [];
398
+ const others = edit ? reports.filter((x) => !x.own) : reports;
319
399
  const keep = [...ctx.params.entries()].filter(([k]) => !k.startsWith(prefix) && !['clear', 'cs'].includes(k));
320
400
  const hidden = keep.map(([k, v]) => html`<input type="hidden" name="${k}" value="${v}">`);
321
- const lists = html`${ctx.user !== 'nobody' ? html`<h3 class="reporter-h">${t('reporter.my_reports')}</h3>${mine.length ? list(mine) : html`<p class="muted">${q ? t('reporter.no_match') : t('reporter.no_reports')}</p>`}` : ''}
322
- ${others.length ? html`<h3 class="reporter-h">${t('reporter.shared_reports')}</h3>${list(others)}` : ''}`;
401
+ const lists = html`${pinned.length ? html`<h3 class="reporter-h">${t('reporter.pinned')}</h3>${list(pinned)}` : ''}
402
+ ${edit && ctx.user !== 'nobody' ? html`<h3 class="reporter-h">${t('reporter.my_reports')}</h3>${mine.length ? list(mine) : html`<p class="muted">${q ? t('reporter.no_match') : t('reporter.no_reports')}</p>`}` : ''}
403
+ ${others.length ? html`<h3 class="reporter-h">${pub ? t('reporter.published_reports') : t('reporter.shared_reports')}</h3>${list(others)}`
404
+ : edit ? '' : html`<h3 class="reporter-h">${pub ? t('reporter.published_reports') : t('reporter.shared_reports')}</h3><p class="muted">${q ? t('reporter.no_match') : t('reporter.no_published')}</p>`}`;
323
405
 
324
406
  if (r.config.welcome === false) {
325
407
  const form = `drn${r.id}`;
326
408
  ctx.detached.push(html`<form id="${form}" method="get" action="${ctx.base}/${ctx.page.page_no}">${hidden}</form>`);
327
409
  return html`<div class="reporter">
328
- <div class="reporter-new">
410
+ ${edit ? html`<div class="reporter-new">
329
411
  <label class="label" for="${form}_src">${t('reporter.new')}</label>
330
412
  <div class="reporter-row">
331
413
  ${sel(`${prefix}src`, form, t('reporter.source'), sources.map((s) => [s.id, ctx.locale.tr(s.label || heading(s.table))]), sources[0].id, `${form}_src`)}
332
414
  <button class="btn btn-hot" form="${form}">${t('reporter.create')}</button>
333
415
  </div>
334
416
  ${sources.some((s) => s.description) ? html`<dl class="reporter-sources">${sources.filter((s) => s.description).map((s) => html`<dt>${ctx.locale.tr(s.label || heading(s.table))}</dt><dd>${ctx.locale.tr(s.description!)}</dd>`)}</dl>` : ''}
335
- </div>
417
+ </div>` : ''}
336
418
  ${lists}
337
419
  </div>`;
338
420
  }
339
421
 
340
422
  const title = typeof r.config.welcome_title === 'string' && r.config.welcome_title.trim() ? ctx.locale.tr(r.config.welcome_title) : t('reporter.welcome_title');
341
- const text = typeof r.config.welcome_text === 'string' && r.config.welcome_text.trim() ? ctx.locale.tr(r.config.welcome_text) : t('reporter.welcome_text');
423
+ const text = typeof r.config.welcome_text === 'string' && r.config.welcome_text.trim() ? ctx.locale.tr(r.config.welcome_text) : edit ? t('reporter.welcome_text') : t('reporter.welcome_viewer');
342
424
  // the search: a GET form of its own (detached: the region may sit inside the page's form)
343
425
  const search = `drq${r.id}`;
344
426
  const searchable = q !== '' || reports.length > 6;
345
427
  if (searchable) ctx.detached.push(html`<form id="${search}" method="get" action="${ctx.base}/${ctx.page.page_no}" role="search">${hidden}</form>`);
346
428
  return html`<div class="reporter reporter-welcome">
347
429
  <div class="reporter-hello"><h3 class="reporter-title">${title}</h3><p class="muted">${text}</p></div>
348
- <h3 class="reporter-h">${t('reporter.new')}</h3>
430
+ ${edit ? html`<h3 class="reporter-h">${t('reporter.new')}</h3>
349
431
  <ul class="reporter-cards">${sources.map((s) => html`<li><a class="reporter-card" href="${url(ctx, r, new URLSearchParams([[`${prefix}src`, s.id]]))}">
350
432
  <strong>${ctx.locale.tr(s.label || heading(s.table))}</strong>
351
433
  ${s.description ? html`<span class="muted">${ctx.locale.tr(s.description)}</span>` : ''}
352
- <span class="reporter-card-meta">${t('reporter.columns_count', { n: s.columns.length })}</span></a></li>`)}</ul>
434
+ <span class="reporter-card-meta">${t('reporter.columns_count', { n: s.columns.length })}</span></a></li>`)}</ul>` : ''}
353
435
  ${searchable ? html`<div class="reporter-row reporter-search"><input type="search" name="${prefix}q" value="${q}" form="${search}" aria-label="${t('reporter.search')}" placeholder="${t('reporter.search')}"><button class="btn" form="${search}">${t('reporter.search')}</button></div>` : ''}
354
436
  ${lists}
355
437
  </div>`;
356
438
  }
357
439
 
358
- async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map<string, Col>, def: Def, saved: Saved | undefined, edited: boolean): Promise<Raw> {
440
+ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map<string, Col>, def: Def, saved: Saved | undefined, edited: boolean, edit = true): Promise<Raw> {
359
441
  const t = ctx.locale.t;
360
442
  const prefix = prefixOf(r);
361
443
  const form = `dre${r.id}`;
@@ -374,7 +456,7 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
374
456
  const editor = html`<details class="reporter-editor"${!saved || edited ? raw(' open') : ''}>
375
457
  <summary>${t('reporter.edit')}</summary>
376
458
  <fieldset class="reporter-set"><legend>${t('reporter.columns')}</legend>
377
- <div class="reporter-checks">${[...cols.values()].map((x) => html`<label class="check"><input type="checkbox" name="${prefix}col" value="${x.name}" form="${form}"${shownCols.includes(x.name) ? raw(' checked') : ''}> ${x.label}</label>`)}</div>
459
+ <div class="reporter-checks">${[...cols.values()].map((x) => html`<label class="check"><input type="checkbox" name="${prefix}col" value="${x.name}" form="${form}"${shownCols.includes(x.name) ? raw(' checked') : ''}> ${x.label}${x.description ? html` <small class="muted reporter-coldesc">${x.description}</small>` : ''}</label>`)}</div>
378
460
  </fieldset>
379
461
  <fieldset class="reporter-set"><legend>${t('reporter.filters')}</legend>
380
462
  ${rows(def.filters, LIMITS.filters, { column: '', op: 'eq', value: '' }).map((f) => html`<div class="reporter-row">
@@ -412,7 +494,8 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
412
494
 
413
495
  // ---- saving (signed-in users)
414
496
  let save: Raw | '' = '';
415
- if (ctx.user !== 'nobody') {
497
+ const pub = publishing(r);
498
+ if (edit && ctx.user !== 'nobody') {
416
499
  const share = await mayShare(ctx, r);
417
500
  const own = saved?.own ? saved : undefined;
418
501
  const sform = `drs${r.id}`;
@@ -429,7 +512,7 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
429
512
  <label class="label" for="${fid('desc')}">${t('reporter.description')}</label>
430
513
  <input id="${fid('desc')}" name="description" form="${sform}" maxlength="500" value="${own?.description ?? ''}">
431
514
  </div>
432
- ${share ? html`<label class="check"><input type="checkbox" name="shared" value="true" form="${sform}"${own?.shared ? raw(' checked') : ''}> ${t('reporter.share')}</label>` : ''}
515
+ ${share ? html`<label class="check"><input type="checkbox" name="shared" value="true" form="${sform}"${own?.shared ? raw(' checked') : ''}> ${pub ? t('reporter.publish_on_save') : t('reporter.share')}</label>` : ''}
433
516
  <div class="buttons">
434
517
  <button class="btn btn-hot" form="${sform}" name="mode" value="save">${t('report.save')}</button>
435
518
  ${own ? html`<button class="btn" form="${sform}" name="mode" value="new">${t('reporter.save_as_new')}</button>
@@ -438,14 +521,40 @@ async function reportView(ctx: PageContext, r: Region, source: Source, cols: Map
438
521
  </fieldset>`;
439
522
  }
440
523
 
524
+ // (sprint 110) a saved report's actions: its stable URL, pin, publish / unpublish, an administrator's unpublish and delete
525
+ let actions: Raw | '' = '';
526
+ if (saved && ctx.user !== 'nobody') {
527
+ const base = `${ctx.base}/${ctx.page.page_no}/reporter/${r.id}`;
528
+ const csrf = html`<input type="hidden" name="__csrf" value="${ctx.session.csrf_token}">`;
529
+ const post = (action: string, mode: string, label: string, cls = 'btn', confirm = '') => {
530
+ const fid2 = `dra${r.id}_${action}_${mode}`;
531
+ ctx.detached.push(html`<form id="${fid2}" method="post" action="${base}/${action}">${csrf}<input type="hidden" name="rep" value="${saved.id}"><input type="hidden" name="mode" value="${mode}"></form>`);
532
+ return html`<button class="${cls}" form="${fid2}"${confirm ? html` data-confirm="${confirm}"` : ''}>${label}</button>`;
533
+ };
534
+ const pins = await pinsOf(ctx, r);
535
+ const moderate = !saved.own && (await mayModerate(ctx, r));
536
+ const buttons: Raw[] = [];
537
+ if (pub && saved.shared) buttons.push(post('pin', pins.includes(saved.id) ? 'unpin' : 'pin', pins.includes(saved.id) ? t('reporter.unpin') : t('reporter.pin')));
538
+ if (pub && saved.own && edit && (saved.shared || (await mayShare(ctx, r)))) buttons.push(post('publish', saved.shared ? 'unpublish' : 'publish', saved.shared ? t('reporter.unpublish') : t('reporter.publish'), saved.shared ? 'btn' : 'btn btn-hot'));
539
+ if (moderate && saved.shared) buttons.push(post('publish', 'unpublish', t('reporter.unpublish')));
540
+ if (moderate) buttons.push(post('delete', 'any', t('reporter.delete'), 'btn btn-danger', t('reporter.delete_confirm', { name: saved.name })));
541
+ const link = pub && saved.shared ? reportLink(ctx, saved.id) : '';
542
+ if (buttons.length || link)
543
+ actions = html`<div class="reporter-actions">
544
+ ${link ? html`<label class="label" for="${fid('link')}">${t('reporter.link')}</label><input id="${fid('link')}" class="reporter-link" readonly value="${link}"> <a href="${link}">${t('reporter.open_link')}</a>` : ''}
545
+ ${buttons.length ? html`<div class="buttons">${buttons}</div>` : ''}
546
+ </div>`;
547
+ }
548
+
441
549
  return html`<div class="reporter">
442
550
  <div class="reporter-head">
443
551
  <a href="${url(ctx, r, null)}">‹ ${t('reporter.back')}</a>
444
552
  <h3 class="reporter-title">${title}</h3>
445
- <span class="muted">${saved ? html`${ctx.locale.tr(source.label || heading(source.table))}${saved.own ? '' : html` · ${t('reporter.by', { user: saved.username })}`}${saved.shared ? html` <span class="tag">${t('reporter.shared_tag')}</span>` : ''}` : ''}</span>
553
+ <span class="muted">${saved ? html`${ctx.locale.tr(source.label || heading(source.table))}${saved.own ? '' : html` · ${t('reporter.by', { user: saved.username })}`}${saved.shared ? html` <span class="tag">${pub ? t('reporter.published_tag') : t('reporter.shared_tag')}</span>` : ''}` : ''}</span>
446
554
  ${saved?.description ? html`<p class="muted reporter-desc">${saved.description}</p>` : ''}
447
555
  </div>
448
- ${editor}
556
+ ${actions}
557
+ ${edit ? editor : ''}
449
558
  ${result}
450
559
  ${save}
451
560
  </div>`;
@@ -514,6 +623,12 @@ async function runReport(ctx: PageContext, r: Region, source: Source, cols: Map<
514
623
 
515
624
  // ---------------------------------------------------------------- saving and deleting
516
625
 
626
+ /** A report of the region the current user can see (own or shared), through meta.data_reports. */
627
+ async function reportOf(ctx: PageContext, r: Region, id: number) {
628
+ if (!id) return undefined;
629
+ return (await ctx.client!.query<{ id: number; name: string; own: boolean; shared: boolean }>('select id, name, own, shared from meta.data_reports where id = $1 and region_id = $2', [id, r.id])).rows[0];
630
+ }
631
+
517
632
  export async function dataReporterRoutes(app: FastifyInstance) {
518
633
  /** Shared checks: CSRF, a signed-in user, the region visible on the page; back to the page afterwards. */
519
634
  const action = async (req: Req, reply: FastifyReply, run: (ctx: PageContext, r: Region) => Promise<{ message: string; back: URLSearchParams }>) => {
@@ -558,7 +673,8 @@ export async function dataReporterRoutes(app: FastifyInstance) {
558
673
  const b = req.body ?? {};
559
674
  const prefix = prefixOf(r);
560
675
  const params = new URLSearchParams(typeof b.params === 'string' ? b.params.slice(0, 8000) : '');
561
- const source = sourcesOf(r).find((s) => s.id === params.get(`${prefix}src`));
676
+ if (!(await mayEdit(ctx, r))) throw new Forbidden('edit');
677
+ const source = (await resolveSources(r, ctx.app.id)).find((s) => s.id === params.get(`${prefix}src`));
562
678
  if (!source) throw new Forbidden('source');
563
679
  const cols = await availableColumns(ctx.client!, source);
564
680
  const def = checkDef(defFromParams(params, prefix), source.id, cols);
@@ -579,10 +695,61 @@ export async function dataReporterRoutes(app: FastifyInstance) {
579
695
  );
580
696
 
581
697
  app.post('/a/:alias/:page/reporter/:id/delete', async (req: Req, reply) =>
582
- action(req, reply, async (ctx) => {
698
+ action(req, reply, async (ctx, r) => {
583
699
  const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
584
- const done = (await ctx.client!.query<{ d: boolean }>('select meta.delete_data_report($1) as d', [id])).rows[0].d;
700
+ const rep = await reportOf(ctx, r, id);
701
+ let done = false;
702
+ if (rep?.own) {
703
+ // (sprint 110) users who may not edit don't delete either
704
+ if (!(await mayEdit(ctx, r))) throw new Forbidden('edit');
705
+ done = (await ctx.client!.query<{ d: boolean }>('select meta.delete_data_report($1) as d', [id])).rows[0].d;
706
+ } else if (rep) {
707
+ // (sprint 110) an administrator of the region deletes anyone's (shared) report
708
+ if (!(await mayModerate(ctx, r))) throw new Forbidden('moderate');
709
+ done = !!(await owner.query('delete from meta.data_report where id = $1 and app_id = $2 and region_id = $3', [id, ctx.app.id, r.id])).rowCount;
710
+ }
585
711
  return { message: ctx.locale.t(done ? 'reporter.deleted' : 'reporter.not_found'), back: new URLSearchParams() };
586
712
  }),
587
713
  );
714
+
715
+ // (sprint 110) publish or unpublish a saved report: the owner (who may edit and share), or an administrator of the region (unpublish)
716
+ app.post('/a/:alias/:page/reporter/:id/publish', async (req: Req, reply) =>
717
+ action(req, reply, async (ctx, r) => {
718
+ const t = ctx.locale.t;
719
+ const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
720
+ const on = req.body?.mode === 'publish';
721
+ const rep = await reportOf(ctx, r, id);
722
+ if (!rep) return { message: t('reporter.not_found'), back: new URLSearchParams() };
723
+ const allowed = rep.own ? (await mayEdit(ctx, r)) && (!on || (await mayShare(ctx, r))) : !on && (await mayModerate(ctx, r));
724
+ if (!allowed) throw new Forbidden('publish');
725
+ await owner.query('update meta.data_report set shared = $4, updated_at = now() where id = $1 and app_id = $2 and region_id = $3', [id, ctx.app.id, r.id, on]);
726
+ return { message: t(on ? 'reporter.published' : 'reporter.unpublished', { name: rep.name }), back: new URLSearchParams([[`${prefixOf(r)}open`, String(id)]]) };
727
+ }),
728
+ );
729
+
730
+ // (sprint 110) pin a shared report to the top of the region's list, or unpin it (a preference of the user)
731
+ app.post('/a/:alias/:page/reporter/:id/pin', async (req: Req, reply) =>
732
+ action(req, reply, async (ctx, r) => {
733
+ const id = /^\d{1,9}$/.test(String(req.body?.rep ?? '')) ? Number(req.body!.rep) : 0;
734
+ const rep = await reportOf(ctx, r, id);
735
+ if (!rep?.shared || !publishing(r)) return { message: ctx.locale.t('reporter.not_found'), back: new URLSearchParams() };
736
+ const pins = (await pinsOf(ctx, r)).filter((x) => x !== id);
737
+ if (req.body?.mode === 'pin') pins.unshift(id);
738
+ await ctx.client!.query('select meta.set_preference($1, $2)', [pinsName(r), pins.slice(0, MAX_PINS).join(',')]);
739
+ return { message: ctx.locale.t(req.body?.mode === 'pin' ? 'reporter.pinned_msg' : 'reporter.unpinned_msg', { name: rep.name }), back: new URLSearchParams([[`${prefixOf(r)}open`, String(id)]]) };
740
+ }),
741
+ );
742
+
743
+ // (sprint 110) a report's stable URL: its page with the report open (the page checks who may see it)
744
+ app.get('/a/:alias/reports/:id', async (req: Req, reply) => {
745
+ const id = /^\d{1,9}$/.test(String(req.params.id ?? '')) ? Number(req.params.id) : 0;
746
+ const hit = id
747
+ ? await owner.one<{ page_no: number; region_id: number; alias: string }>(
748
+ `select p.page_no, d.region_id, a.alias from meta.data_report d join meta.app a on a.id = d.app_id
749
+ join meta.region g on g.id = d.region_id join meta.page p on p.id = g.page_id
750
+ where d.id = $1 and a.alias = $2 and d.shared`, [id, req.params.alias])
751
+ : undefined;
752
+ if (!hit) return simplePage(reply, 404, english('error.not_found'), english('reporter.not_found'));
753
+ return reply.redirect(`/a/${encodeURIComponent(hit.alias)}/${hit.page_no}?dr${hit.region_id}_open=${id}`, 303);
754
+ });
588
755
  }
package/src/session.ts CHANGED
@@ -16,6 +16,8 @@ export interface BuilderWorkspace {
16
16
  /** (093) workspace preferences */
17
17
  sql_workshop?: boolean;
18
18
  rest_services?: boolean;
19
+ /** (119) what /builder opens: the App Builder (null, 'apps') or the Workspace home ('home') */
20
+ builder_home?: string | null;
19
21
  /** (093) extension links, in the builder's Extensions menu */
20
22
  links?: { label: string; url: string; description: string | null }[];
21
23
  }
@@ -91,8 +93,13 @@ export async function saveState(s: Session) {
91
93
  export async function destroySession(reply: FastifyReply, s: Session, path: string) {
92
94
  await runtime.query('delete from meta.session where id = $1', [s.id]);
93
95
  reply.clearCookie(cookieName(s.app_id), { path });
96
+ for (const fn of endHooks) await fn(s.id).catch(() => {});
94
97
  }
95
98
 
99
+ /** (sprint 107) Called when a session is destroyed (sign-out, a new sign-in): the worksheet's transactions end with it. */
100
+ const endHooks: ((sessionId: string) => Promise<unknown>)[] = [];
101
+ export const onSessionEnd = (fn: (sessionId: string) => Promise<unknown>) => endHooks.push(fn);
102
+
96
103
  /** Take (and clear) a one-shot flash message stored in session state. */
97
104
  export function takeFlash(s: Session, key = '__FLASH') {
98
105
  const msg = s.state[key] ?? null;
@@ -3,7 +3,6 @@ import { isNumeric } from './runtime/report.ts';
3
3
  import { parsePlan, planText, type Plan } from './sqlplan.ts';
4
4
  import { splitScript, type ScriptStatement } from './sqlscript.ts';
5
5
  import { skipQuoted } from './binds.ts';
6
- import type { Client } from './db.ts';
7
6
 
8
7
  // (sprint 91, Database Actions DA-1/DA-2) The SQL Workshop's worksheet: which statements a run takes
9
8
  // (the one at the cursor, a selection, or all of them), and running them on one connection with a log
@@ -144,9 +143,10 @@ export interface RunOptions {
144
143
 
145
144
  /**
146
145
  * Run statements on `c` (a connection of its own, destroyed by the caller: the SQL may change the role or
147
- * settings). Each statement autocommits, like Database Actions. Notices are collected per statement.
146
+ * settings). Each statement autocommits, like Database Actions, unless the caller opened a transaction on `c`
147
+ * (sprint 107, the worksheet's transaction mode). Notices are collected per statement.
148
148
  */
149
- export async function runStatements(c: Client, text: string, statements: ScriptStatement[], opts: RunOptions = {}): Promise<WorksheetRun> {
149
+ export async function runStatements(c: pg.ClientBase, text: string, statements: ScriptStatement[], opts: RunOptions = {}): Promise<WorksheetRun> {
150
150
  const started = performance.now();
151
151
  const notices: Notice[] = [];
152
152
  let current = 0;
@@ -206,7 +206,7 @@ export async function runStatements(c: Client, text: string, statements: ScriptS
206
206
  * EXPLAIN the statement (Explain Plan: nothing runs) or EXPLAIN ANALYZE it inside a transaction that is
207
207
  * rolled back (Autotrace: it runs, its changes don't stay).
208
208
  */
209
- export async function explainStatement(c: Client, s: ScriptStatement, analyze: boolean): Promise<WorksheetRun> {
209
+ export async function explainStatement(c: pg.ClientBase, s: ScriptStatement, analyze: boolean, inTransaction = false): Promise<WorksheetRun> {
210
210
  const started = performance.now();
211
211
  const r: WorksheetResult = { n: 1, line: s.line, sql: s.sql, start: s.start, end: s.end, status: 'not_run', ms: 0 };
212
212
  let plan: string | undefined;
@@ -216,7 +216,9 @@ export async function explainStatement(c: Client, s: ScriptStatement, analyze: b
216
216
  r.error = 'Only SQL statements have a plan.';
217
217
  } else {
218
218
  try {
219
- await c.query('begin');
219
+ // (sprint 107) in a worksheet's open transaction: a savepoint, so the plan sees the transaction's changes and
220
+ // Autotrace's own are undone while the transaction's stay (a failing EXPLAIN doesn't abort it either)
221
+ await c.query(inTransaction ? 'savepoint pgkiln_explain' : 'begin');
220
222
  const out = await c.query<{ 'QUERY PLAN': unknown }>(`explain (format json, ${analyze ? 'analyze, buffers, ' : ''}verbose, costs) ${s.sql}`);
221
223
  const json = out.rows[0]?.['QUERY PLAN'];
222
224
  tree = parsePlan(typeof json === 'string' ? JSON.parse(json) : json, analyze);
@@ -229,7 +231,8 @@ export async function explainStatement(c: Client, s: ScriptStatement, analyze: b
229
231
  r.error = `${err.message}${err.code ? ` (${err.code})` : ''}`;
230
232
  r.errorLine = s.line;
231
233
  } finally {
232
- await c.query('rollback').catch(() => {});
234
+ if (inTransaction) await c.query('rollback to savepoint pgkiln_explain').then(() => c.query('release savepoint pgkiln_explain')).catch(() => {});
235
+ else await c.query('rollback').catch(() => {});
233
236
  }
234
237
  }
235
238
  r.ms = Math.round(performance.now() - started);
@@ -11,7 +11,7 @@
11
11
  import pg from 'pg';
12
12
  import { owner } from './db.ts';
13
13
 
14
- export const APP_TYPES = ['standard', 'theme', 'library', 'boilerplate'] as const;
14
+ export const APP_TYPES = ['standard', 'theme', 'library', 'boilerplate', 'reporting'] as const;
15
15
  export type AppType = (typeof APP_TYPES)[number];
16
16
 
17
17
  export const APP_TYPE_LABELS: Record<AppType, string> = {
@@ -19,6 +19,7 @@ export const APP_TYPE_LABELS: Record<AppType, string> = {
19
19
  theme: 'Theme application',
20
20
  library: 'Library application',
21
21
  boilerplate: 'Boilerplate application',
22
+ reporting: 'Reporting application',
22
23
  };
23
24
 
24
25
  /** What can be subscribed to: table, key column, label; and the master types that offer it. */