Skip to content

Commit fe7660d

Browse files
committed
Refactor DbLabel to new metadata decorator & inline getters into EnhancedResource
1 parent 40c07c0 commit fe7660d

File tree

4 files changed

+21
-65
lines changed

4 files changed

+21
-65
lines changed

src/common/db-label.decorator.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
import { cleanSplit, isNotFalsy } from '@seedcompany/common';
2-
import { uniq } from 'lodash';
3-
4-
export const DbLabelSymbol = Symbol('DbLabelSymbol');
5-
6-
export const DbLabel =
7-
(...labels: string[] | [null]): PropertyDecorator & ClassDecorator =>
8-
(target: any, key?: string | symbol) => {
9-
const prev: string[] =
10-
(key
11-
? Reflect.getMetadata(DbLabelSymbol, target, key)
12-
: Reflect.getMetadata(DbLabelSymbol, target)) ?? [];
13-
14-
const now = uniq(
15-
[
16-
...prev,
17-
// Add labels split by `:`
18-
...labels.flatMap((l) => cleanSplit(l ?? '', ':')),
19-
].filter(isNotFalsy),
20-
);
21-
22-
key
23-
? Reflect.defineMetadata(DbLabelSymbol, now, target, key)
24-
: Reflect.defineMetadata(DbLabelSymbol, now, target);
25-
if (!key) {
26-
return target;
27-
}
28-
};
1+
import { cleanSplit, setOf } from '@seedcompany/common';
2+
import { createMetadataDecorator } from '@seedcompany/nest';
3+
4+
export const DbLabel = createMetadataDecorator({
5+
types: ['class', 'property'],
6+
setter: (...labels: string[] | [null]) =>
7+
setOf(labels.flatMap((label) => cleanSplit(label ?? '', ':'))),
8+
merge: ({ previous, next }) => previous?.union(next) ?? next,
9+
});

src/common/db-label.helpers.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export * from './create-and-inject';
1515
export * from './data-object';
1616
export * from './date-filter.input';
1717
export { DbLabel } from './db-label.decorator';
18-
export * from './db-label.helpers';
1918
export { DbSort } from './db-sort.decorator';
2019
export * from './db-unique.decorator';
2120
export * from './disabled.decorator';

src/common/resource.dto.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { type ScopedRole } from '../components/authorization/dto';
2020
import { CalculatedSymbol } from './calculated.decorator';
2121
import { DataObject } from './data-object';
2222
import { DbLabel } from './db-label.decorator';
23-
import { getDbClassLabels, getDbPropertyLabels } from './db-label.helpers';
2423
import { ServerException } from './exceptions';
2524
import { type ID, IdField } from './id-field';
2625
import { DateTimeField } from './luxon.graphql';
@@ -282,7 +281,14 @@ export class EnhancedResource<T extends ResourceShape<any>> {
282281

283282
@Once()
284283
get dbLabels() {
285-
return getDbClassLabels(this.type);
284+
const labels = getParentTypes(this.type).flatMap((cls) => {
285+
if (!isResourceClass(cls)) {
286+
return [];
287+
}
288+
const declared = DbLabel.getOwn(cls);
289+
return declared ? [...declared] : [cls.name];
290+
});
291+
return [...new Set([...labels, 'BaseNode'])];
286292
}
287293
get dbLabel() {
288294
return this.dbLabels[0];
@@ -291,9 +297,10 @@ export class EnhancedResource<T extends ResourceShape<any>> {
291297
get dbPropLabels(): {
292298
readonly [K in keyof T['prototype'] & string]?: readonly string[];
293299
} {
294-
return mapValues.fromList(this.props, (prop) =>
295-
getDbPropertyLabels(this.type, prop),
296-
).asRecord;
300+
return mapValues.fromList(this.props, (prop) => {
301+
const declared = DbLabel.get(this.type, prop as unknown as string);
302+
return [...new Set([...(declared ?? []), 'Property'])];
303+
}).asRecord;
297304
}
298305
}
299306
setInspectOnClass(EnhancedResource, (res) => ({ collapsed }) => {

0 commit comments

Comments
 (0)