Skip to content

Commit 1939d1f

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

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

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

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

frameworks/TypeScript/elysia/src/postgres.ts

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

0 commit comments

Comments
 (0)