@reldens/storage 0.10.0-beta.22 → 0.10.0-beta.26

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()
@@ -27,6 +36,16 @@ class BaseDriver
27
36
  ErrorManager.error('BaseDriver name() not implemented.');
28
37
  }
29
38
 
39
+ tableName()
40
+ {
41
+ ErrorManager.error('BaseDriver tableName() not implemented.');
42
+ }
43
+
44
+ property(propertyName)
45
+ {
46
+ ErrorManager.error('BaseDriver property() not implemented.');
47
+ }
48
+
30
49
  create(params)
31
50
  {
32
51
  ErrorManager.error('BaseDriver create() not implemented.');
@@ -42,7 +61,7 @@ class BaseDriver
42
61
  ErrorManager.error('BaseDriver update() not implemented.');
43
62
  }
44
63
 
45
- updateBy(field, fieldValue, updatePatch)
64
+ updateBy(field, fieldValue, updatePatch, operator = null)
46
65
  {
47
66
  ErrorManager.error('BaseDriver updateBy() not implemented.');
48
67
  }
@@ -62,6 +81,11 @@ class BaseDriver
62
81
  ErrorManager.error('BaseDriver count() not implemented.');
63
82
  }
64
83
 
84
+ countWithRelations(filters, relations)
85
+ {
86
+ ErrorManager.error('BaseDriver countWithRelations() not implemented.');
87
+ }
88
+
65
89
  loadAll()
66
90
  {
67
91
  ErrorManager.error('BaseDriver loadAll() not implemented.');
@@ -82,12 +106,12 @@ class BaseDriver
82
106
  ErrorManager.error('BaseDriver loadWithRelations() not implemented.');
83
107
  }
84
108
 
85
- loadBy(field, value)
109
+ loadBy(field, fieldValue, operator = null)
86
110
  {
87
111
  ErrorManager.error('BaseDriver loadBy() not implemented.');
88
112
  }
89
113
 
90
- loadByWithRelations(field, value, relations)
114
+ loadByWithRelations(field, fieldValue, relations, operator = null)
91
115
  {
92
116
  ErrorManager.error('BaseDriver loadByWithRelations() not implemented.');
93
117
  }
@@ -102,6 +126,11 @@ class BaseDriver
102
126
  ErrorManager.error('BaseDriver loadByIdWithRelations() not implemented.');
103
127
  }
104
128
 
129
+ loadByIds(ids)
130
+ {
131
+ ErrorManager.error('BaseDriver loadByIds() not implemented.');
132
+ }
133
+
105
134
  loadOne(filters)
106
135
  {
107
136
  ErrorManager.error('BaseDriver load() not implemented.');
@@ -112,23 +141,23 @@ class BaseDriver
112
141
  ErrorManager.error('BaseDriver loadWithRelations() not implemented.');
113
142
  }
114
143
 
115
- loadOneBy(field, value)
144
+ loadOneBy(field, fieldValue, operator = null)
116
145
  {
117
146
  ErrorManager.error('BaseDriver loadBy() not implemented.');
118
147
  }
119
148
 
120
- loadOneByWithRelations(field, value, relations)
149
+ loadOneByWithRelations(field, fieldValue, relations, operator = null)
121
150
  {
122
151
  ErrorManager.error('BaseDriver loadByWithRelations() not implemented.');
123
152
  }
124
153
 
125
- executeCustomQuery(methodName)
154
+ executeCustomQuery(methodName, methodOptions)
126
155
  {
127
156
  if(typeof this.rawModel[methodName] !== 'function'){
128
157
  Logger.error('Custom query method not found in raw model.', methodName, this.rawModel);
129
158
  return false;
130
159
  }
131
- return this.rawModel[methodName]();
160
+ return this.rawModel[methodName](methodOptions);
132
161
  }
133
162
 
134
163
  }
@@ -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
- return 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.22",
4
+ "version": "0.10.0-beta.26",
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"