@crombolo/data 0.78.0 → 0.79.1

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.78.0",
3
+ "version": "0.79.1",
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.78.0",
32
+ "@crombolo/core": "0.79.1",
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
@@ -161,6 +161,7 @@ export * from './schema/workspace-conflicts.ts'
161
161
  export * from './schema/workspace-errors.ts'
162
162
  export * from './schema/workspace-links.ts'
163
163
  export * from './schema/workspace-merge.ts'
164
+ export * from './schema/workspace-places.ts'
164
165
  export * from './schema/workspace-privacy.ts'
165
166
  export * from './schema/workspace-processes.ts'
166
167
  export * from './schema/workspace-schedules.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({
@@ -105,6 +105,7 @@ import { workspaceConflictsMigration } from './workspace-conflicts.ts'
105
105
  import { workspaceErrorsMigration } from './workspace-errors.ts'
106
106
  import { workspaceLinksMigration } from './workspace-links.ts'
107
107
  import { workspaceMergeMigration } from './workspace-merge.ts'
108
+ import { workspacePlacesMigration } from './workspace-places.ts'
108
109
  import { workspacePrivacyMigration } from './workspace-privacy.ts'
109
110
  import { workspaceProcessesMigration } from './workspace-processes.ts'
110
111
  import { workspaceSchedulesMigration } from './workspace-schedules.ts'
@@ -209,4 +210,5 @@ export const cromboloPlan = new MigrationPlan([
209
210
  groupStartKeysMigration,
210
211
  workspaceErrorsMigration,
211
212
  workspaceSchedulesMigration,
213
+ workspacePlacesMigration,
212
214
  ])
@@ -186,7 +186,8 @@ export const BUILT_IN_GROUPS = [
186
186
  name: 'Translators',
187
187
  icon: 'icon-globe',
188
188
  hasAccessToAllLanguages: true,
189
- sections: ['translation'],
189
+ // Story 419: Translation is a place under Content, which it needs too.
190
+ sections: ['content', 'translation'],
190
191
  },
191
192
  {
192
193
  id: 5,
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Migration 098 — Content is what a workspace holds (story 419). Library and
3
+ * Translation are places under Content, so a group holding either without Content
4
+ * gains Content, which opens nothing by itself; and languages are Translation's, so a
5
+ * group holding Structure without Translation gains Translation, keeping the
6
+ * languages it could change. Nobody loses anything they could open.
7
+ */
8
+ import type { Db } from '../database.ts'
9
+ import type { Migration } from '../migrations.ts'
10
+ import { STATE_WORKSPACE_SCHEDULES } from './workspace-schedules.ts'
11
+
12
+ export const STATE_WORKSPACE_PLACES = '{4e7a1c93-0098-4b6d-a2f5-7c1e9d3b0098}'
13
+
14
+ /** Each grant a group gains (`alias`) from one it holds (`from`). */
15
+ const GAINS: ReadonlyArray<{ alias: string; from: readonly string[] }> = [
16
+ { alias: 'content', from: ['library', 'translation'] },
17
+ { alias: 'translation', from: ['structure'] },
18
+ ]
19
+
20
+ export const workspacePlacesMigration: Migration = {
21
+ from: STATE_WORKSPACE_SCHEDULES,
22
+ to: STATE_WORKSPACE_PLACES,
23
+ name: 'AddWorkspacePlaces',
24
+ kind: 'expand',
25
+ release: '0.79.0',
26
+ async up(db: Db) {
27
+ for (const { alias, from } of GAINS)
28
+ await db.q
29
+ .insertInto('user_group_section')
30
+ .columns(['user_group_id', 'app_alias'])
31
+ .expression((eb) =>
32
+ eb
33
+ .selectFrom('user_group_section as held')
34
+ .select(['held.user_group_id', eb.val(alias).as('app_alias')])
35
+ .where('held.app_alias', 'in', [...from])
36
+ .where((w) =>
37
+ w.not(
38
+ w.exists(
39
+ w
40
+ .selectFrom('user_group_section as has')
41
+ .select('has.app_alias')
42
+ .whereRef('has.user_group_id', '=', 'held.user_group_id')
43
+ .where('has.app_alias', '=', alias),
44
+ ),
45
+ ),
46
+ )
47
+ .distinct(),
48
+ )
49
+ .execute()
50
+ },
51
+ }