|
| 1 | +import { createMetadataDecorator } from '@seedcompany/nest'; |
1 | 2 | import { startCase } from 'lodash';
|
2 | 3 | import { DbLabel } from './db-label.decorator';
|
3 |
| -import { AbstractClassType } from './types'; |
4 |
| - |
5 |
| -const DbUniqueSymbol = Symbol('DbUnique'); |
6 | 4 |
|
7 | 5 | /**
|
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 |
10 | 8 | * the resource & property name.
|
11 | 9 | */
|
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 | +}); |
22 | 20 |
|
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; |
0 commit comments