@strapi/database 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/examples/connections.js +36 -0
- package/examples/data.sqlite +0 -0
- package/examples/docker-compose.yml +29 -0
- package/examples/index.js +73 -0
- package/examples/models.js +341 -0
- package/examples/typings.ts +17 -0
- package/lib/dialects/dialect.js +45 -0
- package/lib/dialects/index.js +28 -0
- package/lib/dialects/mysql/index.js +51 -0
- package/lib/dialects/mysql/schema-inspector.js +203 -0
- package/lib/dialects/postgresql/index.js +49 -0
- package/lib/dialects/postgresql/schema-inspector.js +229 -0
- package/lib/dialects/sqlite/index.js +74 -0
- package/lib/dialects/sqlite/schema-inspector.js +151 -0
- package/lib/entity-manager.js +886 -0
- package/lib/entity-repository.js +110 -0
- package/lib/errors.js +14 -0
- package/lib/fields.d.ts +9 -0
- package/lib/fields.js +232 -0
- package/lib/index.d.ts +146 -0
- package/lib/index.js +60 -0
- package/lib/lifecycles/index.d.ts +50 -0
- package/lib/lifecycles/index.js +66 -0
- package/lib/lifecycles/subscribers/index.d.ts +9 -0
- package/lib/lifecycles/subscribers/models-lifecycles.js +19 -0
- package/lib/lifecycles/subscribers/timestamps.js +65 -0
- package/lib/metadata/index.js +219 -0
- package/lib/metadata/relations.js +488 -0
- package/lib/migrations/index.d.ts +9 -0
- package/lib/migrations/index.js +69 -0
- package/lib/migrations/storage.js +49 -0
- package/lib/query/helpers/index.js +10 -0
- package/lib/query/helpers/join.js +95 -0
- package/lib/query/helpers/order-by.js +70 -0
- package/lib/query/helpers/populate.js +652 -0
- package/lib/query/helpers/search.js +84 -0
- package/lib/query/helpers/transform.js +84 -0
- package/lib/query/helpers/where.js +322 -0
- package/lib/query/index.js +7 -0
- package/lib/query/query-builder.js +348 -0
- package/lib/schema/__tests__/schema-diff.test.js +181 -0
- package/lib/schema/builder.js +352 -0
- package/lib/schema/diff.js +376 -0
- package/lib/schema/index.d.ts +49 -0
- package/lib/schema/index.js +95 -0
- package/lib/schema/schema.js +209 -0
- package/lib/schema/storage.js +75 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/index.js +34 -0
- package/lib/utils/content-types.js +41 -0
- package/package.json +39 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash/fp');
|
|
4
|
+
|
|
5
|
+
const types = require('../../types');
|
|
6
|
+
const { createJoin } = require('./join');
|
|
7
|
+
const { toColumnName } = require('./transform');
|
|
8
|
+
|
|
9
|
+
const processOrderBy = (orderBy, ctx) => {
|
|
10
|
+
const { db, uid, qb, alias } = ctx;
|
|
11
|
+
const meta = db.metadata.get(uid);
|
|
12
|
+
const { attributes } = meta;
|
|
13
|
+
|
|
14
|
+
if (typeof orderBy === 'string') {
|
|
15
|
+
const attribute = attributes[orderBy];
|
|
16
|
+
|
|
17
|
+
if (!attribute) {
|
|
18
|
+
throw new Error(`Attribute ${orderBy} not found on model ${uid}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const columnName = toColumnName(meta, orderBy);
|
|
22
|
+
|
|
23
|
+
return [{ column: qb.aliasColumn(columnName, alias) }];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (Array.isArray(orderBy)) {
|
|
27
|
+
return orderBy.flatMap(value => processOrderBy(value, ctx));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (_.isPlainObject(orderBy)) {
|
|
31
|
+
return Object.entries(orderBy).flatMap(([key, direction]) => {
|
|
32
|
+
const value = orderBy[key];
|
|
33
|
+
const attribute = attributes[key];
|
|
34
|
+
|
|
35
|
+
if (!attribute) {
|
|
36
|
+
throw new Error(`Attribute ${key} not found on model ${uid}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (types.isScalar(attribute.type)) {
|
|
40
|
+
const columnName = toColumnName(meta, key);
|
|
41
|
+
|
|
42
|
+
return { column: qb.aliasColumn(columnName, alias), order: direction };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (attribute.type === 'relation') {
|
|
46
|
+
const subAlias = createJoin(ctx, {
|
|
47
|
+
alias: alias || qb.alias,
|
|
48
|
+
uid,
|
|
49
|
+
attributeName: key,
|
|
50
|
+
attribute,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return processOrderBy(value, {
|
|
54
|
+
db,
|
|
55
|
+
qb,
|
|
56
|
+
alias: subAlias,
|
|
57
|
+
uid: attribute.target,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
throw new Error(`You cannot order on ${attribute.type} types`);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw new Error('Invalid orderBy syntax');
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
module.exports = {
|
|
69
|
+
processOrderBy,
|
|
70
|
+
};
|
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash/fp');
|
|
4
|
+
|
|
5
|
+
const types = require('../../types');
|
|
6
|
+
const { fromRow } = require('./transform');
|
|
7
|
+
|
|
8
|
+
const getRootLevelPopulate = meta => {
|
|
9
|
+
const populate = {};
|
|
10
|
+
|
|
11
|
+
for (const attributeName in meta.attributes) {
|
|
12
|
+
const attribute = meta.attributes[attributeName];
|
|
13
|
+
if (attribute.type === 'relation') {
|
|
14
|
+
populate[attributeName] = true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return populate;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Converts and prepares the query for populate
|
|
23
|
+
*
|
|
24
|
+
* @param {boolean|string[]|object} populate populate param
|
|
25
|
+
* @param {object} ctx query context
|
|
26
|
+
* @param {object} ctx.db database instance
|
|
27
|
+
* @param {object} ctx.qb query builder instance
|
|
28
|
+
* @param {string} ctx.uid model uid
|
|
29
|
+
*/
|
|
30
|
+
const processPopulate = (populate, ctx) => {
|
|
31
|
+
const { qb, db, uid } = ctx;
|
|
32
|
+
const meta = db.metadata.get(uid);
|
|
33
|
+
|
|
34
|
+
let populateMap = {};
|
|
35
|
+
|
|
36
|
+
if (populate === false || _.isNil(populate)) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (populate === true) {
|
|
41
|
+
populateMap = getRootLevelPopulate(meta);
|
|
42
|
+
} else if (Array.isArray(populate)) {
|
|
43
|
+
for (const key of populate) {
|
|
44
|
+
const [root, ...rest] = key.split('.');
|
|
45
|
+
|
|
46
|
+
if (rest.length > 0) {
|
|
47
|
+
const subPopulate = rest.join('.');
|
|
48
|
+
|
|
49
|
+
if (populateMap[root]) {
|
|
50
|
+
if (populateMap[root] === true) {
|
|
51
|
+
populateMap[root] = {
|
|
52
|
+
populate: [subPopulate],
|
|
53
|
+
};
|
|
54
|
+
} else {
|
|
55
|
+
populateMap[root].populate = [subPopulate].concat(populateMap[root].populate || []);
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
populateMap[root] = {
|
|
59
|
+
populate: [subPopulate],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
populateMap[root] = populateMap[root] ? populateMap[root] : true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
populateMap = populate;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!_.isPlainObject(populateMap)) {
|
|
71
|
+
throw new Error('Populate must be an object');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const finalPopulate = {};
|
|
75
|
+
for (const key in populateMap) {
|
|
76
|
+
const attribute = meta.attributes[key];
|
|
77
|
+
|
|
78
|
+
if (!attribute) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!types.isRelation(attribute.type)) {
|
|
83
|
+
throw new Error(`Invalid populate field. Expected a relation, got ${attribute.type}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// make sure id is present for future populate queries
|
|
87
|
+
if (_.has('id', meta.attributes)) {
|
|
88
|
+
qb.addSelect('id');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
finalPopulate[key] = populateMap[key];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return finalPopulate;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// TODO: Omit limit & offset to avoid needing a query per result to avoid making too many queries
|
|
98
|
+
const pickPopulateParams = _.pick([
|
|
99
|
+
'select',
|
|
100
|
+
'count',
|
|
101
|
+
'where',
|
|
102
|
+
'populate',
|
|
103
|
+
'orderBy',
|
|
104
|
+
'limit',
|
|
105
|
+
'offset',
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
// TODO: cleanup code
|
|
109
|
+
// TODO: create aliases for pivot columns
|
|
110
|
+
// TODO: optimize depth to avoid overfetching
|
|
111
|
+
// TODO: handle count for join columns
|
|
112
|
+
// TODO: cleanup count
|
|
113
|
+
const applyPopulate = async (results, populate, ctx) => {
|
|
114
|
+
const { db, uid, qb } = ctx;
|
|
115
|
+
const meta = db.metadata.get(uid);
|
|
116
|
+
|
|
117
|
+
if (_.isEmpty(results)) {
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const key in populate) {
|
|
122
|
+
const attribute = meta.attributes[key];
|
|
123
|
+
const targetMeta = db.metadata.get(attribute.target);
|
|
124
|
+
|
|
125
|
+
const populateValue = pickPopulateParams(populate[key]);
|
|
126
|
+
const isCount = populateValue.count === true;
|
|
127
|
+
|
|
128
|
+
const fromTargetRow = rowOrRows => fromRow(targetMeta, rowOrRows);
|
|
129
|
+
|
|
130
|
+
if (attribute.relation === 'oneToOne' || attribute.relation === 'manyToOne') {
|
|
131
|
+
if (attribute.joinColumn) {
|
|
132
|
+
const {
|
|
133
|
+
name: joinColumnName,
|
|
134
|
+
referencedColumn: referencedColumnName,
|
|
135
|
+
} = attribute.joinColumn;
|
|
136
|
+
|
|
137
|
+
const referencedValues = _.uniq(
|
|
138
|
+
results.map(r => r[joinColumnName]).filter(value => !_.isNil(value))
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (_.isEmpty(referencedValues)) {
|
|
142
|
+
results.forEach(result => {
|
|
143
|
+
result[key] = null;
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const rows = await db.entityManager
|
|
150
|
+
.createQueryBuilder(targetMeta.uid)
|
|
151
|
+
.init(populateValue)
|
|
152
|
+
.addSelect(`${qb.alias}.${referencedColumnName}`)
|
|
153
|
+
.where({ [referencedColumnName]: referencedValues })
|
|
154
|
+
.execute({ mapResults: false });
|
|
155
|
+
|
|
156
|
+
const map = _.groupBy(referencedColumnName, rows);
|
|
157
|
+
|
|
158
|
+
results.forEach(result => {
|
|
159
|
+
result[key] = fromTargetRow(_.first(map[result[joinColumnName]]));
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (attribute.joinTable) {
|
|
166
|
+
const { joinTable } = attribute;
|
|
167
|
+
|
|
168
|
+
const qb = db.entityManager.createQueryBuilder(targetMeta.uid);
|
|
169
|
+
|
|
170
|
+
const {
|
|
171
|
+
name: joinColumnName,
|
|
172
|
+
referencedColumn: referencedColumnName,
|
|
173
|
+
} = joinTable.joinColumn;
|
|
174
|
+
|
|
175
|
+
const alias = qb.getAlias();
|
|
176
|
+
const joinColAlias = `${alias}.${joinColumnName}`;
|
|
177
|
+
|
|
178
|
+
const referencedValues = _.uniq(
|
|
179
|
+
results.map(r => r[referencedColumnName]).filter(value => !_.isNil(value))
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
if (_.isEmpty(referencedValues)) {
|
|
183
|
+
results.forEach(result => {
|
|
184
|
+
result[key] = null;
|
|
185
|
+
});
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const rows = await qb
|
|
190
|
+
.init(populateValue)
|
|
191
|
+
.join({
|
|
192
|
+
alias,
|
|
193
|
+
referencedTable: joinTable.name,
|
|
194
|
+
referencedColumn: joinTable.inverseJoinColumn.name,
|
|
195
|
+
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
|
196
|
+
rootTable: qb.alias,
|
|
197
|
+
on: joinTable.on,
|
|
198
|
+
orderBy: joinTable.orderBy,
|
|
199
|
+
})
|
|
200
|
+
.addSelect(joinColAlias)
|
|
201
|
+
.where({ [joinColAlias]: referencedValues })
|
|
202
|
+
.execute({ mapResults: false });
|
|
203
|
+
|
|
204
|
+
const map = _.groupBy(joinColumnName, rows);
|
|
205
|
+
|
|
206
|
+
results.forEach(result => {
|
|
207
|
+
result[key] = fromTargetRow(_.first(map[result[referencedColumnName]]));
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
continue;
|
|
214
|
+
} else if (attribute.relation === 'oneToMany') {
|
|
215
|
+
if (attribute.joinColumn) {
|
|
216
|
+
const {
|
|
217
|
+
name: joinColumnName,
|
|
218
|
+
referencedColumn: referencedColumnName,
|
|
219
|
+
} = attribute.joinColumn;
|
|
220
|
+
|
|
221
|
+
const referencedValues = _.uniq(
|
|
222
|
+
results.map(r => r[joinColumnName]).filter(value => !_.isNil(value))
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
if (_.isEmpty(referencedValues)) {
|
|
226
|
+
results.forEach(result => {
|
|
227
|
+
result[key] = null;
|
|
228
|
+
});
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const rows = await db.entityManager
|
|
233
|
+
.createQueryBuilder(targetMeta.uid)
|
|
234
|
+
.init(populateValue)
|
|
235
|
+
.addSelect(`${qb.alias}.${referencedColumnName}`)
|
|
236
|
+
.where({ [referencedColumnName]: referencedValues })
|
|
237
|
+
.execute({ mapResults: false });
|
|
238
|
+
|
|
239
|
+
const map = _.groupBy(referencedColumnName, rows);
|
|
240
|
+
|
|
241
|
+
results.forEach(result => {
|
|
242
|
+
result[key] = fromTargetRow(map[result[joinColumnName]] || []);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (attribute.joinTable) {
|
|
249
|
+
const { joinTable } = attribute;
|
|
250
|
+
|
|
251
|
+
const qb = db.entityManager.createQueryBuilder(targetMeta.uid);
|
|
252
|
+
|
|
253
|
+
const {
|
|
254
|
+
name: joinColumnName,
|
|
255
|
+
referencedColumn: referencedColumnName,
|
|
256
|
+
} = joinTable.joinColumn;
|
|
257
|
+
|
|
258
|
+
const alias = qb.getAlias();
|
|
259
|
+
const joinColAlias = `${alias}.${joinColumnName}`;
|
|
260
|
+
|
|
261
|
+
const referencedValues = _.uniq(
|
|
262
|
+
results.map(r => r[referencedColumnName]).filter(value => !_.isNil(value))
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
if (isCount) {
|
|
266
|
+
if (_.isEmpty(referencedValues)) {
|
|
267
|
+
results.forEach(result => {
|
|
268
|
+
result[key] = { count: 0 };
|
|
269
|
+
});
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const rows = await qb
|
|
274
|
+
.init(populateValue)
|
|
275
|
+
.join({
|
|
276
|
+
alias,
|
|
277
|
+
referencedTable: joinTable.name,
|
|
278
|
+
referencedColumn: joinTable.inverseJoinColumn.name,
|
|
279
|
+
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
|
280
|
+
rootTable: qb.alias,
|
|
281
|
+
on: joinTable.on,
|
|
282
|
+
})
|
|
283
|
+
.select([joinColAlias, qb.raw('count(*) AS count')])
|
|
284
|
+
.where({ [joinColAlias]: referencedValues })
|
|
285
|
+
.groupBy(joinColAlias)
|
|
286
|
+
.execute({ mapResults: false });
|
|
287
|
+
|
|
288
|
+
const map = rows.reduce((map, row) => {
|
|
289
|
+
map[row[joinColumnName]] = { count: Number(row.count) };
|
|
290
|
+
return map;
|
|
291
|
+
}, {});
|
|
292
|
+
|
|
293
|
+
results.forEach(result => {
|
|
294
|
+
result[key] = map[result[referencedColumnName]] || { count: 0 };
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (_.isEmpty(referencedValues)) {
|
|
301
|
+
results.forEach(result => {
|
|
302
|
+
result[key] = [];
|
|
303
|
+
});
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const rows = await qb
|
|
308
|
+
.init(populateValue)
|
|
309
|
+
.join({
|
|
310
|
+
alias,
|
|
311
|
+
referencedTable: joinTable.name,
|
|
312
|
+
referencedColumn: joinTable.inverseJoinColumn.name,
|
|
313
|
+
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
|
314
|
+
rootTable: qb.alias,
|
|
315
|
+
on: joinTable.on,
|
|
316
|
+
orderBy: joinTable.orderBy,
|
|
317
|
+
})
|
|
318
|
+
.addSelect(joinColAlias)
|
|
319
|
+
.where({ [joinColAlias]: referencedValues })
|
|
320
|
+
.execute({ mapResults: false });
|
|
321
|
+
|
|
322
|
+
const map = _.groupBy(joinColumnName, rows);
|
|
323
|
+
|
|
324
|
+
results.forEach(r => {
|
|
325
|
+
r[key] = fromTargetRow(map[r[referencedColumnName]] || []);
|
|
326
|
+
});
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
continue;
|
|
331
|
+
} else if (attribute.relation === 'manyToMany') {
|
|
332
|
+
const { joinTable } = attribute;
|
|
333
|
+
|
|
334
|
+
const qb = db.entityManager.createQueryBuilder(targetMeta.uid);
|
|
335
|
+
|
|
336
|
+
const { name: joinColumnName, referencedColumn: referencedColumnName } = joinTable.joinColumn;
|
|
337
|
+
|
|
338
|
+
const alias = qb.getAlias();
|
|
339
|
+
const joinColAlias = `${alias}.${joinColumnName}`;
|
|
340
|
+
const referencedValues = _.uniq(
|
|
341
|
+
results.map(r => r[referencedColumnName]).filter(value => !_.isNil(value))
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
if (isCount) {
|
|
345
|
+
if (_.isEmpty(referencedValues)) {
|
|
346
|
+
results.forEach(result => {
|
|
347
|
+
result[key] = { count: 0 };
|
|
348
|
+
});
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const rows = await qb
|
|
353
|
+
.init(populateValue)
|
|
354
|
+
.join({
|
|
355
|
+
alias,
|
|
356
|
+
referencedTable: joinTable.name,
|
|
357
|
+
referencedColumn: joinTable.inverseJoinColumn.name,
|
|
358
|
+
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
|
359
|
+
rootTable: qb.alias,
|
|
360
|
+
on: joinTable.on,
|
|
361
|
+
})
|
|
362
|
+
.select([joinColAlias, qb.raw('count(*) AS count')])
|
|
363
|
+
.where({ [joinColAlias]: referencedValues })
|
|
364
|
+
.groupBy(joinColAlias)
|
|
365
|
+
.execute({ mapResults: false });
|
|
366
|
+
|
|
367
|
+
const map = rows.reduce((map, row) => {
|
|
368
|
+
map[row[joinColumnName]] = { count: Number(row.count) };
|
|
369
|
+
return map;
|
|
370
|
+
}, {});
|
|
371
|
+
|
|
372
|
+
results.forEach(result => {
|
|
373
|
+
result[key] = map[result[referencedColumnName]] || { count: 0 };
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (_.isEmpty(referencedValues)) {
|
|
380
|
+
results.forEach(result => {
|
|
381
|
+
result[key] = [];
|
|
382
|
+
});
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const rows = await qb
|
|
387
|
+
.init(populateValue)
|
|
388
|
+
.join({
|
|
389
|
+
alias,
|
|
390
|
+
referencedTable: joinTable.name,
|
|
391
|
+
referencedColumn: joinTable.inverseJoinColumn.name,
|
|
392
|
+
rootColumn: joinTable.inverseJoinColumn.referencedColumn,
|
|
393
|
+
rootTable: qb.alias,
|
|
394
|
+
on: joinTable.on,
|
|
395
|
+
orderBy: joinTable.orderBy,
|
|
396
|
+
})
|
|
397
|
+
.addSelect(joinColAlias)
|
|
398
|
+
.where({ [joinColAlias]: referencedValues })
|
|
399
|
+
.execute({ mapResults: false });
|
|
400
|
+
|
|
401
|
+
const map = _.groupBy(joinColumnName, rows);
|
|
402
|
+
|
|
403
|
+
results.forEach(result => {
|
|
404
|
+
result[key] = fromTargetRow(map[result[referencedColumnName]] || []);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
continue;
|
|
408
|
+
} else if (['morphOne', 'morphMany'].includes(attribute.relation)) {
|
|
409
|
+
const { target, morphBy } = attribute;
|
|
410
|
+
|
|
411
|
+
const targetAttribute = db.metadata.get(target).attributes[morphBy];
|
|
412
|
+
|
|
413
|
+
if (targetAttribute.relation === 'morphToOne') {
|
|
414
|
+
const { idColumn, typeColumn } = targetAttribute.morphColumn;
|
|
415
|
+
|
|
416
|
+
const referencedValues = _.uniq(
|
|
417
|
+
results.map(r => r[idColumn.referencedColumn]).filter(value => !_.isNil(value))
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
if (_.isEmpty(referencedValues)) {
|
|
421
|
+
results.forEach(result => {
|
|
422
|
+
result[key] = null;
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const rows = await db.entityManager
|
|
429
|
+
.createQueryBuilder(target)
|
|
430
|
+
.init(populateValue)
|
|
431
|
+
// .addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
|
|
432
|
+
.where({ [idColumn.name]: referencedValues, [typeColumn.name]: uid })
|
|
433
|
+
.execute({ mapResults: false });
|
|
434
|
+
|
|
435
|
+
const map = _.groupBy(idColumn.name, rows);
|
|
436
|
+
|
|
437
|
+
results.forEach(result => {
|
|
438
|
+
const matchingRows = map[result[idColumn.referencedColumn]];
|
|
439
|
+
|
|
440
|
+
const matchingValue =
|
|
441
|
+
attribute.relation === 'morphOne' ? _.first(matchingRows) : matchingRows;
|
|
442
|
+
|
|
443
|
+
result[key] = fromTargetRow(matchingValue);
|
|
444
|
+
});
|
|
445
|
+
} else if (targetAttribute.relation === 'morphToMany') {
|
|
446
|
+
const { joinTable } = targetAttribute;
|
|
447
|
+
|
|
448
|
+
const { joinColumn, morphColumn } = joinTable;
|
|
449
|
+
|
|
450
|
+
const { idColumn, typeColumn } = morphColumn;
|
|
451
|
+
|
|
452
|
+
const referencedValues = _.uniq(
|
|
453
|
+
results.map(r => r[idColumn.referencedColumn]).filter(value => !_.isNil(value))
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
if (_.isEmpty(referencedValues)) {
|
|
457
|
+
results.forEach(result => {
|
|
458
|
+
result[key] = attribute.relation === 'morphOne' ? null : [];
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// find with join table
|
|
465
|
+
const qb = db.entityManager.createQueryBuilder(target);
|
|
466
|
+
|
|
467
|
+
const alias = qb.getAlias();
|
|
468
|
+
|
|
469
|
+
const rows = await qb
|
|
470
|
+
.init(populateValue)
|
|
471
|
+
.join({
|
|
472
|
+
alias,
|
|
473
|
+
referencedTable: joinTable.name,
|
|
474
|
+
referencedColumn: joinColumn.name,
|
|
475
|
+
rootColumn: joinColumn.referencedColumn,
|
|
476
|
+
rootTable: qb.alias,
|
|
477
|
+
on: {
|
|
478
|
+
...(joinTable.on || {}),
|
|
479
|
+
field: key,
|
|
480
|
+
},
|
|
481
|
+
orderBy: joinTable.orderBy,
|
|
482
|
+
})
|
|
483
|
+
.addSelect([`${alias}.${idColumn.name}`, `${alias}.${typeColumn.name}`])
|
|
484
|
+
.where({
|
|
485
|
+
[`${alias}.${idColumn.name}`]: referencedValues,
|
|
486
|
+
[`${alias}.${typeColumn.name}`]: uid,
|
|
487
|
+
})
|
|
488
|
+
.execute({ mapResults: false });
|
|
489
|
+
|
|
490
|
+
const map = _.groupBy(idColumn.name, rows);
|
|
491
|
+
|
|
492
|
+
results.forEach(result => {
|
|
493
|
+
const matchingRows = map[result[idColumn.referencedColumn]];
|
|
494
|
+
|
|
495
|
+
const matchingValue =
|
|
496
|
+
attribute.relation === 'morphOne' ? _.first(matchingRows) : matchingRows;
|
|
497
|
+
|
|
498
|
+
result[key] = fromTargetRow(matchingValue);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
continue;
|
|
503
|
+
} else if (attribute.relation === 'morphToMany') {
|
|
504
|
+
// find with join table
|
|
505
|
+
const { joinTable } = attribute;
|
|
506
|
+
|
|
507
|
+
const { joinColumn, morphColumn } = joinTable;
|
|
508
|
+
const { idColumn, typeColumn, typeField = '__type' } = morphColumn;
|
|
509
|
+
|
|
510
|
+
// fetch join table to create the ids map then do the same as morphToOne without the first
|
|
511
|
+
|
|
512
|
+
const referencedValues = _.uniq(
|
|
513
|
+
results.map(r => r[joinColumn.referencedColumn]).filter(value => !_.isNil(value))
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
const qb = db.entityManager.createQueryBuilder(joinTable.name);
|
|
517
|
+
|
|
518
|
+
const joinRows = await qb
|
|
519
|
+
.where({
|
|
520
|
+
[joinColumn.name]: referencedValues,
|
|
521
|
+
...(joinTable.on || {}),
|
|
522
|
+
})
|
|
523
|
+
.orderBy([joinColumn.name, 'order'])
|
|
524
|
+
.execute({ mapResults: false });
|
|
525
|
+
|
|
526
|
+
const joinMap = _.groupBy(joinColumn.name, joinRows);
|
|
527
|
+
|
|
528
|
+
const idsByType = joinRows.reduce((acc, result) => {
|
|
529
|
+
const idValue = result[morphColumn.idColumn.name];
|
|
530
|
+
const typeValue = result[morphColumn.typeColumn.name];
|
|
531
|
+
|
|
532
|
+
if (!idValue || !typeValue) {
|
|
533
|
+
return acc;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (!_.has(typeValue, acc)) {
|
|
537
|
+
acc[typeValue] = [];
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
acc[typeValue].push(idValue);
|
|
541
|
+
|
|
542
|
+
return acc;
|
|
543
|
+
}, {});
|
|
544
|
+
|
|
545
|
+
const map = {};
|
|
546
|
+
for (const type in idsByType) {
|
|
547
|
+
const ids = idsByType[type];
|
|
548
|
+
|
|
549
|
+
// type was removed but still in morph relation
|
|
550
|
+
if (!db.metadata.get(type)) {
|
|
551
|
+
map[type] = {};
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const qb = db.entityManager.createQueryBuilder(type);
|
|
556
|
+
|
|
557
|
+
const rows = await qb
|
|
558
|
+
.init(populateValue)
|
|
559
|
+
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
|
|
560
|
+
.where({ [idColumn.referencedColumn]: ids })
|
|
561
|
+
.execute({ mapResults: false });
|
|
562
|
+
|
|
563
|
+
map[type] = _.groupBy(idColumn.referencedColumn, rows);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
results.forEach(result => {
|
|
567
|
+
const joinResults = joinMap[result[joinColumn.referencedColumn]] || [];
|
|
568
|
+
|
|
569
|
+
const matchingRows = joinResults.flatMap(joinResult => {
|
|
570
|
+
const id = joinResult[idColumn.name];
|
|
571
|
+
const type = joinResult[typeColumn.name];
|
|
572
|
+
|
|
573
|
+
const fromTargetRow = rowOrRows => fromRow(db.metadata.get(type), rowOrRows);
|
|
574
|
+
|
|
575
|
+
return (map[type][id] || []).map(row => {
|
|
576
|
+
return {
|
|
577
|
+
[typeField]: type,
|
|
578
|
+
...fromTargetRow(row),
|
|
579
|
+
};
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
result[key] = matchingRows;
|
|
584
|
+
});
|
|
585
|
+
} else if (attribute.relation === 'morphToOne') {
|
|
586
|
+
const { morphColumn } = attribute;
|
|
587
|
+
const { idColumn, typeColumn } = morphColumn;
|
|
588
|
+
|
|
589
|
+
// make a map for each type what ids to return
|
|
590
|
+
// make a nested map per id
|
|
591
|
+
|
|
592
|
+
const idsByType = results.reduce((acc, result) => {
|
|
593
|
+
const idValue = result[morphColumn.idColumn.name];
|
|
594
|
+
const typeValue = result[morphColumn.typeColumn.name];
|
|
595
|
+
|
|
596
|
+
if (!idValue || !typeValue) {
|
|
597
|
+
return acc;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (!_.has(typeValue, acc)) {
|
|
601
|
+
acc[typeValue] = [];
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
acc[typeValue].push(idValue);
|
|
605
|
+
|
|
606
|
+
return acc;
|
|
607
|
+
}, {});
|
|
608
|
+
|
|
609
|
+
const map = {};
|
|
610
|
+
for (const type in idsByType) {
|
|
611
|
+
const ids = idsByType[type];
|
|
612
|
+
|
|
613
|
+
// type was removed but still in morph relation
|
|
614
|
+
if (!db.metadata.get(type)) {
|
|
615
|
+
map[type] = {};
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const qb = db.entityManager.createQueryBuilder(type);
|
|
620
|
+
|
|
621
|
+
const rows = await qb
|
|
622
|
+
.init(populateValue)
|
|
623
|
+
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
|
|
624
|
+
.where({ [idColumn.referencedColumn]: ids })
|
|
625
|
+
.execute({ mapResults: false });
|
|
626
|
+
|
|
627
|
+
map[type] = _.groupBy(idColumn.referencedColumn, rows);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
results.forEach(result => {
|
|
631
|
+
const id = result[idColumn.name];
|
|
632
|
+
const type = result[typeColumn.name];
|
|
633
|
+
|
|
634
|
+
if (!type || !id) {
|
|
635
|
+
result[key] = null;
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const matchingRows = map[type][id];
|
|
640
|
+
|
|
641
|
+
const fromTargetRow = rowOrRows => fromRow(db.metadata.get(type), rowOrRows);
|
|
642
|
+
|
|
643
|
+
result[key] = fromTargetRow(_.first(matchingRows));
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
module.exports = {
|
|
650
|
+
processPopulate,
|
|
651
|
+
applyPopulate,
|
|
652
|
+
};
|