@crombolo/data 0.44.0 → 0.46.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 +2 -2
- package/src/index.ts +1 -0
- package/src/repositories/documents.ts +85 -30
- package/src/repositories/media-access.ts +22 -18
- package/src/repositories/media-listing.ts +34 -0
- package/src/repositories/nodes.ts +87 -13
- package/src/schema/media-tree-order.ts +55 -0
- package/src/schema/plan.ts +2 -0
- package/src/schema/seed.ts +3 -2
- package/src/tables.gen.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.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",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
31
|
+
"@crombolo/core": "0.46.1",
|
|
32
32
|
"kysely": "0.29.6"
|
|
33
33
|
}
|
|
34
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -91,6 +91,7 @@ export * from './schema/media-cache.ts'
|
|
|
91
91
|
export * from './schema/media-member-grants.ts'
|
|
92
92
|
export * from './schema/media-member-uploads.ts'
|
|
93
93
|
export * from './schema/media-permissions.ts'
|
|
94
|
+
export * from './schema/media-tree-order.ts'
|
|
94
95
|
export * from './schema/member-property-type.ts'
|
|
95
96
|
export * from './schema/members.ts'
|
|
96
97
|
export * from './schema/plan.ts'
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
type Page,
|
|
22
22
|
parseMediaPolicy,
|
|
23
23
|
STORAGE_COLUMN,
|
|
24
|
+
SYSTEM_MEDIA_TYPE_KEYS,
|
|
24
25
|
SystemNodes,
|
|
25
26
|
toEditorValue,
|
|
26
27
|
type ValueStorageTypeName,
|
|
@@ -32,6 +33,7 @@ import { DbDate, fromDbBool } from '../dialect.ts'
|
|
|
32
33
|
import { type LockId, Locks } from '../locks.ts'
|
|
33
34
|
import { isPending } from '../pending.ts'
|
|
34
35
|
import { type Insertable, sql } from '../query.ts'
|
|
36
|
+
import { TREE_GROUP_FOLDER, TREE_GROUP_ITEM } from '../schema/media-tree-order.ts'
|
|
35
37
|
import {
|
|
36
38
|
assertNodeMayWrite,
|
|
37
39
|
BASELINE_NODE_STATE,
|
|
@@ -833,6 +835,12 @@ export class DocumentRepository {
|
|
|
833
835
|
objectType: this.objectType,
|
|
834
836
|
text: name,
|
|
835
837
|
userId: input.userId,
|
|
838
|
+
// A media folder comes before the files beside it in the tree (story 246).
|
|
839
|
+
treeGroup:
|
|
840
|
+
this.objectType === ObjectTypes.Media &&
|
|
841
|
+
normaliseUuid(input.contentTypeKey) === SYSTEM_MEDIA_TYPE_KEYS.Folder
|
|
842
|
+
? TREE_GROUP_FOLDER
|
|
843
|
+
: TREE_GROUP_ITEM,
|
|
836
844
|
})
|
|
837
845
|
|
|
838
846
|
await tx.q
|
|
@@ -1454,36 +1462,7 @@ export class DocumentRepository {
|
|
|
1454
1462
|
return this.#db.transaction(async (tx) => {
|
|
1455
1463
|
const node = await new NodeRepository(tx).byKey(key)
|
|
1456
1464
|
if (!node) return false
|
|
1457
|
-
|
|
1458
|
-
.selectFrom('node')
|
|
1459
|
-
.select('id')
|
|
1460
|
-
.where((eb) => eb.or([eb('id', '=', node.id), eb('path', 'like', `${node.path},%`)]))
|
|
1461
|
-
.execute()
|
|
1462
|
-
for (const row of ids.map((r) => Number(r.id)).reverse()) {
|
|
1463
|
-
const versions = tx.q.selectFrom('content_version').select('id').where('node_id', '=', row)
|
|
1464
|
-
await tx.q.deleteFrom('property_value').where('node_id', '=', row).execute()
|
|
1465
|
-
await tx.q.deleteFrom('content_schedule').where('node_id', '=', row).execute()
|
|
1466
|
-
await tx.q.deleteFrom('user_notification').where('node_id', '=', row).execute()
|
|
1467
|
-
await tx.q.deleteFrom('domain').where('node_id', '=', row).execute()
|
|
1468
|
-
await tx.q
|
|
1469
|
-
.deleteFrom('media_access_rule')
|
|
1470
|
-
.where('media_access_id', 'in', (eb) =>
|
|
1471
|
-
eb.selectFrom('media_access').select('id').where('node_id', '=', row),
|
|
1472
|
-
)
|
|
1473
|
-
.execute()
|
|
1474
|
-
await tx.q.deleteFrom('media_access').where('node_id', '=', row).execute()
|
|
1475
|
-
await tx.q.deleteFrom('node_file').where('node_id', '=', row).execute()
|
|
1476
|
-
await tx.q
|
|
1477
|
-
.deleteFrom('content_version_culture_variation')
|
|
1478
|
-
.where('version_id', 'in', versions)
|
|
1479
|
-
.execute()
|
|
1480
|
-
await tx.q.deleteFrom('document_culture_variation').where('node_id', '=', row).execute()
|
|
1481
|
-
await tx.q.deleteFrom('document_version').where('id', 'in', versions).execute()
|
|
1482
|
-
await tx.q.deleteFrom('content_version').where('node_id', '=', row).execute()
|
|
1483
|
-
await tx.q.deleteFrom('document').where('node_id', '=', row).execute()
|
|
1484
|
-
await tx.q.deleteFrom('content').where('node_id', '=', row).execute()
|
|
1485
|
-
await tx.q.deleteFrom('node').where('id', '=', row).execute()
|
|
1486
|
-
}
|
|
1465
|
+
await deleteNodeRows(tx, { branch: node })
|
|
1487
1466
|
// A page deleted from the bin was never served, but an item deleted
|
|
1488
1467
|
// directly may have been: every node is told (story 174).
|
|
1489
1468
|
await this.#child(tx).#instructContent(node.key)
|
|
@@ -1491,6 +1470,16 @@ export class DocumentRepository {
|
|
|
1491
1470
|
})
|
|
1492
1471
|
}
|
|
1493
1472
|
|
|
1473
|
+
/**
|
|
1474
|
+
* Deletes these nodes and their rows, and no others (story 246): a large branch
|
|
1475
|
+
* is deleted a chunk at a time, deepest first, so no transaction holds it all
|
|
1476
|
+
* and a parent always outlasts its children.
|
|
1477
|
+
*/
|
|
1478
|
+
async deleteNodes(ids: readonly number[]): Promise<void> {
|
|
1479
|
+
if (ids.length === 0) return
|
|
1480
|
+
await this.#withTree(() => this.#db.transaction((tx) => deleteNodeRows(tx, { ids })))
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1494
1483
|
/**
|
|
1495
1484
|
* Re-parents a node and its branch under `targetKey` (null: the root), last
|
|
1496
1485
|
* among its new siblings. Refused when the target is the node or below it.
|
|
@@ -2089,6 +2078,9 @@ export class DocumentRepository {
|
|
|
2089
2078
|
join.onRef('pd.id', '=', 'pc.id').on('pd.published', '=', t),
|
|
2090
2079
|
)
|
|
2091
2080
|
.select(['pc.node_id', 'pc.version_date'])
|
|
2081
|
+
// This node's alone: unfiltered, a planner can build it from every
|
|
2082
|
+
// published version in the site before joining one (story 246).
|
|
2083
|
+
.where('pc.node_id', '=', node.id)
|
|
2092
2084
|
.as('pub'),
|
|
2093
2085
|
(join) => join.onRef('pub.node_id', '=', 'n.id'),
|
|
2094
2086
|
)
|
|
@@ -3416,3 +3408,66 @@ export async function latestContentVersionId(db: Db): Promise<number> {
|
|
|
3416
3408
|
.execute()
|
|
3417
3409
|
return Number(rows[0]?.n ?? 0)
|
|
3418
3410
|
}
|
|
3411
|
+
|
|
3412
|
+
/**
|
|
3413
|
+
* Every row a node owns, then the node, for a branch (`branch`: the node and all
|
|
3414
|
+
* below it, by path) or a set of nodes (`ids`), a statement per table over the
|
|
3415
|
+
* whole set rather than one per node (story 246).
|
|
3416
|
+
*/
|
|
3417
|
+
async function deleteNodeRows(
|
|
3418
|
+
tx: Db,
|
|
3419
|
+
among: { branch: Pick<NodeRow, 'id' | 'path'> } | { ids: readonly number[] },
|
|
3420
|
+
): Promise<void> {
|
|
3421
|
+
const nodes = () =>
|
|
3422
|
+
tx.q
|
|
3423
|
+
.selectFrom('node')
|
|
3424
|
+
.select('id')
|
|
3425
|
+
.where((eb) =>
|
|
3426
|
+
'ids' in among
|
|
3427
|
+
? eb('id', 'in', [...among.ids])
|
|
3428
|
+
: eb.or([eb('id', '=', among.branch.id), eb('path', 'like', `${among.branch.path},%`)]),
|
|
3429
|
+
)
|
|
3430
|
+
const versions = () =>
|
|
3431
|
+
tx.q.selectFrom('content_version').select('id').where('node_id', 'in', nodes())
|
|
3432
|
+
await tx.q.deleteFrom('property_value').where('node_id', 'in', nodes()).execute()
|
|
3433
|
+
await tx.q.deleteFrom('content_schedule').where('node_id', 'in', nodes()).execute()
|
|
3434
|
+
await tx.q.deleteFrom('user_notification').where('node_id', 'in', nodes()).execute()
|
|
3435
|
+
await tx.q.deleteFrom('domain').where('node_id', 'in', nodes()).execute()
|
|
3436
|
+
await tx.q
|
|
3437
|
+
.deleteFrom('media_access_rule')
|
|
3438
|
+
.where('media_access_id', 'in', (eb) =>
|
|
3439
|
+
eb.selectFrom('media_access').select('id').where('node_id', 'in', nodes()),
|
|
3440
|
+
)
|
|
3441
|
+
.execute()
|
|
3442
|
+
await tx.q.deleteFrom('media_access').where('node_id', 'in', nodes()).execute()
|
|
3443
|
+
await tx.q.deleteFrom('node_file').where('node_id', 'in', nodes()).execute()
|
|
3444
|
+
await tx.q
|
|
3445
|
+
.deleteFrom('content_version_culture_variation')
|
|
3446
|
+
.where('version_id', 'in', versions())
|
|
3447
|
+
.execute()
|
|
3448
|
+
await tx.q.deleteFrom('document_culture_variation').where('node_id', 'in', nodes()).execute()
|
|
3449
|
+
await tx.q.deleteFrom('document_version').where('id', 'in', versions()).execute()
|
|
3450
|
+
await tx.q.deleteFrom('content_version').where('node_id', 'in', nodes()).execute()
|
|
3451
|
+
await tx.q.deleteFrom('document').where('node_id', 'in', nodes()).execute()
|
|
3452
|
+
await tx.q.deleteFrom('content').where('node_id', 'in', nodes()).execute()
|
|
3453
|
+
if ('ids' in among)
|
|
3454
|
+
await tx.q
|
|
3455
|
+
.deleteFrom('node')
|
|
3456
|
+
.where('id', 'in', [...among.ids])
|
|
3457
|
+
.execute()
|
|
3458
|
+
else
|
|
3459
|
+
await tx.q
|
|
3460
|
+
.deleteFrom('node')
|
|
3461
|
+
.where('id', 'in', (eb) =>
|
|
3462
|
+
eb
|
|
3463
|
+
.selectFrom('node as branch')
|
|
3464
|
+
.select('branch.id')
|
|
3465
|
+
.where((w) =>
|
|
3466
|
+
w.or([
|
|
3467
|
+
w('branch.id', '=', among.branch.id),
|
|
3468
|
+
w('branch.path', 'like', `${among.branch.path},%`),
|
|
3469
|
+
]),
|
|
3470
|
+
),
|
|
3471
|
+
)
|
|
3472
|
+
.execute()
|
|
3473
|
+
}
|
|
@@ -26,7 +26,6 @@ import {
|
|
|
26
26
|
} from '@crombolo/core'
|
|
27
27
|
import type { Db } from '../database.ts'
|
|
28
28
|
import { DbDate, fromDbBool } from '../dialect.ts'
|
|
29
|
-
import { sql } from '../query.ts'
|
|
30
29
|
import { NodeRepository, type NodeRow } from './nodes.ts'
|
|
31
30
|
|
|
32
31
|
export interface MediaAccessGroup {
|
|
@@ -436,7 +435,7 @@ export class MediaAccessRepository {
|
|
|
436
435
|
/**
|
|
437
436
|
* The item whose file is served at a `/media/…` path, its settings and the
|
|
438
437
|
* nearest rule, through the `file_url` index; undefined when none owns it.
|
|
439
|
-
*
|
|
438
|
+
* Indexed lookups: the item, its nearest rule among its ancestors, then that rule.
|
|
440
439
|
*/
|
|
441
440
|
async byUrl(urls: string | readonly string[]): Promise<MediaByUrl | undefined> {
|
|
442
441
|
const wanted = [...new Set(typeof urls === 'string' ? [urls] : urls)]
|
|
@@ -444,7 +443,7 @@ export class MediaAccessRepository {
|
|
|
444
443
|
const row = await this.#db.q
|
|
445
444
|
.selectFrom('content as c')
|
|
446
445
|
.innerJoin('node as n', 'n.id', 'c.node_id')
|
|
447
|
-
.select(
|
|
446
|
+
.select([
|
|
448
447
|
'n.id',
|
|
449
448
|
'n.unique_id',
|
|
450
449
|
'n.path',
|
|
@@ -452,17 +451,6 @@ export class MediaAccessRepository {
|
|
|
452
451
|
'c.read_policy',
|
|
453
452
|
'c.write_policy',
|
|
454
453
|
'c.creator_member_key',
|
|
455
|
-
eb
|
|
456
|
-
.selectFrom('media_access as a')
|
|
457
|
-
.innerJoin('node as an', 'an.id', 'a.node_id')
|
|
458
|
-
.select('a.id')
|
|
459
|
-
.where(
|
|
460
|
-
(inner) =>
|
|
461
|
-
sql<boolean>`(',' || ${inner.ref('n.path')} || ',') like ('%,' || cast(${inner.ref('a.node_id')} as varchar(20)) || ',%')`,
|
|
462
|
-
)
|
|
463
|
-
.orderBy('an.level', 'desc')
|
|
464
|
-
.limit(1)
|
|
465
|
-
.as('rule_id'),
|
|
466
454
|
])
|
|
467
455
|
.where('c.file_url', 'in', wanted)
|
|
468
456
|
.where('n.node_object_type', '=', ObjectTypes.Media)
|
|
@@ -470,10 +458,26 @@ export class MediaAccessRepository {
|
|
|
470
458
|
.limit(1)
|
|
471
459
|
.executeTakeFirst()
|
|
472
460
|
if (!row) return undefined
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
461
|
+
// The nearest rule above it, by the ancestor ids its path already holds: a
|
|
462
|
+
// lookup per ancestor rather than a match against every node (story 246).
|
|
463
|
+
const ancestors = String(row.path)
|
|
464
|
+
.split(',')
|
|
465
|
+
.map(Number)
|
|
466
|
+
.filter((id) => id > 0)
|
|
467
|
+
const nearest =
|
|
468
|
+
ancestors.length === 0
|
|
469
|
+
? undefined
|
|
470
|
+
: await this.#db.q
|
|
471
|
+
.selectFrom('media_access as a')
|
|
472
|
+
.innerJoin('node as an', 'an.id', 'a.node_id')
|
|
473
|
+
.select('a.id')
|
|
474
|
+
.where('a.node_id', 'in', ancestors)
|
|
475
|
+
.orderBy('an.level', 'desc')
|
|
476
|
+
.limit(1)
|
|
477
|
+
.executeTakeFirst()
|
|
478
|
+
const ruleRows = nearest
|
|
479
|
+
? await entries(this.#db).where('a.id', '=', Number(nearest.id)).execute()
|
|
480
|
+
: []
|
|
477
481
|
const entry = (await this.#hydrate(ruleRows))[0]
|
|
478
482
|
return {
|
|
479
483
|
id: Number(row.id),
|
|
@@ -251,6 +251,40 @@ export class MediaListingRepository {
|
|
|
251
251
|
: undefined
|
|
252
252
|
}
|
|
253
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Who made each item's latest version, by key, in one query for a page: the
|
|
256
|
+
* Media section's folder view shows it (story 246). Null where nobody did.
|
|
257
|
+
*/
|
|
258
|
+
async creators(keys: readonly string[]): Promise<Map<string, string | null>> {
|
|
259
|
+
const out = new Map<string, string | null>()
|
|
260
|
+
if (keys.length === 0) return out
|
|
261
|
+
const rows = await this.#db.q
|
|
262
|
+
.selectFrom('node as n')
|
|
263
|
+
.innerJoin('content_version as cv', 'cv.node_id', 'n.id')
|
|
264
|
+
.leftJoin('user_account as u', 'u.id', 'cv.user_id')
|
|
265
|
+
.select(['n.unique_id', 'u.user_name'])
|
|
266
|
+
.where(
|
|
267
|
+
'n.unique_id',
|
|
268
|
+
'in',
|
|
269
|
+
keys.map((key) => normaliseUuid(key)),
|
|
270
|
+
)
|
|
271
|
+
.where((eb) =>
|
|
272
|
+
eb(
|
|
273
|
+
'cv.id',
|
|
274
|
+
'=',
|
|
275
|
+
eb
|
|
276
|
+
.selectFrom('content_version as latest')
|
|
277
|
+
.innerJoin('document_version as dv', 'dv.id', 'latest.id')
|
|
278
|
+
.select((e) => e.fn.max('latest.id').as('id'))
|
|
279
|
+
.whereRef('latest.node_id', '=', 'n.id'),
|
|
280
|
+
),
|
|
281
|
+
)
|
|
282
|
+
.execute()
|
|
283
|
+
for (const row of rows)
|
|
284
|
+
out.set(normaliseUuid(String(row.unique_id)), row.user_name ? String(row.user_name) : null)
|
|
285
|
+
return out
|
|
286
|
+
}
|
|
287
|
+
|
|
254
288
|
/** Each node's name, by id: for the paths of names an answer carries. */
|
|
255
289
|
async names(ids: readonly number[]): Promise<Map<number, string>> {
|
|
256
290
|
const wanted = [...new Set(ids.filter((id) => id > 0))]
|
|
@@ -21,6 +21,8 @@ export interface NodeRow {
|
|
|
21
21
|
text: string | null
|
|
22
22
|
objectType: string | null
|
|
23
23
|
createDate: Date | undefined
|
|
24
|
+
/** 0 for a media folder, which a tree lists before the files beside it (story 246); 1 otherwise. */
|
|
25
|
+
treeGroup: number
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export function mapNode(row: Record<string, unknown>): NodeRow {
|
|
@@ -35,6 +37,7 @@ export function mapNode(row: Record<string, unknown>): NodeRow {
|
|
|
35
37
|
text: (row.text as string | null) ?? null,
|
|
36
38
|
objectType: row.node_object_type ? normaliseUuid(String(row.node_object_type)) : null,
|
|
37
39
|
createDate: DbDate.fromDb(row.create_date),
|
|
40
|
+
treeGroup: row.tree_group === undefined || row.tree_group === null ? 1 : Number(row.tree_group),
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -49,6 +52,7 @@ const NODE_COLUMNS = [
|
|
|
49
52
|
'text',
|
|
50
53
|
'node_object_type',
|
|
51
54
|
'create_date',
|
|
55
|
+
'tree_group',
|
|
52
56
|
] as const
|
|
53
57
|
|
|
54
58
|
export interface CreateNodeInit {
|
|
@@ -58,6 +62,8 @@ export interface CreateNodeInit {
|
|
|
58
62
|
text: string
|
|
59
63
|
userId?: number
|
|
60
64
|
sortOrder?: number
|
|
65
|
+
/** 0 for a media folder (story 246); 1, the default, for everything else. */
|
|
66
|
+
treeGroup?: number
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
export class NodeRepository {
|
|
@@ -119,6 +125,7 @@ export class NodeRepository {
|
|
|
119
125
|
text: init.text,
|
|
120
126
|
node_object_type: init.objectType,
|
|
121
127
|
create_date: now,
|
|
128
|
+
tree_group: init.treeGroup ?? 1,
|
|
122
129
|
})
|
|
123
130
|
.execute()
|
|
124
131
|
const created = await this.byKey(key)
|
|
@@ -159,8 +166,11 @@ export class NodeRepository {
|
|
|
159
166
|
q.where('unique_id', 'not in', excludeKeys.map(normaliseUuid)),
|
|
160
167
|
)
|
|
161
168
|
const totals = await matching.select((eb) => eb.fn.countAll().as('n')).execute()
|
|
169
|
+
// Folders before the files beside them (story 246), each in sort order:
|
|
170
|
+
// `ix_node_parent_tree` pages it without sorting the folder.
|
|
162
171
|
const rows = await matching
|
|
163
172
|
.select(NODE_COLUMNS)
|
|
173
|
+
.orderBy('tree_group')
|
|
164
174
|
.orderBy('sort_order')
|
|
165
175
|
.orderBy('id')
|
|
166
176
|
.limit(take)
|
|
@@ -215,21 +225,16 @@ export class NodeRepository {
|
|
|
215
225
|
.set({ parent_id: newParentId, level: newLevel, path: newPath })
|
|
216
226
|
.where('id', '=', node.id)
|
|
217
227
|
.execute()
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
228
|
+
// The whole subtree in one statement (story 246): each descendant's path keeps
|
|
229
|
+
// what follows the old prefix, so a folder of a million files is one update.
|
|
230
|
+
await this.#db.q
|
|
231
|
+
.updateTable('node')
|
|
232
|
+
.set({
|
|
233
|
+
path: sql<string>`${newPath} || substr(${sql.ref('path')}, ${oldPath.length + 1})`,
|
|
234
|
+
level: sql<number>`${sql.ref('level')} + ${delta}`,
|
|
235
|
+
})
|
|
221
236
|
.where('path', 'like', `${oldPath},%`)
|
|
222
237
|
.execute()
|
|
223
|
-
for (const d of descendants) {
|
|
224
|
-
await this.#db.q
|
|
225
|
-
.updateTable('node')
|
|
226
|
-
.set({
|
|
227
|
-
path: `${newPath}${String(d.path).slice(oldPath.length)}`,
|
|
228
|
-
level: Number(d.level) + delta,
|
|
229
|
-
})
|
|
230
|
-
.where('id', '=', Number(d.id))
|
|
231
|
-
.execute()
|
|
232
|
-
}
|
|
233
238
|
}
|
|
234
239
|
|
|
235
240
|
/** A window of a node's siblings around it: `before` above, `after` below, the node itself included. */
|
|
@@ -242,6 +247,7 @@ export class NodeRepository {
|
|
|
242
247
|
const target = await this.byKey(key)
|
|
243
248
|
if (!target) return undefined
|
|
244
249
|
const types = typeof objectType === 'string' ? [objectType] : [...objectType]
|
|
250
|
+
if (types.length === 1) return this.#siblingWindow(target, types[0] as string, before, after)
|
|
245
251
|
const all = await this.#db.q
|
|
246
252
|
.selectFrom('node')
|
|
247
253
|
.select(NODE_COLUMNS)
|
|
@@ -270,6 +276,74 @@ export class NodeRepository {
|
|
|
270
276
|
return { items: rows.slice(start, end), totalBefore: start, totalAfter: rows.length - end }
|
|
271
277
|
}
|
|
272
278
|
|
|
279
|
+
/**
|
|
280
|
+
* The window round a node among its siblings, in the tree's order (folders,
|
|
281
|
+
* then sort order, then id), read a window at a time: a folder of a million
|
|
282
|
+
* files is never read whole to open the tree at one of them (story 246).
|
|
283
|
+
*/
|
|
284
|
+
async #siblingWindow(
|
|
285
|
+
target: NodeRow,
|
|
286
|
+
objectType: string,
|
|
287
|
+
before: number,
|
|
288
|
+
after: number,
|
|
289
|
+
): Promise<{ items: NodeRow[]; totalBefore: number; totalAfter: number }> {
|
|
290
|
+
const { treeGroup: group, sortOrder: sort, id } = target
|
|
291
|
+
const siblings = this.#db.q
|
|
292
|
+
.selectFrom('node')
|
|
293
|
+
.where('parent_id', '=', target.parentId)
|
|
294
|
+
.where('node_object_type', '=', objectType)
|
|
295
|
+
.where('trashed', '=', this.#db.dialect.boolValue(target.trashed))
|
|
296
|
+
const earlier = siblings.where((eb) =>
|
|
297
|
+
eb.or([
|
|
298
|
+
eb('tree_group', '<', group),
|
|
299
|
+
eb.and([
|
|
300
|
+
eb('tree_group', '=', group),
|
|
301
|
+
eb.or([
|
|
302
|
+
eb('sort_order', '<', sort),
|
|
303
|
+
eb.and([eb('sort_order', '=', sort), eb('id', '<', id)]),
|
|
304
|
+
]),
|
|
305
|
+
]),
|
|
306
|
+
]),
|
|
307
|
+
)
|
|
308
|
+
const later = siblings.where((eb) =>
|
|
309
|
+
eb.or([
|
|
310
|
+
eb('tree_group', '>', group),
|
|
311
|
+
eb.and([
|
|
312
|
+
eb('tree_group', '=', group),
|
|
313
|
+
eb.or([
|
|
314
|
+
eb('sort_order', '>', sort),
|
|
315
|
+
eb.and([eb('sort_order', '=', sort), eb('id', '>', id)]),
|
|
316
|
+
]),
|
|
317
|
+
]),
|
|
318
|
+
]),
|
|
319
|
+
)
|
|
320
|
+
const count = async (q: typeof siblings) =>
|
|
321
|
+
Number((await q.select((eb) => eb.fn.countAll().as('n')).execute())[0]?.n ?? 0)
|
|
322
|
+
const [above, below, countAbove, countBelow] = await Promise.all([
|
|
323
|
+
earlier
|
|
324
|
+
.select(NODE_COLUMNS)
|
|
325
|
+
.orderBy('tree_group', 'desc')
|
|
326
|
+
.orderBy('sort_order', 'desc')
|
|
327
|
+
.orderBy('id', 'desc')
|
|
328
|
+
.limit(before)
|
|
329
|
+
.execute(),
|
|
330
|
+
later
|
|
331
|
+
.select(NODE_COLUMNS)
|
|
332
|
+
.orderBy('tree_group')
|
|
333
|
+
.orderBy('sort_order')
|
|
334
|
+
.orderBy('id')
|
|
335
|
+
.limit(after)
|
|
336
|
+
.execute(),
|
|
337
|
+
count(earlier),
|
|
338
|
+
count(later),
|
|
339
|
+
])
|
|
340
|
+
return {
|
|
341
|
+
items: [...above.reverse().map(mapNode), target, ...below.map(mapNode)],
|
|
342
|
+
totalBefore: countAbove - above.length,
|
|
343
|
+
totalAfter: countBelow - below.length,
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
273
347
|
/**
|
|
274
348
|
* Name search, case-insensitive, optionally under a parent. With `fullText`,
|
|
275
349
|
* an item also matches when the search index holds every word of the query by
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 062: the Media tree, folders first (story 246). `node.tree_group` is
|
|
3
|
+
* 0 for a media folder and 1 for everything else, and an index pages a folder's
|
|
4
|
+
* children in that order, then by sort order, so a folder of a million files
|
|
5
|
+
* lists its sub-folders before them without sorting the folder in full.
|
|
6
|
+
*/
|
|
7
|
+
import { ObjectTypes, SYSTEM_MEDIA_TYPE_KEYS } from '@crombolo/core'
|
|
8
|
+
import type { Db } from '../database.ts'
|
|
9
|
+
import type { Migration } from '../migrations.ts'
|
|
10
|
+
import { schemaOps } from '../schema-ops.ts'
|
|
11
|
+
import { STATE_REMOTE_TRUST } from './remote-trust.ts'
|
|
12
|
+
|
|
13
|
+
export const STATE_MEDIA_TREE_ORDER = '{c3e7a512-0062-4f0b-9d61-a8b2c4e60062}'
|
|
14
|
+
|
|
15
|
+
/** A media folder's `tree_group`: it comes before the files beside it. */
|
|
16
|
+
export const TREE_GROUP_FOLDER = 0
|
|
17
|
+
/** Everything else's. */
|
|
18
|
+
export const TREE_GROUP_ITEM = 1
|
|
19
|
+
|
|
20
|
+
export const mediaTreeOrderMigration: Migration = {
|
|
21
|
+
from: STATE_REMOTE_TRUST,
|
|
22
|
+
to: STATE_MEDIA_TREE_ORDER,
|
|
23
|
+
name: 'AddMediaTreeOrder',
|
|
24
|
+
kind: 'expand',
|
|
25
|
+
release: '0.46.0',
|
|
26
|
+
async up(db: Db) {
|
|
27
|
+
await schemaOps(db).addColumn(
|
|
28
|
+
'node',
|
|
29
|
+
'tree_group',
|
|
30
|
+
`${db.dialect.types.integer} NOT NULL DEFAULT ${TREE_GROUP_ITEM}`,
|
|
31
|
+
)
|
|
32
|
+
const folderType = db.q
|
|
33
|
+
.selectFrom('node')
|
|
34
|
+
.select('id')
|
|
35
|
+
.where('unique_id', '=', SYSTEM_MEDIA_TYPE_KEYS.Folder)
|
|
36
|
+
await db.q
|
|
37
|
+
.updateTable('node')
|
|
38
|
+
.set({ tree_group: TREE_GROUP_FOLDER })
|
|
39
|
+
.where('node_object_type', '=', ObjectTypes.Media)
|
|
40
|
+
.where(
|
|
41
|
+
'id',
|
|
42
|
+
'in',
|
|
43
|
+
db.q.selectFrom('content').select('node_id').where('content_type_id', 'in', folderType),
|
|
44
|
+
)
|
|
45
|
+
.execute()
|
|
46
|
+
await schemaOps(db).addIndex('ix_node_parent_tree', 'node', [
|
|
47
|
+
'parent_id',
|
|
48
|
+
'node_object_type',
|
|
49
|
+
'trashed',
|
|
50
|
+
'tree_group',
|
|
51
|
+
'sort_order',
|
|
52
|
+
'id',
|
|
53
|
+
])
|
|
54
|
+
},
|
|
55
|
+
}
|
package/src/schema/plan.ts
CHANGED
|
@@ -51,6 +51,7 @@ import { mediaCacheMigration } from './media-cache.ts'
|
|
|
51
51
|
import { mediaMemberGrantsMigration } from './media-member-grants.ts'
|
|
52
52
|
import { mediaMemberUploadsMigration } from './media-member-uploads.ts'
|
|
53
53
|
import { mediaPermissionsMigration } from './media-permissions.ts'
|
|
54
|
+
import { mediaTreeOrderMigration } from './media-tree-order.ts'
|
|
54
55
|
import { memberPropertyTypeMigration } from './member-property-type.ts'
|
|
55
56
|
import { membersMigration } from './members.ts'
|
|
56
57
|
import { propertySearchableMigration } from './property-searchable.ts'
|
|
@@ -137,4 +138,5 @@ export const cromboloPlan = new MigrationPlan([
|
|
|
137
138
|
formLibraryMigration,
|
|
138
139
|
bundlesInSettingsMigration,
|
|
139
140
|
remoteTrustMigration,
|
|
141
|
+
mediaTreeOrderMigration,
|
|
140
142
|
])
|
package/src/schema/seed.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Password hashing is injected rather than imported so this package stays free of
|
|
5
5
|
* a dependency on @crombolo/auth.
|
|
6
6
|
*/
|
|
7
|
-
import { MediaPermissions, RemotePermissions } from '@crombolo/core'
|
|
7
|
+
import { EXPLICIT_PERMISSIONS, MediaPermissions, RemotePermissions } from '@crombolo/core'
|
|
8
8
|
import type { Db } from '../database.ts'
|
|
9
9
|
import { DbDate } from '../dialect.ts'
|
|
10
10
|
|
|
@@ -69,7 +69,8 @@ export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
|
|
|
69
69
|
'Umb.Document.PropertyValue.Write',
|
|
70
70
|
...FORM_FULL,
|
|
71
71
|
...ELEMENT_EDIT,
|
|
72
|
-
|
|
72
|
+
// Never one a group has to name (story 241): administrators are not given it.
|
|
73
|
+
...Object.values(RemotePermissions).filter((verb) => !EXPLICIT_PERMISSIONS.has(verb)),
|
|
73
74
|
...MEDIA_FULL,
|
|
74
75
|
],
|
|
75
76
|
editor: [
|