@crombolo/data 0.36.2 → 0.38.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.
Files changed (75) hide show
  1. package/package.json +3 -2
  2. package/src/audit.ts +163 -133
  3. package/src/change-report.ts +66 -43
  4. package/src/cluster.ts +119 -59
  5. package/src/content-query.ts +205 -200
  6. package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
  7. package/src/custom-tables/chain.ts +150 -0
  8. package/src/custom-tables/ddl.ts +159 -0
  9. package/src/custom-tables/handle.ts +141 -0
  10. package/src/{bundle-tables → custom-tables}/index.ts +3 -2
  11. package/src/custom-tables/names.ts +94 -0
  12. package/src/custom-tables/owners.ts +299 -0
  13. package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
  14. package/src/custom-tables/values.ts +181 -0
  15. package/src/database.ts +40 -5
  16. package/src/dialect.ts +43 -16
  17. package/src/index.ts +8 -1
  18. package/src/migrations.ts +130 -88
  19. package/src/query.ts +158 -0
  20. package/src/relations.ts +49 -28
  21. package/src/repositories/approvals.ts +130 -89
  22. package/src/repositories/bundle-grants.ts +74 -0
  23. package/src/repositories/bundles.ts +85 -76
  24. package/src/repositories/content-applied.ts +47 -28
  25. package/src/repositories/content-events.ts +114 -67
  26. package/src/repositories/content-notifications.ts +102 -74
  27. package/src/repositories/content-types.ts +449 -320
  28. package/src/repositories/data-types.ts +105 -56
  29. package/src/repositories/delivery-member-tokens.ts +37 -28
  30. package/src/repositories/dictionary.ts +77 -58
  31. package/src/repositories/documents.ts +956 -580
  32. package/src/repositories/domains.ts +28 -16
  33. package/src/repositories/dynamic-roots.ts +32 -17
  34. package/src/repositories/external-logins.ts +38 -24
  35. package/src/repositories/folders.ts +7 -4
  36. package/src/repositories/form-entries.ts +120 -103
  37. package/src/repositories/form-workflows.ts +87 -62
  38. package/src/repositories/jobs.ts +296 -0
  39. package/src/repositories/languages.ts +51 -38
  40. package/src/repositories/log-searches.ts +7 -5
  41. package/src/repositories/media-access.ts +173 -123
  42. package/src/repositories/media-listing.ts +155 -92
  43. package/src/repositories/members.ts +189 -162
  44. package/src/repositories/nodes.ts +227 -181
  45. package/src/repositories/notifications.ts +22 -17
  46. package/src/repositories/public-access.ts +77 -61
  47. package/src/repositories/redirects.ts +113 -70
  48. package/src/repositories/references.ts +55 -44
  49. package/src/repositories/remote-jobs.ts +45 -35
  50. package/src/repositories/tags.ts +11 -10
  51. package/src/repositories/templates.ts +24 -14
  52. package/src/repositories/transfer-answers.ts +58 -50
  53. package/src/repositories/transfer-runs.ts +93 -75
  54. package/src/repositories/two-factor.ts +74 -55
  55. package/src/repositories/user-data.ts +54 -36
  56. package/src/repositories/users.ts +345 -300
  57. package/src/repositories/webhooks.ts +196 -137
  58. package/src/schema/bundle-grants.ts +32 -0
  59. package/src/schema/jobs.ts +56 -0
  60. package/src/schema/plan.ts +4 -0
  61. package/src/schema-ops.ts +15 -12
  62. package/src/schema-state.ts +53 -38
  63. package/src/search.ts +175 -129
  64. package/src/setup.ts +17 -13
  65. package/src/site-search.ts +134 -83
  66. package/src/table-types.ts +152 -0
  67. package/src/tables.gen.ts +859 -0
  68. package/src/tree-invariants.ts +21 -12
  69. package/src/url-options.ts +17 -13
  70. package/src/workflow-store.ts +17 -13
  71. package/src/bundle-tables/capability.ts +0 -377
  72. package/src/bundle-tables/chain.ts +0 -105
  73. package/src/bundle-tables/ddl.ts +0 -124
  74. package/src/bundle-tables/guard.ts +0 -116
  75. package/src/bundle-tables/names.ts +0 -58
@@ -38,15 +38,18 @@ const GROUP_TYPE_NAMES = ['Group', 'Tab'] as const
38
38
  * settings, and whether the type is in site search: what its items are indexed by.
39
39
  */
40
40
  async function propertySignature(db: Db, contentTypeNodeId: number): Promise<string> {
41
- const rows = await db.query<Record<string, unknown>>(
42
- `SELECT p.id, p.alias, p.variations, p.data_type_id, p.searchable, p.site_search FROM property_type p
43
- WHERE p.content_type_id = ? AND p.retired_at IS NULL ORDER BY p.id`,
44
- [contentTypeNodeId],
45
- )
46
- const [type] = await db.query<{ site_search: unknown }>(
47
- 'SELECT site_search FROM content_type WHERE node_id = ?',
48
- [contentTypeNodeId],
49
- )
41
+ const rows = await db.q
42
+ .selectFrom('property_type as p')
43
+ .select(['p.id', 'p.alias', 'p.variations', 'p.data_type_id', 'p.searchable', 'p.site_search'])
44
+ .where('p.content_type_id', '=', contentTypeNodeId)
45
+ .where('p.retired_at', 'is', null)
46
+ .orderBy('p.id')
47
+ .execute()
48
+ const [type] = await db.q
49
+ .selectFrom('content_type')
50
+ .select('site_search')
51
+ .where('node_id', '=', contentTypeNodeId)
52
+ .execute()
50
53
  return [
51
54
  `site:${type ? fromDbBool(type.site_search) : true}`,
52
55
  ...rows.map(
@@ -145,22 +148,27 @@ export class ContentTypeRepository {
145
148
  alias: string,
146
149
  options: { includeRetired?: boolean } = {},
147
150
  ): Promise<ContentTypeAggregate | undefined> {
148
- const rows = await this.#db.query<{ unique_id: string }>(
149
- `SELECT n.unique_id FROM content_type ct JOIN node n ON n.id = ct.node_id
150
- WHERE ct.alias = ? AND n.node_object_type = ?${options.includeRetired ? '' : ' AND ct.retired_at IS NULL'}`,
151
- [alias, this.#objectType],
152
- )
151
+ const rows = await this.#db.q
152
+ .selectFrom('content_type as ct')
153
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
154
+ .select('n.unique_id')
155
+ .where('ct.alias', '=', alias)
156
+ .where('n.node_object_type', '=', this.#objectType)
157
+ .$if(!options.includeRetired, (q) => q.where('ct.retired_at', 'is', null))
158
+ .execute()
153
159
  return rows[0] ? this.byKey(String(rows[0].unique_id)) : undefined
154
160
  }
155
161
 
156
162
  async aliasExists(alias: string, exceptKey?: string): Promise<boolean> {
157
- // Built conditionally rather than with `? IS NULL`: Postgres cannot infer a
158
- // type for a bare parameter used only in a null test.
159
- const sql = `SELECT COUNT(*) AS n FROM content_type ct
160
- JOIN node n ON n.id = ct.node_id
161
- WHERE ct.alias = ?${exceptKey ? ' AND n.unique_id <> ?' : ''}`
162
- const params = exceptKey ? [alias, normaliseUuid(exceptKey)] : [alias]
163
- const rows = await this.#db.query<{ n: number }>(sql, params)
163
+ const rows = await this.#db.q
164
+ .selectFrom('content_type as ct')
165
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
166
+ .select((eb) => eb.fn.countAll().as('n'))
167
+ .where('ct.alias', '=', alias)
168
+ .$if(Boolean(exceptKey), (q) =>
169
+ q.where('n.unique_id', '<>', normaliseUuid(exceptKey as string)),
170
+ )
171
+ .execute()
164
172
  return Number(rows[0]?.n ?? 0) > 0
165
173
  }
166
174
 
@@ -169,17 +177,19 @@ export class ContentTypeRepository {
169
177
  skip: number,
170
178
  take: number,
171
179
  ): Promise<{ total: number; items: ContentTypeAggregate[] }> {
172
- const totals = await this.#db.query<{ n: number }>(
173
- `SELECT COUNT(*) AS n FROM content_type ct JOIN node n ON n.id = ct.node_id
174
- WHERE ct.allow_at_root = ? AND ct.retired_at IS NULL AND n.node_object_type = ?`,
175
- [this.#db.dialect.boolValue(true), this.#objectType],
176
- )
177
- const rows = await this.#db.query<{ unique_id: string }>(
178
- `SELECT n.unique_id FROM content_type ct JOIN node n ON n.id = ct.node_id
179
- WHERE ct.allow_at_root = ? AND ct.retired_at IS NULL AND n.node_object_type = ?
180
- ORDER BY n.text LIMIT ? OFFSET ?`,
181
- [this.#db.dialect.boolValue(true), this.#objectType, take, skip],
182
- )
180
+ const matching = this.#db.q
181
+ .selectFrom('content_type as ct')
182
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
183
+ .where('ct.allow_at_root', '=', this.#db.dialect.boolValue(true))
184
+ .where('ct.retired_at', 'is', null)
185
+ .where('n.node_object_type', '=', this.#objectType)
186
+ const totals = await matching.select((eb) => eb.fn.countAll().as('n')).execute()
187
+ const rows = await matching
188
+ .select('n.unique_id')
189
+ .orderBy('n.text')
190
+ .limit(take)
191
+ .offset(skip)
192
+ .execute()
183
193
  const items: ContentTypeAggregate[] = []
184
194
  let hidden = 0
185
195
  for (const row of rows) {
@@ -200,22 +210,20 @@ export class ContentTypeRepository {
200
210
  skip: number,
201
211
  take: number,
202
212
  ): Promise<{ total: number; items: ContentTypeAggregate[] }> {
203
- const where = `WHERE ct.is_element = ? AND ct.allow_in_library = ? AND ct.retired_at IS NULL
204
- AND n.node_object_type = ?`
205
- const params = [
206
- this.#db.dialect.boolValue(true),
207
- this.#db.dialect.boolValue(true),
208
- this.#objectType,
209
- ]
210
- const totals = await this.#db.query<{ n: number }>(
211
- `SELECT COUNT(*) AS n FROM content_type ct JOIN node n ON n.id = ct.node_id ${where}`,
212
- params,
213
- )
214
- const rows = await this.#db.query<{ unique_id: string }>(
215
- `SELECT n.unique_id FROM content_type ct JOIN node n ON n.id = ct.node_id ${where}
216
- ORDER BY n.text LIMIT ? OFFSET ?`,
217
- [...params, take, skip],
218
- )
213
+ const matching = this.#db.q
214
+ .selectFrom('content_type as ct')
215
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
216
+ .where('ct.is_element', '=', this.#db.dialect.boolValue(true))
217
+ .where('ct.allow_in_library', '=', this.#db.dialect.boolValue(true))
218
+ .where('ct.retired_at', 'is', null)
219
+ .where('n.node_object_type', '=', this.#objectType)
220
+ const totals = await matching.select((eb) => eb.fn.countAll().as('n')).execute()
221
+ const rows = await matching
222
+ .select('n.unique_id')
223
+ .orderBy('n.text')
224
+ .limit(take)
225
+ .offset(skip)
226
+ .execute()
219
227
  const items: ContentTypeAggregate[] = []
220
228
  let hidden = 0
221
229
  for (const row of rows) {
@@ -232,11 +240,12 @@ export class ContentTypeRepository {
232
240
  * level), as its parent's type allows children; nothing when it may.
233
241
  */
234
242
  async placementRefusal(typeKey: string, parentKey: string | null): Promise<string | undefined> {
235
- const types = await this.#db.query<{ id: number; text: string | null; allow_at_root: unknown }>(
236
- `SELECT n.id, n.text, ct.allow_at_root FROM node n JOIN content_type ct ON ct.node_id = n.id
237
- WHERE n.unique_id = ?`,
238
- [normaliseUuid(typeKey)],
239
- )
243
+ const types = await this.#db.q
244
+ .selectFrom('node as n')
245
+ .innerJoin('content_type as ct', 'ct.node_id', 'n.id')
246
+ .select(['n.id', 'n.text', 'ct.allow_at_root'])
247
+ .where('n.unique_id', '=', normaliseUuid(typeKey))
248
+ .execute()
240
249
  const type = types[0]
241
250
  if (!type) return undefined
242
251
  const typeName = String(type.text ?? typeKey)
@@ -244,18 +253,20 @@ export class ContentTypeRepository {
244
253
  return fromDbBool(type.allow_at_root)
245
254
  ? undefined
246
255
  : `"${typeName}" isn't allowed at the top level.`
247
- const parents = await this.#db.query<{ text: string | null; content_type_id: number }>(
248
- `SELECT n.text, c.content_type_id FROM node n JOIN content c ON c.node_id = n.id
249
- WHERE n.unique_id = ?`,
250
- [normaliseUuid(parentKey)],
251
- )
256
+ const parents = await this.#db.q
257
+ .selectFrom('node as n')
258
+ .innerJoin('content as c', 'c.node_id', 'n.id')
259
+ .select(['n.text', 'c.content_type_id'])
260
+ .where('n.unique_id', '=', normaliseUuid(parentKey))
261
+ .execute()
252
262
  const parent = parents[0]
253
263
  if (!parent) return undefined
254
- const allowed = await this.#db.query<{ n: number }>(
255
- `SELECT COUNT(*) AS n FROM content_type_allowed_child
256
- WHERE content_type_id = ? AND allowed_content_type_id = ?`,
257
- [parent.content_type_id, type.id],
258
- )
264
+ const allowed = await this.#db.q
265
+ .selectFrom('content_type_allowed_child')
266
+ .select((eb) => eb.fn.countAll().as('n'))
267
+ .where('content_type_id', '=', parent.content_type_id)
268
+ .where('allowed_content_type_id', '=', type.id)
269
+ .execute()
259
270
  return Number(allowed[0]?.n ?? 0) > 0
260
271
  ? undefined
261
272
  : `"${typeName}" isn't allowed under "${parent.text ?? parentKey}".`
@@ -268,12 +279,13 @@ export class ContentTypeRepository {
268
279
  ): Promise<{ total: number; items: ContentTypeAggregate[] }> {
269
280
  const node = await this.#nodes.byKey(key)
270
281
  if (!node) return { total: 0, items: [] }
271
- const rows = await this.#db.query<{ unique_id: string }>(
272
- `SELECT child.unique_id FROM content_type_allowed_child ac
273
- JOIN node child ON child.id = ac.allowed_content_type_id
274
- WHERE ac.content_type_id = ? ORDER BY ac.sort_order`,
275
- [node.id],
276
- )
282
+ const rows = await this.#db.q
283
+ .selectFrom('content_type_allowed_child as ac')
284
+ .innerJoin('node as child', 'child.id', 'ac.allowed_content_type_id')
285
+ .select('child.unique_id')
286
+ .where('ac.content_type_id', '=', node.id)
287
+ .orderBy('ac.sort_order')
288
+ .execute()
277
289
  const items: ContentTypeAggregate[] = []
278
290
  for (const row of rows.slice(skip, skip + take)) {
279
291
  const aggregate = await this.byKey(String(row.unique_id))
@@ -316,76 +328,92 @@ export class ContentTypeRepository {
316
328
  }
317
329
 
318
330
  const variations = variationFlags(aggregate.variesByCulture, aggregate.variesBySegment)
319
- const typeValues = [
320
- aggregate.alias,
321
- aggregate.icon,
322
- aggregate.description,
323
- aggregate.collectionKey,
324
- bool(aggregate.isElement),
325
- bool(aggregate.allowedInLibrary),
326
- bool(aggregate.allowedAsRoot),
331
+ const typeValues = {
332
+ alias: aggregate.alias,
333
+ icon: aggregate.icon ?? null,
334
+ description: aggregate.description ?? null,
335
+ list_view: aggregate.collectionKey ?? null,
336
+ is_element: bool(aggregate.isElement),
337
+ allow_in_library: bool(aggregate.allowedInLibrary),
338
+ allow_at_root: bool(aggregate.allowedAsRoot),
327
339
  variations,
328
- bool(aggregate.cleanup.preventCleanup),
329
- aggregate.cleanup.keepAllVersionsNewerThanDays,
330
- aggregate.cleanup.keepLatestVersionPerDayForDays,
331
- aggregate.sinceVersion ?? null,
332
- ]
340
+ prevent_cleanup: bool(aggregate.cleanup.preventCleanup),
341
+ keep_all_versions_newer_than_days: aggregate.cleanup.keepAllVersionsNewerThanDays ?? null,
342
+ keep_latest_version_per_day_for_days:
343
+ aggregate.cleanup.keepLatestVersionPerDayForDays ?? null,
344
+ since_version: aggregate.sinceVersion ?? null,
345
+ }
333
346
  if (existing) {
334
- await tx.exec(
335
- `UPDATE content_type SET alias = ?, icon = ?, description = ?, list_view = ?, is_element = ?,
336
- allow_in_library = ?, allow_at_root = ?, variations = ?, prevent_cleanup = ?,
337
- keep_all_versions_newer_than_days = ?, keep_latest_version_per_day_for_days = ?, since_version = ?, retired_at = NULL
338
- WHERE node_id = ?`,
339
- [...typeValues, nodeId],
340
- )
347
+ await tx.q
348
+ .updateTable('content_type')
349
+ .set({ ...typeValues, retired_at: null })
350
+ .where('node_id', '=', nodeId)
351
+ .execute()
341
352
  } else {
342
- await tx.exec(
343
- `INSERT INTO content_type
344
- (node_id, alias, icon, thumbnail, description, list_view, is_element, allow_in_library, allow_at_root,
345
- variations, prevent_cleanup, keep_all_versions_newer_than_days, keep_latest_version_per_day_for_days, since_version, since_state_id)
346
- VALUES (?, ?, ?, 'folder.png', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
347
- [nodeId, ...typeValues, options.sinceStateId ?? null],
348
- )
353
+ await tx.q
354
+ .insertInto('content_type')
355
+ .values({
356
+ node_id: nodeId,
357
+ ...typeValues,
358
+ thumbnail: 'folder.png',
359
+ since_state_id: options.sinceStateId ?? null,
360
+ })
361
+ .execute()
349
362
  }
350
363
  // As for a property's flags: only a schema sync says.
351
364
  if (aggregate.siteSearch !== undefined)
352
- await tx.exec('UPDATE content_type SET site_search = ? WHERE node_id = ?', [
353
- bool(aggregate.siteSearch),
354
- nodeId,
355
- ])
365
+ await tx.q
366
+ .updateTable('content_type')
367
+ .set({ site_search: bool(aggregate.siteSearch) })
368
+ .where('node_id', '=', nodeId)
369
+ .execute()
356
370
 
357
371
  // ---- groups: upsert by key
358
372
  const groupIdByKey = new Map<string, number>()
359
373
  for (const container of aggregate.containers) {
360
374
  const type = container.type === 'Tab' ? PropertyGroupType.Tab : PropertyGroupType.Group
361
- const found = await tx.query<{ id: number }>(
362
- 'SELECT id FROM property_type_group WHERE unique_id = ?',
363
- [container.key],
364
- )
375
+ const groupId = () =>
376
+ tx.q
377
+ .selectFrom('property_type_group')
378
+ .select('id')
379
+ .where('unique_id', '=', container.key)
380
+ .execute()
381
+ const found = await groupId()
365
382
  if (found[0]) {
366
- await tx.exec(
367
- 'UPDATE property_type_group SET type = ?, text = ?, alias = ?, sort_order = ? WHERE id = ?',
368
- [type, container.name, container.alias, container.sortOrder, Number(found[0].id)],
369
- )
383
+ await tx.q
384
+ .updateTable('property_type_group')
385
+ .set({
386
+ type,
387
+ text: container.name ?? null,
388
+ alias: container.alias ?? null,
389
+ sort_order: container.sortOrder,
390
+ })
391
+ .where('id', '=', Number(found[0].id))
392
+ .execute()
370
393
  groupIdByKey.set(container.key, Number(found[0].id))
371
394
  } else {
372
- await tx.exec(
373
- 'INSERT INTO property_type_group (unique_id, content_type_node_id, type, text, alias, sort_order) VALUES (?, ?, ?, ?, ?, ?)',
374
- [container.key, nodeId, type, container.name, container.alias, container.sortOrder],
375
- )
376
- const created = await tx.query<{ id: number }>(
377
- 'SELECT id FROM property_type_group WHERE unique_id = ?',
378
- [container.key],
379
- )
395
+ await tx.q
396
+ .insertInto('property_type_group')
397
+ .values({
398
+ unique_id: container.key,
399
+ content_type_node_id: nodeId,
400
+ type,
401
+ text: container.name ?? null,
402
+ alias: container.alias ?? null,
403
+ sort_order: container.sortOrder,
404
+ })
405
+ .execute()
406
+ const created = await groupId()
380
407
  groupIdByKey.set(container.key, Number(created[0]?.id))
381
408
  }
382
409
  }
383
410
  for (const container of aggregate.containers) {
384
411
  const parent = container.parentKey ? groupIdByKey.get(container.parentKey) : undefined
385
- await tx.exec('UPDATE property_type_group SET parent_id = ? WHERE unique_id = ?', [
386
- parent ?? null,
387
- container.key,
388
- ])
412
+ await tx.q
413
+ .updateTable('property_type_group')
414
+ .set({ parent_id: parent ?? null })
415
+ .where('unique_id', '=', container.key)
416
+ .execute()
389
417
  }
390
418
 
391
419
  // ---- properties: upsert by key; retire the absent; revive the returning
@@ -399,151 +427,177 @@ export class ContentTypeRepository {
399
427
  // A key the type has never had, but an alias it has (live or retired), is
400
428
  // that property coming back: reuse the row, so its values return with it.
401
429
  let key = normaliseUuid(property.key)
402
- const byKey = await tx.query('SELECT id FROM property_type WHERE unique_id = ?', [key])
430
+ const byKey = await tx.q
431
+ .selectFrom('property_type')
432
+ .select('id')
433
+ .where('unique_id', '=', key)
434
+ .execute()
403
435
  if (byKey.length === 0) {
404
- const sameAlias = await tx.query<{ unique_id: string }>(
405
- 'SELECT unique_id FROM property_type WHERE content_type_id = ? AND alias = ?',
406
- [nodeId, property.alias],
407
- )
436
+ const sameAlias = await tx.q
437
+ .selectFrom('property_type')
438
+ .select('unique_id')
439
+ .where('content_type_id', '=', nodeId)
440
+ .where('alias', '=', property.alias)
441
+ .execute()
408
442
  if (sameAlias[0]) key = normaliseUuid(String(sameAlias[0].unique_id))
409
443
  }
410
444
  kept.add(key)
411
445
  const groupId = property.containerKey
412
446
  ? (groupIdByKey.get(property.containerKey) ?? null)
413
447
  : null
414
- const columns = [
415
- dataTypeNode.id,
416
- groupId,
417
- property.alias,
418
- property.name,
419
- property.description,
420
- property.sortOrder,
421
- bool(property.mandatory),
422
- property.mandatoryMessage,
423
- property.regEx,
424
- property.regExMessage,
425
- bool(property.labelOnTop),
426
- variationFlags(property.variesByCulture, property.variesBySegment),
427
- property.sinceVersion ?? null,
428
- ]
429
- const found = await tx.query<{ id: number }>(
430
- 'SELECT id FROM property_type WHERE unique_id = ?',
431
- [key],
432
- )
448
+ const columns = {
449
+ data_type_id: dataTypeNode.id,
450
+ property_type_group_id: groupId,
451
+ alias: property.alias,
452
+ name: property.name,
453
+ description: property.description ?? null,
454
+ sort_order: property.sortOrder,
455
+ mandatory: bool(property.mandatory),
456
+ mandatory_message: property.mandatoryMessage ?? null,
457
+ validation_reg_exp: property.regEx ?? null,
458
+ validation_reg_exp_message: property.regExMessage ?? null,
459
+ label_on_top: bool(property.labelOnTop),
460
+ variations: variationFlags(property.variesByCulture, property.variesBySegment),
461
+ since_version: property.sinceVersion ?? null,
462
+ }
463
+ const found = await tx.q
464
+ .selectFrom('property_type')
465
+ .select('id')
466
+ .where('unique_id', '=', key)
467
+ .execute()
433
468
  if (found[0]) {
434
- await tx.exec(
435
- `UPDATE property_type SET data_type_id = ?, property_type_group_id = ?, alias = ?, name = ?, description = ?,
436
- sort_order = ?, mandatory = ?, mandatory_message = ?, validation_reg_exp = ?, validation_reg_exp_message = ?,
437
- label_on_top = ?, variations = ?, since_version = ?, retired_at = NULL
438
- WHERE id = ?`,
439
- [...columns, Number(found[0].id)],
440
- )
469
+ const id = Number(found[0].id)
470
+ await tx.q
471
+ .updateTable('property_type')
472
+ .set({ ...columns, retired_at: null })
473
+ .where('id', '=', id)
474
+ .execute()
441
475
  // Only a schema sync says; a backoffice save leaves it as it was.
442
476
  if (property.searchable !== undefined)
443
- await tx.exec('UPDATE property_type SET searchable = ? WHERE id = ?', [
444
- bool(property.searchable),
445
- Number(found[0].id),
446
- ])
477
+ await tx.q
478
+ .updateTable('property_type')
479
+ .set({ searchable: bool(property.searchable) })
480
+ .where('id', '=', id)
481
+ .execute()
447
482
  if (property.siteSearch !== undefined)
448
- await tx.exec('UPDATE property_type SET site_search = ? WHERE id = ?', [
449
- siteSearchColumn(property.siteSearch),
450
- Number(found[0].id),
451
- ])
483
+ await tx.q
484
+ .updateTable('property_type')
485
+ .set({ site_search: siteSearchColumn(property.siteSearch) })
486
+ .where('id', '=', id)
487
+ .execute()
452
488
  } else {
453
- await tx.exec(
454
- `INSERT INTO property_type
455
- (unique_id, content_type_id, data_type_id, property_type_group_id, alias, name, description, sort_order,
456
- mandatory, mandatory_message, validation_reg_exp, validation_reg_exp_message, label_on_top, variations, since_version, since_state_id, searchable, site_search)
457
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
458
- [
459
- key,
460
- nodeId,
489
+ await tx.q
490
+ .insertInto('property_type')
491
+ .values({
492
+ unique_id: key,
493
+ content_type_id: nodeId,
461
494
  ...columns,
462
- options.sinceStateId ?? null,
463
- bool(property.searchable !== false),
464
- siteSearchColumn(property.siteSearch),
465
- ],
466
- )
495
+ since_state_id: options.sinceStateId ?? null,
496
+ searchable: bool(property.searchable !== false),
497
+ site_search: siteSearchColumn(property.siteSearch),
498
+ })
499
+ .execute()
467
500
  }
468
501
  }
469
502
  if (this.#kind === 'member') {
470
503
  const bool = (v: boolean) => tx.dialect.boolValue(v)
471
504
  for (const property of aggregate.properties) {
472
505
  // By alias: the row may have been reused under its old key above.
473
- const row = await tx.query<{ id: number }>(
474
- 'SELECT id FROM property_type WHERE content_type_id = ? AND alias = ?',
475
- [nodeId, property.alias],
476
- )
506
+ const row = await tx.q
507
+ .selectFrom('property_type')
508
+ .select('id')
509
+ .where('content_type_id', '=', nodeId)
510
+ .where('alias', '=', property.alias)
511
+ .execute()
477
512
  const id = Number(row[0]?.id)
478
513
  if (!id) continue
479
- await tx.exec('DELETE FROM member_property_type WHERE property_type_id = ?', [id])
480
- await tx.exec(
481
- 'INSERT INTO member_property_type (property_type_id, member_can_edit, member_can_view, is_sensitive) VALUES (?, ?, ?, ?)',
482
- [
483
- id,
484
- bool(property.memberCanEdit === true),
485
- bool(property.memberCanView === true),
486
- bool(property.isSensitive === true),
487
- ],
488
- )
514
+ await tx.q.deleteFrom('member_property_type').where('property_type_id', '=', id).execute()
515
+ await tx.q
516
+ .insertInto('member_property_type')
517
+ .values({
518
+ property_type_id: id,
519
+ member_can_edit: bool(property.memberCanEdit === true),
520
+ member_can_view: bool(property.memberCanView === true),
521
+ is_sensitive: bool(property.isSensitive === true),
522
+ })
523
+ .execute()
489
524
  }
490
525
  }
491
- const present = await tx.query(
492
- 'SELECT id, unique_id, retired_at FROM property_type WHERE content_type_id = ?',
493
- [nodeId],
494
- )
526
+ const present = await tx.q
527
+ .selectFrom('property_type')
528
+ .select(['id', 'unique_id', 'retired_at'])
529
+ .where('content_type_id', '=', nodeId)
530
+ .execute()
495
531
  for (const row of present) {
496
532
  if (kept.has(normaliseUuid(String(row.unique_id)))) continue
497
533
  if (row.retired_at !== null && row.retired_at !== undefined) continue
498
534
  // Retired: hidden and detached from its group; every value stays.
499
- await tx.exec(
500
- 'UPDATE property_type SET retired_at = ?, property_type_group_id = NULL WHERE id = ?',
501
- [now, Number(row.id)],
502
- )
535
+ await tx.q
536
+ .updateTable('property_type')
537
+ .set({ retired_at: now, property_type_group_id: null })
538
+ .where('id', '=', Number(row.id))
539
+ .execute()
503
540
  }
504
541
  const containerKeys = new Set(aggregate.containers.map((c) => c.key))
505
- for (const group of await tx.query(
506
- 'SELECT id, unique_id FROM property_type_group WHERE content_type_node_id = ?',
507
- [nodeId],
508
- )) {
542
+ for (const group of await tx.q
543
+ .selectFrom('property_type_group')
544
+ .select(['id', 'unique_id'])
545
+ .where('content_type_node_id', '=', nodeId)
546
+ .execute()) {
509
547
  if (containerKeys.has(String(group.unique_id))) continue
510
- const inUse = await tx.query<{ n: number }>(
511
- 'SELECT COUNT(*) AS n FROM property_type WHERE property_type_group_id = ?',
512
- [Number(group.id)],
513
- )
548
+ const inUse = await tx.q
549
+ .selectFrom('property_type')
550
+ .select((eb) => eb.fn.countAll().as('n'))
551
+ .where('property_type_group_id', '=', Number(group.id))
552
+ .execute()
514
553
  if (Number(inUse[0]?.n ?? 0) === 0)
515
- await tx.exec('DELETE FROM property_type_group WHERE id = ?', [Number(group.id)])
554
+ await tx.q.deleteFrom('property_type_group').where('id', '=', Number(group.id)).execute()
516
555
  }
517
556
 
518
557
  // ---- relations: replaced wholesale; nothing references these rows
519
- await tx.exec('DELETE FROM content_type_composition WHERE child_content_type_id = ?', [
520
- nodeId,
521
- ])
558
+ await tx.q
559
+ .deleteFrom('content_type_composition')
560
+ .where('child_content_type_id', '=', nodeId)
561
+ .execute()
522
562
  for (const composition of aggregate.compositions) {
523
563
  const parent = await repo.nodes.byKey(composition.contentTypeKey)
524
564
  if (parent)
525
- await tx.exec(
526
- 'INSERT INTO content_type_composition (parent_content_type_id, child_content_type_id) VALUES (?, ?)',
527
- [parent.id, nodeId],
528
- )
565
+ await tx.q
566
+ .insertInto('content_type_composition')
567
+ .values({ parent_content_type_id: parent.id, child_content_type_id: nodeId })
568
+ .execute()
529
569
  }
530
- await tx.exec('DELETE FROM content_type_allowed_child WHERE content_type_id = ?', [nodeId])
570
+ await tx.q
571
+ .deleteFrom('content_type_allowed_child')
572
+ .where('content_type_id', '=', nodeId)
573
+ .execute()
531
574
  for (const allowed of aggregate.allowedContentTypes) {
532
575
  const child = await repo.nodes.byKey(allowed.contentTypeKey)
533
576
  if (child)
534
- await tx.exec(
535
- 'INSERT INTO content_type_allowed_child (content_type_id, allowed_content_type_id, sort_order) VALUES (?, ?, ?)',
536
- [nodeId, child.id, allowed.sortOrder],
537
- )
577
+ await tx.q
578
+ .insertInto('content_type_allowed_child')
579
+ .values({
580
+ content_type_id: nodeId,
581
+ allowed_content_type_id: child.id,
582
+ sort_order: allowed.sortOrder,
583
+ })
584
+ .execute()
538
585
  }
539
- await tx.exec('DELETE FROM content_type_template WHERE content_type_node_id = ?', [nodeId])
586
+ await tx.q
587
+ .deleteFrom('content_type_template')
588
+ .where('content_type_node_id', '=', nodeId)
589
+ .execute()
540
590
  for (const componentKey of aggregate.allowedComponentKeys) {
541
591
  const template = await repo.nodes.byKey(componentKey)
542
592
  if (template)
543
- await tx.exec(
544
- 'INSERT INTO content_type_template (content_type_node_id, template_node_id, is_default) VALUES (?, ?, ?)',
545
- [nodeId, template.id, bool(componentKey === aggregate.defaultComponentKey)],
546
- )
593
+ await tx.q
594
+ .insertInto('content_type_template')
595
+ .values({
596
+ content_type_node_id: nodeId,
597
+ template_node_id: template.id,
598
+ is_default: bool(componentKey === aggregate.defaultComponentKey),
599
+ })
600
+ .execute()
547
601
  }
548
602
 
549
603
  if (existing && propertiesBefore !== (await propertySignature(tx, nodeId))) {
@@ -560,10 +614,12 @@ export class ContentTypeRepository {
560
614
  async retiredProperties(typeKey: string): Promise<RetiredProperty[]> {
561
615
  const node = await this.#nodes.byKey(typeKey)
562
616
  if (!node) return []
563
- const rows = await this.#db.query(
564
- 'SELECT unique_id, alias, retired_at FROM property_type WHERE content_type_id = ? AND retired_at IS NOT NULL',
565
- [node.id],
566
- )
617
+ const rows = await this.#db.q
618
+ .selectFrom('property_type')
619
+ .select(['unique_id', 'alias', 'retired_at'])
620
+ .where('content_type_id', '=', node.id)
621
+ .where('retired_at', 'is not', null)
622
+ .execute()
567
623
  return rows.map((row) => ({
568
624
  key: normaliseUuid(String(row.unique_id)),
569
625
  alias: String(row.alias),
@@ -577,20 +633,20 @@ export class ContentTypeRepository {
577
633
  * ever held. The contract step; nothing else in the system deletes values.
578
634
  */
579
635
  async purgeRetired(olderThan: Date): Promise<Array<{ type: string; alias: string }>> {
580
- const rows = await this.#db.query<{ id: number; alias: string; type_alias: string }>(
581
- `SELECT p.id, p.alias, ct.alias AS type_alias FROM property_type p
582
- JOIN content_type ct ON ct.node_id = p.content_type_id
583
- WHERE p.retired_at IS NOT NULL AND p.retired_at < ?`,
584
- [DbDate.toDb(olderThan)],
585
- )
636
+ const rows = await this.#db.q
637
+ .selectFrom('property_type as p')
638
+ .innerJoin('content_type as ct', 'ct.node_id', 'p.content_type_id')
639
+ .select(['p.id', 'p.alias', 'ct.alias as type_alias'])
640
+ .where('p.retired_at', 'is not', null)
641
+ .where('p.retired_at', '<', DbDate.toDb(olderThan))
642
+ .execute()
586
643
  const purged: Array<{ type: string; alias: string }> = []
587
644
  await this.#db.transaction(async (tx) => {
588
645
  for (const row of rows) {
589
- await tx.exec('DELETE FROM property_value WHERE property_type_id = ?', [Number(row.id)])
590
- await tx.exec('DELETE FROM member_property_type WHERE property_type_id = ?', [
591
- Number(row.id),
592
- ])
593
- await tx.exec('DELETE FROM property_type WHERE id = ?', [Number(row.id)])
646
+ const id = Number(row.id)
647
+ await tx.q.deleteFrom('property_value').where('property_type_id', '=', id).execute()
648
+ await tx.q.deleteFrom('member_property_type').where('property_type_id', '=', id).execute()
649
+ await tx.q.deleteFrom('property_type').where('id', '=', id).execute()
594
650
  purged.push({ type: String(row.type_alias), alias: String(row.alias) })
595
651
  }
596
652
  })
@@ -609,21 +665,26 @@ export class ContentTypeRepository {
609
665
 
610
666
  /** Every live type, cheap enough while types number in the dozens. */
611
667
  async all(): Promise<ContentTypeAggregate[]> {
612
- const rows = await this.#db.query<{ unique_id: string }>(
613
- `SELECT n.unique_id FROM content_type ct JOIN node n ON n.id = ct.node_id
614
- WHERE ct.retired_at IS NULL AND n.node_object_type = ? ORDER BY n.text`,
615
- [this.#objectType],
616
- )
668
+ const rows = await this.#db.q
669
+ .selectFrom('content_type as ct')
670
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
671
+ .select('n.unique_id')
672
+ .where('ct.retired_at', 'is', null)
673
+ .where('n.node_object_type', '=', this.#objectType)
674
+ .orderBy('n.text')
675
+ .execute()
617
676
  return this.byKeys(rows.map((r) => String(r.unique_id)))
618
677
  }
619
678
 
620
679
  /** Every live type's alias by its key, without loading the types themselves. */
621
680
  async aliasesByKey(): Promise<Map<string, string>> {
622
- const rows = await this.#db.query<{ unique_id: string; alias: string }>(
623
- `SELECT n.unique_id, ct.alias FROM content_type ct JOIN node n ON n.id = ct.node_id
624
- WHERE ct.retired_at IS NULL AND n.node_object_type = ?`,
625
- [this.#objectType],
626
- )
681
+ const rows = await this.#db.q
682
+ .selectFrom('content_type as ct')
683
+ .innerJoin('node as n', 'n.id', 'ct.node_id')
684
+ .select(['n.unique_id', 'ct.alias'])
685
+ .where('ct.retired_at', 'is', null)
686
+ .where('n.node_object_type', '=', this.#objectType)
687
+ .execute()
627
688
  return new Map(rows.map((r) => [normaliseUuid(String(r.unique_id)), String(r.alias)]))
628
689
  }
629
690
 
@@ -684,11 +745,12 @@ export class ContentTypeRepository {
684
745
  async compositionReferences(key: string): Promise<ContentTypeAggregate[]> {
685
746
  const node = await this.#nodes.byKey(key)
686
747
  if (!node) return []
687
- const rows = await this.#db.query<{ unique_id: string }>(
688
- `SELECT child.unique_id FROM content_type_composition c JOIN node child ON child.id = c.child_content_type_id
689
- WHERE c.parent_content_type_id = ?`,
690
- [node.id],
691
- )
748
+ const rows = await this.#db.q
749
+ .selectFrom('content_type_composition as c')
750
+ .innerJoin('node as child', 'child.id', 'c.child_content_type_id')
751
+ .select('child.unique_id')
752
+ .where('c.parent_content_type_id', '=', node.id)
753
+ .execute()
692
754
  return this.byKeys(rows.map((r) => String(r.unique_id)))
693
755
  }
694
756
 
@@ -696,11 +758,12 @@ export class ContentTypeRepository {
696
758
  async allowedParents(key: string): Promise<string[]> {
697
759
  const node = await this.#nodes.byKey(key)
698
760
  if (!node) return []
699
- const rows = await this.#db.query<{ unique_id: string }>(
700
- `SELECT parent.unique_id FROM content_type_allowed_child ac JOIN node parent ON parent.id = ac.content_type_id
701
- WHERE ac.allowed_content_type_id = ?`,
702
- [node.id],
703
- )
761
+ const rows = await this.#db.q
762
+ .selectFrom('content_type_allowed_child as ac')
763
+ .innerJoin('node as parent', 'parent.id', 'ac.content_type_id')
764
+ .select('parent.unique_id')
765
+ .where('ac.allowed_content_type_id', '=', node.id)
766
+ .execute()
704
767
  return rows.map((r) => normaliseUuid(String(r.unique_id)))
705
768
  }
706
769
 
@@ -749,10 +812,11 @@ export class ContentTypeRepository {
749
812
  async contentCount(typeKey: string): Promise<number> {
750
813
  const node = await this.#nodes.byKey(typeKey)
751
814
  if (!node) return 0
752
- const rows = await this.#db.query<{ n: number }>(
753
- 'SELECT COUNT(*) AS n FROM content WHERE content_type_id = ?',
754
- [node.id],
755
- )
815
+ const rows = await this.#db.q
816
+ .selectFrom('content')
817
+ .select((eb) => eb.fn.countAll().as('n'))
818
+ .where('content_type_id', '=', node.id)
819
+ .execute()
756
820
  return Number(rows[0]?.n ?? 0)
757
821
  }
758
822
 
@@ -760,27 +824,42 @@ export class ContentTypeRepository {
760
824
  const node = await this.#nodes.byKey(key)
761
825
  if (!node || node.objectType !== this.#objectType) return false
762
826
  return this.#db.transaction(async (tx) => {
763
- await tx.exec(
764
- 'DELETE FROM property_value WHERE property_type_id IN (SELECT id FROM property_type WHERE content_type_id = ?)',
765
- [node.id],
766
- )
767
- await tx.exec(
768
- 'DELETE FROM member_property_type WHERE property_type_id IN (SELECT id FROM property_type WHERE content_type_id = ?)',
769
- [node.id],
770
- )
771
- await tx.exec('DELETE FROM property_type WHERE content_type_id = ?', [node.id])
772
- await tx.exec('DELETE FROM property_type_group WHERE content_type_node_id = ?', [node.id])
773
- await tx.exec(
774
- 'DELETE FROM content_type_composition WHERE child_content_type_id = ? OR parent_content_type_id = ?',
775
- [node.id, node.id],
776
- )
777
- await tx.exec(
778
- 'DELETE FROM content_type_allowed_child WHERE content_type_id = ? OR allowed_content_type_id = ?',
779
- [node.id, node.id],
780
- )
781
- await tx.exec('DELETE FROM content_type_template WHERE content_type_node_id = ?', [node.id])
782
- await tx.exec('DELETE FROM content_type WHERE node_id = ?', [node.id])
783
- await tx.exec('DELETE FROM node WHERE id = ?', [node.id])
827
+ const ownProperties = () =>
828
+ tx.q.selectFrom('property_type').select('id').where('content_type_id', '=', node.id)
829
+ await tx.q
830
+ .deleteFrom('property_value')
831
+ .where('property_type_id', 'in', ownProperties())
832
+ .execute()
833
+ await tx.q
834
+ .deleteFrom('member_property_type')
835
+ .where('property_type_id', 'in', ownProperties())
836
+ .execute()
837
+ await tx.q.deleteFrom('property_type').where('content_type_id', '=', node.id).execute()
838
+ await tx.q
839
+ .deleteFrom('property_type_group')
840
+ .where('content_type_node_id', '=', node.id)
841
+ .execute()
842
+ await tx.q
843
+ .deleteFrom('content_type_composition')
844
+ .where((eb) =>
845
+ eb.or([
846
+ eb('child_content_type_id', '=', node.id),
847
+ eb('parent_content_type_id', '=', node.id),
848
+ ]),
849
+ )
850
+ .execute()
851
+ await tx.q
852
+ .deleteFrom('content_type_allowed_child')
853
+ .where((eb) =>
854
+ eb.or([eb('content_type_id', '=', node.id), eb('allowed_content_type_id', '=', node.id)]),
855
+ )
856
+ .execute()
857
+ await tx.q
858
+ .deleteFrom('content_type_template')
859
+ .where('content_type_node_id', '=', node.id)
860
+ .execute()
861
+ await tx.q.deleteFrom('content_type').where('node_id', '=', node.id).execute()
862
+ await tx.q.deleteFrom('node').where('id', '=', node.id).execute()
784
863
  return true
785
864
  })
786
865
  }
@@ -808,22 +887,46 @@ export class ContentTypeRepository {
808
887
  name: string,
809
888
  parentId: number,
810
889
  ): Promise<ContentTypeAggregate | undefined> {
811
- const typeRows = await this.#db.query(
812
- `SELECT ct.alias, ct.icon, ct.description, ct.list_view, ct.is_element, ct.allow_in_library, ct.allow_at_root, ct.variations,
813
- ct.prevent_cleanup, ct.keep_all_versions_newer_than_days, ct.keep_latest_version_per_day_for_days,
814
- ct.since_version, ct.site_search, ss.version AS since_v, ss.revision AS since_r
815
- FROM content_type ct LEFT JOIN schema_state ss ON ss.id = ct.since_state_id WHERE ct.node_id = ?`,
816
- [nodeId],
817
- )
890
+ const typeRows = await this.#db.q
891
+ .selectFrom('content_type as ct')
892
+ .leftJoin('schema_state as ss', 'ss.id', 'ct.since_state_id')
893
+ .select([
894
+ 'ct.alias',
895
+ 'ct.icon',
896
+ 'ct.description',
897
+ 'ct.list_view',
898
+ 'ct.is_element',
899
+ 'ct.allow_in_library',
900
+ 'ct.allow_at_root',
901
+ 'ct.variations',
902
+ 'ct.prevent_cleanup',
903
+ 'ct.keep_all_versions_newer_than_days',
904
+ 'ct.keep_latest_version_per_day_for_days',
905
+ 'ct.since_version',
906
+ 'ct.site_search',
907
+ 'ss.version as since_v',
908
+ 'ss.revision as since_r',
909
+ ])
910
+ .where('ct.node_id', '=', nodeId)
911
+ .execute()
818
912
  const type = typeRows[0]
819
913
  if (!type) return undefined
820
914
 
821
- const groupRows = await this.#db.query(
822
- `SELECT g.unique_id, g.text, g.alias, g.type, g.sort_order, parent.unique_id AS parent_key
823
- FROM property_type_group g LEFT JOIN property_type_group parent ON parent.id = g.parent_id
824
- WHERE g.content_type_node_id = ? ORDER BY g.sort_order, g.id`,
825
- [nodeId],
826
- )
915
+ const groupRows = await this.#db.q
916
+ .selectFrom('property_type_group as g')
917
+ .leftJoin('property_type_group as parent', 'parent.id', 'g.parent_id')
918
+ .select([
919
+ 'g.unique_id',
920
+ 'g.text',
921
+ 'g.alias',
922
+ 'g.type',
923
+ 'g.sort_order',
924
+ 'parent.unique_id as parent_key',
925
+ ])
926
+ .where('g.content_type_node_id', '=', nodeId)
927
+ .orderBy('g.sort_order')
928
+ .orderBy('g.id')
929
+ .execute()
827
930
  const containers: PropertyGroupModel[] = groupRows.map((row) => ({
828
931
  key: normaliseUuid(String(row.unique_id)),
829
932
  name: (row.text as string | null) ?? null,
@@ -834,21 +937,40 @@ export class ContentTypeRepository {
834
937
  }))
835
938
 
836
939
  // Retired properties are hidden here; `retiredProperties()` lists them.
837
- const propertyRows = await this.#db.query(
838
- `SELECT p.unique_id, p.alias, p.name, p.description, p.sort_order, p.mandatory, p.mandatory_message,
839
- p.validation_reg_exp, p.validation_reg_exp_message, p.label_on_top, p.variations,
840
- dtn.unique_id AS data_type_key, g.unique_id AS container_key,
841
- p.since_version, ss.version AS since_v, ss.revision AS since_r,
842
- m.member_can_edit, m.member_can_view, m.is_sensitive, p.searchable, p.site_search
843
- FROM property_type p
844
- JOIN node dtn ON dtn.id = p.data_type_id
845
- LEFT JOIN property_type_group g ON g.id = p.property_type_group_id
846
- LEFT JOIN schema_state ss ON ss.id = p.since_state_id
847
- LEFT JOIN member_property_type m ON m.property_type_id = p.id
848
- WHERE p.content_type_id = ? AND p.retired_at IS NULL
849
- ORDER BY p.sort_order, p.id`,
850
- [nodeId],
851
- )
940
+ const propertyRows = await this.#db.q
941
+ .selectFrom('property_type as p')
942
+ .innerJoin('node as dtn', 'dtn.id', 'p.data_type_id')
943
+ .leftJoin('property_type_group as g', 'g.id', 'p.property_type_group_id')
944
+ .leftJoin('schema_state as ss', 'ss.id', 'p.since_state_id')
945
+ .leftJoin('member_property_type as m', 'm.property_type_id', 'p.id')
946
+ .select([
947
+ 'p.unique_id',
948
+ 'p.alias',
949
+ 'p.name',
950
+ 'p.description',
951
+ 'p.sort_order',
952
+ 'p.mandatory',
953
+ 'p.mandatory_message',
954
+ 'p.validation_reg_exp',
955
+ 'p.validation_reg_exp_message',
956
+ 'p.label_on_top',
957
+ 'p.variations',
958
+ 'dtn.unique_id as data_type_key',
959
+ 'g.unique_id as container_key',
960
+ 'p.since_version',
961
+ 'ss.version as since_v',
962
+ 'ss.revision as since_r',
963
+ 'm.member_can_edit',
964
+ 'm.member_can_view',
965
+ 'm.is_sensitive',
966
+ 'p.searchable',
967
+ 'p.site_search',
968
+ ])
969
+ .where('p.content_type_id', '=', nodeId)
970
+ .where('p.retired_at', 'is', null)
971
+ .orderBy('p.sort_order')
972
+ .orderBy('p.id')
973
+ .execute()
852
974
  const properties: PropertyTypeModel[] = propertyRows.map((row) => ({
853
975
  key: normaliseUuid(String(row.unique_id)),
854
976
  alias: String(row.alias),
@@ -877,18 +999,25 @@ export class ContentTypeRepository {
877
999
  : {}),
878
1000
  }))
879
1001
 
880
- const compositionRows = await this.#db.query<{ unique_id: string }>(
881
- 'SELECT parent.unique_id FROM content_type_composition c JOIN node parent ON parent.id = c.parent_content_type_id WHERE c.child_content_type_id = ?',
882
- [nodeId],
883
- )
884
- const allowedRows = await this.#db.query(
885
- 'SELECT child.unique_id, ac.sort_order FROM content_type_allowed_child ac JOIN node child ON child.id = ac.allowed_content_type_id WHERE ac.content_type_id = ? ORDER BY ac.sort_order',
886
- [nodeId],
887
- )
888
- const templateRows = await this.#db.query(
889
- 'SELECT tn.unique_id, ctt.is_default FROM content_type_template ctt JOIN node tn ON tn.id = ctt.template_node_id WHERE ctt.content_type_node_id = ?',
890
- [nodeId],
891
- )
1002
+ const compositionRows = await this.#db.q
1003
+ .selectFrom('content_type_composition as c')
1004
+ .innerJoin('node as parent', 'parent.id', 'c.parent_content_type_id')
1005
+ .select('parent.unique_id')
1006
+ .where('c.child_content_type_id', '=', nodeId)
1007
+ .execute()
1008
+ const allowedRows = await this.#db.q
1009
+ .selectFrom('content_type_allowed_child as ac')
1010
+ .innerJoin('node as child', 'child.id', 'ac.allowed_content_type_id')
1011
+ .select(['child.unique_id', 'ac.sort_order'])
1012
+ .where('ac.content_type_id', '=', nodeId)
1013
+ .orderBy('ac.sort_order')
1014
+ .execute()
1015
+ const templateRows = await this.#db.q
1016
+ .selectFrom('content_type_template as ctt')
1017
+ .innerJoin('node as tn', 'tn.id', 'ctt.template_node_id')
1018
+ .select(['tn.unique_id', 'ctt.is_default'])
1019
+ .where('ctt.content_type_node_id', '=', nodeId)
1020
+ .execute()
892
1021
  const parentNode = parentId > 0 ? await this.#nodes.byId(parentId) : undefined
893
1022
  const flags = Number(type.variations ?? ContentVariation.Nothing)
894
1023
  const num = (value: unknown) => (value === null || value === undefined ? null : Number(value))