Skip to content

Commit 8897d85

Browse files
committed
Merge branch 'develop'
2 parents f0b8bcc + f464fc3 commit 8897d85

File tree

7 files changed

+40
-29
lines changed

7 files changed

+40
-29
lines changed

src/common/db-label.decorator.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1+
import { compact, uniq } from 'lodash';
2+
13
export const DbLabelSymbol = Symbol('DbLabelSymbol');
24

35
export const DbLabel =
4-
(...labels: string[]): PropertyDecorator & ClassDecorator =>
6+
(...labels: string[] | [null]): PropertyDecorator & ClassDecorator =>
57
(target: any, key?: string | symbol) => {
8+
const prev: string[] =
9+
(key
10+
? Reflect.getMetadata(DbLabelSymbol, target, key)
11+
: Reflect.getMetadata(DbLabelSymbol, target)) ?? [];
12+
13+
const now = uniq(
14+
compact([
15+
...prev,
16+
...labels.flatMap((l) => l?.split(':').map((s) => s.trim())),
17+
]),
18+
);
19+
20+
key
21+
? Reflect.defineMetadata(DbLabelSymbol, now, target, key)
22+
: Reflect.defineMetadata(DbLabelSymbol, now, target);
623
if (!key) {
7-
const current = Reflect.getMetadata(DbLabelSymbol, target) ?? [];
8-
Reflect.defineMetadata(DbLabelSymbol, [...current, ...labels], target);
924
return target;
1025
}
11-
const current = Reflect.getMetadata(DbLabelSymbol, target, key) ?? [];
12-
Reflect.defineMetadata(DbLabelSymbol, [...current, ...labels], target, key);
1326
};

src/common/db-label.helpers.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
import { uniq } from 'lodash';
2+
import { AbstractClass } from 'type-fest';
23
import { DbLabelSymbol } from './db-label.decorator';
34
import { getParentTypes } from './parent-types';
45
import { isResourceClass } from './resource.dto';
5-
import { AbstractClassType } from './types';
66

7-
export const getDbPropertyLabels = (
8-
type: AbstractClassType<unknown>,
9-
property: string,
10-
) => {
7+
type Class = AbstractClass<any>;
8+
9+
export const getDbPropertyLabels = (type: Class, property: string) => {
1110
const labels: string[] | undefined = Reflect.getMetadata(
1211
DbLabelSymbol,
1312
type.prototype,
1413
property,
1514
);
16-
const normalized = labels?.flatMap((l) => l.split(':'));
17-
return uniq([...(normalized ?? []), 'Property']);
15+
return uniq([...(labels ?? []), 'Property']);
1816
};
1917

20-
export const getDbClassLabels = (
21-
type: AbstractClassType<unknown>,
22-
): readonly string[] => {
18+
export const getDbClassLabels = (type: Class): readonly string[] => {
2319
const labels = getParentTypes(type)
2420
.filter(isResourceClass)
2521
.flatMap(getDbClassOwnLabels);
2622
return uniq([...labels, 'BaseNode']);
2723
};
2824

29-
const getDbClassOwnLabels = (
30-
type: AbstractClassType<unknown>,
31-
): readonly string[] => {
25+
const getDbClassOwnLabels = (type: Class): readonly string[] => {
3226
const decorated: string[] | null = Reflect.getOwnMetadata(
3327
DbLabelSymbol,
3428
type,
3529
);
36-
return decorated?.flatMap((l) => l.split(':')) ?? [type.name];
30+
return decorated ?? [type.name];
3731
};

src/components/file/dto/node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { MergeExclusive, Opaque } from 'type-fest';
77
import { RegisterResource } from '~/core/resources';
88
import {
99
DateTimeField,
10+
DbLabel,
1011
ID,
1112
InputException,
1213
NameField,
@@ -77,6 +78,7 @@ export { FileNode as IFileNode, AnyFileNode as FileNode };
7778
@ObjectType({
7879
isAbstract: true,
7980
})
81+
@DbLabel(null)
8082
/**
8183
* Both file and file version have these properties
8284
*/

src/components/file/media/media.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const resolveMedia = (val: Pick<AnyMedia, '__typename'>) => {
3535
@InputType()
3636
@InterfaceType({ isAbstract: true })
3737
@ObjectType({ isAbstract: true })
38-
@DbLabel(/* none */)
38+
@DbLabel(null)
3939
export class MediaUserMetadata extends DataObject {
4040
static readonly Props: string[] = keysOf<MediaUserMetadata>();
4141

src/components/file/media/media.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class MediaRepository extends CommonRepository {
115115
},
116116
true,
117117
)
118+
.with('node, fv')
118119
.apply(this.hydrate());
119120

120121
const result = await query.first();

src/core/database/query/create-node.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { DateTime } from 'luxon';
33
import {
44
// eslint-disable-next-line @seedcompany/no-unused-vars -- used in jsdoc
55
DbLabel,
6+
EnhancedResource,
67
entries,
78
generateId,
8-
getDbClassLabels,
9-
getDbPropertyLabels,
109
ID,
1110
ResourceShape,
1211
UnsecuredDto,
@@ -47,9 +46,10 @@ type InitialPropsOf<T> = {
4746
* })
4847
*/
4948
export const createNode = async <TResourceStatic extends ResourceShape<any>>(
50-
resource: TResourceStatic,
49+
resource: TResourceStatic | EnhancedResource<TResourceStatic>,
5150
{ initialProps = {}, baseNodeProps = {} }: CreateNodeOptions<TResourceStatic>,
5251
) => {
52+
const res = EnhancedResource.of(resource);
5353
const {
5454
id = baseNodeProps.id ?? (await generateId()),
5555
createdAt = baseNodeProps.createdAt ?? DateTime.local(),
@@ -65,7 +65,7 @@ export const createNode = async <TResourceStatic extends ResourceShape<any>>(
6565
sub
6666
.create([
6767
[
68-
node('node', getDbClassLabels(resource), {
68+
node('node', res.dbLabels, {
6969
...baseNodeProps,
7070
createdAt,
7171
id,
@@ -74,7 +74,7 @@ export const createNode = async <TResourceStatic extends ResourceShape<any>>(
7474
...entries(restInitialProps).map(([prop, value]) => [
7575
node('node'),
7676
relation('out', '', prop, { active: true, createdAt }),
77-
node('', getDbPropertyLabels(resource, prop), {
77+
node('', res.dbPropLabels[prop] ?? ['Property'], {
7878
createdAt,
7979
value,
8080
}),

src/core/database/query/create-relationships.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { node, Query, relation } from 'cypher-query-builder';
33
import { RelationDirection } from 'cypher-query-builder/dist/typings/clauses/relation-pattern';
44
import { Maybe as Nullable } from 'graphql/jsutils/Maybe';
55
import { DateTime } from 'luxon';
6-
import { ID, many, ResourceShape } from '~/common';
6+
import { EnhancedResource, ID, many, ResourceShape } from '~/common';
77
import { ResourceMap } from '~/core';
88
import { Variable } from '../query-augmentation/condition-variables';
99

@@ -74,19 +74,20 @@ type AnyDirectionalDefinition = Partial<
7474
* // Note how `user` is imported, not matched, and not returned.
7575
*/
7676
export function createRelationships<TResourceStatic extends ResourceShape<any>>(
77-
resource: TResourceStatic,
77+
resource: TResourceStatic | EnhancedResource<TResourceStatic>,
7878
direction: RelationDirection,
7979
labelsToRelationships: RelationshipDefinition,
8080
): (query: Query) => Query;
8181
export function createRelationships<TResourceStatic extends ResourceShape<any>>(
82-
resource: TResourceStatic,
82+
resource: TResourceStatic | EnhancedResource<TResourceStatic>,
8383
definition: AnyDirectionalDefinition,
8484
): (query: Query) => Query;
8585
export function createRelationships<TResourceStatic extends ResourceShape<any>>(
86-
resource: TResourceStatic,
86+
resource: TResourceStatic | EnhancedResource<TResourceStatic>,
8787
directionOrDefinition: RelationDirection | AnyDirectionalDefinition,
8888
maybeLabelsToRelationships?: RelationshipDefinition,
8989
) {
90+
resource = EnhancedResource.of(resource);
9091
const normalizedArgs =
9192
typeof directionOrDefinition === 'string'
9293
? { [directionOrDefinition]: maybeLabelsToRelationships }

0 commit comments

Comments
 (0)