@reldens/storage 0.10.0-beta.36 → 0.10.0-beta.40

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
  }
@@ -56,6 +56,7 @@ class MikroOrmDriver extends BaseDriver
56
56
  {
57
57
  let newInstance = this.rawModel.entity.createByProps(params);
58
58
  await this.repository.persist(newInstance).flush();
59
+ await this.orm.em.flush();
59
60
  return newInstance;
60
61
  }
61
62
 
@@ -74,7 +75,8 @@ class MikroOrmDriver extends BaseDriver
74
75
  }
75
76
  for(let entity of entities){
76
77
  Object.assign(entity, updatePatch);
77
- this.repository.persist(entity).flush();
78
+ await this.repository.persist(entity).flush();
79
+ await this.orm.em.flush();
78
80
  }
79
81
  return entities;
80
82
  }
@@ -88,7 +90,8 @@ class MikroOrmDriver extends BaseDriver
88
90
  }
89
91
  for(let entity of entities){
90
92
  Object.assign(entity, updatePatch);
91
- this.repository.persist(entity).flush();
93
+ await this.repository.persist(entity).flush();
94
+ await this.orm.em.flush();
92
95
  }
93
96
  return entities;
94
97
  }
@@ -132,7 +135,7 @@ class MikroOrmDriver extends BaseDriver
132
135
 
133
136
  async loadAllWithRelations(relations)
134
137
  {
135
- let entities = await this.repository.find(filter, this.queryBuilder(true, true, true));
138
+ let entities = await this.loadAll();
136
139
  return await this.appendRelationsToCollection(entities, relations);
137
140
  }
138
141
 
@@ -232,17 +235,22 @@ class MikroOrmDriver extends BaseDriver
232
235
  async appendRelationsToCollection(entitiesCollection, relations)
233
236
  {
234
237
  // @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;
238
+ if('function' !== typeof this.rawModel.entity.relationMappings){
239
+ return entitiesCollection;
237
240
  }
238
241
  if(!sc.isArray(relations) || 0 === relations.length){
239
- relations = Object.keys(this.rawModel.entity.relationsMappings() || {});
242
+ relations = Object.keys(this.rawModel.entity.relationMappings() || {});
240
243
  }
241
244
  if(0 === relations.length){
242
245
  return entitiesCollection;
243
246
  }
244
- for(let entity of entitiesCollection){
245
- await this.appendRelatedEntities(entity, relations);
247
+ if(sc.isArray(entitiesCollection)){
248
+ for(let entity of entitiesCollection){
249
+ await this.appendRelatedEntities(entity, relations);
250
+ }
251
+ }
252
+ if(!sc.isArray(entitiesCollection)){
253
+ await this.appendRelatedEntities(entitiesCollection, relations);
246
254
  }
247
255
  return entitiesCollection;
248
256
  }
@@ -250,12 +258,12 @@ class MikroOrmDriver extends BaseDriver
250
258
  async appendRelatedEntities(entity, relations)
251
259
  {
252
260
  // @TODO - BETA - Refactor and improve.
253
- if(typeof this.rawModel.entity.relationsMappings !== 'function'){
254
- return false;
261
+ if('function' !== typeof this.rawModel.entity.relationMappings){
262
+ return entity;
255
263
  }
256
- let relationsMappings = this.rawModel.entity.relationsMappings();
264
+ let relationMappings = this.rawModel.entity.relationMappings();
257
265
  for(let i of relations){
258
- let relation = relationsMappings[i];
266
+ let relation = relationMappings[i];
259
267
  let relationRepository = this.server.getEntity(relation.entityName);
260
268
  let isManyToOne = 'm:1' === relation.reference;
261
269
  let isOneToMany = '1:m' === relation.reference;
@@ -266,51 +274,51 @@ class MikroOrmDriver extends BaseDriver
266
274
  entity[i] = await relationRepository.loadBy(relation.join.to, entity[relation.join.from]);
267
275
  }
268
276
  }
277
+ return entity;
269
278
  }
270
279
 
271
280
  async createNested(newInstance, params)
272
281
  {
273
- if(typeof this.rawModel.entity.relationsMappings !== 'function'){
282
+ if('function' !== typeof this.rawModel.entity.relationMappings){
274
283
  return false;
275
284
  }
276
- let relationsMappings = this.rawModel.entity.relationsMappings();
277
- for(let i of Object.keys(relationsMappings)){
285
+ let relationMappings = this.rawModel.entity.relationMappings();
286
+ for(let i of Object.keys(relationMappings)){
278
287
  // @TODO - BETA - Refactor and improve.
279
- let relationData = relationsMappings[i];
280
- let relationEntity = this.server.entityManager.get(relationData.entityName).rawModel.entity;
288
+ let relation = relationMappings[i];
289
+ let relationEntity = this.server.entityManager.get(relation.entityName).rawModel.entity;
281
290
  if(!relationEntity){
282
- Logger.warning('Factory not found for relation definition:', relationData);
291
+ Logger.warning('Factory not found for relation definition:', relation);
283
292
  continue;
284
293
  }
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]){
294
+ let isArray = sc.isArray(params);
295
+ if(!isArray){
296
+ if(!sc.hasOwn(params, relation.join.from) || !params[relation.join.from]){
289
297
  continue;
290
298
  }
291
- await this.createOne(params, i, relationEntity, newInstance, relationData);
299
+ await this.createOne(params, i, relationEntity, newInstance, relation);
292
300
  continue;
293
301
  }
294
- if(isOneToMany){
295
- await this.createMany(params, i, relationEntity, newInstance, relationData);
302
+ if(isArray){
303
+ await this.createMany(params, i, relationEntity, newInstance, relation);
296
304
  }
297
305
  }
298
306
  }
299
307
 
300
- async createOne(params, i, relationEntity, newInstance, relationData)
308
+ async createOne(params, i, relationEntity, newInstance, relation)
301
309
  {
302
- params[i] = newInstance[relationData.join.from];
310
+ params[i] = newInstance[relation.join.from];
303
311
  let nestedObject = relationEntity.createByProps(params[i]);
304
312
  await this.repository.persist(nestedObject).flush();
305
313
  await this.orm.em.flush();
306
314
  newInstance[i] = nestedObject;
307
315
  }
308
316
 
309
- async createMany(params, i, relationEntity, newInstance, relationData)
317
+ async createMany(params, i, relationEntity, newInstance, relation)
310
318
  {
311
319
  let nestedArray = [];
312
320
  for(let objectData of params[i]){
313
- objectData[relationData.join.to] = newInstance[relationData.join.from];
321
+ objectData[relation.join.to] = newInstance[relation.join.from];
314
322
  let nestedObject = relationEntity.createByProps(objectData);
315
323
  await this.repository.persist(nestedObject).flush();
316
324
  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.36",
4
+ "version": "0.10.0-beta.40",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",