@reldens/storage 0.10.0-beta.33 → 0.10.0-beta.37
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 +24 -15
- package/package.json +3 -3
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
|
}
|
|
@@ -54,7 +54,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
54
54
|
|
|
55
55
|
async create(params)
|
|
56
56
|
{
|
|
57
|
-
let newInstance = this.rawModel.
|
|
57
|
+
let newInstance = this.rawModel.entity.createByProps(params);
|
|
58
58
|
await this.repository.persist(newInstance).flush();
|
|
59
59
|
return newInstance;
|
|
60
60
|
}
|
|
@@ -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,8 +232,11 @@ 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 entitiesCollection;
|
|
237
|
+
}
|
|
235
238
|
if(!sc.isArray(relations) || 0 === relations.length){
|
|
236
|
-
relations = Object.keys(this.rawModel.relationsMappings || {});
|
|
239
|
+
relations = Object.keys(this.rawModel.entity.relationsMappings() || {});
|
|
237
240
|
}
|
|
238
241
|
if(0 === relations.length){
|
|
239
242
|
return entitiesCollection;
|
|
@@ -247,8 +250,12 @@ class MikroOrmDriver extends BaseDriver
|
|
|
247
250
|
async appendRelatedEntities(entity, relations)
|
|
248
251
|
{
|
|
249
252
|
// @TODO - BETA - Refactor and improve.
|
|
253
|
+
if(typeof this.rawModel.entity.relationsMappings !== 'function'){
|
|
254
|
+
return entity;
|
|
255
|
+
}
|
|
256
|
+
let relationsMappings = this.rawModel.entity.relationsMappings();
|
|
250
257
|
for(let i of relations){
|
|
251
|
-
let relation =
|
|
258
|
+
let relation = relationsMappings[i];
|
|
252
259
|
let relationRepository = this.server.getEntity(relation.entityName);
|
|
253
260
|
let isManyToOne = 'm:1' === relation.reference;
|
|
254
261
|
let isOneToMany = '1:m' === relation.reference;
|
|
@@ -259,18 +266,20 @@ class MikroOrmDriver extends BaseDriver
|
|
|
259
266
|
entity[i] = await relationRepository.loadBy(relation.join.to, entity[relation.join.from]);
|
|
260
267
|
}
|
|
261
268
|
}
|
|
269
|
+
return entity;
|
|
262
270
|
}
|
|
263
271
|
|
|
264
272
|
async createNested(newInstance, params)
|
|
265
273
|
{
|
|
266
|
-
if(
|
|
274
|
+
if(typeof this.rawModel.entity.relationsMappings !== 'function'){
|
|
267
275
|
return false;
|
|
268
276
|
}
|
|
269
|
-
|
|
277
|
+
let relationsMappings = this.rawModel.entity.relationsMappings();
|
|
278
|
+
for(let i of Object.keys(relationsMappings)){
|
|
270
279
|
// @TODO - BETA - Refactor and improve.
|
|
271
|
-
let relationData =
|
|
272
|
-
let
|
|
273
|
-
if(!
|
|
280
|
+
let relationData = relationsMappings[i];
|
|
281
|
+
let relationEntity = this.server.entityManager.get(relationData.entityName).rawModel.entity;
|
|
282
|
+
if(!relationEntity){
|
|
274
283
|
Logger.warning('Factory not found for relation definition:', relationData);
|
|
275
284
|
continue;
|
|
276
285
|
}
|
|
@@ -280,30 +289,30 @@ class MikroOrmDriver extends BaseDriver
|
|
|
280
289
|
if(!sc.hasOwn(params, relationData.join.from) || !params[relationData.join.from]){
|
|
281
290
|
continue;
|
|
282
291
|
}
|
|
283
|
-
await this.createOne(params, i,
|
|
292
|
+
await this.createOne(params, i, relationEntity, newInstance, relationData);
|
|
284
293
|
continue;
|
|
285
294
|
}
|
|
286
295
|
if(isOneToMany){
|
|
287
|
-
await this.createMany(params, i,
|
|
296
|
+
await this.createMany(params, i, relationEntity, newInstance, relationData);
|
|
288
297
|
}
|
|
289
298
|
}
|
|
290
299
|
}
|
|
291
300
|
|
|
292
|
-
async createOne(params, i,
|
|
301
|
+
async createOne(params, i, relationEntity, newInstance, relationData)
|
|
293
302
|
{
|
|
294
303
|
params[i] = newInstance[relationData.join.from];
|
|
295
|
-
let nestedObject =
|
|
304
|
+
let nestedObject = relationEntity.createByProps(params[i]);
|
|
296
305
|
await this.repository.persist(nestedObject).flush();
|
|
297
306
|
await this.orm.em.flush();
|
|
298
307
|
newInstance[i] = nestedObject;
|
|
299
308
|
}
|
|
300
309
|
|
|
301
|
-
async createMany(params, i,
|
|
310
|
+
async createMany(params, i, relationEntity, newInstance, relationData)
|
|
302
311
|
{
|
|
303
312
|
let nestedArray = [];
|
|
304
313
|
for(let objectData of params[i]){
|
|
305
314
|
objectData[relationData.join.to] = newInstance[relationData.join.from];
|
|
306
|
-
let nestedObject =
|
|
315
|
+
let nestedObject = relationEntity.createByProps(objectData);
|
|
307
316
|
await this.repository.persist(nestedObject).flush();
|
|
308
317
|
await this.orm.em.flush();
|
|
309
318
|
nestedArray.push(nestedObject);
|
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.
|
|
4
|
+
"version": "0.10.0-beta.37",
|
|
5
5
|
"description": "Reldens - Storage",
|
|
6
6
|
"author": "Damian A. Pastorini",
|
|
7
7
|
"license": "MIT",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"url": "https://github.com/damian-pastorini/reldens-storage/issues"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@mikro-orm/core": "^5.0.
|
|
48
|
-
"@mikro-orm/mongodb": "^5.0.
|
|
47
|
+
"@mikro-orm/core": "^5.0.1",
|
|
48
|
+
"@mikro-orm/mongodb": "^5.0.1",
|
|
49
49
|
"@reldens/utils": "^0.10.0-beta.4",
|
|
50
50
|
"knex": "^1.0.3",
|
|
51
51
|
"mysql": "^2.18.1",
|