@crombolo/data 0.74.1 → 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.74.1",
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.74.1",
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'
@@ -112,6 +113,7 @@ export * from './schema/form-library-types.ts'
112
113
  export * from './schema/form-permission-prefix.ts'
113
114
  export * from './schema/form-workflow-secrets.ts'
114
115
  export * from './schema/group-permissions.ts'
116
+ export * from './schema/group-start-keys.ts'
115
117
  export * from './schema/identity.ts'
116
118
  export * from './schema/image-alt-text.ts'
117
119
  export * from './schema/jobs.ts'
@@ -156,6 +158,7 @@ export * from './schema/users-localisation.ts'
156
158
  export * from './schema/values.ts'
157
159
  export * from './schema/webhooks.ts'
158
160
  export * from './schema/workspace-conflicts.ts'
161
+ export * from './schema/workspace-errors.ts'
159
162
  export * from './schema/workspace-links.ts'
160
163
  export * from './schema/workspace-merge.ts'
161
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()
@@ -24,6 +24,8 @@ export interface StartNodeSelection {
24
24
  export interface GroupStartNode {
25
25
  root: boolean
26
26
  key: string | null
27
+ /** Story 405: held by key, its item not on this site yet, so it gives no start here. */
28
+ waiting?: boolean
27
29
  }
28
30
 
29
31
  export interface UserRecord {
@@ -96,6 +98,8 @@ export interface UserGroupInput {
96
98
  startNodes: Record<StartNodeTree, GroupStartNode>
97
99
  permissions: string[]
98
100
  granular: GranularPermission[]
101
+ /** Story 405: a start node whose item is not here is kept by key, to apply when it arrives. */
102
+ holdStartNodes?: boolean
99
103
  }
100
104
 
101
105
  /** A group as a site's files declare it (story 228). */
@@ -710,6 +714,9 @@ const GROUP_COLUMNS = [
710
714
  'start_content_id',
711
715
  'start_media_id',
712
716
  'start_element_id',
717
+ 'start_content_key',
718
+ 'start_media_key',
719
+ 'start_element_key',
713
720
  ] as const
714
721
 
715
722
  const GROUP_START_COLUMN: Record<StartNodeTree, string> = {
@@ -718,6 +725,13 @@ const GROUP_START_COLUMN: Record<StartNodeTree, string> = {
718
725
  element: 'start_element_id',
719
726
  }
720
727
 
728
+ /** Story 405: where a start node is held by key while its item is not here. */
729
+ const GROUP_START_KEY_COLUMN: Record<StartNodeTree, string> = {
730
+ document: 'start_content_key',
731
+ media: 'start_media_key',
732
+ element: 'start_element_key',
733
+ }
734
+
721
735
  const groups = (db: Db) => db.q.selectFrom('user_group').select(GROUP_COLUMNS)
722
736
 
723
737
  export class UserGroupRepository {
@@ -772,12 +786,37 @@ export class UserGroupRepository {
772
786
  .where('id', 'in', positive)
773
787
  .execute())
774
788
  nodes.set(Number(row.id), normaliseUuid(String(row.unique_id)))
789
+ // Story 405: held start nodes, and which of them have arrived since.
790
+ const heldKeys = [
791
+ ...new Set(
792
+ rows.flatMap((r) =>
793
+ TREES.flatMap((tree) => {
794
+ const held = r[GROUP_START_KEY_COLUMN[tree] as keyof typeof r]
795
+ return typeof held === 'string' && held ? [normaliseUuid(held)] : []
796
+ }),
797
+ ),
798
+ ),
799
+ ]
800
+ const arrived = new Set<string>()
801
+ if (heldKeys.length > 0)
802
+ for (const row of await this.#db.q
803
+ .selectFrom('node')
804
+ .select('unique_id')
805
+ .where('unique_id', 'in', heldKeys)
806
+ .where('workspace_id', 'is', null)
807
+ .execute())
808
+ arrived.add(normaliseUuid(String(row.unique_id)))
775
809
 
776
810
  return rows.map((row) => {
777
811
  const id = Number(row.id)
778
812
  const key = normaliseUuid(String(row.key))
779
813
  const start = (tree: StartNodeTree): GroupStartNode => {
780
814
  const value = row[GROUP_START_COLUMN[tree]]
815
+ const held = row[GROUP_START_KEY_COLUMN[tree] as keyof typeof row]
816
+ if ((value === null || value === undefined) && typeof held === 'string' && held) {
817
+ const key = normaliseUuid(held)
818
+ return { root: false, key, ...(arrived.has(key) ? {} : { waiting: true }) }
819
+ }
781
820
  if (value === null || value === undefined) return { root: false, key: null }
782
821
  const nodeId = Number(value)
783
822
  if (nodeId === ROOT_ID) return { root: true, key: null }
@@ -893,13 +932,20 @@ export class UserGroupRepository {
893
932
  media: null,
894
933
  element: null,
895
934
  }
935
+ const held: Record<StartNodeTree, string | null> = {
936
+ document: null,
937
+ media: null,
938
+ element: null,
939
+ }
896
940
  for (const tree of TREES) {
897
941
  const start = input.startNodes[tree]
898
942
  if (start.root) starts[tree] = ROOT_ID
899
943
  else if (start.key) {
900
944
  const ids = await users.nodeIds([start.key])
901
- if (!ids) return { missingNode: tree }
902
- starts[tree] = ids.get(normaliseUuid(start.key)) ?? null
945
+ // Story 405: one not here yet is held by key, giving no start until it arrives.
946
+ if (!ids && input.holdStartNodes) held[tree] = normaliseUuid(start.key)
947
+ else if (!ids) return { missingNode: tree }
948
+ else starts[tree] = ids.get(normaliseUuid(start.key)) ?? null
903
949
  }
904
950
  }
905
951
  const languageIds: number[] = []
@@ -925,6 +971,9 @@ export class UserGroupRepository {
925
971
  start_content_id: starts.document,
926
972
  start_media_id: starts.media,
927
973
  start_element_id: starts.element,
974
+ start_content_key: held.document,
975
+ start_media_key: held.media,
976
+ start_element_key: held.element,
928
977
  }
929
978
  const existing = await new UserGroupRepository(tx).byKey(key)
930
979
  if (existing)
@@ -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.
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Migration 095 — a group's start node kept by key while its item is not on this site
3
+ * (story 405): a group defined outside the site names its start nodes by key, and one
4
+ * whose item has not arrived yet waits here, giving no start in that tree until it does.
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import type { Migration } from '../migrations.ts'
8
+ import { schemaOps } from '../schema-ops.ts'
9
+ import { STATE_CHANGE_SOURCES } from './change-sources.ts'
10
+
11
+ export const STATE_GROUP_START_KEYS = '{7e4c1a83-0095-4d9f-a031-5c8f1e6d0095}'
12
+
13
+ export const groupStartKeysMigration: Migration = {
14
+ from: STATE_CHANGE_SOURCES,
15
+ to: STATE_GROUP_START_KEYS,
16
+ name: 'AddGroupStartKeys',
17
+ kind: 'expand',
18
+ release: '0.75.0',
19
+ async up(db: Db) {
20
+ const ops = schemaOps(db)
21
+ const text = db.dialect.types.varchar(36)
22
+ for (const column of ['start_content_key', 'start_media_key', 'start_element_key'])
23
+ await ops.addColumn('user_group', column, text)
24
+ },
25
+ }
@@ -57,6 +57,7 @@ import { formWorkflowSecretsMigration } from './form-workflow-secrets.ts'
57
57
  import { formWorkflowsMigration } from './form-workflows.ts'
58
58
  import { formsMigration } from './forms.ts'
59
59
  import { groupPermissionsMigration } from './group-permissions.ts'
60
+ import { groupStartKeysMigration } from './group-start-keys.ts'
60
61
  import { identityMigration } from './identity.ts'
61
62
  import { imageAltTextMigration } from './image-alt-text.ts'
62
63
  import { jobsMigration } from './jobs.ts'
@@ -101,6 +102,7 @@ import { usersLocalisationMigration } from './users-localisation.ts'
101
102
  import { valuesMigration } from './values.ts'
102
103
  import { webhooksMigration } from './webhooks.ts'
103
104
  import { workspaceConflictsMigration } from './workspace-conflicts.ts'
105
+ import { workspaceErrorsMigration } from './workspace-errors.ts'
104
106
  import { workspaceLinksMigration } from './workspace-links.ts'
105
107
  import { workspaceMergeMigration } from './workspace-merge.ts'
106
108
  import { workspacePrivacyMigration } from './workspace-privacy.ts'
@@ -203,4 +205,6 @@ export const cromboloPlan = new MigrationPlan([
203
205
  workspaceProcessesMigration,
204
206
  workspaceConflictsMigration,
205
207
  changeSourcesMigration,
208
+ groupStartKeysMigration,
209
+ workspaceErrorsMigration,
206
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
@@ -932,8 +932,11 @@ export interface CoreTables {
932
932
  key: string
933
933
  name: string
934
934
  start_content_id: number | null
935
+ start_content_key: string | null
935
936
  start_element_id: number | null
937
+ start_element_key: string | null
936
938
  start_media_id: number | null
939
+ start_media_key: string | null
937
940
  update_date: Timestamp
938
941
  }
939
942
  user_group_granular_permission: {
@@ -1070,6 +1073,14 @@ export interface CoreTables {
1070
1073
  published_name: string | null
1071
1074
  workspace_id: Generated<number>
1072
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
+ }
1073
1084
  workspace_file: {
1074
1085
  base_hash: string | null
1075
1086
  class: string | null