@crombolo/content 0.37.0 → 0.39.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/content",
3
- "version": "0.37.0",
3
+ "version": "0.39.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The rules every page and media write follows, whoever starts it: who may, where it may go, approvals, and what it announces. No transport.",
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.37.0",
33
- "@crombolo/data": "0.37.0"
32
+ "@crombolo/core": "0.39.0",
33
+ "@crombolo/data": "0.39.0"
34
34
  }
35
35
  }
@@ -226,9 +226,11 @@ export function createMediaOperations(options: MediaOperationsOptions) {
226
226
 
227
227
  const userIdOf = async (actor: MediaActor): Promise<number | undefined> => {
228
228
  if (actor.kind !== 'user') return undefined
229
- const rows = await db.query<{ id: number }>('SELECT id FROM user_account WHERE key = ?', [
230
- actor.principal.id,
231
- ])
229
+ const rows = await db.q
230
+ .selectFrom('user_account')
231
+ .select('id')
232
+ .where('key', '=', actor.principal.id)
233
+ .execute()
232
234
  return rows[0] ? Number(rows[0].id) : undefined
233
235
  }
234
236
 
@@ -252,10 +254,12 @@ export function createMediaOperations(options: MediaOperationsOptions) {
252
254
  const childKeys = async (parentKey: string | null): Promise<string[]> => {
253
255
  const parentId = parentKey ? (await nodes.byKey(parentKey))?.id : SystemNodes.Root
254
256
  if (parentId === undefined) return []
255
- const rows = await db.query<{ unique_id: string }>(
256
- 'SELECT unique_id FROM node WHERE parent_id = ? AND node_object_type = ?',
257
- [parentId, ObjectTypes.Media],
258
- )
257
+ const rows = await db.q
258
+ .selectFrom('node')
259
+ .select('unique_id')
260
+ .where('parent_id', '=', parentId)
261
+ .where('node_object_type', '=', ObjectTypes.Media)
262
+ .execute()
259
263
  return rows.map((row) => normaliseUuid(String(row.unique_id)))
260
264
  }
261
265
 
@@ -308,20 +312,24 @@ export function createMediaOperations(options: MediaOperationsOptions) {
308
312
  const placement = async (actor: MediaActor, typeKey: string, parentKey: string | null) =>
309
313
  actor.kind === 'system' ? undefined : types.placementRefusal(typeKey, parentKey)
310
314
  const typeOf = async (key: string): Promise<string | undefined> => {
311
- const rows = await db.query<{ unique_id: string }>(
312
- `SELECT t.unique_id FROM node n JOIN content c ON c.node_id = n.id
313
- JOIN node t ON t.id = c.content_type_id WHERE n.unique_id = ?`,
314
- [normaliseUuid(key)],
315
- )
315
+ const rows = await db.q
316
+ .selectFrom('node as n')
317
+ .innerJoin('content as c', 'c.node_id', 'n.id')
318
+ .innerJoin('node as t', 't.id', 'c.content_type_id')
319
+ .select('t.unique_id')
320
+ .where('n.unique_id', '=', normaliseUuid(key))
321
+ .execute()
316
322
  return rows[0] ? normaliseUuid(String(rows[0].unique_id)) : undefined
317
323
  }
318
324
 
319
325
  /** The files a branch holds, read before its rows go. */
320
326
  const filesIn = async (root: NodeRow): Promise<string[]> => {
321
- const rows = await db.query<{ unique_id: string }>(
322
- 'SELECT unique_id FROM node WHERE (id = ? OR path LIKE ?) AND node_object_type = ?',
323
- [root.id, `${root.path},%`, ObjectTypes.Media],
324
- )
327
+ const rows = await db.q
328
+ .selectFrom('node')
329
+ .select('unique_id')
330
+ .where((eb) => eb.or([eb('id', '=', root.id), eb('path', 'like', `${root.path},%`)]))
331
+ .where('node_object_type', '=', ObjectTypes.Media)
332
+ .execute()
325
333
  const found: string[] = []
326
334
  for (const row of rows) {
327
335
  const media = await repo.byKey(normaliseUuid(String(row.unique_id)))
package/src/operations.ts CHANGED
@@ -25,6 +25,7 @@ import {
25
25
  PublishBlockedError,
26
26
  type SetDomainsResult,
27
27
  StaleRevisionError,
28
+ sql,
28
29
  WriteRejectedError,
29
30
  } from '@crombolo/data'
30
31
  import type { Actor, ContentPrincipal } from './actor.ts'
@@ -226,9 +227,11 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
226
227
  /** The version author: the user's row, when the actor is one. */
227
228
  const userIdOf = async (actor: Actor): Promise<number | undefined> => {
228
229
  if (actor.kind !== 'user') return undefined
229
- const rows = await db.query<{ id: number }>('SELECT id FROM user_account WHERE key = ?', [
230
- actor.principal.id,
231
- ])
230
+ const rows = await db.q
231
+ .selectFrom('user_account')
232
+ .select('id')
233
+ .where('key', '=', actor.principal.id)
234
+ .execute()
232
235
  return rows[0] ? Number(rows[0].id) : undefined
233
236
  }
234
237
 
@@ -313,16 +316,22 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
313
316
  const propertiesOfType = async (contentTypeKey: string) => {
314
317
  const type = await nodes.byKey(contentTypeKey)
315
318
  if (!type) return undefined
316
- const rows = await db.query<{ alias: string; unique_id: string; name: string }>(
317
- `WITH RECURSIVE ancestry(id) AS (
318
- SELECT ?
319
- UNION
320
- SELECT c.parent_content_type_id FROM content_type_composition c JOIN ancestry a ON a.id = c.child_content_type_id
321
- )
322
- SELECT p.alias, p.unique_id, p.name FROM property_type p
323
- WHERE p.content_type_id IN (SELECT id FROM ancestry) AND p.retired_at IS NULL`,
324
- [type.id],
325
- )
319
+ const rows = await db.q
320
+ .withRecursive('ancestry(id)', (qb) =>
321
+ qb
322
+ .selectNoFrom(sql<number>`${type.id}`.as('id'))
323
+ .union(
324
+ qb
325
+ .selectFrom('content_type_composition as c')
326
+ .innerJoin('ancestry as a', 'a.id', 'c.child_content_type_id')
327
+ .select('c.parent_content_type_id as id'),
328
+ ),
329
+ )
330
+ .selectFrom('property_type as p')
331
+ .select(['p.alias', 'p.unique_id', 'p.name'])
332
+ .where('p.content_type_id', 'in', (eb) => eb.selectFrom('ancestry').select('id'))
333
+ .where('p.retired_at', 'is', null)
334
+ .execute()
326
335
  return rows.map((row) => ({
327
336
  alias: String(row.alias),
328
337
  key: normaliseUuid(String(row.unique_id)),
@@ -336,11 +345,13 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
336
345
  ? undefined
337
346
  : types.placementRefusal(typeKey, parentKey)
338
347
  const typeOf = async (key: string): Promise<string | undefined> => {
339
- const rows = await db.query<{ unique_id: string }>(
340
- `SELECT t.unique_id FROM node n JOIN content c ON c.node_id = n.id
341
- JOIN node t ON t.id = c.content_type_id WHERE n.unique_id = ?`,
342
- [normaliseUuid(key)],
343
- )
348
+ const rows = await db.q
349
+ .selectFrom('node as n')
350
+ .innerJoin('content as c', 'c.node_id', 'n.id')
351
+ .innerJoin('node as t', 't.id', 'c.content_type_id')
352
+ .select('t.unique_id')
353
+ .where('n.unique_id', '=', normaliseUuid(key))
354
+ .execute()
344
355
  return rows[0] ? normaliseUuid(String(rows[0].unique_id)) : undefined
345
356
  }
346
357
  const misplacedAt = async (actor: Actor, key: string, parentKey: string | null) => {
@@ -888,10 +899,13 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
888
899
  const root = await placeOf(key)
889
900
  if (!root) return refuse('notFound', 404, NOT_FOUND)
890
901
  if (actor.kind === 'user') {
891
- const below = await db.query<{ unique_id: string }>(
892
- 'SELECT unique_id FROM node WHERE id = ? OR path LIKE ?',
893
- [root.node.id, `${root.node.path},%`],
894
- )
902
+ const below = await db.q
903
+ .selectFrom('node')
904
+ .select('unique_id')
905
+ .where((eb) =>
906
+ eb.or([eb('id', '=', root.node.id), eb('path', 'like', `${root.node.path},%`)]),
907
+ )
908
+ .execute()
895
909
  for (const row of below)
896
910
  if (!(await permits(actor, 'publish', normaliseUuid(String(row.unique_id)))))
897
911
  return forbidden()