File tree Expand file tree Collapse file tree 2 files changed +5
-10
lines changed
frameworks/TypeScript/elysia/src Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Original file line number Diff line number Diff line change @@ -58,10 +58,13 @@ export const dbHandlers = new Elysia()
5858 const worldPromises = new Array ( num ) ;
5959
6060 for ( let i = 0 ; i < num ; i ++ )
61- worldPromises [ i ] = db . findThenRand ( rand ( ) ) ;
61+ worldPromises [ i ] = db . find ( rand ( ) ) ;
6262
6363 const worlds = await Promise . all ( worldPromises ) ;
6464
65+ for ( let i = 0 ; i < num ; i ++ )
66+ worldPromises [ i ] . randomNumber = rand ( ) ;
67+
6568 await db . bulkUpdate ( worlds ) ;
6669 return worlds ;
6770 } ) ;
Original file line number Diff line number Diff line change @@ -17,22 +17,14 @@ export const find = (id: number) =>
1717 ( arr ) => arr [ 0 ] ,
1818 ) ;
1919
20- export const findThenRand = ( id : number ) =>
21- sql < World [ ] > `SELECT id, randomNumber FROM world WHERE id = ${ id } ` . then (
22- ( arr ) => {
23- arr [ 0 ] . randomNumber = rand ( ) ;
24- return arr [ 0 ] ;
25- } ,
26- ) ;
27-
2820export const bulkUpdate = ( worlds : World [ ] ) => {
2921 const sorted = worlds . toSorted ( ( a , b ) => a . id - b . id ) ;
3022
3123 const values = new Array ( sorted . length ) ;
3224 for ( let i = 0 ; i < sorted . length ; i ++ )
3325 values [ i ] = [ sorted [ i ] . id , sorted [ i ] . randomNumber ] ;
3426
35- sql `UPDATE world SET randomNumber = (update_data.randomNumber)::int
27+ return sql `UPDATE world SET randomNumber = (update_data.randomNumber)::int
3628 FROM (VALUES ${ values } ) AS update_data (id, randomNumber)
3729 WHERE world.id = (update_data.id)::int` ;
3830} ;
You can’t perform that action at this time.
0 commit comments