Skip to content

Commit c892bd1

Browse files
committed
Refactor DbLabel to normalize on decorator use and to allow single null
This single null better conveys the intent of no label compared to no args given.
1 parent fda6184 commit c892bd1

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
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/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

0 commit comments

Comments
 (0)