Skip to content

fix(elysia): bulkUpdate fail #9640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions frameworks/TypeScript/elysia/bun.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "elysia",
"dependencies": {
"elysia": "^1.2.25",
"postgres": "^3.4.5",
},
"devDependencies": {
"@types/bun": "^1.2.4",
"typescript": "^5.7.2",
},
},
},
"packages": {
"@sinclair/typebox": ["@sinclair/[email protected]", "", {}, "sha512-xTzI0LlHZ0eahKuN+2R+yJbEIUyV/ZSdtqvR3sHukgN56nvCW1tz+Yr6fQ6/7+WJ34AhaU9ZmYcHXv8xgj2Lvg=="],

"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.2.4" } }, "sha512-QtuV5OMR8/rdKJs213iwXDpfVvnskPXY/S0ZiFbsTjQZycuqPbMW8Gf/XhLfwE5njW8sxI2WjISURXPlHypMFA=="],

"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-I6LPUvlRH+O6VRUqYOcMudhaIdUVWfsjnZavnsraHvpBwaEyMN29ry+0UVJhImYL16xsscu0aske3yA+uPOWfw=="],

"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="],

"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*", "@types/ws": "~8.5.10" } }, "sha512-nDPymR207ZZEoWD4AavvEaa/KZe/qlrbMSchqpQwovPZCKc7pwMoENjEtHgMKaAjJhy+x6vfqSBA1QU3bJgs0Q=="],

"cookie": ["[email protected]", "", {}, "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA=="],

"elysia": ["[email protected]", "", { "dependencies": { "@sinclair/typebox": "^0.34.27", "cookie": "^1.0.2", "memoirist": "^0.3.0", "openapi-types": "^12.1.3" }, "peerDependencies": { "typescript": ">= 5.0.0" }, "optionalPeers": ["typescript"] }, "sha512-WsdQpORJvb4uszzeqYT0lg97knw1iBW1NTzJ1Jm57tiHg+DfAotlWXYbjmvQ039ssV0fYELDHinLLoUazZkEHg=="],

"memoirist": ["[email protected]", "", {}, "sha512-wR+4chMgVPq+T6OOsk40u9Wlpw1Pjx66NMNiYxCQQ4EUJ7jDs3D9kTCeKdBOkvAiqXlHLVJlvYL01PvIJ1MPNg=="],

"openapi-types": ["[email protected]", "", {}, "sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw=="],

"postgres": ["[email protected]", "", {}, "sha512-cDWgoah1Gez9rN3H4165peY9qfpEo+SA61oQv65O3cRUE1pOEoJWwddwcqKE8XZYjbblOJlYDlLV4h67HrEVDg=="],

"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ=="],

"undici-types": ["[email protected]", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="],
}
}
Binary file removed frameworks/TypeScript/elysia/bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions frameworks/TypeScript/elysia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"module": "src/index.js",
"devDependencies": {
"@types/bun": "^1.1.14",
"@types/bun": "^1.2.4",
"typescript": "^5.7.2"
},
"scripts": {
Expand All @@ -13,7 +13,7 @@
"compile": "bun build --compile --minify --target bun --outfile server src/index.ts"
},
"dependencies": {
"elysia": "^1.2.9",
"elysia": "^1.2.25",
"postgres": "^3.4.5"
}
}
26 changes: 15 additions & 11 deletions frameworks/TypeScript/elysia/src/db-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Elysia, t } from "elysia";
import * as db from "./postgres";
import { Fortune } from "./types";
import {
find,
fortunes as getFortunes,
findThenRand,
bulkUpdate,
} from "./postgres";

export function rand() {
return Math.ceil(Math.random() * 10000);
return ~~(Math.random() * 10000);
}

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

fortunes.push({
id: 0,
Expand Down Expand Up @@ -47,21 +51,21 @@ export const dbHandlers = new Elysia()
// ? Mark as async for Promise result to prevent double Elysia's mapResponse execution
.get("/queries", async (c) => {
const num = parseQueriesNumber(c.query.queries);
const worldPromises = new Array(num);

for (let i = 0; i < num; i++) worldPromises[i] = db.find(rand());
const worldPromises = new Array(num);
for (let i = 0; i < num; i++) worldPromises[i] = find(rand());

return Promise.all(worldPromises);
return await Promise.all(worldPromises);
})
.get("/updates", async (c) => {
const num = parseQueriesNumber(c.query.queries);
const worldPromises = new Array(num);

for (let i = 0; i < num; i++)
worldPromises[i] = db.findThenRand(rand());
for (let i = 0; i < num; i++) worldPromises[i] = findThenRand(rand());

const worlds = await Promise.all(worldPromises);

await db.bulkUpdate(worlds);
await bulkUpdate(worlds);

return worlds;
});
26 changes: 12 additions & 14 deletions frameworks/TypeScript/elysia/src/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ const sql = new SQL({
max: 1,
});

export const fortunes = () => sql<Fortune[]>`SELECT id, message FROM fortune`;
export const fortunes = () =>
sql`SELECT id, message FROM fortune`.execute() as Promise<Fortune[]>;

export const find = (id: number) =>
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
(arr) => arr[0],
);
sql`SELECT id, randomNumber FROM world WHERE id = ${id}`
.execute()
.then((arr) => arr[0]) as Promise<World[]>;

export const findThenRand = (id: number) =>
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
(arr) => {
sql`SELECT id, randomNumber FROM world WHERE id = ${id}`
.execute()
.then((arr) => {
arr[0].randomNumber = rand();
return arr[0];
},
);
}) as Promise<World[]>;

export const bulkUpdate = (worlds: World[]) => {
worlds = worlds.toSorted((a, b) => a.id - b.id);

const values = new Array(worlds.length);
for (let i = 0; i < worlds.length; i++) {
for (let i = 0; i < worlds.length; i++)
values[i] = [worlds[i].id, worlds[i].randomNumber];
}

return sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
return sql`UPDATE world SET randomNumber = update_data.randomNumber
FROM (VALUES ${sql(values)}) AS update_data (id, randomNumber)
WHERE world.id = (update_data.id)::int`;
WHERE world.id = update_data.id`.execute();
};
Loading