Skip to content

Commit 0f6c558

Browse files
committed
Add the root node and resource attached to root node to hydrated FileNode results
1 parent ddbc471 commit 0f6c558

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/components/file/dto/node.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DateTime } from 'luxon';
44
import { Readable } from 'stream';
55
import { keys as keysOf } from 'ts-transformer-keys';
66
import { MergeExclusive, Opaque } from 'type-fest';
7+
import { BaseNode } from '~/core/database/results';
78
import { RegisterResource } from '~/core/resources';
89
import {
910
DateTimeField,
@@ -69,6 +70,12 @@ abstract class FileNode extends Resource {
6970
readonly public: boolean;
7071

7172
readonly createdById: ID;
73+
74+
/** The root FileNode. This could be self */
75+
readonly root: BaseNode;
76+
77+
/** The resource the root FileNode is attached to */
78+
readonly rootAttachedTo: [resource: BaseNode, relationName: string];
7279
}
7380

7481
// class name has to match schema name for interface resolvers to work.

src/components/file/file.repository.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'cypher-query-builder';
1212
import { Direction } from 'cypher-query-builder/dist/typings/clauses/order-by';
1313
import { AnyConditions } from 'cypher-query-builder/dist/typings/clauses/where-utils';
14-
import { isEmpty } from 'lodash';
14+
import { isEmpty, keyBy } from 'lodash';
1515
import { DateTime } from 'luxon';
1616
import {
1717
ID,
@@ -174,7 +174,27 @@ export class FileRepository extends CommonRepository {
174174
.where({ node: hasLabel(FileNodeType.Directory) })
175175
.apply(this.hydrateDirectory()),
176176
)
177-
.return<{ dto: FileNode }>('dto');
177+
.subQuery('node', (sub) =>
178+
sub
179+
.raw('MATCH p=(node)-[:parent*0..]->(root:FileNode)')
180+
.return('root')
181+
.orderBy('length(p)', 'DESC')
182+
.raw('LIMIT 1'),
183+
)
184+
.subQuery('root', (sub) =>
185+
sub
186+
.raw('MATCH (resource:BaseNode)-[rel]->(root)')
187+
// Need to filter out FileNodes which are children of this dir
188+
// (the schema was mistakenly pointing these relationships in the wrong direction)
189+
// Also filter to ACTIVE, if applicable.
190+
.raw(
191+
'WHERE NOT resource:FileNode AND coalesce(rel.active, true) <> false',
192+
)
193+
.return('[resource, type(rel)] as rootAttachedTo'),
194+
)
195+
.return<{ dto: FileNode }>(
196+
merge('dto', keyBy(['root', 'rootAttachedTo'])).as('dto'),
197+
);
178198
}
179199

180200
private hydrateFile() {

0 commit comments

Comments
 (0)