@reldens/storage 0.10.0-beta.29 → 0.10.0-beta.30

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.
@@ -12,21 +12,36 @@ class BaseDataServer
12
12
 
13
13
  constructor(props)
14
14
  {
15
- this.config = sc.get(props, 'config', false);
16
- this.client = sc.get(props, 'client', false);
17
- this.poolConfig = sc.get(props, 'poolConfig', false);
18
- this.connectString = sc.get(props, 'connectString', false);
15
+ this.config = sc.get(props, 'config', {});
16
+ if(!this.config.host){
17
+ this.config.host = 'localhost';
18
+ }
19
+ this.client = sc.get(props, 'client', '');
20
+ this.poolConfig = sc.get(props, 'poolConfig', {});
21
+ this.connectStringOptions = sc.get(props, 'connectStringOptions', '');
22
+ this.connectString = sc.get(props, 'connectString', this.createConnectionString());
19
23
  this.rawModel = sc.get(props, 'rawModel', false);
20
24
  this.name = sc.get(props, 'name', false);
21
25
  this.initialized = sc.get(props, 'initialized', false);
22
26
  this.rawEntities = props.rawEntities;
23
- let entities = props.entities || props.rawEntities ? this.generateEntities(props.rawEntities) : {};
24
- this.entityManager = new EntityManager({entities});
27
+ this.entities = props.entities || {};
28
+ this.entityManager = new EntityManager({entities: this.entities});
25
29
  }
26
30
 
27
- connect()
31
+ createConnectionString()
28
32
  {
29
- ErrorManager.error('BaseDriver connect() not implemented.');
33
+ return (this.client || 'client-not-specified')+'://'
34
+ +(this.config.user || 'user-not-specified')
35
+ +(this.config.password ? ':'+this.config.password : '')
36
+ +'@'+this.config.host
37
+ +':'+(this.config.port || 3306)
38
+ +(this.config.database ? '/'+this.config.database : '')
39
+ +(this.connectStringOptions ? '?'+this.connectStringOptions : '');
40
+ }
41
+
42
+ getEntity(entityName)
43
+ {
44
+ return this.entityManager.get(entityName);
30
45
  }
31
46
 
32
47
  generateEntities(rawEntities)
@@ -34,9 +49,9 @@ class BaseDataServer
34
49
  ErrorManager.error('BaseDriver generateEntities() not implemented.');
35
50
  }
36
51
 
37
- getEntity(entityName)
52
+ async connect()
38
53
  {
39
- return this.entityManager.get(entityName);
54
+ ErrorManager.error('BaseDriver connect() not implemented.');
40
55
  }
41
56
 
42
57
  }
@@ -4,15 +4,66 @@
4
4
  *
5
5
  */
6
6
 
7
+ const { MikroOrmDriver } = require('./mikro-orm-driver');
7
8
  const { BaseDataServer } = require('../base-data-server');
9
+ const { MikroORM } = require('@mikro-orm/core');
10
+ const { Logger } = require('@reldens/utils');
8
11
 
9
12
  class MikroOrmDataServer extends BaseDataServer
10
13
  {
11
14
 
12
- constructor(props)
15
+ orm = {};
16
+ clientsMapped = {
17
+ mysql: 'mysql',
18
+ mongodb: 'mongo'
19
+ };
20
+
21
+ async connect()
22
+ {
23
+ if(this.initialized){
24
+ return this.initialized;
25
+ }
26
+ this.orm = await MikroORM.init({
27
+ entities: Object.values(this.rawEntities),
28
+ dbName: this.config.database,
29
+ type: this.clientsMapped[(this.client || 'mongo')],
30
+ clientUrl: this.connectString,
31
+ host: this.config.host,
32
+ port: this.config.port,
33
+ user: this.config.user,
34
+ password: this.config.password
35
+ });
36
+ this.initialized = Date.now();
37
+ return this.initialized;
38
+ }
39
+
40
+ generateEntities(rawEntities)
41
+ {
42
+ if(!this.initialized){
43
+ Logger.critical('In order to generate entities with Mikro ORM driver you need to connect to the server.');
44
+ return {};
45
+ }
46
+ if(!this.rawEntities){
47
+ Logger.warning('Empty raw entities array, none entities generated.');
48
+ return {};
49
+ }
50
+ this.entities = {};
51
+ for(let i of Object.keys(rawEntities)){
52
+ let rawEntity = rawEntities[i];
53
+ this.entities[i] = new MikroOrmDriver({
54
+ rawModel: rawEntity,
55
+ id: i,
56
+ name: i,
57
+ config: this.orm.driver.connection.options,
58
+ orm: this.orm
59
+ });
60
+ }
61
+ return this.entities;
62
+ }
63
+
64
+ name()
13
65
  {
14
- super(props);
15
- // @TODO: implement.
66
+ return this.name || 'Mikro-ORM Data Server Driver';
16
67
  }
17
68
 
18
69
  }
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  const { BaseDriver } = require('../base-driver');
8
+ const { ErrorManager, sc } = require('@reldens/utils');
8
9
 
9
10
  class MikroOrmDriver extends BaseDriver
10
11
  {
@@ -12,7 +13,218 @@ class MikroOrmDriver extends BaseDriver
12
13
  constructor(props)
13
14
  {
14
15
  super(props);
15
- // @TODO: implement.
16
+ if(!props.orm){
17
+ ErrorManager.error('Missing ORM on Mikro ORM driver.');
18
+ }
19
+ if(!this.rawModel){
20
+ ErrorManager.error('Missing raw entity on Mikro ORM driver.');
21
+ }
22
+ this.orm = props.orm;
23
+ this.repository = this.orm.em.getRepository(this.rawModel.entity);
24
+ }
25
+
26
+ databaseName()
27
+ {
28
+ return this.config.dbName || '';
29
+ }
30
+
31
+ id()
32
+ {
33
+ return this.rawModel.entity || this.name();
34
+ }
35
+
36
+ name()
37
+ {
38
+ return this.rawModel.entity || this.config.dbName;
39
+ }
40
+
41
+ tableName()
42
+ {
43
+ return this.rawModel.entity;
44
+ }
45
+
46
+ property(propertyName)
47
+ {
48
+ return this.rawModel[propertyName] || null;
49
+ }
50
+
51
+ create(params)
52
+ {
53
+ return this.orm.em.create(this.rawModel, params);
54
+ }
55
+
56
+ createWithRelations(params, relations)
57
+ {
58
+ return this.create(params, relations);
59
+ }
60
+
61
+ update(filters, updatePatch)
62
+ {
63
+ let queryBuilder = this.queryBuilder(true, true, true);
64
+ this.appendFilters(queryBuilder, filters);
65
+ return queryBuilder.patch(updatePatch);
66
+ }
67
+
68
+ updateBy(field, fieldValue, updatePatch, operator = null)
69
+ {
70
+ let queryBuilder = this.queryBuilder(true, true, true);
71
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
72
+ return queryBuilder.patch(updatePatch);
73
+ }
74
+
75
+ updateById(id, params)
76
+ {
77
+ return this.queryBuilder().patchAndFetchById(id, params);
78
+ }
79
+
80
+ delete(id)
81
+ {
82
+ return this.queryBuilder().deleteById(id);
83
+ }
84
+
85
+ async count(filters)
86
+ {
87
+ let queryBuilder = this.queryBuilder(true, true, true);
88
+ this.appendFilters(queryBuilder, filters);
89
+ let count = await queryBuilder.count().first();
90
+ return count ? count['count(*)'] : 0;
91
+ }
92
+
93
+ async countWithRelations(filters, relations)
94
+ {
95
+ let queryBuilder = this.queryBuilder(true, true, true);
96
+ this.appendFilters(queryBuilder, filters);
97
+ let count = await this.appendRelationsToQuery(queryBuilder, relations).count().first();
98
+ return count ? count['count(*)'] : 0;
99
+ }
100
+
101
+ loadAll()
102
+ {
103
+ return this.queryBuilder();
104
+ }
105
+
106
+ loadAllWithRelations(relations)
107
+ {
108
+ return this.appendRelationsToQuery(this.queryBuilder(), relations);
109
+ }
110
+
111
+ load(filters)
112
+ {
113
+ let queryBuilder = this.queryBuilder(true, true, true);
114
+ this.appendFilters(queryBuilder, filters);
115
+ return queryBuilder;
116
+ }
117
+
118
+ loadWithRelations(filters, relations)
119
+ {
120
+ let queryBuilder = this.queryBuilder(true, true, true);
121
+ this.appendFilters(queryBuilder, filters);
122
+ return this.appendRelationsToQuery(queryBuilder, relations);
123
+ }
124
+
125
+ loadBy(field, fieldValue, operator = null)
126
+ {
127
+ let queryBuilder = this.queryBuilder(true, true, true);
128
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
129
+ return queryBuilder;
130
+ }
131
+
132
+ loadByWithRelations(field, fieldValue, relations, operator = null)
133
+ {
134
+ let queryBuilder = this.queryBuilder(true, true, true);
135
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
136
+ return this.appendRelationsToQuery(queryBuilder, relations);
137
+ }
138
+
139
+ loadById(id)
140
+ {
141
+ return this.queryBuilder().findById(id);
142
+ }
143
+
144
+ loadByIdWithRelations(id, relations)
145
+ {
146
+ return this.appendRelationsToQuery(this.queryBuilder().findById(id), relations);
147
+ }
148
+
149
+ loadByIds(ids)
150
+ {
151
+ return this.queryBuilder().findByIds(ids);
152
+ }
153
+
154
+ loadOne(filters)
155
+ {
156
+ let queryBuilder = this.queryBuilder(false, true, true);
157
+ this.appendFilters(queryBuilder, filters);
158
+ return queryBuilder.limit(1).first();
159
+ }
160
+
161
+ loadOneWithRelations(filters, relations)
162
+ {
163
+ let queryBuilder = this.queryBuilder(false, true, true);
164
+ this.appendFilters(queryBuilder, filters);
165
+ return this.appendRelationsToQuery(queryBuilder, relations).limit(1).first();
166
+ }
167
+
168
+ loadOneBy(field, fieldValue, operator = null)
169
+ {
170
+ let queryBuilder = this.queryBuilder(false, true, true);
171
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
172
+ return queryBuilder.limit(1).first();
173
+ }
174
+
175
+ loadOneByWithRelations(field, fieldValue, relations, operator = null)
176
+ {
177
+ let queryBuilder = this.queryBuilder(false, true, true);
178
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
179
+ return this.appendRelationsToQuery(queryBuilder, relations).limit(1).first();
180
+ }
181
+
182
+ queryBuilder(useLimit = false, useOffset = false, useSort = false)
183
+ {
184
+ let queryBuilder = this.orm.em.getKnex();
185
+ if(useLimit && 0 !== this.limit){
186
+ queryBuilder.limit(this.limit)
187
+ }
188
+ if(useOffset && 0 !== this.offset){
189
+ queryBuilder.offset(this.offset)
190
+ }
191
+ if(useSort && false !== this.sortBy && false !== this.sortDirection){
192
+ queryBuilder.orderBy(this.sortBy, this.sortDirection)
193
+ }
194
+ return queryBuilder;
195
+ }
196
+
197
+ appendSingleFilter(queryBuilder, field, fieldValue, operator = null)
198
+ {
199
+ (null === operator) ? queryBuilder.where(field, fieldValue) : queryBuilder.where(field, operator, fieldValue);
200
+ return queryBuilder;
201
+ }
202
+
203
+ appendFilters(queryBuilder, filters)
204
+ {
205
+ let filtersKeys = Object.keys(filters);
206
+ if(0 === filtersKeys.length){
207
+ return queryBuilder;
208
+ }
209
+ for(let i of filtersKeys){
210
+ let filter = filters[i];
211
+ sc.hasOwn(filter, 'operator')
212
+ ? queryBuilder.where(i, filter.operator, filter.value)
213
+ : queryBuilder.where(i, filter);
214
+ }
215
+ return queryBuilder;
216
+ }
217
+
218
+ async appendRelationsToQuery(queryBuilder, relations)
219
+ {
220
+ if(!sc.isArray(relations) || 0 === relations.length){
221
+ relations = Object.keys(this.rawModel.relationMappings || {});
222
+ }
223
+ if(0 < relations.length){
224
+ let result = await queryBuilder;
225
+ return this.orm.em.populate(result, relations);
226
+ }
227
+ return queryBuilder;
16
228
  }
17
229
 
18
230
  }
@@ -8,6 +8,7 @@ const { BaseDataServer } = require('../base-data-server');
8
8
  const { ObjectionJsDriver } = require('./objection-js-driver');
9
9
  const { Model } = require('objection');
10
10
  const Knex = require('knex');
11
+ const { Logger } = require('@reldens/utils');
11
12
 
12
13
  class ObjectionJsDataServer extends BaseDataServer
13
14
  {
@@ -21,26 +22,31 @@ class ObjectionJsDataServer extends BaseDataServer
21
22
  }
22
23
  }
23
24
 
24
- connect()
25
+ async connect()
25
26
  {
26
27
  if(this.initialized){
27
28
  return this.initialized;
28
29
  }
29
30
  // initialize knex, the query builder:
30
- this.knex = Knex({client: this.client, connection: this.config, pool: this.poolConfig});
31
+ this.knex = await Knex({client: this.client, connection: this.config, pool: this.poolConfig});
31
32
  // give the knex instance to Objection:
32
33
  this.rawModel.knex(this.knex);
33
34
  this.initialized = Date.now();
35
+ return this.initialized;
34
36
  }
35
37
 
36
- generateEntities(rawEntities)
38
+ generateEntities()
37
39
  {
38
- let entities = {};
39
- for(let i of Object.keys(rawEntities)){
40
- let rawEntity = rawEntities[i];
41
- entities[i] = new ObjectionJsDriver({rawModel: rawEntity, id: i, name: i, config: rawEntity.knex});
40
+ if(!this.rawEntities){
41
+ Logger.warning('Empty raw entities array, none entities generated.');
42
+ return {};
42
43
  }
43
- return entities;
44
+ this.entities = {};
45
+ for(let i of Object.keys(this.rawEntities)){
46
+ let rawEntity = this.rawEntities[i];
47
+ this.entities[i] = new ObjectionJsDriver({rawModel: rawEntity, id: i, name: i, config: rawEntity.knex});
48
+ }
49
+ return this.entities;
44
50
  }
45
51
 
46
52
  name()
@@ -10,11 +10,6 @@ const { sc } = require('@reldens/utils');
10
10
  class ObjectionJsDriver extends BaseDriver
11
11
  {
12
12
 
13
- constructor(props)
14
- {
15
- super(props);
16
- }
17
-
18
13
  databaseName()
19
14
  {
20
15
  return this.rawModel.knex().client.config.connection.database || '';
@@ -209,10 +204,10 @@ class ObjectionJsDriver extends BaseDriver
209
204
 
210
205
  appendRelationsToQuery(queryBuilder, relations)
211
206
  {
212
- if(!sc.isArray(relations) || relations.length === 0){
207
+ if(!sc.isArray(relations) || 0 === relations.length){
213
208
  relations = Object.keys(this.rawModel.relationMappings || {});
214
209
  }
215
- if(relations.length > 0){
210
+ if(0 < relations.length){
216
211
  queryBuilder.withGraphFetched('['+relations.join(',')+']');
217
212
  }
218
213
  return queryBuilder;
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.29",
4
+ "version": "0.10.0-beta.30",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",
@@ -44,9 +44,11 @@
44
44
  "url": "https://github.com/damian-pastorini/reldens-storage/issues"
45
45
  },
46
46
  "dependencies": {
47
+ "@mikro-orm/core": "^4.5.10",
48
+ "@mikro-orm/mongodb": "^4.5.10",
47
49
  "@reldens/utils": "^0.10.0-beta.3",
48
- "knex": "^0.95.15",
49
- "objection": "^3.0.1",
50
- "mysql": "^2.18.1"
50
+ "knex": "^1.0.1",
51
+ "mysql": "^2.18.1",
52
+ "objection": "^3.0.1"
51
53
  }
52
54
  }