@reldens/storage 0.5.0-beta.9 → 0.10.0-beta.23

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.
@@ -34,6 +34,11 @@ class BaseDataServer
34
34
  ErrorManager.error('BaseDriver generateEntities() not implemented.');
35
35
  }
36
36
 
37
+ getEntity(entityName)
38
+ {
39
+ return this.entityManager.get(entityName);
40
+ }
41
+
37
42
  }
38
43
 
39
44
  module.exports.BaseDataServer = BaseDataServer;
@@ -4,7 +4,7 @@
4
4
  *
5
5
  */
6
6
 
7
- const { ErrorManager, sc, Logger} = require('@reldens/utils');
7
+ const { ErrorManager, Logger, sc } = require('@reldens/utils');
8
8
 
9
9
  class BaseDriver
10
10
  {
@@ -13,8 +13,17 @@ class BaseDriver
13
13
  {
14
14
  this.config = sc.getDef(props, 'config', false);
15
15
  this.rawModel = sc.getDef(props, 'rawModel', false);
16
- this.id = sc.getDef(props, 'id', false);
17
- this.name = sc.getDef(props, 'name', false);
16
+ this.rawId = sc.getDef(props, 'id', false);
17
+ this.rawName = sc.getDef(props, 'name', false);
18
+ this.limit = sc.getDef(props, 'limit', 0);
19
+ this.offset = sc.getDef(props, 'offset', 0);
20
+ this.sortBy = sc.getDef(props, 'sortBy', false);
21
+ this.sortDirection = sc.getDef(props, 'sortDirection', false);
22
+ }
23
+
24
+ databaseName()
25
+ {
26
+ ErrorManager.error('BaseDriver databaseName() not implemented.');
18
27
  }
19
28
 
20
29
  id()
@@ -32,12 +41,17 @@ class BaseDriver
32
41
  ErrorManager.error('BaseDriver create() not implemented.');
33
42
  }
34
43
 
44
+ createWithRelations(params, relations)
45
+ {
46
+ ErrorManager.error('BaseDriver create() not implemented.');
47
+ }
48
+
35
49
  update(filters, updatePatch)
36
50
  {
37
51
  ErrorManager.error('BaseDriver update() not implemented.');
38
52
  }
39
53
 
40
- updateBy(field, fieldValue, updatePatch)
54
+ updateBy(field, fieldValue, updatePatch, operator = null)
41
55
  {
42
56
  ErrorManager.error('BaseDriver updateBy() not implemented.');
43
57
  }
@@ -57,6 +71,11 @@ class BaseDriver
57
71
  ErrorManager.error('BaseDriver count() not implemented.');
58
72
  }
59
73
 
74
+ countWithRelations(filters, relations)
75
+ {
76
+ ErrorManager.error('BaseDriver countWithRelations() not implemented.');
77
+ }
78
+
60
79
  loadAll()
61
80
  {
62
81
  ErrorManager.error('BaseDriver loadAll() not implemented.');
@@ -77,12 +96,12 @@ class BaseDriver
77
96
  ErrorManager.error('BaseDriver loadWithRelations() not implemented.');
78
97
  }
79
98
 
80
- loadBy(field, value)
99
+ loadBy(field, fieldValue, operator = null)
81
100
  {
82
101
  ErrorManager.error('BaseDriver loadBy() not implemented.');
83
102
  }
84
103
 
85
- loadByWithRelations(field, value, relations)
104
+ loadByWithRelations(field, fieldValue, relations, operator = null)
86
105
  {
87
106
  ErrorManager.error('BaseDriver loadByWithRelations() not implemented.');
88
107
  }
@@ -97,6 +116,11 @@ class BaseDriver
97
116
  ErrorManager.error('BaseDriver loadByIdWithRelations() not implemented.');
98
117
  }
99
118
 
119
+ loadByIds(ids)
120
+ {
121
+ ErrorManager.error('BaseDriver loadByIds() not implemented.');
122
+ }
123
+
100
124
  loadOne(filters)
101
125
  {
102
126
  ErrorManager.error('BaseDriver load() not implemented.');
@@ -107,23 +131,23 @@ class BaseDriver
107
131
  ErrorManager.error('BaseDriver loadWithRelations() not implemented.');
108
132
  }
109
133
 
110
- loadOneBy(field, value)
134
+ loadOneBy(field, fieldValue, operator = null)
111
135
  {
112
136
  ErrorManager.error('BaseDriver loadBy() not implemented.');
113
137
  }
114
138
 
115
- loadOneByWithRelations(field, value, relations)
139
+ loadOneByWithRelations(field, fieldValue, relations, operator = null)
116
140
  {
117
141
  ErrorManager.error('BaseDriver loadByWithRelations() not implemented.');
118
142
  }
119
143
 
120
- executeCustomQuery(methodName)
144
+ executeCustomQuery(methodName, methodOptions)
121
145
  {
122
146
  if(typeof this.rawModel[methodName] !== 'function'){
123
147
  Logger.error('Custom query method not found in raw model.', methodName, this.rawModel);
124
148
  return false;
125
149
  }
126
- return this.rawModel[methodName]();
150
+ return this.rawModel[methodName](methodOptions);
127
151
  }
128
152
 
129
153
  }
@@ -15,104 +15,195 @@ class ObjectionJsDriver extends BaseDriver
15
15
  super(props);
16
16
  }
17
17
 
18
+ databaseName()
19
+ {
20
+ return this.rawModel.knex().client.config.connection.database || '';
21
+ }
22
+
18
23
  id()
19
24
  {
20
- return this.id || this.name();
25
+ return this.rawModel.tableName || this.name();
21
26
  }
22
27
 
23
28
  name()
24
29
  {
25
- return this.name || this.rawModel.tableName;
30
+ return this.rawName || this.rawModel.tableName;
31
+ }
32
+
33
+ tableName()
34
+ {
35
+ return this.rawModel.tableName;
26
36
  }
27
37
 
28
38
  create(params)
29
39
  {
30
- this.rawModel.query().insert(params);
40
+ return this.queryBuilder().insert(params);
41
+ }
42
+
43
+ createWithRelations(params, relations)
44
+ {
45
+ return this.queryBuilder().insertGraphAndFetch(params);
31
46
  }
32
47
 
33
48
  update(filters, updatePatch)
34
49
  {
35
- return this.rawModel.query().patch(updatePatch).where(filters);
50
+ let queryBuilder = this.queryBuilder(true, true, true);
51
+ this.appendFilters(queryBuilder, filters);
52
+ return queryBuilder.patch(updatePatch);
36
53
  }
37
54
 
38
- updateBy(field, fieldValue, updatePatch)
55
+ updateBy(field, fieldValue, updatePatch, operator = null)
39
56
  {
40
- return this.rawModel.query().patch(updatePatch).where(field, fieldValue);
57
+ let queryBuilder = this.queryBuilder(true, true, true);
58
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
59
+ return queryBuilder.patch(updatePatch);
41
60
  }
42
61
 
43
62
  updateById(id, params)
44
63
  {
45
- return this.rawModel.query().patch(params).where(this.id, id);
64
+ return this.queryBuilder().patchAndFetchById(id, params);
46
65
  }
47
66
 
48
67
  delete(id)
49
68
  {
50
- return this.rawModel.delete().where(this.id, id);
69
+ return this.queryBuilder().deleteById(id);
51
70
  }
52
71
 
53
- count(filters)
72
+ async count(filters)
54
73
  {
55
- return this.rawModel.query().count().where(filters);
74
+ let queryBuilder = this.queryBuilder(true, true, true);
75
+ this.appendFilters(queryBuilder, filters);
76
+ let count = await queryBuilder.count().first();
77
+ return count ? count['count(*)'] : 0;
78
+ }
79
+
80
+ async countWithRelations(filters, relations)
81
+ {
82
+ let queryBuilder = this.queryBuilder(true, true, true);
83
+ this.appendFilters(queryBuilder, filters);
84
+ let count = await this.appendRelationsToQuery(queryBuilder, relations).count().first();
85
+ return count ? count['count(*)'] : 0;
56
86
  }
57
87
 
58
88
  loadAll()
59
89
  {
60
- return this.rawModel.query();
90
+ return this.queryBuilder();
61
91
  }
62
92
 
63
93
  loadAllWithRelations(relations)
64
94
  {
65
- return this.appendRelationsToQuery(this.rawModel.query(), relations);
95
+ return this.appendRelationsToQuery(this.queryBuilder(), relations);
66
96
  }
67
97
 
68
98
  load(filters)
69
99
  {
70
- return this.rawModel.query().where(filters);
100
+ let queryBuilder = this.queryBuilder(true, true, true);
101
+ this.appendFilters(queryBuilder, filters);
102
+ return queryBuilder;
71
103
  }
72
104
 
73
105
  loadWithRelations(filters, relations)
74
106
  {
75
- return this.appendRelationsToQuery(this.rawModel.query().where(filters), relations);
107
+ let queryBuilder = this.queryBuilder(true, true, true);
108
+ this.appendFilters(queryBuilder, filters);
109
+ return this.appendRelationsToQuery(queryBuilder, relations);
76
110
  }
77
111
 
78
- loadBy(field, value)
112
+ loadBy(field, fieldValue, operator = null)
79
113
  {
80
- return this.rawModel.query().where(field, value);
114
+ let queryBuilder = this.queryBuilder(true, true, true);
115
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
116
+ return queryBuilder;
81
117
  }
82
118
 
83
- loadByWithRelations(field, value, relations)
119
+ loadByWithRelations(field, fieldValue, relations, operator = null)
84
120
  {
85
- return this.appendRelationsToQuery(this.rawModel.query().where(field, value), relations);
121
+ let queryBuilder = this.queryBuilder(true, true, true);
122
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
123
+ return this.appendRelationsToQuery(queryBuilder, relations);
86
124
  }
87
125
 
88
126
  loadById(id)
89
127
  {
90
- return this.rawModel.query().findById(id);
128
+ return this.queryBuilder().findById(id);
91
129
  }
92
130
 
93
131
  loadByIdWithRelations(id, relations)
94
132
  {
95
- return this.appendRelationsToQuery(this.rawModel.query().findById(id), relations);
133
+ return this.appendRelationsToQuery(this.queryBuilder().findById(id), relations);
134
+ }
135
+
136
+ loadByIds(ids)
137
+ {
138
+ let queryBuilder = this.queryBuilder();
139
+ for(let id of ids){
140
+ queryBuilder.where(this.rawModel.idColumn, id);
141
+ }
142
+ return queryBuilder;
96
143
  }
97
144
 
98
145
  loadOne(filters)
99
146
  {
100
- return this.rawModel.query().where(filters).limit(1).first();
147
+ let queryBuilder = this.queryBuilder(false, true, true);
148
+ this.appendFilters(queryBuilder, filters);
149
+ return queryBuilder.limit(1).first();
101
150
  }
102
151
 
103
152
  loadOneWithRelations(filters, relations)
104
153
  {
105
- return this.appendRelationsToQuery(this.rawModel.query().where(field, value), relations).limit(1).first();
154
+ let queryBuilder = this.queryBuilder(false, true, true);
155
+ this.appendFilters(queryBuilder, filters);
156
+ return this.appendRelationsToQuery(queryBuilder, relations).limit(1).first();
157
+ }
158
+
159
+ loadOneBy(field, fieldValue, operator = null)
160
+ {
161
+ let queryBuilder = this.queryBuilder(false, true, true);
162
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
163
+ return queryBuilder.limit(1).first();
164
+ }
165
+
166
+ loadOneByWithRelations(field, fieldValue, relations, operator = null)
167
+ {
168
+ let queryBuilder = this.queryBuilder(false, true, true);
169
+ this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
170
+ return this.appendRelationsToQuery(queryBuilder, relations).limit(1).first();
171
+ }
172
+
173
+ queryBuilder(useLimit = false, useOffset = false, useSort = false)
174
+ {
175
+ let queryBuilder = this.rawModel.query();
176
+ if(useLimit && 0 !== this.limit){
177
+ queryBuilder.limit(this.limit)
178
+ }
179
+ if(useOffset && 0 !== this.offset){
180
+ queryBuilder.offset(this.offset)
181
+ }
182
+ if(useSort && false !== this.sortBy && false !== this.sortDirection){
183
+ queryBuilder.orderBy(this.sortBy, this.sortDirection)
184
+ }
185
+ return queryBuilder;
106
186
  }
107
187
 
108
- loadOneBy(field, value)
188
+ appendSingleFilter(queryBuilder, field, fieldValue, operator = null)
109
189
  {
110
- return this.rawModel.query().where(field, value).first();
190
+ (null === operator) ? queryBuilder.where(field, fieldValue) : queryBuilder.where(field, operator, fieldValue);
191
+ return queryBuilder;
111
192
  }
112
193
 
113
- loadOneByWithRelations(field, value, relations)
194
+ appendFilters(queryBuilder, filters)
114
195
  {
115
- return this.appendRelationsToQuery(this.rawModel.query().where(field, value), relations).limit(1).first();
196
+ let filtersKeys = Object.keys(filters);
197
+ if(0 === filtersKeys.length){
198
+ return queryBuilder;
199
+ }
200
+ for(let i of filtersKeys){
201
+ let filter = filters[i];
202
+ sc.hasOwn(filter, 'operator')
203
+ ? queryBuilder.where(i, filter.operator, filter.value)
204
+ : queryBuilder.where(i, filter);
205
+ }
206
+ return queryBuilder;
116
207
  }
117
208
 
118
209
  appendRelationsToQuery(queryBuilder, relations)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.5.0-beta.9",
4
+ "version": "0.10.0-beta.23",
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
- "@reldens/utils": "^0.4.6",
48
- "knex": "^0.21.21",
49
- "objection": "^2.2.15",
47
+ "@reldens/utils": "^0.5.0",
48
+ "knex": "^0.95.14",
49
+ "objection": "^3.0.0",
50
50
  "mysql": "^2.18.1"
51
51
  }
52
52
  }