-
Notifications
You must be signed in to change notification settings - Fork 45
/
link.ts
115 lines (107 loc) · 3.85 KB
/
link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { EntityBase, EntityIdentifiable, Constructable } from '../entity';
import type { Expandable } from '../expandable';
import type { Selectable } from './selectable';
/**
* Represents a navigation property of an OData entity.
*
* OData is a relational data model, i.e. entities can be related to one another.
* For example, BusinessPartner is in a 1:n relation with BusinessPartnerAddress and in a 1:1 relation with Customer.
* Like normal properties, navigation properties can be used for selecting (expanding) and filtering.
* For example, when constructing a query on the BusinessPartner entity, an instance of `Link<BusinessPartner, Customer>`
* can be passed as argument to the select function, e.g. `BusinessPartner.TO_CUSTOMER`.
*
* NOTE: Due to historical development the Link and its extensions are used in the following way:
* OData v2 entity: 1:N -> [[Link]], 1:0..1 -> [[OneToOneLink]]
* OData v4 entity: 1:N -> [[OneToManyLink]], 1:0..1 -> [[OneToOneLink]]
*
* See also: [[Selectable]]
*
* @typeparam EntityT - Type of the entity to be linked from
* @typeparam LinkedEntityT - Type of the entity to be linked to
*/
export class Link<
EntityT extends EntityBase,
LinkedEntityT extends EntityBase = any
> implements EntityIdentifiable<EntityT> {
/**
* @deprecated Since v1.21.0. Use [[clone]] instead.
* Create a new link based on a given link.
*
* @typeparam EntityT - Type of the entity to be linked from
* @typeparam LinkedEntityT - Type of the entity to be linked to
* @param link - Link to be cloned
* @returns Newly created link
*/
static clone<EntityT1 extends EntityBase, LinkedEntityT1 extends EntityBase>(
link: Link<EntityT1, LinkedEntityT1>
): Link<EntityT1, LinkedEntityT1> {
const clonedLink = new Link<EntityT1, LinkedEntityT1>(
link._fieldName,
link._entityConstructor,
link._linkedEntity
);
clonedLink._selects = link._selects;
return clonedLink;
}
readonly _entity: EntityT;
/**
* @deprecated Since v1.21.0. Use [[_selects]] directly.
* List of selectables on the linked entity.
*/
get selects() {
return this._selects;
}
/**
* List of selectables on the linked entity.
*/
_selects: Selectable<LinkedEntityT>[] = [];
_expand: Expandable<LinkedEntityT>[] = [];
/**
* Creates an instance of Link.
*
* @param _fieldName - Name of the linking field to be used in the OData request.
* @param _entityConstructor - Constructor type of the entity the field belongs to
* @param _linkedEntity - Constructor type of the linked entity
*/
constructor(
readonly _fieldName: string,
readonly _entityConstructor: Constructable<EntityT>,
readonly _linkedEntity: Constructable<LinkedEntityT>
) {}
/**
* Creates a selection on a linked entity. Has the same behavior as [[GetAllRequestBuilderV2.select]] and [[GetByKeyRequestBuilderV4.select]] but for linked entities.
*
* See also, [[Selectable]]
*
* @param selects - Selection of fields or links on a linked entity
* @returns The link itself, to facilitate method chaining
*/
select(...selects: Selectable<LinkedEntityT>[]): this {
const link = this.clone();
link._selects = selects;
return link;
}
expand(...expands: Expandable<LinkedEntityT>[]): this {
const link = this.clone();
link._expand = expands;
return link;
}
/**
* Create a new link based on a given link.
*
* @typeparam EntityT - Type of the entity to be linked from
* @typeparam LinkedEntityT - Type of the entity to be linked to
* @param link - Link to be cloned
* @returns Newly created link
*/
clone(): this {
const clonedLink = new (this.constructor as any)(
this._fieldName,
this._entityConstructor,
this._linkedEntity
);
clonedLink._selects = this._selects;
clonedLink._expand = this._expand;
return clonedLink;
}
}