@reldens/storage 0.60.0 → 0.62.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 { EntityManager } = require('./entity-manager');
8
- const { ErrorManager, sc } = require('@reldens/utils');
8
+ const { ErrorManager, Logger, sc } = require('@reldens/utils');
9
9
 
10
10
  class BaseDataServer
11
11
  {
@@ -16,6 +16,9 @@ class BaseDataServer
16
16
  if(!this.config.host){
17
17
  this.config.host = 'localhost';
18
18
  }
19
+ if(!this.config.port){
20
+ this.config.port = 3306;
21
+ }
19
22
  this.client = sc.get(props, 'client', '');
20
23
  this.poolConfig = sc.get(props, 'poolConfig', {});
21
24
  this.connectStringOptions = sc.get(props, 'connectStringOptions', '');
@@ -33,11 +36,17 @@ class BaseDataServer
33
36
 
34
37
  createConnectionString()
35
38
  {
36
- return (this.client || 'client-not-specified')+'://'
37
- +(this.config.user || 'user-not-specified')
39
+ if('' === this.client){
40
+ Logger.warning('Client is empty, connection may fail.');
41
+ }
42
+ if('' === this.config.user){
43
+ Logger.warning('User is empty, connection may fail.');
44
+ }
45
+ return (this.client || '')+'://'
46
+ +(this.config.user || '')
38
47
  +(this.config.password ? ':'+this.config.password : '')
39
48
  +'@'+this.config.host
40
- +':'+(this.config.port || 3306)
49
+ +':'+this.config.port
41
50
  +(this.config.database ? '/'+this.config.database : '')
42
51
  +(this.connectStringOptions ? '?'+this.connectStringOptions : '');
43
52
  }
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  const { BaseDriver } = require('../base-driver');
8
- const { ErrorManager, Logger, sc } = require('@reldens/utils');
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
- ErrorManager.error('Missing ORM on Mikro ORM driver.');
17
+ Logger.critical('Missing ORM on Mikro ORM driver.');
18
+ return false;
18
19
  }
19
20
  if(!props.server){
20
- ErrorManager.error('Missing Server Driver on Mikro ORM driver.');
21
+ Logger.critical('Missing Server Driver on Mikro ORM driver.');
22
+ return false;
21
23
  }
22
24
  if(!this.rawModel){
23
- ErrorManager.error('Missing raw entity on Mikro ORM driver.');
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
- let newInstance = await this.create(params, relations);
65
- await this.createNested(newInstance, params);
66
- return newInstance;
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
- return this.repository.count(processedFilters);
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 = Object.keys(this.rawModel.entity.relationMappings() || {});
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){
@@ -38,7 +38,7 @@ class PrismaDriver extends BaseDriver
38
38
 
39
39
  initModelMetadata()
40
40
  {
41
- let dmmf = this.prisma._runtimeDataModel || Prisma.dmmf?.datamodel;
41
+ let dmmf = this.getDmmf();
42
42
  if(!dmmf){
43
43
  Logger.warning('Could not access Prisma DMMF metadata');
44
44
  return;
@@ -61,6 +61,28 @@ class PrismaDriver extends BaseDriver
61
61
  }
62
62
  }
63
63
 
64
+ getDmmf()
65
+ {
66
+ try {
67
+ if(this.prisma._dmmf){
68
+ return this.prisma._dmmf.datamodel;
69
+ }
70
+ if(this.prisma._getDmmf && sc.isFunction(this.prisma._getDmmf)){
71
+ return this.prisma._getDmmf().datamodel;
72
+ }
73
+ if(this.prisma._runtimeDataModel){
74
+ return this.prisma._runtimeDataModel;
75
+ }
76
+ if(Prisma.dmmf && Prisma.dmmf.datamodel){
77
+ return Prisma.dmmf.datamodel;
78
+ }
79
+ return null;
80
+ } catch(error) {
81
+ Logger.warning('Failed to access DMMF: '+error.message);
82
+ return null;
83
+ }
84
+ }
85
+
64
86
  processFieldMetadata(field)
65
87
  {
66
88
  this.fieldTypes[field.name] = field.type;
@@ -102,6 +124,11 @@ class PrismaDriver extends BaseDriver
102
124
  }
103
125
  }
104
126
 
127
+ getAllRelations()
128
+ {
129
+ return Object.keys(this.relationMetadata || {});
130
+ }
131
+
105
132
  isNumericFieldType(fieldType)
106
133
  {
107
134
  return ('Int' === fieldType || 'BigInt' === fieldType || 'Float' === fieldType || 'Decimal' === fieldType);
@@ -312,7 +339,7 @@ class PrismaDriver extends BaseDriver
312
339
 
313
340
  tableName()
314
341
  {
315
- return this.model.name || '';
342
+ return this.model.$name || this.model.name || '';
316
343
  }
317
344
 
318
345
  property(propertyName)
@@ -337,7 +364,7 @@ class PrismaDriver extends BaseDriver
337
364
  return data;
338
365
  }
339
366
 
340
- prepareDataWithRelations(params)
367
+ prepareDataWithRelations(params, isCreate = false)
341
368
  {
342
369
  let data = {};
343
370
  let relations = {};
@@ -351,9 +378,11 @@ class PrismaDriver extends BaseDriver
351
378
  };
352
379
  continue;
353
380
  }
354
- relations[relationName] = {
355
- disconnect: true
356
- };
381
+ if(!isCreate){
382
+ relations[relationName] = {
383
+ disconnect: true
384
+ };
385
+ }
357
386
  continue;
358
387
  }
359
388
  let fieldType = this.fieldTypes[key];
@@ -476,7 +505,7 @@ class PrismaDriver extends BaseDriver
476
505
 
477
506
  async create(params)
478
507
  {
479
- let preparedData = this.prepareDataWithRelations(params);
508
+ let preparedData = this.prepareDataWithRelations(params, true);
480
509
  this.ensureRequiredFields(preparedData);
481
510
  try {
482
511
  return await this.model.create({
@@ -490,10 +519,15 @@ class PrismaDriver extends BaseDriver
490
519
 
491
520
  async createWithRelations(params, relations)
492
521
  {
522
+ if(!sc.isArray(relations) || 0 === relations.length){
523
+ relations = this.getAllRelations();
524
+ }
493
525
  let preparedData = this.prepareData(params);
494
526
  this.ensureRequiredFields(preparedData);
495
527
  let createData = {data: preparedData};
496
- if(sc.isArray(relations) && 0 < relations.length){
528
+ if(0 < relations.length){
529
+ let includeConfig = this.buildIncludeObject(relations);
530
+ createData.include = includeConfig;
497
531
  for(let relation of relations){
498
532
  if(!sc.hasOwn(params, relation)){
499
533
  continue;
@@ -511,7 +545,7 @@ class PrismaDriver extends BaseDriver
511
545
  }
512
546
  }
513
547
  try {
514
- return await this.model.create(createData);
548
+ return this.transformRelationResults(await this.model.create(createData), createData.include);
515
549
  } catch(error) {
516
550
  Logger.error('Create with relations error: '+error.message);
517
551
  return false;
@@ -650,11 +684,14 @@ class PrismaDriver extends BaseDriver
650
684
 
651
685
  async countWithRelations(filters, relations)
652
686
  {
687
+ if(!sc.isArray(relations) || 0 === relations.length){
688
+ relations = this.getAllRelations();
689
+ }
653
690
  let processedFilters = this.processFilters(filters);
654
691
  let query = {
655
692
  where: processedFilters
656
693
  };
657
- if(sc.isArray(relations) && 0 < relations.length){
694
+ if(0 < relations.length){
658
695
  query.include = this.buildIncludeObject(relations);
659
696
  }
660
697
  try {
@@ -677,9 +714,12 @@ class PrismaDriver extends BaseDriver
677
714
 
678
715
  async loadAllWithRelations(relations)
679
716
  {
717
+ if(!sc.isArray(relations) || 0 === relations.length){
718
+ relations = this.getAllRelations();
719
+ }
680
720
  let query = this.buildQueryOptions();
681
721
  let includeConfig = {};
682
- if(sc.isArray(relations) && 0 < relations.length){
722
+ if(0 < relations.length){
683
723
  includeConfig = this.buildIncludeObject(relations);
684
724
  query.include = includeConfig;
685
725
  }
@@ -707,11 +747,14 @@ class PrismaDriver extends BaseDriver
707
747
 
708
748
  async loadWithRelations(filters, relations)
709
749
  {
750
+ if(!sc.isArray(relations) || 0 === relations.length){
751
+ relations = this.getAllRelations();
752
+ }
710
753
  let processedFilters = this.processFilters(filters);
711
754
  let query = this.buildQueryOptions();
712
755
  query.where = processedFilters;
713
756
  let includeConfig = {};
714
- if(sc.isArray(relations) && 0 < relations.length){
757
+ if(0 < relations.length){
715
758
  includeConfig = this.buildIncludeObject(relations);
716
759
  query.include = includeConfig;
717
760
  }
@@ -739,11 +782,14 @@ class PrismaDriver extends BaseDriver
739
782
 
740
783
  async loadByWithRelations(field, fieldValue, relations, operator = null)
741
784
  {
785
+ if(!sc.isArray(relations) || 0 === relations.length){
786
+ relations = this.getAllRelations();
787
+ }
742
788
  let filter = this.createSingleFilter(field, fieldValue, operator);
743
789
  let query = this.buildQueryOptions();
744
790
  query.where = filter;
745
791
  let includeConfig = {};
746
- if(sc.isArray(relations) && 0 < relations.length){
792
+ if(0 < relations.length){
747
793
  includeConfig = this.buildIncludeObject(relations);
748
794
  query.include = includeConfig;
749
795
  }
@@ -771,12 +817,15 @@ class PrismaDriver extends BaseDriver
771
817
 
772
818
  async loadByIdWithRelations(id, relations)
773
819
  {
820
+ if(!sc.isArray(relations) || 0 === relations.length){
821
+ relations = this.getAllRelations();
822
+ }
774
823
  let castedId = this.castToIdType(id);
775
824
  let query = {
776
825
  where: {id: castedId}
777
826
  };
778
827
  let includeConfig = {};
779
- if(sc.isArray(relations) && 0 < relations.length){
828
+ if(0 < relations.length){
780
829
  includeConfig = this.buildIncludeObject(relations);
781
830
  query.include = includeConfig;
782
831
  }
@@ -819,11 +868,14 @@ class PrismaDriver extends BaseDriver
819
868
 
820
869
  async loadOneWithRelations(filters, relations)
821
870
  {
871
+ if(!sc.isArray(relations) || 0 === relations.length){
872
+ relations = this.getAllRelations();
873
+ }
822
874
  let processedFilters = this.processFilters(filters);
823
875
  let query = this.buildQueryOptions(false);
824
876
  query.where = processedFilters;
825
877
  let includeConfig = {};
826
- if(sc.isArray(relations) && 0 < relations.length){
878
+ if(0 < relations.length){
827
879
  includeConfig = this.buildIncludeObject(relations);
828
880
  query.include = includeConfig;
829
881
  }
@@ -851,11 +903,14 @@ class PrismaDriver extends BaseDriver
851
903
 
852
904
  async loadOneByWithRelations(field, fieldValue, relations, operator = null)
853
905
  {
906
+ if(!sc.isArray(relations) || 0 === relations.length){
907
+ relations = this.getAllRelations();
908
+ }
854
909
  let filter = this.createSingleFilter(field, fieldValue, operator);
855
910
  let query = this.buildQueryOptions(false);
856
911
  query.where = filter;
857
912
  let includeConfig = {};
858
- if(sc.isArray(relations) && 0 < relations.length){
913
+ if(0 < relations.length){
859
914
  includeConfig = this.buildIncludeObject(relations);
860
915
  query.include = includeConfig;
861
916
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.60.0",
4
+ "version": "0.62.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.10.1",
49
- "@reldens/server-utils": "^0.20.0",
48
+ "@prisma/client": "^6.12.0",
49
+ "@reldens/server-utils": "^0.23.0",
50
50
  "@reldens/utils": "^0.50.0",
51
51
  "knex": "^3.1.0",
52
52
  "mysql": "^2.18.1",
53
- "mysql2": "^3.14.1",
53
+ "mysql2": "^3.14.2",
54
54
  "objection": "^3.1.5",
55
- "prisma": "^6.10.1"
55
+ "prisma": "^6.12.0"
56
56
  }
57
57
  }