Skip to content

Commit 8335531

Browse files
committed
fix(elysia): bulk update cast to number
1 parent face2ef commit 8335531

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed
-394 Bytes
Binary file not shown.

frameworks/TypeScript/elysia/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"compile": "bun build --compile --minify --target bun --outfile server src/index.ts"
1414
},
1515
"dependencies": {
16-
"elysia": "^1.2.9",
16+
"elysia": "^1.2.25",
1717
"postgres": "^3.4.5"
1818
}
1919
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { Elysia, t } from "elysia";
2-
import * as db from "./postgres";
3-
import { Fortune } from "./types";
2+
import {
3+
find,
4+
fortunes as getFortunes,
5+
findThenRand,
6+
bulkUpdate,
7+
} from "./postgres";
48

59
export function rand() {
6-
return Math.ceil(Math.random() * 10000);
10+
return ~~(Math.random() * 10000);
711
}
812

913
function parseQueriesNumber(q?: string) {
@@ -16,9 +20,9 @@ export const dbHandlers = new Elysia()
1620
server: "Elysia",
1721
})
1822
// ? Mark as async for Promise result to prevent double Elysia's mapResponse execution
19-
.get("/db", async () => db.find(rand()))
23+
.get("/db", async () => find(rand()))
2024
.get("/fortunes", async (c) => {
21-
const fortunes = await db.fortunes();
25+
const fortunes = await getFortunes();
2226

2327
fortunes.push({
2428
id: 0,
@@ -49,19 +53,19 @@ export const dbHandlers = new Elysia()
4953
const num = parseQueriesNumber(c.query.queries);
5054
const worldPromises = new Array(num);
5155

52-
for (let i = 0; i < num; i++) worldPromises[i] = db.find(rand());
56+
for (let i = 0; i < num; i++) worldPromises[i] = find(rand());
5357

5458
return Promise.all(worldPromises);
5559
})
5660
.get("/updates", async (c) => {
5761
const num = parseQueriesNumber(c.query.queries);
5862
const worldPromises = new Array(num);
5963

60-
for (let i = 0; i < num; i++)
61-
worldPromises[i] = db.findThenRand(rand());
64+
for (let i = 0; i < num; i++) worldPromises[i] = findThenRand(rand());
6265

6366
const worlds = await Promise.all(worldPromises);
6467

65-
await db.bulkUpdate(worlds);
68+
await bulkUpdate(worlds);
69+
6670
return worlds;
6771
});

frameworks/TypeScript/elysia/src/postgres.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ export const findThenRand = (id: number) =>
2626
);
2727

2828
export const bulkUpdate = (worlds: World[]) => {
29-
worlds = worlds.toSorted((a, b) => a.id - b.id);
30-
3129
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-
}
30+
for (let i = 0; i < worlds.length; i++)
31+
values[i] = [+worlds[i].id, +worlds[i].randomNumber];
3532

3633
return sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
3734
FROM (VALUES ${sql(values)}) AS update_data (id, randomNumber)

frameworks/TypeScript/elysia/src/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Elysia } from "elysia";
22
import { dbHandlers } from "./db-handlers";
33

4-
const app = new Elysia()
4+
const app = new Elysia({ precompile: true })
55
.headers({
66
server: "Elysia",
77
})
@@ -17,4 +17,6 @@ const app = new Elysia()
1717
})
1818
.listen(8080);
1919

20+
console.log(app.routes[5].composed.toString())
21+
2022
console.info(`🦊 Elysia is running at ${app.server!.url}`);

0 commit comments

Comments
 (0)