@crombolo/data 0.79.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.79.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.79.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/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({