1
+ import { InvocationArgs , InvocationContext } from "@loopback/context" ;
2
+ import { Application , CoreBindings } from "@loopback/core" ;
1
3
import {
2
4
juggler ,
3
5
Class ,
4
6
DefaultCrudRepository ,
7
+ EntityNotFoundError ,
5
8
DataObject ,
6
9
Options ,
7
10
Entity ,
8
11
Filter ,
9
12
Where ,
13
+ Fields ,
10
14
Count ,
11
15
} from "@loopback/repository" ;
12
16
13
17
import { Ctor } from "../types" ;
14
18
19
+ /**
20
+ * Repository Config
21
+ */
22
+ export interface FilterContext <
23
+ Model extends Entity ,
24
+ ModelID ,
25
+ ModelRelations extends object = { }
26
+ > {
27
+ target : DefaultCrudRepository < Model , ModelID , ModelRelations > ;
28
+ methodName : keyof DefaultCrudRepository < Model , ModelID , ModelRelations > ;
29
+ args : InvocationArgs ;
30
+ invocationContext : InvocationContext ;
31
+ }
32
+
33
+ export interface RepositoryConfig <
34
+ Model extends Entity ,
35
+ ModelID ,
36
+ ModelRelations extends object = { }
37
+ > {
38
+ id : keyof Model ;
39
+ where : (
40
+ context : FilterContext < Model , ModelID , ModelRelations > ,
41
+ where : Where < Model >
42
+ ) => Promise < Where < Model > > ;
43
+ fields : (
44
+ context : FilterContext < Model , ModelID , ModelRelations > ,
45
+ fields : Fields < Model >
46
+ ) => Promise < Fields < Model > > ;
47
+ }
48
+
15
49
/**
16
50
* Repository Type
17
51
*/
@@ -28,7 +62,7 @@ export function FilterCrudRepositoryMixin<
28
62
Model extends Entity ,
29
63
ModelID ,
30
64
ModelRelations extends object = { }
31
- > ( ) {
65
+ > ( config : RepositoryConfig < Model , ModelID , ModelRelations > ) {
32
66
/**
33
67
* Return function with generic type of repository class, returns mixed in class
34
68
*
@@ -50,25 +84,41 @@ export function FilterCrudRepositoryMixin<
50
84
51
85
class Repository extends parentClass
52
86
implements FilterCrudRepository < Model , ModelID , ModelRelations > {
53
- constructor ( ctor : Ctor < Model > , dataSource : juggler . DataSource ) {
87
+ private application : Application ;
88
+
89
+ constructor (
90
+ ctor : Ctor < Model > ,
91
+ dataSource : juggler . DataSource ,
92
+ application : Application
93
+ ) {
54
94
super ( ctor , dataSource ) ;
95
+
96
+ this . application = application ;
55
97
}
56
98
57
99
/**
58
- * Create methods
100
+ * Get FilterContext method
59
101
*/
60
- async create (
61
- entity : DataObject < Model > ,
62
- options ?: Options
63
- ) : Promise < Model > {
64
- return super . create ( entity , options ) ;
65
- }
66
-
67
- async createAll (
68
- entities : DataObject < Model > [ ] ,
69
- options ?: Options
70
- ) : Promise < Model [ ] > {
71
- return super . createAll ( entities , options ) ;
102
+ private getFilterContext (
103
+ args : IArguments
104
+ ) : FilterContext < Model , ModelID , ModelRelations > {
105
+ return {
106
+ target : this ,
107
+ methodName : args . callee . name as any ,
108
+ args : Array . from ( args ) ,
109
+ invocationContext : new InvocationContext (
110
+ this . application ,
111
+ this . application . getSync (
112
+ CoreBindings . CONTROLLER_CURRENT
113
+ ) as any ,
114
+ this . application . getSync (
115
+ CoreBindings . CONTROLLER_METHOD_NAME
116
+ ) ,
117
+ this . application . getSync (
118
+ CoreBindings . CONTROLLER_METHOD_META
119
+ )
120
+ ) ,
121
+ } ;
72
122
}
73
123
74
124
/**
@@ -78,82 +128,163 @@ export function FilterCrudRepositoryMixin<
78
128
filter ?: Filter < Model > ,
79
129
options ?: Options
80
130
) : Promise < ( Model & ModelRelations ) [ ] > {
81
- return super . find ( filter , options ) ;
131
+ const filterContext = this . getFilterContext ( arguments ) ;
132
+
133
+ return await super . find (
134
+ {
135
+ ...filter ,
136
+ where : await config . where (
137
+ filterContext ,
138
+ filter ?. where || { }
139
+ ) ,
140
+ fields : await config . fields (
141
+ filterContext ,
142
+ filter ?. fields || { }
143
+ ) ,
144
+ } ,
145
+ options
146
+ ) ;
82
147
}
83
148
84
149
async findOne (
85
150
filter ?: Filter < Model > ,
86
151
options ?: Options
87
152
) : Promise < ( Model & ModelRelations ) | null > {
88
- return super . findOne ( filter , options ) ;
153
+ const filterContext = this . getFilterContext ( arguments ) ;
154
+
155
+ return await super . findOne (
156
+ {
157
+ ...filter ,
158
+ where : await config . where (
159
+ filterContext ,
160
+ filter ?. where || { }
161
+ ) ,
162
+ fields : await config . fields (
163
+ filterContext ,
164
+ filter ?. fields || { }
165
+ ) ,
166
+ } ,
167
+ options
168
+ ) ;
89
169
}
90
170
91
171
async findById (
92
172
id : ModelID ,
93
173
filter ?: Filter < Model > ,
94
174
options ?: Options
95
175
) : Promise < Model & ModelRelations > {
96
- return super . findById ( id , filter , options ) ;
176
+ const item = await this . findOne (
177
+ {
178
+ ...filter ,
179
+ where : filter ?. where
180
+ ? {
181
+ and : [
182
+ filter . where ,
183
+ {
184
+ [ config . id as any ] : id ,
185
+ } ,
186
+ ] ,
187
+ }
188
+ : {
189
+ [ config . id as any ] : id ,
190
+ } ,
191
+ } ,
192
+ options
193
+ ) ;
194
+
195
+ if ( ! item ) {
196
+ throw new EntityNotFoundError ( this . entityClass , id ) ;
197
+ }
198
+
199
+ return item ;
97
200
}
98
201
99
202
async count (
100
203
where ?: Where < Model > ,
101
204
options ?: Options
102
205
) : Promise < Count > {
103
- return super . count ( where , options ) ;
206
+ const filterContext = this . getFilterContext ( arguments ) ;
207
+
208
+ return await super . count (
209
+ await config . where ( filterContext , where || { } ) ,
210
+ options
211
+ ) ;
104
212
}
105
213
106
214
async exists ( id : ModelID , options ?: Options ) : Promise < boolean > {
107
- return super . exists ( id , options ) ;
215
+ const count = await this . count (
216
+ { [ config . id as any ] : id } ,
217
+ options
218
+ ) ;
219
+
220
+ return count . count > 0 ;
108
221
}
109
222
110
223
/**
111
224
* Update methods
112
225
*/
113
226
async update ( entity : Model , options ?: Options ) : Promise < void > {
114
- return super . update ( entity , options ) ;
227
+ await super . updateAll (
228
+ entity ,
229
+ { [ config . id as any ] : ( entity as any ) [ config . id ] } ,
230
+ options
231
+ ) ;
115
232
}
116
233
117
234
async updateAll (
118
235
data : DataObject < Model > ,
119
236
where ?: Where < Model > ,
120
237
options ?: Options
121
238
) : Promise < Count > {
122
- return super . updateAll ( data , where , options ) ;
239
+ const filterContext = this . getFilterContext ( arguments ) ;
240
+
241
+ return await super . updateAll (
242
+ data ,
243
+ await config . where ( filterContext , where || { } ) ,
244
+ options
245
+ ) ;
123
246
}
124
247
125
248
async updateById (
126
249
id : ModelID ,
127
250
data : DataObject < Model > ,
128
251
options ?: Options
129
252
) : Promise < void > {
130
- return super . updateById ( id , data , options ) ;
253
+ await this . updateAll ( data , { [ config . id as any ] : id } , options ) ;
131
254
}
132
255
133
256
async replaceById (
134
257
id : ModelID ,
135
258
data : DataObject < Model > ,
136
259
options ?: Options
137
260
) : Promise < void > {
138
- return super . replaceById ( id , data , options ) ;
261
+ await this . updateAll ( data , { [ config . id as any ] : id } , options ) ;
139
262
}
140
263
141
264
/**
142
265
* Delete methods
143
266
*/
144
267
async delete ( entity : Model , options ?: Options ) : Promise < void > {
145
- return super . delete ( entity , options ) ;
268
+ await super . deleteAll (
269
+ { [ config . id as any ] : ( entity as any ) [ config . id ] } ,
270
+ options
271
+ ) ;
146
272
}
147
273
148
274
async deleteAll (
149
275
where ?: Where < Model > ,
150
276
options ?: Options
151
277
) : Promise < Count > {
152
- return super . deleteAll ( where , options ) ;
278
+ const filterContext = this . getFilterContext ( arguments ) ;
279
+
280
+ return await super . deleteAll (
281
+ await config . where ( filterContext , where || { } ) ,
282
+ options
283
+ ) ;
153
284
}
154
285
155
286
async deleteById ( id : ModelID , options ?: Options ) : Promise < void > {
156
- return super . deleteById ( id , options ) ;
287
+ await this . deleteAll ( { [ config . id as any ] : id } , options ) ;
157
288
}
158
289
}
159
290
0 commit comments