@crombolo/data 0.79.0 → 0.80.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.79.0",
3
+ "version": "0.80.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.79.0",
32
+ "@crombolo/core": "0.80.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
package/src/database.ts CHANGED
@@ -14,6 +14,8 @@ export interface Db {
14
14
  readonly q: Query
15
15
  query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>
16
16
  exec(sql: string, params?: unknown[]): Promise<void>
17
+ /** A statement that changes rows, answering how many it changed (story 426). */
18
+ run(sql: string, params?: unknown[]): Promise<number>
17
19
  transaction<T>(fn: (tx: Db) => Promise<T>): Promise<T>
18
20
  /**
19
21
  * Runs `fn` against one consistent, read-only view of committed data (story
@@ -98,6 +100,10 @@ class SqliteDb implements Db {
98
100
  this.#db.query(this.dialect.render(sql)).run(...(params as never[]))
99
101
  }
100
102
 
103
+ async run(sql: string, params: unknown[] = []): Promise<number> {
104
+ return this.#db.query(this.dialect.render(sql)).run(...(params as never[])).changes
105
+ }
106
+
101
107
  async transaction<T>(fn: (tx: Db) => Promise<T>): Promise<T> {
102
108
  if (this.#inTransaction) return fn(this)
103
109
  const tx = new SqliteDb(this.#db, this.locks, true, this.#gate)
@@ -220,6 +226,12 @@ class PostgresDb implements Db {
220
226
  await this.#sql.unsafe(this.dialect.render(sql), params)
221
227
  }
222
228
 
229
+ async run(sql: string, params: unknown[] = []): Promise<number> {
230
+ if (this.#wraps) return this.transaction((tx) => tx.run(sql, params))
231
+ const result = (await this.#sql.unsafe(this.dialect.render(sql), params)) as { count?: number }
232
+ return Number(result.count ?? 0)
233
+ }
234
+
223
235
  confine(role: string, schema: string): Db {
224
236
  const q = (id: string) => this.dialect.quote(id)
225
237
  return new PostgresDb(this.#sql, this.locks, false, undefined, this.#readOnly, [
package/src/index.ts CHANGED
@@ -154,10 +154,12 @@ export * from './schema/two-factor.ts'
154
154
  export * from './schema/upgrade-report.ts'
155
155
  export * from './schema/user-group-owners.ts'
156
156
  export * from './schema/user-start-nodes-dropped.ts'
157
+ export * from './schema/users-in-settings.ts'
157
158
  export * from './schema/users-localisation.ts'
158
159
  export * from './schema/values.ts'
159
160
  export * from './schema/webhooks.ts'
160
161
  export * from './schema/workspace-conflicts.ts'
162
+ export * from './schema/workspace-description.ts'
161
163
  export * from './schema/workspace-errors.ts'
162
164
  export * from './schema/workspace-links.ts'
163
165
  export * from './schema/workspace-merge.ts'
package/src/migrations.ts CHANGED
@@ -579,6 +579,11 @@ class RecordingDb implements Db {
579
579
  )
580
580
  }
581
581
 
582
+ async run(sql: string, params: unknown[] = []): Promise<number> {
583
+ await this.exec(sql, params)
584
+ return 0
585
+ }
586
+
582
587
  async transaction<T>(fn: (tx: Db) => Promise<T>): Promise<T> {
583
588
  return fn(this)
584
589
  }
package/src/query.ts CHANGED
@@ -59,8 +59,7 @@ class DbConnection implements DatabaseConnection {
59
59
  async executeQuery<R>(compiled: CompiledQuery): Promise<QueryResult<R>> {
60
60
  const params = compiled.parameters as unknown[]
61
61
  if (readsRows(compiled)) return { rows: await this.db.query<R>(compiled.sql, params) }
62
- await this.db.exec(compiled.sql, params)
63
- return { rows: [] }
62
+ return { rows: [], numAffectedRows: BigInt(await this.db.run(compiled.sql, params)) }
64
63
  }
65
64
 
66
65
  // biome-ignore lint/correctness/useYield: refuses before streaming anything
@@ -150,7 +149,7 @@ export function compilerFor<Tables = CoreTables>(dialect: DbDialect): Kysely<Tab
150
149
  const refuse = async (): Promise<never> => {
151
150
  throw new Error('This builder only compiles; run what it compiles on a Db.')
152
151
  }
153
- const stub = { dialect, query: refuse, exec: refuse } as unknown as Db
152
+ const stub = { dialect, query: refuse, exec: refuse, run: refuse } as unknown as Db
154
153
  k = new Kysely<unknown>({ dialect: kyselyDialect(stub) })
155
154
  compilers.set(dialect.name, k)
156
155
  }
@@ -35,6 +35,13 @@ export interface PagePlace {
35
35
  trashed?: boolean
36
36
  }
37
37
 
38
+ /** Story 425: where a change keeps the order a page it made or moved stood in: `siblings/<parent>` or `siblings/root`. */
39
+ export const SIBLINGS_PREFIX = 'siblings/'
40
+
41
+ /** The item path for the order a parent's children stood in when a change put a page among them. */
42
+ export const siblingsPath = (parentKey: string | null) =>
43
+ `${SIBLINGS_PREFIX}${parentKey ? normaliseUuid(parentKey) : 'root'}`
44
+
38
45
  /** The item path for a parent's children's order. */
39
46
  export const orderPath = (parentKey: string | null) =>
40
47
  `${ORDER_PREFIX}${parentKey ? normaliseUuid(parentKey) : 'root'}`
@@ -288,6 +295,21 @@ export class ChangeRepository {
288
295
  return true
289
296
  }
290
297
 
298
+ /**
299
+ * Story 425: a page a person made or moved straight in Shared, with its parent's children
300
+ * in order then, so it takes the same place where the change is taken.
301
+ */
302
+ async amongSiblings(
303
+ madeBy: string,
304
+ parentKey: string | null,
305
+ keys: readonly string[],
306
+ now = new Date(),
307
+ ): Promise<void> {
308
+ const id = await this.#openEdit(madeBy, now)
309
+ await this.#keepItem(id, siblingsPath(parentKey), null, keys)
310
+ await this.#touch(id, now)
311
+ }
312
+
291
313
  /** A parent's children a person reordered straight in Shared (story 411), before and now. */
292
314
  async ordered(
293
315
  madeBy: string,
@@ -585,6 +607,8 @@ export class ChangeRepository {
585
607
  .selectFrom('change_item')
586
608
  .select(['run_id', (eb) => eb.fn.countAll<number>().as('n')])
587
609
  .where('run_id', 'in', ids)
610
+ // Story 425: where a page stood among its siblings is not something a change lists.
611
+ .where('path', 'not like', `${SIBLINGS_PREFIX}%`)
588
612
  .groupBy('run_id')
589
613
  .execute())
590
614
  items.set(String(row.run_id), Number(row.n))
@@ -899,14 +899,22 @@ export class DocumentRepository {
899
899
  date: DbDate.toDb(new Date()),
900
900
  })
901
901
  .execute()
902
- const changed = await this.#db.q
903
- .updateTable('workspace_document_culture')
904
- .set({ name: variant.name, available: t, edited: t })
902
+ const held = await this.#db.q
903
+ .selectFrom('workspace_document_culture')
904
+ .select('language_id')
905
905
  .where('workspace_id', '=', workspace)
906
906
  .where('node_id', '=', nodeId)
907
907
  .where('language_id', '=', languageId)
908
- .executeTakeFirst()
909
- if (Number(changed.numUpdatedRows ?? 0) === 0)
908
+ .execute()
909
+ if (held.length > 0)
910
+ await this.#db.q
911
+ .updateTable('workspace_document_culture')
912
+ .set({ name: variant.name, available: t, edited: t })
913
+ .where('workspace_id', '=', workspace)
914
+ .where('node_id', '=', nodeId)
915
+ .where('language_id', '=', languageId)
916
+ .execute()
917
+ else
910
918
  await this.#db.q
911
919
  .insertInto('workspace_document_culture')
912
920
  .values({
@@ -21,6 +21,9 @@ import {
21
21
  import { WorkspaceFileRepository } from './workspace-files.ts'
22
22
 
23
23
  /** A change staged in a workspace (story 383), with the schedules its merge sets (story 395). */
24
+ /** How lately a workspace changed a page for someone else opening it to be told. */
25
+ export const TOUCHED_WITHIN_MS = 7 * 24 * 60 * 60 * 1000
26
+
24
27
  export interface StagedChange {
25
28
  key: string
26
29
  name: string | null
@@ -55,8 +58,10 @@ export interface Workspace {
55
58
  stagedReadOnly: boolean
56
59
  /** Who can see it (story 384): its members, or everyone who may edit, reading only. */
57
60
  visibility: 'members' | 'editors'
58
- /** Whether someone opening an item it changed is told so, and by whose workspace. */
61
+ /** No longer chosen: anyone opening an item it changed lately is told so. Kept as stored. */
59
62
  tellOthers: boolean
63
+ /** What it is for, in its owner's words. */
64
+ description: string | null
60
65
  }
61
66
 
62
67
  /** A move, reorder or delete waiting for the merge, with where its page stood (story 380). */
@@ -109,6 +114,7 @@ export class WorkspaceRepository {
109
114
  personal?: boolean
110
115
  visibility?: 'members' | 'editors'
111
116
  tellOthers?: boolean
117
+ description?: string | null
112
118
  /** Opened only to see what something would do, then discarded: raises nothing. */
113
119
  quiet?: boolean
114
120
  }): Promise<Workspace> {
@@ -123,6 +129,7 @@ export class WorkspaceRepository {
123
129
  personal: this.#db.dialect.boolValue(input.personal === true) as boolean,
124
130
  visibility: input.visibility ?? 'members',
125
131
  tell_others: this.#db.dialect.boolValue(input.tellOthers !== false) as boolean,
132
+ description: input.description?.trim() || null,
126
133
  created_at: now,
127
134
  updated_at: now,
128
135
  })
@@ -327,17 +334,37 @@ export class WorkspaceRepository {
327
334
  await this.#db.q.updateTable('workspace').set(set).where('id', '=', id).execute()
328
335
  }
329
336
 
330
- /** Open workspaces, other than `except`'s own, that changed a page and say so (story 384). */
331
- async touching(nodeId: number, exceptUser: string): Promise<Workspace[]> {
337
+ /** What it is for, changed by its owner; empty clears it. */
338
+ async setDescription(id: number, description: string | null): Promise<void> {
339
+ await this.#db.q
340
+ .updateTable('workspace')
341
+ .set({ description: description?.trim() || null })
342
+ .where('id', '=', id)
343
+ .execute()
344
+ }
345
+
346
+ /**
347
+ * Open workspaces, other than `except`'s own, that changed a page lately (story 384):
348
+ * since `since`, seven days ago unless said. An older change is not worth a warning.
349
+ */
350
+ async touching(
351
+ nodeId: number,
352
+ exceptUser: string,
353
+ since = new Date(Date.now() - TOUCHED_WITHIN_MS),
354
+ ): Promise<Workspace[]> {
355
+ const from = DbDate.toDb(since)
332
356
  const documents = await this.#db.q
333
- .selectFrom('workspace_document')
357
+ .selectFrom('content_version')
334
358
  .select('workspace_id')
335
359
  .where('node_id', '=', nodeId)
360
+ .where('workspace_id', 'is not', null)
361
+ .where('version_date', '>=', from)
336
362
  .execute()
337
363
  const pending = await this.#db.q
338
364
  .selectFrom('workspace_pending')
339
365
  .select('workspace_id')
340
366
  .where('node_id', '=', nodeId)
367
+ .where('recorded_at', '>=', from)
341
368
  .execute()
342
369
  const ids = [...new Set([...documents, ...pending].map((r) => Number(r.workspace_id)))]
343
370
  if (ids.length === 0) return []
@@ -349,7 +376,6 @@ export class WorkspaceRepository {
349
376
  .execute()
350
377
  return (await this.#withMembers(rows)).filter(
351
378
  (w) =>
352
- w.tellOthers &&
353
379
  w.ownerKey !== normaliseUuid(exceptUser) &&
354
380
  !w.memberKeys.includes(normaliseUuid(exceptUser)),
355
381
  )
@@ -638,6 +664,7 @@ export class WorkspaceRepository {
638
664
  staged_changes?: string | null
639
665
  visibility?: string | null
640
666
  tell_others?: unknown
667
+ description?: string | null
641
668
  }>,
642
669
  ): Promise<Workspace[]> {
643
670
  if (rows.length === 0) return []
@@ -668,6 +695,7 @@ export class WorkspaceRepository {
668
695
  ...stagedOf(row.staged_changes),
669
696
  visibility: row.visibility === 'editors' ? 'editors' : 'members',
670
697
  tellOthers: row.tell_others === undefined ? true : fromDbBool(row.tell_others),
698
+ description: row.description ?? null,
671
699
  }))
672
700
  }
673
701
  }
@@ -98,10 +98,12 @@ import { twoFactorMigration } from './two-factor.ts'
98
98
  import { upgradeReportMigration } from './upgrade-report.ts'
99
99
  import { userGroupOwnersMigration } from './user-group-owners.ts'
100
100
  import { userStartNodesDroppedMigration } from './user-start-nodes-dropped.ts'
101
+ import { usersInSettingsMigration } from './users-in-settings.ts'
101
102
  import { usersLocalisationMigration } from './users-localisation.ts'
102
103
  import { valuesMigration } from './values.ts'
103
104
  import { webhooksMigration } from './webhooks.ts'
104
105
  import { workspaceConflictsMigration } from './workspace-conflicts.ts'
106
+ import { workspaceDescriptionMigration } from './workspace-description.ts'
105
107
  import { workspaceErrorsMigration } from './workspace-errors.ts'
106
108
  import { workspaceLinksMigration } from './workspace-links.ts'
107
109
  import { workspaceMergeMigration } from './workspace-merge.ts'
@@ -211,4 +213,6 @@ export const cromboloPlan = new MigrationPlan([
211
213
  workspaceErrorsMigration,
212
214
  workspaceSchedulesMigration,
213
215
  workspacePlacesMigration,
216
+ workspaceDescriptionMigration,
217
+ usersInSettingsMigration,
214
218
  ])
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Migration 100 — Users are in Settings (story 429), and need it: each group holding Users
3
+ * without Settings gains Settings, so nobody loses the users they could open.
4
+ */
5
+ import type { Db } from '../database.ts'
6
+ import type { Migration } from '../migrations.ts'
7
+ import { STATE_WORKSPACE_DESCRIPTION } from './workspace-description.ts'
8
+
9
+ export const STATE_USERS_IN_SETTINGS = '{2f6b9d40-0100-4c8e-b173-5e0d8a6c0100}'
10
+
11
+ export const usersInSettingsMigration: Migration = {
12
+ from: STATE_WORKSPACE_DESCRIPTION,
13
+ to: STATE_USERS_IN_SETTINGS,
14
+ name: 'PutUsersInSettings',
15
+ kind: 'expand',
16
+ release: '0.80.0',
17
+ async up(db: Db) {
18
+ await db.q
19
+ .insertInto('user_group_section')
20
+ .columns(['user_group_id', 'app_alias'])
21
+ .expression((eb) =>
22
+ eb
23
+ .selectFrom('user_group_section as held')
24
+ .select(['held.user_group_id', eb.val('settings').as('app_alias')])
25
+ .where('held.app_alias', '=', 'users')
26
+ .where((w) =>
27
+ w.not(
28
+ w.exists(
29
+ w
30
+ .selectFrom('user_group_section as has')
31
+ .select('has.app_alias')
32
+ .whereRef('has.user_group_id', '=', 'held.user_group_id')
33
+ .where('has.app_alias', '=', 'settings'),
34
+ ),
35
+ ),
36
+ )
37
+ .distinct(),
38
+ )
39
+ .execute()
40
+ },
41
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Migration 099 — what a workspace is for: a description, said when it is
3
+ * opened and changed by its owner, beside its name.
4
+ */
5
+ import type { Db } from '../database.ts'
6
+ import type { Migration } from '../migrations.ts'
7
+ import { sql } from '../query.ts'
8
+ import { STATE_WORKSPACE_PLACES } from './workspace-places.ts'
9
+
10
+ export const STATE_WORKSPACE_DESCRIPTION = '{7d2c5a18-0099-4f3b-8e61-9a4b0c7d0099}'
11
+
12
+ export const workspaceDescriptionMigration: Migration = {
13
+ from: STATE_WORKSPACE_PLACES,
14
+ to: STATE_WORKSPACE_DESCRIPTION,
15
+ name: 'AddWorkspaceDescription',
16
+ kind: 'expand',
17
+ release: '0.80.0',
18
+ async up(db: Db) {
19
+ await db.q.schema
20
+ .alterTable('workspace')
21
+ .addColumn('description', sql.raw(db.dialect.types.varchar(1000)))
22
+ .execute()
23
+ },
24
+ }
package/src/tables.gen.ts CHANGED
@@ -1032,6 +1032,7 @@ export interface CoreTables {
1032
1032
  }
1033
1033
  workspace: {
1034
1034
  created_at: Timestamp
1035
+ description: string | null
1035
1036
  id: Generated<number>
1036
1037
  key: string
1037
1038
  merged_at: Timestamp | null