-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
MorphToMany.ts
258 lines (216 loc) · 5.81 KB
/
MorphToMany.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Schema as NormalizrSchema } from 'normalizr'
import Utils from '../../support/Utils'
import Schema from '../../schema/Schema'
import { Record, Records, NormalizedData, Collection } from '../../data'
import Model from '../../model/Model'
import Query from '../../query/Query'
import Constraint from '../../query/contracts/RelationshipConstraint'
import Relation from './Relation'
export type Entity = typeof Model | string
export default class MorphToMany extends Relation {
/**
* The related model.
*/
related: typeof Model
/**
* The pivot model.
*/
pivot: typeof Model
/**
* The field name that contains id of the related model.
*/
relatedId: string
/**
* The field name that contains id of the parent model.
*/
id: string
/**
* The field name that contains type of the parent model.
*/
type: string
/**
* The key name of the parent model.
*/
parentKey: string
/**
* The key name of the related model.
*/
relatedKey: string
/**
* The key name of the pivot data.
*/
pivotKey: string = 'pivot'
/**
* Create a new belongs to instance.
*/
constructor(
model: typeof Model,
related: Entity,
pivot: Entity,
relatedId: string,
id: string,
type: string,
parentKey: string,
relatedKey: string
) {
super(model) /* istanbul ignore next */
this.related = this.model.relation(related)
this.pivot = this.model.relation(pivot)
this.relatedId = relatedId
this.id = id
this.type = type
this.parentKey = parentKey
this.relatedKey = relatedKey
}
/**
* Specify the custom pivot accessor to use for the relationship.
*/
as(accessor: string): this {
this.pivotKey = accessor
return this
}
/**
* Define the normalizr schema for the relationship.
*/
define(schema: Schema): NormalizrSchema {
return schema.many(this.related)
}
/**
* Attach the relational key to the given record. Since morph to many
* relationship doesn't have any foreign key, it would do nothing.
*/
attach(_key: any, _record: Record, _data: NormalizedData): void {
return
}
/**
* Convert given value to the appropriate value for the attribute.
*/
make(value: any, _parent: Record, _key: string): Model[] {
return this.makeManyRelation(value, this.related)
}
/**
* Load the morph to many relationship for the collection.
*/
load(
query: Query,
collection: Collection,
name: string,
constraints: Constraint[]
): void {
const relatedQuery = this.getRelation(
query,
this.related.entity,
constraints
)
const pivotQuery = query.newQuery(this.pivot.entity)
this.addEagerConstraintForPivot(pivotQuery, collection, query.entity)
const pivots = pivotQuery.get()
this.addEagerConstraintForRelated(relatedQuery, pivots)
const relateds = this.mapPivotRelations(pivots, relatedQuery)
collection.forEach((item) => {
const related = relateds[item[this.parentKey]]
item[name] = related || []
})
}
/**
* Set the constraints for the pivot relation.
*/
addEagerConstraintForPivot(
query: Query,
collection: Collection,
type: string
): void {
query
.whereFk(this.type, type)
.whereFk(this.id, this.getKeys(collection, this.parentKey))
}
/**
* Set the constraints for the related relation.
*/
addEagerConstraintForRelated(query: Query, collection: Collection): void {
query.whereFk(this.relatedKey, this.getKeys(collection, this.relatedId))
}
/**
* Create a new indexed map for the pivot relation.
*/
mapPivotRelations(pivots: Collection, relatedQuery: Query): Records {
const relations = this.mapManyRelations(relatedQuery.get(), this.relatedKey)
if (relatedQuery.orders.length) {
return this.mapRelationsByOrders(
pivots,
relations,
this.id,
this.relatedId
)
}
return pivots.reduce<Record>((records, record) => {
const id = record[this.id]
if (!records[id]) {
records[id] = []
}
const related = relations.get(record[this.relatedId])
/* istanbul ignore if */
if (related === undefined || related.length === 0) {
return records
}
records[id] = records[id].concat(
related.map((model: Record) => {
model[this.pivotKey] = record
return model
})
)
return records
}, {})
}
/**
* Create pivot records for the given records if needed.
*/
createPivots(
parent: typeof Model,
data: NormalizedData,
key: string
): NormalizedData {
Utils.forOwn(data[parent.entity], (record) => {
const relatedIds = parent
.query()
.newQuery(this.pivot.entity)
.where(this.id, record[this.parentKey])
.where(this.type, parent.entity)
.get()
const relateds = (record[key] || []).filter(
(relatedId: any) => !relatedIds.includes(relatedId)
)
if (!Utils.isArray(relateds) || relateds.length === 0) {
return
}
this.createPivotRecord(parent, data, record, relateds)
})
return data
}
/**
* Create a pivot record.
*/
createPivotRecord(
parent: typeof Model,
data: NormalizedData,
record: Record,
related: any[]
): void {
related.forEach((id) => {
const parentId = record[this.parentKey]
const relatedId = data[this.related.entity][id][this.relatedKey]
const pivotKey = `${parentId}_${id}_${parent.entity}`
const pivotData = data[this.related.entity][id][this.pivotKey] || {}
data[this.pivot.entity] = {
...data[this.pivot.entity],
[pivotKey]: {
...pivotData,
$id: pivotKey,
[this.relatedId]: relatedId,
[this.id]: parentId,
[this.type]: parent.entity
}
}
})
}
}