@reldens/storage 0.10.0-beta.21 → 0.10.0-beta.25

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