Created
May 5, 2024 20:23
-
-
Save mayankchoubey/78467d64d77e5a7693b0bd0e92c0b91e to your computer and use it in GitHub Desktop.
Node vs Deno vs Bun: Database reads application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { env } from "node:process"; | |
import { DataTypes, Sequelize } from "sequelize"; | |
const dbUser = env.dbUser; | |
const dbUserPass = env.dbUserPass; | |
const dbName = env.dbName; | |
const sequelize = new Sequelize( | |
`postgres://${dbUser}:${dbUserPass}@localhost:5432/${dbName}`, | |
{ | |
logging: false, | |
pool: { | |
max: 10, | |
min: 10, | |
}, | |
}, | |
); | |
await sequelize.authenticate(); | |
const User = sequelize.define("user", { | |
email: { | |
type: DataTypes.STRING, | |
primaryKey: true, | |
}, | |
first: DataTypes.STRING, | |
last: DataTypes.STRING, | |
city: DataTypes.STRING, | |
county: DataTypes.STRING, | |
}, { | |
timestamps: false, | |
}); | |
export async function getUser(userEmail) { | |
return await User.findOne({ | |
where: { | |
email: userEmail, | |
}, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getUser } from "./service.mjs"; | |
export async function handleRequest(ctx) { | |
const userEmail = ctx?.body?.userEmail; | |
if (!userEmail) { | |
ctx.set.status = 400; | |
return; | |
} | |
const user = await getUser(userEmail); | |
if (!user) { | |
ctx.set.status = 204; | |
return; | |
} | |
return Response.json(user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Elysia } from "elysia"; | |
import { handleRequest } from "./elysiaController.js"; | |
const app = new Elysia(); | |
app.post("/searchUser", handleRequest); | |
app.listen({ | |
port: 3000, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getUser } from "./service.mjs"; | |
export async function handleRequest(req, rep) { | |
const userEmail = req?.body?.userEmail; | |
if (!userEmail) { | |
return rep.code(400).send(); | |
} | |
const user = await getUser(userEmail); | |
if (!user) { | |
return rep.code(204).send(); | |
} | |
rep.send(user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fastify from "fastify"; | |
import { handleRequest } from "./fastifyController.mjs"; | |
const app = Fastify({ | |
logger: false, | |
}); | |
app.post("/searchUser", handleRequest); | |
app.listen({ port: 3000 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getUser } from "./service.mjs"; | |
import { HTTPException } from "npm:hono/http-exception"; | |
export async function handleRequest(ctx) { | |
const body = await ctx.req.json(); | |
const userEmail = body?.userEmail; | |
if (!userEmail) { | |
throw new HTTPException(400); | |
} | |
const user = await getUser(userEmail); | |
if (!user) { | |
throw new HTTPException(204); | |
} | |
ctx.status(200); | |
return ctx.json(user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Hono } from "npm:hono"; | |
import { handleRequest } from "./honoController.js"; | |
const app = new Hono(); | |
app.post("/searchUser", handleRequest); | |
Deno.serve({ port: 3000 }, app.fetch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getUser as getDbUser } from "./db.mjs"; | |
export async function getUser(userEmail) { | |
const user = await getDbUser(userEmail); | |
if (!user) { | |
return; | |
} | |
return user; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment