File tree Expand file tree Collapse file tree 3 files changed +21
-32
lines changed Expand file tree Collapse file tree 3 files changed +21
-32
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " better-auth-mikro-orm " : patch
3+ ---
4+
5+ Use ` nativeDelete ` and ` nativeUpdate ` ORM methods for ` updateMany ` /` deleteMany ` . However, this change mean that Identity Map won't be updated after ` deleteMany ` and ` updateMany ` methods.
Original file line number Diff line number Diff line change @@ -145,23 +145,11 @@ export const mikroOrmAdapter = (
145145 async updateMany ( { model, where, update} ) {
146146 const metadata = getEntityMetadata ( model )
147147
148- const rows = await orm . em . find (
148+ return orm . em . nativeUpdate (
149149 metadata . class ,
150-
151150 normalizeWhereClauses ( metadata , where ) ,
152-
153- {
154- fields : [ "id" ]
155- }
156- )
157-
158- rows . forEach ( entity =>
159- orm . em . assign ( entity , normalizeInput ( metadata , update as any ) )
151+ normalizeInput ( metadata , update as any )
160152 )
161-
162- await orm . em . flush ( )
163-
164- return rows . length
165153 } ,
166154
167155 async delete ( { model, where} ) {
@@ -185,19 +173,10 @@ export const mikroOrmAdapter = (
185173 async deleteMany ( { model, where} ) {
186174 const metadata = getEntityMetadata ( model )
187175
188- const [ rows , count ] = await orm . em . findAndCount (
176+ return orm . em . nativeDelete (
189177 metadata . class ,
190-
191- normalizeWhereClauses ( metadata , where ) ,
192-
193- {
194- fields : [ "id" ]
195- }
178+ normalizeWhereClauses ( metadata , where )
196179 )
197-
198- await orm . em . removeAndFlush ( rows )
199-
200- return count
201180 }
202181 }
203182 }
Original file line number Diff line number Diff line change @@ -329,7 +329,7 @@ suite("updateMany", () => {
329329 test ( "updates matched rows" , async ( ) => {
330330 const [ user1 , user2 , user3 ] = await randomUsers . createAndFlushMany ( 3 )
331331
332- const actual = await adapter . updateMany ( {
332+ const affected = await adapter . updateMany ( {
333333 model : "user" ,
334334 where : [
335335 {
@@ -344,12 +344,17 @@ suite("updateMany", () => {
344344 }
345345 } )
346346
347- expect ( actual ) . toBe ( 2 )
348- expect ( [
349- user1 . emailVerified ,
350- user2 . emailVerified ,
351- user3 . emailVerified
352- ] ) . toMatchObject ( [ true , false , true ] )
347+ expect ( affected ) . toBe ( 2 )
348+
349+ const users = await orm . em . find ( entities . User , {
350+ id : { $in : [ user1 . id , user2 . id , user3 . id ] }
351+ } )
352+
353+ expect ( users . map ( ( { emailVerified} ) => emailVerified ) ) . toMatchObject ( [
354+ true ,
355+ false ,
356+ true
357+ ] )
353358 } )
354359
355360 test ( "does not clear Identity Map" , async ( ) => {
You can’t perform that action at this time.
0 commit comments