@reldens/storage 0.10.0-beta.35 → 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.
- package/lib/base-driver.js +1 -1
- package/lib/mikro-orm/mikro-orm-driver.js +32 -27
- package/package.json +1 -1
package/lib/base-driver.js
CHANGED
|
@@ -162,7 +162,7 @@ class BaseDriver
|
|
|
162
162
|
Logger.error('Custom query method not found in raw model.', methodName, this.rawModel);
|
|
163
163
|
return false;
|
|
164
164
|
}
|
|
165
|
-
return this.rawModel[methodName](methodOptions);
|
|
165
|
+
return this.rawModel[methodName](methodOptions, this);
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
}
|
|
@@ -132,7 +132,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
132
132
|
|
|
133
133
|
async loadAllWithRelations(relations)
|
|
134
134
|
{
|
|
135
|
-
let entities = await this.
|
|
135
|
+
let entities = await this.loadAll();
|
|
136
136
|
return await this.appendRelationsToCollection(entities, relations);
|
|
137
137
|
}
|
|
138
138
|
|
|
@@ -232,17 +232,22 @@ 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.
|
|
236
|
-
return
|
|
235
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
236
|
+
return entitiesCollection;
|
|
237
237
|
}
|
|
238
238
|
if(!sc.isArray(relations) || 0 === relations.length){
|
|
239
|
-
relations = Object.keys(this.rawModel.entity.
|
|
239
|
+
relations = Object.keys(this.rawModel.entity.relationMappings() || {});
|
|
240
240
|
}
|
|
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,12 +255,12 @@ 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.
|
|
254
|
-
return
|
|
258
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
259
|
+
return entity;
|
|
255
260
|
}
|
|
256
|
-
let
|
|
261
|
+
let relationMappings = this.rawModel.entity.relationMappings();
|
|
257
262
|
for(let i of relations){
|
|
258
|
-
let relation =
|
|
263
|
+
let relation = relationMappings[i];
|
|
259
264
|
let relationRepository = this.server.getEntity(relation.entityName);
|
|
260
265
|
let isManyToOne = 'm:1' === relation.reference;
|
|
261
266
|
let isOneToMany = '1:m' === relation.reference;
|
|
@@ -266,51 +271,51 @@ class MikroOrmDriver extends BaseDriver
|
|
|
266
271
|
entity[i] = await relationRepository.loadBy(relation.join.to, entity[relation.join.from]);
|
|
267
272
|
}
|
|
268
273
|
}
|
|
274
|
+
return entity;
|
|
269
275
|
}
|
|
270
276
|
|
|
271
277
|
async createNested(newInstance, params)
|
|
272
278
|
{
|
|
273
|
-
if(typeof this.rawModel.entity.
|
|
279
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
274
280
|
return false;
|
|
275
281
|
}
|
|
276
|
-
let
|
|
277
|
-
for(let i of Object.keys(
|
|
282
|
+
let relationMappings = this.rawModel.entity.relationMappings();
|
|
283
|
+
for(let i of Object.keys(relationMappings)){
|
|
278
284
|
// @TODO - BETA - Refactor and improve.
|
|
279
|
-
let
|
|
280
|
-
let relationEntity = this.server.entityManager.get(
|
|
285
|
+
let relation = relationMappings[i];
|
|
286
|
+
let relationEntity = this.server.entityManager.get(relation.entityName).rawModel.entity;
|
|
281
287
|
if(!relationEntity){
|
|
282
|
-
Logger.warning('Factory not found for relation definition:',
|
|
288
|
+
Logger.warning('Factory not found for relation definition:', relation);
|
|
283
289
|
continue;
|
|
284
290
|
}
|
|
285
|
-
let
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
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]){
|
|
289
294
|
continue;
|
|
290
295
|
}
|
|
291
|
-
await this.createOne(params, i, relationEntity, newInstance,
|
|
296
|
+
await this.createOne(params, i, relationEntity, newInstance, relation);
|
|
292
297
|
continue;
|
|
293
298
|
}
|
|
294
|
-
if(
|
|
295
|
-
await this.createMany(params, i, relationEntity, newInstance,
|
|
299
|
+
if(isArray){
|
|
300
|
+
await this.createMany(params, i, relationEntity, newInstance, relation);
|
|
296
301
|
}
|
|
297
302
|
}
|
|
298
303
|
}
|
|
299
304
|
|
|
300
|
-
async createOne(params, i, relationEntity, newInstance,
|
|
305
|
+
async createOne(params, i, relationEntity, newInstance, relation)
|
|
301
306
|
{
|
|
302
|
-
params[i] = newInstance[
|
|
307
|
+
params[i] = newInstance[relation.join.from];
|
|
303
308
|
let nestedObject = relationEntity.createByProps(params[i]);
|
|
304
309
|
await this.repository.persist(nestedObject).flush();
|
|
305
310
|
await this.orm.em.flush();
|
|
306
311
|
newInstance[i] = nestedObject;
|
|
307
312
|
}
|
|
308
313
|
|
|
309
|
-
async createMany(params, i, relationEntity, newInstance,
|
|
314
|
+
async createMany(params, i, relationEntity, newInstance, relation)
|
|
310
315
|
{
|
|
311
316
|
let nestedArray = [];
|
|
312
317
|
for(let objectData of params[i]){
|
|
313
|
-
objectData[
|
|
318
|
+
objectData[relation.join.to] = newInstance[relation.join.from];
|
|
314
319
|
let nestedObject = relationEntity.createByProps(objectData);
|
|
315
320
|
await this.repository.persist(nestedObject).flush();
|
|
316
321
|
await this.orm.em.flush();
|