@crombolo/data 0.75.0 → 0.76.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.75.0",
3
+ "version": "0.76.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/core": "0.75.0",
32
+ "@crombolo/core": "0.76.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
package/src/index.ts CHANGED
@@ -63,6 +63,7 @@ export * from './repositories/two-factor.ts'
63
63
  export * from './repositories/user-data.ts'
64
64
  export * from './repositories/users.ts'
65
65
  export * from './repositories/webhooks.ts'
66
+ export * from './repositories/workspace-errors.ts'
66
67
  export * from './repositories/workspace-events.ts'
67
68
  export * from './repositories/workspace-files.ts'
68
69
  export * from './repositories/workspace-links.ts'
@@ -157,6 +158,7 @@ export * from './schema/users-localisation.ts'
157
158
  export * from './schema/values.ts'
158
159
  export * from './schema/webhooks.ts'
159
160
  export * from './schema/workspace-conflicts.ts'
161
+ export * from './schema/workspace-errors.ts'
160
162
  export * from './schema/workspace-links.ts'
161
163
  export * from './schema/workspace-merge.ts'
162
164
  export * from './schema/workspace-privacy.ts'
@@ -453,6 +453,44 @@ export class ChangeRepository {
453
453
  return this.#view(rows[0], counts.items.get(id) ?? 0, counts.pages.get(id) ?? 0)
454
454
  }
455
455
 
456
+ /**
457
+ * The changes added to since this one was last added to, oldest first (story 406);
458
+ * undefined where the site holds no such change. One applied from another site is
459
+ * known by the key it travelled with too.
460
+ */
461
+ async addedToSince(id: string): Promise<Change[] | undefined> {
462
+ const wanted = normaliseUuid(id)
463
+ const applied = await this.#db.q
464
+ .selectFrom('applied_change')
465
+ .select('run_id')
466
+ .where('change_key', '=', wanted)
467
+ .execute()
468
+ const base = (
469
+ await this.#db.q
470
+ .selectFrom('content_transfer_run')
471
+ .select(['id', 'started_at', 'finished_at'])
472
+ .where('id', 'in', [wanted, ...applied.map((a) => String(a.run_id))])
473
+ .execute()
474
+ )[0]
475
+ if (!base) return undefined
476
+ const rows = await this.#db.q
477
+ .selectFrom('content_transfer_run')
478
+ .selectAll()
479
+ .where('id', '<>', String(base.id))
480
+ .where(
481
+ (eb) => eb.fn.coalesce('finished_at', 'started_at'),
482
+ '>=',
483
+ base.finished_at ?? base.started_at,
484
+ )
485
+ .orderBy((eb) => eb.fn.coalesce('finished_at', 'started_at'))
486
+ .orderBy('id')
487
+ .execute()
488
+ const counts = await this.#counts(rows.map((r) => String(r.id)))
489
+ return rows.map((r) =>
490
+ this.#view(r, counts.items.get(String(r.id)) ?? 0, counts.pages.get(String(r.id)) ?? 0),
491
+ )
492
+ }
493
+
456
494
  async #texts(hashes: readonly (string | null)[]): Promise<Map<string, string>> {
457
495
  const wanted = [...new Set(hashes.filter((h): h is string => Boolean(h)))]
458
496
  if (wanted.length === 0) return new Map()
@@ -0,0 +1,87 @@
1
+ /**
2
+ * What went wrong rendering a workspace's pages, as each node notes it (story 408):
3
+ * the newest kept per workspace, so any node answers for the whole site.
4
+ */
5
+ import { normaliseUuid } from '@crombolo/core'
6
+ import type { Db } from '../database.ts'
7
+ import { DbDate } from '../dialect.ts'
8
+
9
+ /** How many of a workspace's errors are kept, the newest. */
10
+ export const WORKSPACE_ERRORS_KEPT = 200
11
+
12
+ export interface WorkspaceError {
13
+ at: Date
14
+ node: string
15
+ page: string
16
+ says: string
17
+ }
18
+
19
+ export class WorkspaceErrorRepository {
20
+ #db: Db
21
+
22
+ constructor(db: Db) {
23
+ this.#db = db
24
+ }
25
+
26
+ /** Notes one, and drops what is past the newest kept. */
27
+ async note(
28
+ workspaceKey: string,
29
+ error: { node: string; page: string; says: string },
30
+ now = new Date(),
31
+ kept = WORKSPACE_ERRORS_KEPT,
32
+ ): Promise<void> {
33
+ const key = normaliseUuid(workspaceKey)
34
+ await this.#db.q
35
+ .insertInto('workspace_error')
36
+ .values({
37
+ workspace_key: key,
38
+ node_id: error.node.slice(0, 64),
39
+ page: error.page.slice(0, 1000),
40
+ says: error.says.slice(0, 4000),
41
+ noted_at: DbDate.toDb(now),
42
+ })
43
+ .execute()
44
+ const oldestKept = (
45
+ await this.#db.q
46
+ .selectFrom('workspace_error')
47
+ .select('id')
48
+ .where('workspace_key', '=', key)
49
+ .orderBy('id', 'desc')
50
+ .limit(1)
51
+ .offset(kept - 1)
52
+ .execute()
53
+ )[0]
54
+ if (oldestKept)
55
+ await this.#db.q
56
+ .deleteFrom('workspace_error')
57
+ .where('workspace_key', '=', key)
58
+ .where('id', '<', oldestKept.id)
59
+ .execute()
60
+ }
61
+
62
+ /** A workspace's errors from every node, oldest first, from `since` on. */
63
+ async of(workspaceKey: string, since?: Date): Promise<WorkspaceError[]> {
64
+ const rows = await this.#db.q
65
+ .selectFrom('workspace_error')
66
+ .select(['node_id', 'page', 'says', 'noted_at'])
67
+ .where('workspace_key', '=', normaliseUuid(workspaceKey))
68
+ .orderBy('id')
69
+ .execute()
70
+ return rows
71
+ .map((r) => ({
72
+ at: DbDate.fromDb(r.noted_at) ?? new Date(0),
73
+ node: String(r.node_id),
74
+ page: String(r.page),
75
+ says: String(r.says),
76
+ }))
77
+ .filter((e) => !since || e.at >= since)
78
+ }
79
+
80
+ /** A merged or discarded workspace keeps none. */
81
+ async clear(workspaceKey: string): Promise<void> {
82
+ await this.#db.q
83
+ .deleteFrom('workspace_error')
84
+ .where('workspace_key', '=', normaliseUuid(workspaceKey))
85
+ .execute()
86
+ }
87
+ }
@@ -534,6 +534,20 @@ export class WorkspaceRepository {
534
534
  })
535
535
  }
536
536
 
537
+ /** Story 407: how many items each workspace has in conflict, as last found; none where it has none. */
538
+ async conflictCounts(ids: readonly number[]): Promise<Map<number, number>> {
539
+ const out = new Map<number, number>()
540
+ if (ids.length === 0) return out
541
+ for (const row of await this.#db.q
542
+ .selectFrom('workspace_conflict_raised')
543
+ .select(['workspace_id', (eb) => eb.fn.countAll<number>().as('n')])
544
+ .where('workspace_id', 'in', [...ids])
545
+ .groupBy('workspace_id')
546
+ .execute())
547
+ out.set(Number(row.workspace_id), Number(row.n))
548
+ return out
549
+ }
550
+
537
551
  /**
538
552
  * Story 400: what a person settled in the workspace, a page by its key or a file by its
539
553
  * path, so a conflict there later is raised again. `keepFiles` are files still in conflict.
@@ -102,6 +102,7 @@ import { usersLocalisationMigration } from './users-localisation.ts'
102
102
  import { valuesMigration } from './values.ts'
103
103
  import { webhooksMigration } from './webhooks.ts'
104
104
  import { workspaceConflictsMigration } from './workspace-conflicts.ts'
105
+ import { workspaceErrorsMigration } from './workspace-errors.ts'
105
106
  import { workspaceLinksMigration } from './workspace-links.ts'
106
107
  import { workspaceMergeMigration } from './workspace-merge.ts'
107
108
  import { workspacePrivacyMigration } from './workspace-privacy.ts'
@@ -205,4 +206,5 @@ export const cromboloPlan = new MigrationPlan([
205
206
  workspaceConflictsMigration,
206
207
  changeSourcesMigration,
207
208
  groupStartKeysMigration,
209
+ workspaceErrorsMigration,
208
210
  ])
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Migration 096 — what went wrong rendering a workspace's pages, as each node notes it
3
+ * (story 408), so any node answers a workspace's errors for the whole site.
4
+ */
5
+ import type { Db } from '../database.ts'
6
+ import type { Migration } from '../migrations.ts'
7
+ import { sql } from '../query.ts'
8
+ import { STATE_GROUP_START_KEYS } from './group-start-keys.ts'
9
+
10
+ export const STATE_WORKSPACE_ERRORS = '{3b9f6d52-0096-4e1a-b7c4-2a8d5f1e0096}'
11
+
12
+ export const workspaceErrorsMigration: Migration = {
13
+ from: STATE_GROUP_START_KEYS,
14
+ to: STATE_WORKSPACE_ERRORS,
15
+ name: 'AddWorkspaceErrors',
16
+ kind: 'expand',
17
+ release: '0.76.0',
18
+ async up(db: Db) {
19
+ const t = db.dialect.types
20
+ await db.q.schema
21
+ .createTable('workspace_error')
22
+ .addColumn('id', sql.raw(t.identity))
23
+ .addColumn('workspace_key', sql.raw(t.varchar(64)), (c) => c.notNull())
24
+ .addColumn('node_id', sql.raw(t.varchar(64)), (c) => c.notNull())
25
+ .addColumn('page', sql.raw(t.varchar(1000)), (c) => c.notNull())
26
+ .addColumn('says', sql.raw(t.varchar(4000)), (c) => c.notNull())
27
+ .addColumn('noted_at', sql.raw(t.timestamp), (c) => c.notNull())
28
+ .execute()
29
+ await db.q.schema
30
+ .createIndex('ix_workspace_error_workspace')
31
+ .on('workspace_error')
32
+ .columns(['workspace_key', 'id'])
33
+ .execute()
34
+ },
35
+ }
package/src/tables.gen.ts CHANGED
@@ -1073,6 +1073,14 @@ export interface CoreTables {
1073
1073
  published_name: string | null
1074
1074
  workspace_id: Generated<number>
1075
1075
  }
1076
+ workspace_error: {
1077
+ id: Generated<number>
1078
+ node_id: string
1079
+ noted_at: Timestamp
1080
+ page: string
1081
+ says: string
1082
+ workspace_key: string
1083
+ }
1076
1084
  workspace_file: {
1077
1085
  base_hash: string | null
1078
1086
  class: string | null