1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {PropertyDecoratorFactory} from '@loopback/core';
|
7 | import {buildModelDefinition} from '../decorators';
|
8 | import {Model, RelationDefinitionMap} from '../model';
|
9 | import {RelationType} from './relation.types';
|
10 |
|
11 | export const RELATIONS_KEY = 'loopback:relations';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function relation(definition?: Object) {
|
19 |
|
20 | return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, definition, {
|
21 | decoratorName: '@relation',
|
22 | });
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export function getModelRelations(
|
32 | modelCtor: typeof Model,
|
33 | ): RelationDefinitionMap {
|
34 |
|
35 | const modelDef = buildModelDefinition(modelCtor);
|
36 | return modelDef?.relations || {};
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function embedsOne(definition?: Object) {
|
50 | const rel = Object.assign({type: RelationType.embedsOne}, definition);
|
51 | return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
52 | decoratorName: '@embedsOne',
|
53 | });
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export function embedsMany(definition?: Object) {
|
62 | const rel = Object.assign({type: RelationType.embedsMany}, definition);
|
63 | return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
64 | decoratorName: '@embedsMany',
|
65 | });
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | export function referencesOne(definition?: Object) {
|
74 | const rel = Object.assign({type: RelationType.referencesOne}, definition);
|
75 | return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
76 | decoratorName: '@referencesOne',
|
77 | });
|
78 | }
|