Skip to content

Commit d659116

Browse files
committed
fix(elysia): incorrect /update
1 parent 1939d1f commit d659116

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

frameworks/TypeScript/elysia/src/db-handlers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff 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
});

frameworks/TypeScript/elysia/src/postgres.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff 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+
2028
export 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
};

0 commit comments

Comments
 (0)