@@ -74,7 +74,7 @@ export class Resource extends BaseResource {
74
74
}
75
75
76
76
public async count ( filter : Filter ) : Promise < number > {
77
- return this . orm . em . getRepository ( this . model ) . count (
77
+ return this . orm . em . fork ( ) . getRepository ( this . model ) . count (
78
78
convertFilter ( filter ) ,
79
79
) ;
80
80
}
@@ -84,6 +84,7 @@ export class Resource extends BaseResource {
84
84
const { direction, sortBy } = sort as { direction : 'asc' | 'desc' , sortBy : string } ;
85
85
86
86
const results = await this . orm . em
87
+ . fork ( )
87
88
. getRepository ( this . model )
88
89
. find (
89
90
convertFilter ( filter ) , {
@@ -100,6 +101,7 @@ export class Resource extends BaseResource {
100
101
101
102
public async findOne ( id : string | number ) : Promise < BaseRecord | null > {
102
103
const result = await this . orm . em
104
+ . fork ( )
103
105
. getRepository ( this . model )
104
106
. findOne ( id as any ) ; // mikroorm has incorrect types for `findOne`
105
107
@@ -115,34 +117,37 @@ export class Resource extends BaseResource {
115
117
if ( ! pk ) return [ ] ;
116
118
117
119
const results = await this . orm . em
120
+ . fork ( )
118
121
. getRepository ( this . model )
119
122
. find ( { [ pk ] : { $in : ids } } ) ;
120
123
121
124
return results . map ( ( result ) => new BaseRecord ( wrap ( result ) . toJSON ( ) , this ) ) ;
122
125
}
123
126
124
127
public async create ( params : Record < string , any > ) : Promise < Record < string , any > > {
125
- const instance = this . orm . em
128
+ const em = this . orm . em . fork ( ) ;
129
+ const instance = em
126
130
. getRepository ( this . model )
127
131
. create ( flat . unflatten ( params ) ) ;
128
132
129
- await this . validateAndSave ( instance ) ;
133
+ await this . validateAndSave ( instance , em ) ;
130
134
131
135
const returnedParams : Record < string , any > = flat . flatten ( wrap ( instance ) . toJSON ( ) ) ;
132
136
133
137
return returnedParams ;
134
138
}
135
139
136
140
public async update ( pk : string | number , params : Record < string , any > = { } ) : Promise < Record < string , any > > {
137
- const instance = await this . orm . em
141
+ const em = this . orm . em . fork ( ) ;
142
+ const instance = await em
138
143
. getRepository ( this . model )
139
144
. findOne ( pk as any ) ; // mikroorm has incorrect types for findOneOrFail
140
145
141
146
if ( ! instance ) throw new Error ( 'Record to update not found' ) ;
142
147
143
148
const updatedInstance = wrap ( instance ) . assign ( flat . unflatten ( params ) ) ;
144
149
145
- await this . validateAndSave ( updatedInstance ) ;
150
+ await this . validateAndSave ( updatedInstance , em ) ;
146
151
147
152
const returnedParams : Record < string , any > = flat . flatten ( wrap ( updatedInstance ) . toJSON ( ) ) ;
148
153
@@ -151,6 +156,7 @@ export class Resource extends BaseResource {
151
156
152
157
public async delete ( id : string | number ) : Promise < void > {
153
158
await this . orm . em
159
+ . fork ( )
154
160
. getRepository ( this . model )
155
161
. nativeDelete ( id as any ) ; // mikroorm has incorrect types for nativeDelete
156
162
}
@@ -161,7 +167,7 @@ export class Resource extends BaseResource {
161
167
return ! ! model ?. name && ! ! orm ?. getMetadata ?.( ) . find ?.( model . name ) ;
162
168
}
163
169
164
- async validateAndSave ( instance : Loaded < AnyEntity > ) : Promise < void > {
170
+ async validateAndSave ( instance : Loaded < AnyEntity > , em : EntityManager ) : Promise < void > {
165
171
if ( Resource . validate ) {
166
172
const errors = await Resource . validate ( instance ) ;
167
173
if ( errors && errors . length ) {
@@ -179,7 +185,7 @@ export class Resource extends BaseResource {
179
185
}
180
186
}
181
187
try {
182
- await this . orm . em . persistAndFlush ( instance ) ;
188
+ await em . persistAndFlush ( instance ) ;
183
189
} catch ( error ) {
184
190
// TODO: figure out how to get column name from MikroORM's error metadata
185
191
// It currently seems to return only whole Entity
0 commit comments