@crombolo/data 0.69.1 → 0.71.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.
@@ -7,6 +7,8 @@ import {
7
7
  SystemNodes,
8
8
  } from '@crombolo/core'
9
9
  import type { Db } from '../database.ts'
10
+ import { DbDate } from '../dialect.ts'
11
+ import { type WorkspaceScope, workspaceRowKey } from '../workspace-keys.ts'
10
12
  import { NodeRepository } from './nodes.ts'
11
13
 
12
14
  function parseValues(config: unknown): Array<{ alias: string; value: unknown }> {
@@ -29,10 +31,69 @@ function parseValues(config: unknown): Array<{ alias: string; value: unknown }>
29
31
  export class DataTypeRepository {
30
32
  #db: Db
31
33
  #nodes: NodeRepository
34
+ #workspace: WorkspaceScope | undefined
32
35
 
33
- constructor(db: Db) {
36
+ /** With `workspace`, its view (story 376): its copies where it has them, Shared's otherwise. */
37
+ constructor(db: Db, options: { workspace?: WorkspaceScope } = {}) {
34
38
  this.#db = db
35
39
  this.#nodes = new NodeRepository(db)
40
+ this.#workspace = options.workspace
41
+ }
42
+
43
+ /** The same repository in a workspace's view (undefined: Shared's). */
44
+ inWorkspace(workspace: WorkspaceScope | undefined): DataTypeRepository {
45
+ return new DataTypeRepository(this.#db, workspace ? { workspace } : {})
46
+ }
47
+
48
+ #stored(key: string): string {
49
+ return workspaceRowKey((this.#workspace as WorkspaceScope).key, normaliseUuid(key))
50
+ }
51
+
52
+ /** The workspace's copy of a data type, by the key it stands for. */
53
+ async #copyOf(db: Db, key: string): Promise<{ nodeId: number; retired: boolean } | undefined> {
54
+ const rows = await db.q
55
+ .selectFrom('data_type as d')
56
+ .innerJoin('node as n', 'n.id', 'd.node_id')
57
+ .select(['d.node_id', 'd.retired_at'])
58
+ .where('n.unique_id', '=', this.#stored(key))
59
+ .where('d.workspace_id', '=', (this.#workspace as WorkspaceScope).id)
60
+ .execute()
61
+ const row = rows[0]
62
+ return row ? { nodeId: Number(row.node_id), retired: row.retired_at !== null } : undefined
63
+ }
64
+
65
+ /** Every data type the workspace sees, by the key it stands for. */
66
+ async #listedKeys(): Promise<string[]> {
67
+ const workspace = this.#workspace as WorkspaceScope
68
+ const copies = await this.#db.q
69
+ .selectFrom('data_type as d')
70
+ .innerJoin('node as n', 'n.id', 'd.node_id')
71
+ .select(['d.origin_key', 'd.retired_at', 'n.text'])
72
+ .where('d.workspace_id', '=', workspace.id)
73
+ .execute()
74
+ const copied = new Set(copies.map((row) => normaliseUuid(String(row.origin_key))))
75
+ const shared = await this.#db.q
76
+ .selectFrom('data_type as d')
77
+ .innerJoin('node as n', 'n.id', 'd.node_id')
78
+ .select(['n.unique_id', 'n.text'])
79
+ .where('d.workspace_id', 'is', null)
80
+ .execute()
81
+ return [
82
+ ...copies
83
+ .filter((row) => row.retired_at === null)
84
+ .map((row) => ({
85
+ key: normaliseUuid(String(row.origin_key)),
86
+ name: String(row.text ?? ''),
87
+ })),
88
+ ...shared
89
+ .filter((row) => !copied.has(normaliseUuid(String(row.unique_id))))
90
+ .map((row) => ({
91
+ key: normaliseUuid(String(row.unique_id)),
92
+ name: String(row.text ?? ''),
93
+ })),
94
+ ]
95
+ .sort((a, b) => a.name.localeCompare(b.name))
96
+ .map((row) => row.key)
36
97
  }
37
98
 
38
99
  get nodes(): NodeRepository {
@@ -40,6 +101,12 @@ export class DataTypeRepository {
40
101
  }
41
102
 
42
103
  async byKey(key: string): Promise<DataTypeModel | undefined> {
104
+ if (this.#workspace) {
105
+ const copy = await this.#copyOf(this.#db, key)
106
+ if (copy?.retired) return undefined
107
+ const found = await this.inWorkspace(undefined).byKey(copy ? this.#stored(key) : key)
108
+ return found && copy ? { ...found, key: normaliseUuid(key) } : found
109
+ }
43
110
  const rows = await this.#db.q
44
111
  .selectFrom('data_type as d')
45
112
  .innerJoin('node as n', 'n.id', 'd.node_id')
@@ -72,12 +139,24 @@ export class DataTypeRepository {
72
139
  }
73
140
 
74
141
  async byAlias(alias: string): Promise<DataTypeModel | undefined> {
142
+ if (this.#workspace) {
143
+ const copies = await this.#db.q
144
+ .selectFrom('data_type')
145
+ .select(['origin_key', 'retired_at'])
146
+ .where('workspace_id', '=', this.#workspace.id)
147
+ .where('alias', '=', alias)
148
+ .execute()
149
+ if (copies[0]) return this.byKey(String(copies[0].origin_key))
150
+ }
75
151
  const rows = await this.#db.q
76
152
  .selectFrom('data_type as d')
77
153
  .innerJoin('node as n', 'n.id', 'd.node_id')
78
154
  .select('n.unique_id')
79
155
  .where('d.alias', '=', alias)
156
+ .where('d.workspace_id', 'is', null)
80
157
  .execute()
158
+ if (this.#workspace && rows[0] && (await this.#copyOf(this.#db, String(rows[0].unique_id))))
159
+ return undefined
81
160
  return rows[0] ? this.byKey(String(rows[0].unique_id)) : undefined
82
161
  }
83
162
 
@@ -112,10 +191,12 @@ export class DataTypeRepository {
112
191
  }
113
192
 
114
193
  async all(): Promise<DataTypeModel[]> {
194
+ if (this.#workspace) return this.byKeys(await this.#listedKeys())
115
195
  const rows = await this.#db.q
116
196
  .selectFrom('data_type as d')
117
197
  .innerJoin('node as n', 'n.id', 'd.node_id')
118
198
  .select('n.unique_id')
199
+ .where('d.workspace_id', 'is', null)
119
200
  .orderBy('n.text')
120
201
  .execute()
121
202
  return this.byKeys(rows.map((r) => String(r.unique_id)))
@@ -249,6 +330,37 @@ export class DataTypeRepository {
249
330
  }
250
331
 
251
332
  async save(model: DataTypeModel, userId?: number): Promise<DataTypeModel> {
333
+ if (this.#workspace) {
334
+ const workspace = this.#workspace
335
+ return this.#db.transaction(async (tx) => {
336
+ const stored = this.#stored(model.key)
337
+ const existing = await this.#copyOf(tx, model.key)
338
+ await new DataTypeRepository(tx).#write(
339
+ { ...model, key: stored },
340
+ userId,
341
+ existing ? undefined : { workspaceId: workspace.id, originKey: normaliseUuid(model.key) },
342
+ )
343
+ if (existing?.retired)
344
+ await tx.q
345
+ .updateTable('data_type')
346
+ .set({ retired_at: null })
347
+ .where('node_id', '=', existing.nodeId)
348
+ .execute()
349
+ return (await this.#within(tx).byKey(model.key)) as DataTypeModel
350
+ })
351
+ }
352
+ return this.#write(model, userId)
353
+ }
354
+
355
+ #within(db: Db): DataTypeRepository {
356
+ return new DataTypeRepository(db, this.#workspace ? { workspace: this.#workspace } : {})
357
+ }
358
+
359
+ async #write(
360
+ model: DataTypeModel,
361
+ userId: number | undefined,
362
+ copy?: { workspaceId: number; originKey: string },
363
+ ): Promise<DataTypeModel> {
252
364
  return this.#db.transaction(async (tx) => {
253
365
  const repo = new DataTypeRepository(tx)
254
366
  const existing = await repo.nodes.byKey(model.key)
@@ -275,7 +387,7 @@ export class DataTypeRepository {
275
387
  const node = await repo.nodes.create({
276
388
  key: model.key,
277
389
  parentId,
278
- objectType: ObjectTypes.DataType,
390
+ objectType: copy ? ObjectTypes.WorkspaceDataType : ObjectTypes.DataType,
279
391
  text: model.name,
280
392
  userId,
281
393
  })
@@ -283,6 +395,8 @@ export class DataTypeRepository {
283
395
  .insertInto('data_type')
284
396
  .values({
285
397
  node_id: node.id,
398
+ workspace_id: copy?.workspaceId ?? null,
399
+ origin_key: copy?.originKey ?? null,
286
400
  alias: model.alias ?? null,
287
401
  editor_alias: model.editorAlias,
288
402
  editor_ui_alias: model.editorUiAlias ?? null,
@@ -298,8 +412,33 @@ export class DataTypeRepository {
298
412
  }
299
413
 
300
414
  async delete(key: string): Promise<boolean> {
415
+ if (this.#workspace) {
416
+ const workspace = this.#workspace
417
+ const copy = await this.#copyOf(this.#db, key)
418
+ if (copy?.retired) return false
419
+ const shared = (await this.#nodes.byKey(key))?.objectType === ObjectTypes.DataType
420
+ if (!shared) return copy ? new DataTypeRepository(this.#db).#remove(copy.nodeId) : false
421
+ // A Shared data type removed in the workspace is a retired copy, which hides Shared's.
422
+ if (!copy) {
423
+ const found = await this.inWorkspace(undefined).byKey(key)
424
+ if (!found) return false
425
+ await this.save(found)
426
+ }
427
+ await this.#db.q
428
+ .updateTable('data_type')
429
+ .set({ retired_at: DbDate.toDb(new Date()) })
430
+ .where('workspace_id', '=', workspace.id)
431
+ .where('origin_key', '=', normaliseUuid(key))
432
+ .execute()
433
+ return true
434
+ }
301
435
  const node = await this.#nodes.byKey(key)
302
436
  if (!node || node.objectType !== ObjectTypes.DataType) return false
437
+ return this.#remove(node.id)
438
+ }
439
+
440
+ async #remove(nodeId: number): Promise<boolean> {
441
+ const node = { id: nodeId }
303
442
  return this.#db.transaction(async (tx) => {
304
443
  await tx.q.deleteFrom('data_type').where('node_id', '=', node.id).execute()
305
444
  await tx.q.deleteFrom('node').where('id', '=', node.id).execute()