@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.
@@ -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.repository.find(filter, this.queryBuilder(true, true, true));
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.relationsMappings !== 'function'){
236
- return false;
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.relationsMappings() || {});
239
+ relations = Object.keys(this.rawModel.entity.relationMappings() || {});
240
240
  }
241
241
  if(0 === relations.length){
242
242
  return entitiesCollection;
243
243
  }
244
- for(let entity of entitiesCollection){
245
- await this.appendRelatedEntities(entity, relations);
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.relationsMappings !== 'function'){
254
- return false;
258
+ if('function' !== typeof this.rawModel.entity.relationMappings){
259
+ return entity;
255
260
  }
256
- let relationsMappings = typeof this.rawModel.entity.relationsMappings();
261
+ let relationMappings = this.rawModel.entity.relationMappings();
257
262
  for(let i of relations){
258
- let relation = relationsMappings[i];
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.relationsMappings !== 'function'){
279
+ if('function' !== typeof this.rawModel.entity.relationMappings){
274
280
  return false;
275
281
  }
276
- let relationsMappings = this.rawModel.entity.relationsMappings();
277
- for(let i of Object.keys(relationsMappings)){
282
+ let relationMappings = this.rawModel.entity.relationMappings();
283
+ for(let i of Object.keys(relationMappings)){
278
284
  // @TODO - BETA - Refactor and improve.
279
- let relationData = relationsMappings[i];
280
- let relationEntity = this.server.entityManager.get(relationData.entityName).rawModel.entity;
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:', relationData);
288
+ Logger.warning('Factory not found for relation definition:', relation);
283
289
  continue;
284
290
  }
285
- let isManyToOne = 'm:1' === relationData.reference;
286
- let isOneToMany = '1:m' === relationData.reference;
287
- if(isManyToOne){
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, relationData);
296
+ await this.createOne(params, i, relationEntity, newInstance, relation);
292
297
  continue;
293
298
  }
294
- if(isOneToMany){
295
- await this.createMany(params, i, relationEntity, newInstance, relationData);
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, relationData)
305
+ async createOne(params, i, relationEntity, newInstance, relation)
301
306
  {
302
- params[i] = newInstance[relationData.join.from];
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, relationData)
314
+ async createMany(params, i, relationEntity, newInstance, relation)
310
315
  {
311
316
  let nestedArray = [];
312
317
  for(let objectData of params[i]){
313
- objectData[relationData.join.to] = newInstance[relationData.join.from];
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();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.10.0-beta.35",
4
+ "version": "0.10.0-beta.39",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",