@reldens/storage 0.10.0-beta.37 → 0.11.0
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/README.md +4 -4
- package/lib/mikro-orm/mikro-orm-driver.js +40 -28
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
## About this package
|
|
6
6
|
This package is designed to provide a standard drivers support for managing Reldens project data.
|
|
7
7
|
|
|
8
|
-
This way in Reldens project we can trust that any driver
|
|
8
|
+
This way in any Reldens project we can trust that any driver implementation will have the exact same methods available to fetch and manage the project entities.
|
|
9
9
|
|
|
10
10
|
Every time you need to load, create, update or delete data in Reldens, the drivers from this package will be used.
|
|
11
11
|
|
|
12
12
|
## Current features
|
|
13
|
-
The
|
|
14
|
-
- Objection JS (with Knex), for everything that's SQL related.
|
|
13
|
+
The package currently has drivers to support two ORMs:
|
|
14
|
+
- Objection JS (with Knex), for everything that's SQL related (this is our base and recommended package).
|
|
15
15
|
```javascript
|
|
16
16
|
let server = new ObjectionJsDataServer({
|
|
17
17
|
client: 'mysql',
|
|
@@ -23,7 +23,7 @@ let server = new ObjectionJsDataServer({
|
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
25
|
```
|
|
26
|
-
- Mikro-ORM, to offer support for MongoDB.
|
|
26
|
+
- Mikro-ORM, to offer support for nonSQL/MongoDB (this package is not finished, if you like to use it please contact us).
|
|
27
27
|
```javascript
|
|
28
28
|
let server = new MikroOrmDataServer({
|
|
29
29
|
client: 'mongodb',
|
|
@@ -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
|
}
|
|
@@ -200,6 +203,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
200
203
|
return this.appendRelationsToCollection(entitiesCollection, relations);
|
|
201
204
|
}
|
|
202
205
|
|
|
206
|
+
// @TODO - BETA - Refactor and improve.
|
|
203
207
|
queryBuilder(useLimit = false, useOffset = false, useSort = false)
|
|
204
208
|
{
|
|
205
209
|
let queryBuilder = {};
|
|
@@ -217,6 +221,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
217
221
|
return queryBuilder;
|
|
218
222
|
}
|
|
219
223
|
|
|
224
|
+
// @TODO - BETA - Refactor and improve.
|
|
220
225
|
createSingleFilter(field, fieldValue, operator = null)
|
|
221
226
|
{
|
|
222
227
|
let filter = {};
|
|
@@ -229,33 +234,39 @@ class MikroOrmDriver extends BaseDriver
|
|
|
229
234
|
return filter;
|
|
230
235
|
}
|
|
231
236
|
|
|
237
|
+
// @TODO - BETA - Refactor and improve.
|
|
232
238
|
async appendRelationsToCollection(entitiesCollection, relations)
|
|
233
239
|
{
|
|
234
240
|
// @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.
|
|
241
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
236
242
|
return entitiesCollection;
|
|
237
243
|
}
|
|
238
244
|
if(!sc.isArray(relations) || 0 === relations.length){
|
|
239
|
-
relations = Object.keys(this.rawModel.entity.
|
|
245
|
+
relations = Object.keys(this.rawModel.entity.relationMappings() || {});
|
|
240
246
|
}
|
|
241
247
|
if(0 === relations.length){
|
|
242
248
|
return entitiesCollection;
|
|
243
249
|
}
|
|
244
|
-
|
|
245
|
-
|
|
250
|
+
if(sc.isArray(entitiesCollection)){
|
|
251
|
+
for(let entity of entitiesCollection){
|
|
252
|
+
await this.appendRelatedEntities(entity, relations);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if(!sc.isArray(entitiesCollection)){
|
|
256
|
+
await this.appendRelatedEntities(entitiesCollection, relations);
|
|
246
257
|
}
|
|
247
258
|
return entitiesCollection;
|
|
248
259
|
}
|
|
249
260
|
|
|
261
|
+
// @TODO - BETA - Refactor and improve.
|
|
250
262
|
async appendRelatedEntities(entity, relations)
|
|
251
263
|
{
|
|
252
|
-
|
|
253
|
-
if(typeof this.rawModel.entity.relationsMappings !== 'function'){
|
|
264
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
254
265
|
return entity;
|
|
255
266
|
}
|
|
256
|
-
let
|
|
267
|
+
let relationMappings = this.rawModel.entity.relationMappings();
|
|
257
268
|
for(let i of relations){
|
|
258
|
-
let relation =
|
|
269
|
+
let relation = relationMappings[i];
|
|
259
270
|
let relationRepository = this.server.getEntity(relation.entityName);
|
|
260
271
|
let isManyToOne = 'm:1' === relation.reference;
|
|
261
272
|
let isOneToMany = '1:m' === relation.reference;
|
|
@@ -269,49 +280,50 @@ class MikroOrmDriver extends BaseDriver
|
|
|
269
280
|
return entity;
|
|
270
281
|
}
|
|
271
282
|
|
|
283
|
+
// @TODO - BETA - Refactor and improve.
|
|
272
284
|
async createNested(newInstance, params)
|
|
273
285
|
{
|
|
274
|
-
if(typeof this.rawModel.entity.
|
|
286
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
275
287
|
return false;
|
|
276
288
|
}
|
|
277
|
-
let
|
|
278
|
-
for(let i of Object.keys(
|
|
279
|
-
|
|
280
|
-
let
|
|
281
|
-
let relationEntity = this.server.entityManager.get(relationData.entityName).rawModel.entity;
|
|
289
|
+
let relationMappings = this.rawModel.entity.relationMappings();
|
|
290
|
+
for(let i of Object.keys(relationMappings)){
|
|
291
|
+
let relation = relationMappings[i];
|
|
292
|
+
let relationEntity = this.server.entityManager.get(relation.entityName).rawModel.entity;
|
|
282
293
|
if(!relationEntity){
|
|
283
|
-
Logger.warning('Factory not found for relation definition:',
|
|
294
|
+
Logger.warning('Factory not found for relation definition:', relation);
|
|
284
295
|
continue;
|
|
285
296
|
}
|
|
286
|
-
let
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if(!sc.hasOwn(params, relationData.join.from) || !params[relationData.join.from]){
|
|
297
|
+
let isArray = sc.isArray(params);
|
|
298
|
+
if(!isArray){
|
|
299
|
+
if(!sc.hasOwn(params, relation.join.from) || !params[relation.join.from]){
|
|
290
300
|
continue;
|
|
291
301
|
}
|
|
292
|
-
await this.createOne(params, i, relationEntity, newInstance,
|
|
302
|
+
await this.createOne(params, i, relationEntity, newInstance, relation);
|
|
293
303
|
continue;
|
|
294
304
|
}
|
|
295
|
-
if(
|
|
296
|
-
await this.createMany(params, i, relationEntity, newInstance,
|
|
305
|
+
if(isArray){
|
|
306
|
+
await this.createMany(params, i, relationEntity, newInstance, relation);
|
|
297
307
|
}
|
|
298
308
|
}
|
|
299
309
|
}
|
|
300
310
|
|
|
301
|
-
|
|
311
|
+
// @TODO - BETA - Refactor and improve.
|
|
312
|
+
async createOne(params, i, relationEntity, newInstance, relation)
|
|
302
313
|
{
|
|
303
|
-
params[i] = newInstance[
|
|
314
|
+
params[i] = newInstance[relation.join.from];
|
|
304
315
|
let nestedObject = relationEntity.createByProps(params[i]);
|
|
305
316
|
await this.repository.persist(nestedObject).flush();
|
|
306
317
|
await this.orm.em.flush();
|
|
307
318
|
newInstance[i] = nestedObject;
|
|
308
319
|
}
|
|
309
320
|
|
|
310
|
-
|
|
321
|
+
// @TODO - BETA - Refactor and improve.
|
|
322
|
+
async createMany(params, i, relationEntity, newInstance, relation)
|
|
311
323
|
{
|
|
312
324
|
let nestedArray = [];
|
|
313
325
|
for(let objectData of params[i]){
|
|
314
|
-
objectData[
|
|
326
|
+
objectData[relation.join.to] = newInstance[relation.join.from];
|
|
315
327
|
let nestedObject = relationEntity.createByProps(objectData);
|
|
316
328
|
await this.repository.persist(nestedObject).flush();
|
|
317
329
|
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.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"description": "Reldens - Storage",
|
|
6
6
|
"author": "Damian A. Pastorini",
|
|
7
7
|
"license": "MIT",
|
|
@@ -44,9 +44,9 @@
|
|
|
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.
|
|
49
|
-
"@reldens/utils": "^0.
|
|
47
|
+
"@mikro-orm/core": "^5.0.3",
|
|
48
|
+
"@mikro-orm/mongodb": "^5.0.3",
|
|
49
|
+
"@reldens/utils": "^0.11.0",
|
|
50
50
|
"knex": "^1.0.3",
|
|
51
51
|
"mysql": "^2.18.1",
|
|
52
52
|
"objection": "^3.0.1"
|