1
- import { InvocationArgs , InvocationContext } from "@loopback/context" ;
2
- import { Application , CoreBindings } from "@loopback/core" ;
1
+ import { InvocationContext } from "@loopback/context" ;
3
2
import {
4
3
juggler ,
5
4
Class ,
@@ -16,32 +15,14 @@ import {
16
15
17
16
import { Ctor } from "../types" ;
18
17
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
- > {
18
+ export interface RepositoryConfig < Model extends Entity > {
38
19
id : keyof Model ;
39
20
where : (
40
- context : FilterContext < Model , ModelID , ModelRelations > ,
21
+ context : InvocationContext ,
41
22
where : Where < Model >
42
23
) => Promise < Where < Model > > ;
43
24
fields : (
44
- context : FilterContext < Model , ModelID , ModelRelations > ,
25
+ context : InvocationContext ,
45
26
fields : Fields < Model >
46
27
) => Promise < Fields < Model > > ;
47
28
}
@@ -62,7 +43,7 @@ export function FilterCrudRepositoryMixin<
62
43
Model extends Entity ,
63
44
ModelID ,
64
45
ModelRelations extends object = { }
65
- > ( config : RepositoryConfig < Model , ModelID , ModelRelations > ) {
46
+ > ( config : RepositoryConfig < Model > ) {
66
47
/**
67
48
* Return function with generic type of repository class, returns mixed in class
68
49
*
@@ -84,41 +65,8 @@ export function FilterCrudRepositoryMixin<
84
65
85
66
class Repository extends parentClass
86
67
implements FilterCrudRepository < Model , ModelID , ModelRelations > {
87
- private application : Application ;
88
-
89
- constructor (
90
- ctor : Ctor < Model > ,
91
- dataSource : juggler . DataSource ,
92
- application : Application
93
- ) {
68
+ constructor ( ctor : Ctor < Model > , dataSource : juggler . DataSource ) {
94
69
super ( ctor , dataSource ) ;
95
-
96
- this . application = application ;
97
- }
98
-
99
- /**
100
- * Get FilterContext method
101
- */
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
- } ;
122
70
}
123
71
124
72
/**
@@ -128,7 +76,12 @@ export function FilterCrudRepositoryMixin<
128
76
filter ?: Filter < Model > ,
129
77
options ?: Options
130
78
) : Promise < ( Model & ModelRelations ) [ ] > {
131
- const filterContext = this . getFilterContext ( arguments ) ;
79
+ const filterContext = new InvocationContext (
80
+ undefined as any ,
81
+ this ,
82
+ "read" ,
83
+ Array . from ( arguments )
84
+ ) ;
132
85
133
86
return await super . find (
134
87
{
@@ -150,7 +103,12 @@ export function FilterCrudRepositoryMixin<
150
103
filter ?: Filter < Model > ,
151
104
options ?: Options
152
105
) : Promise < ( Model & ModelRelations ) | null > {
153
- const filterContext = this . getFilterContext ( arguments ) ;
106
+ const filterContext = new InvocationContext (
107
+ undefined as any ,
108
+ this ,
109
+ "read" ,
110
+ Array . from ( arguments )
111
+ ) ;
154
112
155
113
return await super . findOne (
156
114
{
@@ -203,7 +161,12 @@ export function FilterCrudRepositoryMixin<
203
161
where ?: Where < Model > ,
204
162
options ?: Options
205
163
) : Promise < Count > {
206
- const filterContext = this . getFilterContext ( arguments ) ;
164
+ const filterContext = new InvocationContext (
165
+ undefined as any ,
166
+ this ,
167
+ "count" ,
168
+ Array . from ( arguments )
169
+ ) ;
207
170
208
171
return await super . count (
209
172
await config . where ( filterContext , where || { } ) ,
@@ -236,7 +199,12 @@ export function FilterCrudRepositoryMixin<
236
199
where ?: Where < Model > ,
237
200
options ?: Options
238
201
) : Promise < Count > {
239
- const filterContext = this . getFilterContext ( arguments ) ;
202
+ const filterContext = new InvocationContext (
203
+ undefined as any ,
204
+ this ,
205
+ "update" ,
206
+ Array . from ( arguments )
207
+ ) ;
240
208
241
209
return await super . updateAll (
242
210
data ,
@@ -275,7 +243,12 @@ export function FilterCrudRepositoryMixin<
275
243
where ?: Where < Model > ,
276
244
options ?: Options
277
245
) : Promise < Count > {
278
- const filterContext = this . getFilterContext ( arguments ) ;
246
+ const filterContext = new InvocationContext (
247
+ undefined as any ,
248
+ this ,
249
+ "delete" ,
250
+ Array . from ( arguments )
251
+ ) ;
279
252
280
253
return await super . deleteAll (
281
254
await config . where ( filterContext , where || { } ) ,
0 commit comments