Skip to content

Commit 76d83c5

Browse files
committed
Replace DbUnique with the new metadata decorator
1 parent bd34399 commit 76d83c5

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/common/db-unique.decorator.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1+
import { createMetadataDecorator } from '@seedcompany/nest';
12
import { startCase } from 'lodash';
23
import { DbLabel } from './db-label.decorator';
3-
import { AbstractClassType } from './types';
4-
5-
const DbUniqueSymbol = Symbol('DbUnique');
64

75
/**
8-
* This property value should have a unique constraint in database.
9-
* The property node needs a unique label, which can be given or will based on
6+
* This property value should have a unique constraint in the neo4j database.
7+
* The property node needs a unique label, which can be given or will be based on
108
* the resource & property name.
119
*/
12-
export const DbUnique =
13-
(label?: string): PropertyDecorator =>
14-
(target, propertyKey) => {
15-
if (typeof propertyKey === 'symbol') {
16-
throw new Error('DbUnique() cannot be used on symbol properties');
17-
}
18-
label ??= target.constructor.name + startCase(propertyKey);
19-
Reflect.defineMetadata(DbUniqueSymbol, label, target, propertyKey);
20-
DbLabel(label)(target, propertyKey);
21-
};
10+
export const DbUnique = (label?: string) => (target: object, key: string) => {
11+
label ??= target.constructor.name + startCase(key);
12+
DbUniqueInner(label)(target, key);
13+
DbLabel(label)(target, key);
14+
};
15+
16+
const DbUniqueInner = createMetadataDecorator({
17+
types: ['property'],
18+
setter: (label?: string) => label,
19+
});
2220

23-
export const getDbPropertyUnique = (
24-
type: AbstractClassType<unknown>,
25-
property: string,
26-
): string | undefined =>
27-
Reflect.getMetadata(DbUniqueSymbol, type.prototype, property);
21+
DbUnique.get = DbUniqueInner.get;

src/core/database/common.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { setOf } from '@seedcompany/common';
33
import { inArray, node, relation } from 'cypher-query-builder';
44
import { DateTime } from 'luxon';
55
import {
6+
DbUnique,
67
EnhancedResource,
78
getDbClassLabels,
8-
getDbPropertyUnique,
99
ID,
1010
InputException,
1111
isIdLike,
@@ -175,7 +175,7 @@ export class CommonRepository {
175175
? [createUniqueConstraint(getDbClassLabels(resource)[0], 'id')]
176176
: []),
177177
...resource.Props.flatMap((prop) => {
178-
const label = getDbPropertyUnique(resource, prop);
178+
const label = DbUnique.get(resource, prop);
179179
return label
180180
? createUniqueConstraint(label, 'value', `${resource.name}_${prop}`)
181181
: [];

src/core/database/dto.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { inArray, Query } from 'cypher-query-builder';
33
import { LazyGetter as Once } from 'lazy-get-decorator';
44
import { lowerCase } from 'lodash';
55
import {
6+
DbUnique,
67
EnhancedResource,
7-
getDbPropertyUnique,
88
ID,
99
NotFoundException,
1010
ResourceShape,
@@ -66,7 +66,7 @@ export const DtoRepository = <
6666
}
6767
@Once() private get uniqueLabel() {
6868
const labels = resource.Props.flatMap(
69-
(p) => getDbPropertyUnique(resource, p) ?? [],
69+
(p) => DbUnique.get(resource, p) ?? [],
7070
);
7171
if (labels.length === 0) {
7272
return new ServerException(

0 commit comments

Comments
 (0)