Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created May 5, 2024 20:23
Show Gist options
  • Save mayankchoubey/78467d64d77e5a7693b0bd0e92c0b91e to your computer and use it in GitHub Desktop.
Save mayankchoubey/78467d64d77e5a7693b0bd0e92c0b91e to your computer and use it in GitHub Desktop.
Node vs Deno vs Bun: Database reads application
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,
},
});
}
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);
}
import { Elysia } from "elysia";
import { handleRequest } from "./elysiaController.js";
const app = new Elysia();
app.post("/searchUser", handleRequest);
app.listen({
port: 3000,
});
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);
}
import Fastify from "fastify";
import { handleRequest } from "./fastifyController.mjs";
const app = Fastify({
logger: false,
});
app.post("/searchUser", handleRequest);
app.listen({ port: 3000 });
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);
}
import { Hono } from "npm:hono";
import { handleRequest } from "./honoController.js";
const app = new Hono();
app.post("/searchUser", handleRequest);
Deno.serve({ port: 3000 }, app.fetch);
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