UNPKG

5.07 kBTypeScriptView Raw
1import { DataObject, Entity, HasManyDefinition } from '../..';
2export type HasManyThroughResolvedDefinition = HasManyDefinition & {
3 keyTo: string;
4 keyFrom: string;
5 through: {
6 keyTo: string;
7 keyFrom: string;
8 polymorphic: false | {
9 discriminator: string;
10 };
11 };
12};
13/**
14 * Creates target constraint based on through models
15 * @param relationMeta - resolved hasManyThrough metadata
16 * @param throughInstances - an array of through instances
17 *
18 * @example
19 * ```ts
20 * const resolvedMetadata = {
21 * // .. other props
22 * keyFrom: 'id',
23 * keyTo: 'id',
24 * through: {
25 * model: () => CategoryProductLink,
26 * keyFrom: 'categoryId',
27 * keyTo: 'productId',
28 * },
29 * };
30 * createTargetConstraintFromThrough(resolvedMetadata,[{
31 id: 2,
32 categoryId: 2,
33 productId: 8,
34 }]);
35 * >>> {id: 8}
36 * createTargetConstraintFromThrough(resolvedMetadata, [
37 {
38 id: 2,
39 categoryId: 2,
40 productId: 8,
41 }, {
42 id: 1,
43 categoryId: 2,
44 productId: 9,
45 }
46 ]);
47
48 >>> {id: {inq: [9, 8]}}
49 * ```
50 */
51export declare function createTargetConstraintFromThrough<Target extends Entity, Through extends Entity>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through[]): DataObject<Target>;
52/**
53 * Returns an array of target fks of the given throughInstances.
54 *
55 * @param relationMeta - resolved hasManyThrough metadata
56 * @param throughInstances - an array of through instances
57 *
58 * @example
59 * ```ts
60 * const resolvedMetadata = {
61 * // .. other props
62 * keyFrom: 'id',
63 * keyTo: 'id',
64 * through: {
65 * model: () => CategoryProductLink,
66 * keyFrom: 'categoryId',
67 * keyTo: 'productId',
68 * },
69 * };
70 * getTargetKeysFromThroughModels(resolvedMetadata,[{
71 id: 2,
72 categoryId: 2,
73 productId: 8,
74 }]);
75 * >>> [8]
76 * getTargetKeysFromThroughModels(resolvedMetadata, [
77 {
78 id: 2,
79 categoryId: 2,
80 productId: 8,
81 }, {
82 id: 1,
83 categoryId: 2,
84 productId: 9,
85 }
86 ]);
87 >>> [8, 9]
88 */
89export declare function getTargetKeysFromThroughModels<Through extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, throughInstances: Through[]): TargetID[];
90/**
91 * Creates through constraint based on the source key
92 *
93 * @param relationMeta - resolved hasManyThrough metadata
94 * @param fkValue - foreign key of the source instance
95 * @internal
96 *
97 * @example
98 * ```ts
99 * const resolvedMetadata = {
100 * // .. other props
101 * keyFrom: 'id',
102 * keyTo: 'id',
103 * through: {
104 * model: () => CategoryProductLink,
105 * keyFrom: 'categoryId',
106 * keyTo: 'productId',
107 * },
108 * };
109 * createThroughConstraintFromSource(resolvedMetadata, 1);
110 *
111 * >>> {categoryId: 1}
112 * ```
113 */
114export declare function createThroughConstraintFromSource<Through extends Entity, SourceID>(relationMeta: HasManyThroughResolvedDefinition, fkValue: SourceID): DataObject<Through>;
115/**
116 * Returns an array of target ids of the given target instances.
117 *
118 * @param relationMeta - resolved hasManyThrough metadata
119 * @param targetInstances - an array of target instances
120 *
121 * @example
122 * ```ts
123 * const resolvedMetadata = {
124 * // .. other props
125 * keyFrom: 'id',
126 * keyTo: 'id',
127 * through: {
128 * model: () => CategoryProductLink,
129 * keyFrom: 'categoryId',
130 * keyTo: 'productId',
131 * },
132 * };
133 * getTargetKeysFromTargetModels(resolvedMetadata,[{
134 id: 2,
135 des: 'a target',
136 }]);
137 * >>> [2]
138 * getTargetKeysFromTargetModels(resolvedMetadata, [
139 {
140 id: 2,
141 des: 'a target',
142 }, {
143 id: 1,
144 des: 'a target',
145 }
146 ]);
147 >>> [2, 1]
148 */
149export declare function getTargetIdsFromTargetModels<Target extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, targetInstances: Target[]): TargetID[];
150/**
151 * Creates through constraint based on the target foreign key
152 *
153 * @param relationMeta - resolved hasManyThrough metadata
154 * @param fkValue an array of the target instance foreign keys
155 * @internal
156 *
157 * @example
158 * ```ts
159 * const resolvedMetadata = {
160 * // .. other props
161 * keyFrom: 'id',
162 * keyTo: 'id',
163 * through: {
164 * model: () => CategoryProductLink,
165 * keyFrom: 'categoryId',
166 * keyTo: 'productId',
167 * },
168 * };
169 * createThroughConstraintFromTarget(resolvedMetadata, [3]);
170 *
171 * >>> {productId: 3}
172 *
173 * createThroughConstraintFromTarget(resolvedMetadata, [3,4]);
174 *
175 * >>> {productId: {inq:[3,4]}}
176 */
177export declare function createThroughConstraintFromTarget<Through extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, fkValues: TargetID[]): DataObject<Through>;
178/**
179 * Resolves given hasMany metadata if target is specified to be a resolver.
180 * Mainly used to infer what the `keyTo` property should be from the target's
181 * belongsTo metadata
182 * @param relationMeta - hasManyThrough metadata to resolve
183 * @internal
184 */
185export declare function resolveHasManyThroughMetadata(relationMeta: HasManyDefinition): HasManyThroughResolvedDefinition;