@reldens/storage 0.10.0-beta.38 → 0.10.0-beta.39
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.
|
@@ -232,7 +232,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
232
232
|
async appendRelationsToCollection(entitiesCollection, relations)
|
|
233
233
|
{
|
|
234
234
|
// @TODO - BETA - Refactor. I would like to use populate but it may not work if the driver is not Mongo DB.
|
|
235
|
-
if(typeof this.rawModel.entity.relationMappings
|
|
235
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
236
236
|
return entitiesCollection;
|
|
237
237
|
}
|
|
238
238
|
if(!sc.isArray(relations) || 0 === relations.length){
|
|
@@ -241,8 +241,13 @@ class MikroOrmDriver extends BaseDriver
|
|
|
241
241
|
if(0 === relations.length){
|
|
242
242
|
return entitiesCollection;
|
|
243
243
|
}
|
|
244
|
-
|
|
245
|
-
|
|
244
|
+
if(sc.isArray(entitiesCollection)){
|
|
245
|
+
for(let entity of entitiesCollection){
|
|
246
|
+
await this.appendRelatedEntities(entity, relations);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if(!sc.isArray(entitiesCollection)){
|
|
250
|
+
await this.appendRelatedEntities(entitiesCollection, relations);
|
|
246
251
|
}
|
|
247
252
|
return entitiesCollection;
|
|
248
253
|
}
|
|
@@ -250,7 +255,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
250
255
|
async appendRelatedEntities(entity, relations)
|
|
251
256
|
{
|
|
252
257
|
// @TODO - BETA - Refactor and improve.
|
|
253
|
-
if(typeof this.rawModel.entity.relationMappings
|
|
258
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
254
259
|
return entity;
|
|
255
260
|
}
|
|
256
261
|
let relationMappings = this.rawModel.entity.relationMappings();
|
|
@@ -271,47 +276,46 @@ class MikroOrmDriver extends BaseDriver
|
|
|
271
276
|
|
|
272
277
|
async createNested(newInstance, params)
|
|
273
278
|
{
|
|
274
|
-
if(typeof this.rawModel.entity.relationMappings
|
|
279
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
275
280
|
return false;
|
|
276
281
|
}
|
|
277
282
|
let relationMappings = this.rawModel.entity.relationMappings();
|
|
278
283
|
for(let i of Object.keys(relationMappings)){
|
|
279
284
|
// @TODO - BETA - Refactor and improve.
|
|
280
|
-
let
|
|
281
|
-
let relationEntity = this.server.entityManager.get(
|
|
285
|
+
let relation = relationMappings[i];
|
|
286
|
+
let relationEntity = this.server.entityManager.get(relation.entityName).rawModel.entity;
|
|
282
287
|
if(!relationEntity){
|
|
283
|
-
Logger.warning('Factory not found for relation definition:',
|
|
288
|
+
Logger.warning('Factory not found for relation definition:', relation);
|
|
284
289
|
continue;
|
|
285
290
|
}
|
|
286
|
-
let
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if(!sc.hasOwn(params, relationData.join.from) || !params[relationData.join.from]){
|
|
291
|
+
let isArray = sc.isArray(params);
|
|
292
|
+
if(!isArray){
|
|
293
|
+
if(!sc.hasOwn(params, relation.join.from) || !params[relation.join.from]){
|
|
290
294
|
continue;
|
|
291
295
|
}
|
|
292
|
-
await this.createOne(params, i, relationEntity, newInstance,
|
|
296
|
+
await this.createOne(params, i, relationEntity, newInstance, relation);
|
|
293
297
|
continue;
|
|
294
298
|
}
|
|
295
|
-
if(
|
|
296
|
-
await this.createMany(params, i, relationEntity, newInstance,
|
|
299
|
+
if(isArray){
|
|
300
|
+
await this.createMany(params, i, relationEntity, newInstance, relation);
|
|
297
301
|
}
|
|
298
302
|
}
|
|
299
303
|
}
|
|
300
304
|
|
|
301
|
-
async createOne(params, i, relationEntity, newInstance,
|
|
305
|
+
async createOne(params, i, relationEntity, newInstance, relation)
|
|
302
306
|
{
|
|
303
|
-
params[i] = newInstance[
|
|
307
|
+
params[i] = newInstance[relation.join.from];
|
|
304
308
|
let nestedObject = relationEntity.createByProps(params[i]);
|
|
305
309
|
await this.repository.persist(nestedObject).flush();
|
|
306
310
|
await this.orm.em.flush();
|
|
307
311
|
newInstance[i] = nestedObject;
|
|
308
312
|
}
|
|
309
313
|
|
|
310
|
-
async createMany(params, i, relationEntity, newInstance,
|
|
314
|
+
async createMany(params, i, relationEntity, newInstance, relation)
|
|
311
315
|
{
|
|
312
316
|
let nestedArray = [];
|
|
313
317
|
for(let objectData of params[i]){
|
|
314
|
-
objectData[
|
|
318
|
+
objectData[relation.join.to] = newInstance[relation.join.from];
|
|
315
319
|
let nestedObject = relationEntity.createByProps(objectData);
|
|
316
320
|
await this.repository.persist(nestedObject).flush();
|
|
317
321
|
await this.orm.em.flush();
|