File tree Expand file tree Collapse file tree 2 files changed +15
-9
lines changed
frameworks/TypeScript/elysia/src Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Original file line number Diff line number Diff line change @@ -58,13 +58,10 @@ export const dbHandlers = new Elysia()
5858 const worldPromises = new Array ( num ) ;
5959
6060 for ( let i = 0 ; i < num ; i ++ )
61- worldPromises [ i ] = db . find ( rand ( ) ) ;
61+ worldPromises [ i ] = db . findThenRand ( rand ( ) ) ;
6262
6363 const worlds = await Promise . all ( worldPromises ) ;
6464
65- for ( let i = 0 ; i < num ; i ++ )
66- worldPromises [ i ] . randomNumber = rand ( ) ;
67-
6865 await db . bulkUpdate ( worlds ) ;
6966 return worlds ;
7067 } ) ;
Original file line number Diff line number Diff line change @@ -17,14 +17,23 @@ 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+
2028export const bulkUpdate = ( worlds : World [ ] ) => {
21- const sorted = worlds . toSorted ( ( a , b ) => a . id - b . id ) ;
29+ worlds = worlds . toSorted ( ( a , b ) => a . id - b . id ) ;
2230
23- const values = new Array ( sorted . length ) ;
24- for ( let i = 0 ; i < sorted . length ; i ++ )
25- values [ i ] = [ sorted [ i ] . id , sorted [ i ] . randomNumber ] ;
31+ const values = new Array ( worlds . length ) ;
32+ for ( let i = 0 ; i < worlds . length ; i ++ ) {
33+ values [ i ] = [ worlds [ i ] . id , worlds [ i ] . randomNumber ] ;
34+ }
2635
2736 return sql `UPDATE world SET randomNumber = (update_data.randomNumber)::int
28- FROM (VALUES ${ values } ) AS update_data (id, randomNumber)
37+ FROM (VALUES ${ sql ( values ) } ) AS update_data (id, randomNumber)
2938 WHERE world.id = (update_data.id)::int` ;
3039} ;
You can’t perform that action at this time.
0 commit comments