1 | import { InclusionFilter } from '@loopback/filter';
|
2 | import { Options } from '../common-types';
|
3 | import { Entity } from '../model';
|
4 | import { TypeResolver } from '../type-resolver';
|
5 | export declare enum RelationType {
|
6 | belongsTo = "belongsTo",
|
7 | hasOne = "hasOne",
|
8 | hasMany = "hasMany",
|
9 | embedsOne = "embedsOne",
|
10 | embedsMany = "embedsMany",
|
11 | referencesOne = "referencesOne",
|
12 | referencesMany = "referencesMany"
|
13 | }
|
14 | export interface RelationDefinitionBase {
|
15 | /**
|
16 | * The type of the relation, must be one of RelationType values.
|
17 | */
|
18 | type: RelationType;
|
19 | /**
|
20 | * True for relations targeting multiple instances (e.g. HasMany),
|
21 | * false for relations with a single target (e.g. BelongsTo, HasOne).
|
22 | * This property is needed by OpenAPI/JSON Schema generator.
|
23 | */
|
24 | targetsMany: boolean;
|
25 | /**
|
26 | * The relation name, typically matching the name of the accessor property
|
27 | * defined on the source model. For example "orders" or "customer".
|
28 | */
|
29 | name: string;
|
30 | /**
|
31 | * The source model of this relation.
|
32 | *
|
33 | * E.g. when a Customer has many Order instances, then Customer is the source.
|
34 | */
|
35 | source: typeof Entity;
|
36 | /**
|
37 | * The target model of this relation.
|
38 | *
|
39 | * E.g. when a Customer has many Order instances, then Order is the target.
|
40 | */
|
41 | target: TypeResolver<Entity, typeof Entity>;
|
42 | }
|
43 | /**
|
44 | * HasManyDefinition defines one-to-many relations and also possible defines
|
45 | * many-to-many relations with through models.
|
46 | */
|
47 | export interface HasManyDefinition extends RelationDefinitionBase {
|
48 | type: RelationType.hasMany;
|
49 | targetsMany: true;
|
50 | /**
|
51 | * keyTo: The foreign key used by the target model for this relation.
|
52 | * keyFrom: The source key used by the source model for this relation.
|
53 | *
|
54 | * E.g. when a Customer has many Order instances, then keyTo is "customerId".
|
55 | * Note that "customerId" is the default FK assumed by the framework, users
|
56 | * can provide a custom FK name by setting "keyTo".
|
57 | * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
|
58 | * Users can provide a custom source key name by setting "keyTo".
|
59 | *
|
60 | */
|
61 | keyTo?: string;
|
62 | keyFrom?: string;
|
63 | /**
|
64 | * With current architecture design, polymorphic type cannot be supported without through
|
65 | * Consider using Source-hasMany->Through->hasOne->Target(polymorphic) for one-to-many relations
|
66 | */
|
67 | /**
|
68 | * Description of the through model of the hasManyThrough relation.
|
69 | *
|
70 | * A `hasManyThrough` relation defines a many-to-many connection with another model.
|
71 | * This relation indicates that the declaring model can be matched with zero or more
|
72 | * instances of another model by proceeding through a third model.
|
73 | *
|
74 | * E.g a Category has many Products, and a Product can have many Categories.
|
75 | * CategoryProductLink can be the through model.
|
76 | * Such a through model has information of foreign keys of the source model(Category) and the target model(Product).
|
77 | *
|
78 | * Warning: The hasManyThrough interface is experimental and is subject to change.
|
79 | * If backwards-incompatible changes are made, a new major version may not be
|
80 | * released.
|
81 | */
|
82 | through?: {
|
83 | /**
|
84 | * The through model of this relation.
|
85 | *
|
86 | * E.g. when a Category has many CategoryProductLink instances and a Product has many CategoryProductLink instances,
|
87 | * then CategoryProductLink is through.
|
88 | */
|
89 | model: TypeResolver<Entity, typeof Entity>;
|
90 | /**
|
91 | * The foreign key of the source model defined in the through model, e.g. CategoryProductLink#categoryId
|
92 | */
|
93 | keyFrom?: string;
|
94 | /**
|
95 | * The foreign key of the target model defined in the through model, e.g. CategoryProductLink#productId
|
96 | */
|
97 | keyTo?: string;
|
98 | /**
|
99 | * The polymorphism of the target model. The discriminator is a key of *through* model.
|
100 | * If the target model is not polymorphic, then the value should be left undefined or false;
|
101 | * If the key on through model indicating the concrete class of the through instance is default
|
102 | * i.e. camelCase(classNameOf(targetModelInstance)) + "Id"
|
103 | * then the discriminator field can be undefined
|
104 | *
|
105 | * With current architecture design, polymorphic type cannot be supported without through
|
106 | * Consider using Source hasMany Through hasOne Target(polymorphic)
|
107 | * or Source hasMany Through belongsTo Target(polymorphic) for one-to-many relations
|
108 | */
|
109 | polymorphic?: boolean | {
|
110 | discriminator: string;
|
111 | };
|
112 | };
|
113 | }
|
114 | export interface BelongsToDefinition extends RelationDefinitionBase {
|
115 | type: RelationType.belongsTo;
|
116 | targetsMany: false;
|
117 | keyFrom?: string;
|
118 | keyTo?: string;
|
119 | /**
|
120 | * The polymorphism of the target model. The discriminator is a key of source model.
|
121 | * If the target model is not polymorphic, then the value should be left undefined or false;
|
122 | * If the key on source model indicating the concrete class of the target instance is default
|
123 | * i.e. camelCase(classNameOf(throughModelInstance)) + "Id"
|
124 | * Then the discriminator field can be undefined
|
125 | */
|
126 | polymorphic?: boolean | {
|
127 | discriminator: string;
|
128 | };
|
129 | }
|
130 | export interface HasOneDefinition extends RelationDefinitionBase {
|
131 | type: RelationType.hasOne;
|
132 | targetsMany: false;
|
133 | /**
|
134 | * keyTo: The foreign key used by the target model for this relation.
|
135 | * keyFrom: The source key used by the source model for this relation.
|
136 | *
|
137 | * E.g. when a Customer has one Address instance, then keyTo is "customerId".
|
138 | * Note that "customerId" is the default FK assumed by the framework, users
|
139 | * can provide a custom FK name by setting "keyTo".
|
140 | * And Customer.id is keyFrom. keyFrom defaults to the id property of a model.
|
141 | * Users can provide a custom source key name by setting "keyTo".
|
142 | */
|
143 | keyTo?: string;
|
144 | keyFrom?: string;
|
145 | /**
|
146 | * The polymorphism of the target model. The discriminator is a key of source model.
|
147 | * If the target model is not polymorphic, then the value should be left undefined or false;
|
148 | * If the key on source model indicating the concrete class of the target instance is default
|
149 | * i.e. camelCase(classNameOf(throughModelInstance)) + "Id"
|
150 | * Then the discriminator field can be undefined
|
151 | */
|
152 | polymorphic?: boolean | {
|
153 | discriminator: string;
|
154 | };
|
155 | }
|
156 | export interface ReferencesManyDefinition extends RelationDefinitionBase {
|
157 | type: RelationType.referencesMany;
|
158 | targetsMany: true;
|
159 | /**
|
160 | * keyTo: The foreign key used by the target model for this relation.
|
161 | * keyFrom: The source key used by the source model for this relation.
|
162 | *
|
163 | * TODO(bajtos) Add relation description.
|
164 | *
|
165 | */
|
166 | keyTo?: string;
|
167 | keyFrom?: string;
|
168 | }
|
169 | /**
|
170 | * A union type describing all possible Relation metadata objects.
|
171 | */
|
172 | export type RelationMetadata = HasManyDefinition | BelongsToDefinition | HasOneDefinition | ReferencesManyDefinition | RelationDefinitionBase;
|
173 | export { Getter } from '@loopback/core';
|
174 | /**
|
175 | * @returns An array of resolved values, the items must be ordered in the same
|
176 | * way as `sourceEntities`. The resolved value can be one of:
|
177 | * - `undefined` when no target model(s) were found
|
178 | * - `Entity` for relations targeting a single model
|
179 | * - `Entity[]` for relations targeting multiple models
|
180 | */
|
181 | export type InclusionResolver<S extends Entity, T extends Entity> = (
|
182 | /**
|
183 | * List of source models as returned by the first database query.
|
184 | */
|
185 | sourceEntities: S[],
|
186 | /**
|
187 | * Inclusion requested by the user (e.g. scope constraints to apply).
|
188 | */
|
189 | inclusion: InclusionFilter,
|
190 | /**
|
191 | * Generic options object, e.g. carrying the Transaction object.
|
192 | */
|
193 | options?: Options) => Promise<(T | undefined)[] | (T[] | undefined)[]>;
|