@reldens/storage 0.61.0 → 0.63.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.
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const { BaseDriver } = require('../base-driver');
|
|
8
|
-
const {
|
|
8
|
+
const { Logger, sc } = require('@reldens/utils');
|
|
9
9
|
|
|
10
10
|
class MikroOrmDriver extends BaseDriver
|
|
11
11
|
{
|
|
@@ -14,13 +14,16 @@ class MikroOrmDriver extends BaseDriver
|
|
|
14
14
|
{
|
|
15
15
|
super(props);
|
|
16
16
|
if(!props.orm){
|
|
17
|
-
|
|
17
|
+
Logger.critical('Missing ORM on Mikro ORM driver.');
|
|
18
|
+
return false;
|
|
18
19
|
}
|
|
19
20
|
if(!props.server){
|
|
20
|
-
|
|
21
|
+
Logger.critical('Missing Server Driver on Mikro ORM driver.');
|
|
22
|
+
return false;
|
|
21
23
|
}
|
|
22
24
|
if(!this.rawModel){
|
|
23
|
-
|
|
25
|
+
Logger.critical('Missing raw entity on Mikro ORM driver.');
|
|
26
|
+
return false;
|
|
24
27
|
}
|
|
25
28
|
this.orm = props.orm;
|
|
26
29
|
this.server = props.server;
|
|
@@ -52,6 +55,14 @@ class MikroOrmDriver extends BaseDriver
|
|
|
52
55
|
return this.rawModel[propertyName] || null;
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
getAllRelations()
|
|
59
|
+
{
|
|
60
|
+
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
return Object.keys(this.rawModel.entity.relationMappings() || {});
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
async create(params)
|
|
56
67
|
{
|
|
57
68
|
let newInstance = await this.orm.em.create(this.rawModel, params);
|
|
@@ -61,9 +72,12 @@ class MikroOrmDriver extends BaseDriver
|
|
|
61
72
|
|
|
62
73
|
async createWithRelations(params, relations)
|
|
63
74
|
{
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
76
|
+
relations = this.getAllRelations();
|
|
77
|
+
}
|
|
78
|
+
let newInstance = await this.create(params);
|
|
79
|
+
await this.createNested(newInstance, params, relations);
|
|
80
|
+
return await this.appendRelationsToCollection(newInstance, relations);
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
async update(filters, updatePatch)
|
|
@@ -143,8 +157,42 @@ class MikroOrmDriver extends BaseDriver
|
|
|
143
157
|
|
|
144
158
|
async countWithRelations(filters, relations)
|
|
145
159
|
{
|
|
160
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
161
|
+
relations = this.getAllRelations();
|
|
162
|
+
}
|
|
146
163
|
let processedFilters = this.processFilters(filters);
|
|
147
|
-
|
|
164
|
+
if(0 === relations.length || 'function' !== typeof this.rawModel.entity.relationMappings){
|
|
165
|
+
return this.repository.count(processedFilters);
|
|
166
|
+
}
|
|
167
|
+
let queryBuilder = this.orm.em.createQueryBuilder(this.rawModel);
|
|
168
|
+
let relationMappings = this.rawModel.entity.relationMappings();
|
|
169
|
+
let aliasCounter = 0;
|
|
170
|
+
for(let relationName of relations){
|
|
171
|
+
let relation = relationMappings[relationName];
|
|
172
|
+
if(!relation){
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
let relationEntity = this.server.entityManager.get(relation.entityName);
|
|
176
|
+
if(!relationEntity || !relationEntity.rawModel){
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
let alias = 'rel_'+aliasCounter;
|
|
180
|
+
let joinField = relation.join.from;
|
|
181
|
+
let targetField = relation.join.to;
|
|
182
|
+
let isManyToOne = 'm:1' === relation.reference;
|
|
183
|
+
let isOneToMany = '1:m' === relation.reference;
|
|
184
|
+
if(isManyToOne){
|
|
185
|
+
queryBuilder.leftJoin(relationEntity.rawModel, alias, 'e.'+joinField+' = '+alias+'.'+targetField);
|
|
186
|
+
}
|
|
187
|
+
if(isOneToMany){
|
|
188
|
+
queryBuilder.leftJoin(relationEntity.rawModel, alias, 'e.'+joinField+' = '+alias+'.'+targetField);
|
|
189
|
+
}
|
|
190
|
+
aliasCounter++;
|
|
191
|
+
}
|
|
192
|
+
if(sc.isObject(processedFilters) && 0 < Object.keys(processedFilters).length){
|
|
193
|
+
queryBuilder.where(processedFilters);
|
|
194
|
+
}
|
|
195
|
+
return queryBuilder.getCount();
|
|
148
196
|
}
|
|
149
197
|
|
|
150
198
|
loadAll()
|
|
@@ -154,6 +202,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
154
202
|
|
|
155
203
|
async loadAllWithRelations(relations)
|
|
156
204
|
{
|
|
205
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
206
|
+
relations = this.getAllRelations();
|
|
207
|
+
}
|
|
157
208
|
let entities = await this.loadAll();
|
|
158
209
|
return await this.appendRelationsToCollection(entities, relations);
|
|
159
210
|
}
|
|
@@ -166,6 +217,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
166
217
|
|
|
167
218
|
async loadWithRelations(filters, relations)
|
|
168
219
|
{
|
|
220
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
221
|
+
relations = this.getAllRelations();
|
|
222
|
+
}
|
|
169
223
|
let processedFilters = this.processFilters(filters);
|
|
170
224
|
let entitiesCollection = await this.repository.find(processedFilters, this.queryBuilder(true, true, true));
|
|
171
225
|
return await this.appendRelationsToCollection(entitiesCollection, relations);
|
|
@@ -180,6 +234,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
180
234
|
|
|
181
235
|
async loadByWithRelations(field, fieldValue, relations, operator = null)
|
|
182
236
|
{
|
|
237
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
238
|
+
relations = this.getAllRelations();
|
|
239
|
+
}
|
|
183
240
|
let filter = this.createSingleFilter(field, fieldValue, operator);
|
|
184
241
|
let processedFilters = this.processFilters(filter);
|
|
185
242
|
let entitiesCollection = await this.repository.find(processedFilters, this.queryBuilder(true, true, true));
|
|
@@ -193,6 +250,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
193
250
|
|
|
194
251
|
async loadByIdWithRelations(id, relations)
|
|
195
252
|
{
|
|
253
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
254
|
+
relations = this.getAllRelations();
|
|
255
|
+
}
|
|
196
256
|
let entity = await this.loadBy('id', id);
|
|
197
257
|
return await this.appendRelationsToCollection(entity, relations);
|
|
198
258
|
}
|
|
@@ -211,6 +271,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
211
271
|
|
|
212
272
|
async loadOneWithRelations(filters, relations)
|
|
213
273
|
{
|
|
274
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
275
|
+
relations = this.getAllRelations();
|
|
276
|
+
}
|
|
214
277
|
let processedFilters = this.processFilters(filters);
|
|
215
278
|
let entitiesCollection = await this.repository.findOne(processedFilters, this.queryBuilder(false, true, true));
|
|
216
279
|
return this.appendRelationsToCollection(entitiesCollection, relations);
|
|
@@ -225,6 +288,9 @@ class MikroOrmDriver extends BaseDriver
|
|
|
225
288
|
|
|
226
289
|
async loadOneByWithRelations(field, fieldValue, relations, operator = null)
|
|
227
290
|
{
|
|
291
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
292
|
+
relations = this.getAllRelations();
|
|
293
|
+
}
|
|
228
294
|
let filter = this.createSingleFilter(field, fieldValue, operator);
|
|
229
295
|
let processedFilters = this.processFilters(filter);
|
|
230
296
|
let entitiesCollection = await this.repository.findOne(processedFilters, this.queryBuilder(false, true, true));
|
|
@@ -321,7 +387,7 @@ class MikroOrmDriver extends BaseDriver
|
|
|
321
387
|
return entitiesCollection;
|
|
322
388
|
}
|
|
323
389
|
if(!sc.isArray(relations) || 0 === relations.length){
|
|
324
|
-
relations =
|
|
390
|
+
relations = this.getAllRelations();
|
|
325
391
|
}
|
|
326
392
|
if(0 === relations.length){
|
|
327
393
|
return entitiesCollection;
|
|
@@ -358,13 +424,16 @@ class MikroOrmDriver extends BaseDriver
|
|
|
358
424
|
return entity;
|
|
359
425
|
}
|
|
360
426
|
|
|
361
|
-
async createNested(newInstance, params)
|
|
427
|
+
async createNested(newInstance, params, relations)
|
|
362
428
|
{
|
|
363
429
|
if('function' !== typeof this.rawModel.entity.relationMappings){
|
|
364
430
|
return false;
|
|
365
431
|
}
|
|
366
432
|
let relationMappings = this.rawModel.entity.relationMappings();
|
|
367
433
|
for(let i of Object.keys(relationMappings)){
|
|
434
|
+
if(sc.isArray(relations) && 0 < relations.length && -1 === relations.indexOf(i)){
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
368
437
|
let relation = relationMappings[i];
|
|
369
438
|
let relationEntity = this.server.entityManager.get(relation.entityName).rawModel.entity;
|
|
370
439
|
if(!relationEntity){
|
|
@@ -124,6 +124,11 @@ class PrismaDriver extends BaseDriver
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
getAllRelations()
|
|
128
|
+
{
|
|
129
|
+
return Object.keys(this.relationMetadata || {});
|
|
130
|
+
}
|
|
131
|
+
|
|
127
132
|
isNumericFieldType(fieldType)
|
|
128
133
|
{
|
|
129
134
|
return ('Int' === fieldType || 'BigInt' === fieldType || 'Float' === fieldType || 'Decimal' === fieldType);
|
|
@@ -514,10 +519,15 @@ class PrismaDriver extends BaseDriver
|
|
|
514
519
|
|
|
515
520
|
async createWithRelations(params, relations)
|
|
516
521
|
{
|
|
522
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
523
|
+
relations = this.getAllRelations();
|
|
524
|
+
}
|
|
517
525
|
let preparedData = this.prepareData(params);
|
|
518
526
|
this.ensureRequiredFields(preparedData);
|
|
519
527
|
let createData = {data: preparedData};
|
|
520
|
-
if(
|
|
528
|
+
if(0 < relations.length){
|
|
529
|
+
let includeConfig = this.buildIncludeObject(relations);
|
|
530
|
+
createData.include = includeConfig;
|
|
521
531
|
for(let relation of relations){
|
|
522
532
|
if(!sc.hasOwn(params, relation)){
|
|
523
533
|
continue;
|
|
@@ -535,7 +545,7 @@ class PrismaDriver extends BaseDriver
|
|
|
535
545
|
}
|
|
536
546
|
}
|
|
537
547
|
try {
|
|
538
|
-
return await this.model.create(createData);
|
|
548
|
+
return this.transformRelationResults(await this.model.create(createData), createData.include);
|
|
539
549
|
} catch(error) {
|
|
540
550
|
Logger.error('Create with relations error: '+error.message);
|
|
541
551
|
return false;
|
|
@@ -674,11 +684,14 @@ class PrismaDriver extends BaseDriver
|
|
|
674
684
|
|
|
675
685
|
async countWithRelations(filters, relations)
|
|
676
686
|
{
|
|
687
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
688
|
+
relations = this.getAllRelations();
|
|
689
|
+
}
|
|
677
690
|
let processedFilters = this.processFilters(filters);
|
|
678
691
|
let query = {
|
|
679
692
|
where: processedFilters
|
|
680
693
|
};
|
|
681
|
-
if(
|
|
694
|
+
if(0 < relations.length){
|
|
682
695
|
query.include = this.buildIncludeObject(relations);
|
|
683
696
|
}
|
|
684
697
|
try {
|
|
@@ -701,9 +714,12 @@ class PrismaDriver extends BaseDriver
|
|
|
701
714
|
|
|
702
715
|
async loadAllWithRelations(relations)
|
|
703
716
|
{
|
|
717
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
718
|
+
relations = this.getAllRelations();
|
|
719
|
+
}
|
|
704
720
|
let query = this.buildQueryOptions();
|
|
705
721
|
let includeConfig = {};
|
|
706
|
-
if(
|
|
722
|
+
if(0 < relations.length){
|
|
707
723
|
includeConfig = this.buildIncludeObject(relations);
|
|
708
724
|
query.include = includeConfig;
|
|
709
725
|
}
|
|
@@ -731,11 +747,14 @@ class PrismaDriver extends BaseDriver
|
|
|
731
747
|
|
|
732
748
|
async loadWithRelations(filters, relations)
|
|
733
749
|
{
|
|
750
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
751
|
+
relations = this.getAllRelations();
|
|
752
|
+
}
|
|
734
753
|
let processedFilters = this.processFilters(filters);
|
|
735
754
|
let query = this.buildQueryOptions();
|
|
736
755
|
query.where = processedFilters;
|
|
737
756
|
let includeConfig = {};
|
|
738
|
-
if(
|
|
757
|
+
if(0 < relations.length){
|
|
739
758
|
includeConfig = this.buildIncludeObject(relations);
|
|
740
759
|
query.include = includeConfig;
|
|
741
760
|
}
|
|
@@ -763,11 +782,14 @@ class PrismaDriver extends BaseDriver
|
|
|
763
782
|
|
|
764
783
|
async loadByWithRelations(field, fieldValue, relations, operator = null)
|
|
765
784
|
{
|
|
785
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
786
|
+
relations = this.getAllRelations();
|
|
787
|
+
}
|
|
766
788
|
let filter = this.createSingleFilter(field, fieldValue, operator);
|
|
767
789
|
let query = this.buildQueryOptions();
|
|
768
790
|
query.where = filter;
|
|
769
791
|
let includeConfig = {};
|
|
770
|
-
if(
|
|
792
|
+
if(0 < relations.length){
|
|
771
793
|
includeConfig = this.buildIncludeObject(relations);
|
|
772
794
|
query.include = includeConfig;
|
|
773
795
|
}
|
|
@@ -795,12 +817,15 @@ class PrismaDriver extends BaseDriver
|
|
|
795
817
|
|
|
796
818
|
async loadByIdWithRelations(id, relations)
|
|
797
819
|
{
|
|
820
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
821
|
+
relations = this.getAllRelations();
|
|
822
|
+
}
|
|
798
823
|
let castedId = this.castToIdType(id);
|
|
799
824
|
let query = {
|
|
800
825
|
where: {id: castedId}
|
|
801
826
|
};
|
|
802
827
|
let includeConfig = {};
|
|
803
|
-
if(
|
|
828
|
+
if(0 < relations.length){
|
|
804
829
|
includeConfig = this.buildIncludeObject(relations);
|
|
805
830
|
query.include = includeConfig;
|
|
806
831
|
}
|
|
@@ -843,11 +868,14 @@ class PrismaDriver extends BaseDriver
|
|
|
843
868
|
|
|
844
869
|
async loadOneWithRelations(filters, relations)
|
|
845
870
|
{
|
|
871
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
872
|
+
relations = this.getAllRelations();
|
|
873
|
+
}
|
|
846
874
|
let processedFilters = this.processFilters(filters);
|
|
847
875
|
let query = this.buildQueryOptions(false);
|
|
848
876
|
query.where = processedFilters;
|
|
849
877
|
let includeConfig = {};
|
|
850
|
-
if(
|
|
878
|
+
if(0 < relations.length){
|
|
851
879
|
includeConfig = this.buildIncludeObject(relations);
|
|
852
880
|
query.include = includeConfig;
|
|
853
881
|
}
|
|
@@ -875,11 +903,14 @@ class PrismaDriver extends BaseDriver
|
|
|
875
903
|
|
|
876
904
|
async loadOneByWithRelations(field, fieldValue, relations, operator = null)
|
|
877
905
|
{
|
|
906
|
+
if(!sc.isArray(relations) || 0 === relations.length){
|
|
907
|
+
relations = this.getAllRelations();
|
|
908
|
+
}
|
|
878
909
|
let filter = this.createSingleFilter(field, fieldValue, operator);
|
|
879
910
|
let query = this.buildQueryOptions(false);
|
|
880
911
|
query.where = filter;
|
|
881
912
|
let includeConfig = {};
|
|
882
|
-
if(
|
|
913
|
+
if(0 < relations.length){
|
|
883
914
|
includeConfig = this.buildIncludeObject(relations);
|
|
884
915
|
query.include = includeConfig;
|
|
885
916
|
}
|
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.63.0",
|
|
5
5
|
"description": "Reldens - Storage",
|
|
6
6
|
"author": "Damian A. Pastorini",
|
|
7
7
|
"license": "MIT",
|
|
@@ -45,13 +45,13 @@
|
|
|
45
45
|
"@mikro-orm/core": "^6.4.16",
|
|
46
46
|
"@mikro-orm/mongodb": "^6.4.16",
|
|
47
47
|
"@mikro-orm/mysql": "^6.4.16",
|
|
48
|
-
"@prisma/client": "^6.
|
|
49
|
-
"@reldens/server-utils": "^0.
|
|
50
|
-
"@reldens/utils": "^0.
|
|
48
|
+
"@prisma/client": "^6.12.0",
|
|
49
|
+
"@reldens/server-utils": "^0.23.0",
|
|
50
|
+
"@reldens/utils": "^0.51.0",
|
|
51
51
|
"knex": "^3.1.0",
|
|
52
52
|
"mysql": "^2.18.1",
|
|
53
|
-
"mysql2": "^3.14.
|
|
53
|
+
"mysql2": "^3.14.2",
|
|
54
54
|
"objection": "^3.1.5",
|
|
55
|
-
"prisma": "^6.
|
|
55
|
+
"prisma": "^6.12.0"
|
|
56
56
|
}
|
|
57
57
|
}
|