@reldens/storage 0.10.0-beta.22 → 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.
- package/lib/base-driver.js +29 -10
- package/lib/objection-js/objection-js-driver.js +113 -27
- package/package.json +1 -1
package/lib/base-driver.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { ErrorManager,
|
|
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.
|
|
17
|
-
this.
|
|
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,
|
|
99
|
+
loadBy(field, fieldValue, operator = null)
|
|
86
100
|
{
|
|
87
101
|
ErrorManager.error('BaseDriver loadBy() not implemented.');
|
|
88
102
|
}
|
|
89
103
|
|
|
90
|
-
loadByWithRelations(field,
|
|
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,
|
|
134
|
+
loadOneBy(field, fieldValue, operator = null)
|
|
116
135
|
{
|
|
117
136
|
ErrorManager.error('BaseDriver loadBy() not implemented.');
|
|
118
137
|
}
|
|
119
138
|
|
|
120
|
-
loadOneByWithRelations(field,
|
|
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,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.
|
|
25
|
+
return this.rawModel.tableName || this.name();
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
name()
|
|
24
29
|
{
|
|
25
|
-
return this.
|
|
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
|
-
return this.
|
|
40
|
+
return this.queryBuilder().insert(params);
|
|
31
41
|
}
|
|
32
42
|
|
|
33
43
|
createWithRelations(params, relations)
|
|
34
44
|
{
|
|
35
|
-
return this.
|
|
45
|
+
return this.queryBuilder().insertGraphAndFetch(params);
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
update(filters, updatePatch)
|
|
39
49
|
{
|
|
40
|
-
|
|
50
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
51
|
+
this.appendFilters(queryBuilder, filters);
|
|
52
|
+
return queryBuilder.patch(updatePatch);
|
|
41
53
|
}
|
|
42
54
|
|
|
43
|
-
updateBy(field, fieldValue, updatePatch)
|
|
55
|
+
updateBy(field, fieldValue, updatePatch, operator = null)
|
|
44
56
|
{
|
|
45
|
-
|
|
57
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
58
|
+
this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
|
|
59
|
+
return queryBuilder.patch(updatePatch);
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
updateById(id, params)
|
|
49
63
|
{
|
|
50
|
-
return this.
|
|
64
|
+
return this.queryBuilder().patchAndFetchById(id, params);
|
|
51
65
|
}
|
|
52
66
|
|
|
53
67
|
delete(id)
|
|
54
68
|
{
|
|
55
|
-
return this.
|
|
69
|
+
return this.queryBuilder().deleteById(id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async count(filters)
|
|
73
|
+
{
|
|
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;
|
|
56
78
|
}
|
|
57
79
|
|
|
58
|
-
|
|
80
|
+
async countWithRelations(filters, relations)
|
|
59
81
|
{
|
|
60
|
-
|
|
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;
|
|
61
86
|
}
|
|
62
87
|
|
|
63
88
|
loadAll()
|
|
64
89
|
{
|
|
65
|
-
return this.
|
|
90
|
+
return this.queryBuilder();
|
|
66
91
|
}
|
|
67
92
|
|
|
68
93
|
loadAllWithRelations(relations)
|
|
69
94
|
{
|
|
70
|
-
return this.appendRelationsToQuery(this.
|
|
95
|
+
return this.appendRelationsToQuery(this.queryBuilder(), relations);
|
|
71
96
|
}
|
|
72
97
|
|
|
73
98
|
load(filters)
|
|
74
99
|
{
|
|
75
|
-
|
|
100
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
101
|
+
this.appendFilters(queryBuilder, filters);
|
|
102
|
+
return queryBuilder;
|
|
76
103
|
}
|
|
77
104
|
|
|
78
105
|
loadWithRelations(filters, relations)
|
|
79
106
|
{
|
|
80
|
-
|
|
107
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
108
|
+
this.appendFilters(queryBuilder, filters);
|
|
109
|
+
return this.appendRelationsToQuery(queryBuilder, relations);
|
|
81
110
|
}
|
|
82
111
|
|
|
83
|
-
loadBy(field,
|
|
112
|
+
loadBy(field, fieldValue, operator = null)
|
|
84
113
|
{
|
|
85
|
-
|
|
114
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
115
|
+
this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
|
|
116
|
+
return queryBuilder;
|
|
86
117
|
}
|
|
87
118
|
|
|
88
|
-
loadByWithRelations(field,
|
|
119
|
+
loadByWithRelations(field, fieldValue, relations, operator = null)
|
|
89
120
|
{
|
|
90
|
-
|
|
121
|
+
let queryBuilder = this.queryBuilder(true, true, true);
|
|
122
|
+
this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
|
|
123
|
+
return this.appendRelationsToQuery(queryBuilder, relations);
|
|
91
124
|
}
|
|
92
125
|
|
|
93
126
|
loadById(id)
|
|
94
127
|
{
|
|
95
|
-
return this.
|
|
128
|
+
return this.queryBuilder().findById(id);
|
|
96
129
|
}
|
|
97
130
|
|
|
98
131
|
loadByIdWithRelations(id, relations)
|
|
99
132
|
{
|
|
100
|
-
return this.appendRelationsToQuery(this.
|
|
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;
|
|
101
143
|
}
|
|
102
144
|
|
|
103
145
|
loadOne(filters)
|
|
104
146
|
{
|
|
105
|
-
|
|
147
|
+
let queryBuilder = this.queryBuilder(false, true, true);
|
|
148
|
+
this.appendFilters(queryBuilder, filters);
|
|
149
|
+
return queryBuilder.limit(1).first();
|
|
106
150
|
}
|
|
107
151
|
|
|
108
152
|
loadOneWithRelations(filters, relations)
|
|
109
153
|
{
|
|
110
|
-
|
|
154
|
+
let queryBuilder = this.queryBuilder(false, true, true);
|
|
155
|
+
this.appendFilters(queryBuilder, filters);
|
|
156
|
+
return this.appendRelationsToQuery(queryBuilder, relations).limit(1).first();
|
|
111
157
|
}
|
|
112
158
|
|
|
113
|
-
loadOneBy(field,
|
|
159
|
+
loadOneBy(field, fieldValue, operator = null)
|
|
114
160
|
{
|
|
115
|
-
|
|
161
|
+
let queryBuilder = this.queryBuilder(false, true, true);
|
|
162
|
+
this.appendSingleFilter(queryBuilder, field, fieldValue, operator);
|
|
163
|
+
return queryBuilder.limit(1).first();
|
|
116
164
|
}
|
|
117
165
|
|
|
118
|
-
loadOneByWithRelations(field,
|
|
166
|
+
loadOneByWithRelations(field, fieldValue, relations, operator = null)
|
|
119
167
|
{
|
|
120
|
-
|
|
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;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
appendSingleFilter(queryBuilder, field, fieldValue, operator = null)
|
|
189
|
+
{
|
|
190
|
+
(null === operator) ? queryBuilder.where(field, fieldValue) : queryBuilder.where(field, operator, fieldValue);
|
|
191
|
+
return queryBuilder;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
appendFilters(queryBuilder, filters)
|
|
195
|
+
{
|
|
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;
|
|
121
207
|
}
|
|
122
208
|
|
|
123
209
|
appendRelationsToQuery(queryBuilder, relations)
|