Prisma and Clean Code Pattern For Elysiajs #1

M Sadewa Wicaksana
3 min readSep 16, 2024

--

When do some researchers about back-end development frameworks in typescript based, i found an unique framework. That’s not just unique but i think, special the name is Elysia.JS.

elysiajs_logo

This frameworks is still new and build based on bun, the one the my interest about this frameworks is the motto.

Ergonomic Framework for Humans

When scrolldown from the website, i found what they disclaimer will be the hack for other populer programming languange for backend like Go.

interesting topic

Technical Exploration

  1. Create a new project, but make sure you already install bun. My bun version is 1.1.27 and node v20.16.0
bun create elysia app-name

2. Install prisma, prisma client, and postgresql as our sample data source provider

bun add -g prisma
bun add @prisma/client
bun add pg

3. Init prisma configurations

prisma init

4. Create database in your local postgresql, as example i named it with db_elysiajs. Then execute this command to create a generate uuid integrations

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

5. Configuration .env file with your local setup

DATABASE_URL="postgresql://sadewawicak:postgres@localhost:5432/db_elysiajs?schema=public"

6. Create a table using migrations

# prisma/schema.prisma
model User {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
name String
email String @unique
password String @db.Text
created_at DateTime @default(now())
updated_at DateTime @default(now())
deleted_at DateTime?
}

run and create migrations using this command

prisma migrate dev --name create_table_user

7. Do some hardening with add a new folder which the name are routes, utils, collections, controller, and model. Specifications for each folder you can find in my readme github project.

#controller/UserController.ts
import { PrismaClient } from "@prisma/client";
import { strings } from "../utils/strings";
import { UserCreateProps } from "../model/UserModel"; // Add this import

const prisma = new PrismaClient();

export const UserController = {
addUser: async ({ body }: { body: UserCreateProps }) => {
try {
const user = await prisma.user.create({
data: body,
});
return {
data: user,
message: strings.response.success,
};
} catch (error) {
console.log("🚀 ~ addUser: ~ error:", error);
return {
data: [],
message: strings.response.failed,
};
}
},
getUser: async () => {
try {
const user = await prisma.user.findMany({});
return {
data: user,
message: strings.response.success,
};
} catch (error) {
console.log("🚀 ~ getUser: ~ error:", error);
return {
data: [],
message: strings.response.failed,
};
}
},
};
# model/UserModel
import { t } from "elysia";

export const UserCreateModels = t.Object({
name: t.String({ maxLength: 250, default: "" }),
email: t.String({ format: "email", default: "sampel@tes.id" }),
password: t.String({ default: "12345" }),
});

export interface UserCreateProps {
name: string;
email: string;
password: string;
}
# routes/RouteUser
import Elysia from "elysia";
import { UserController } from "../controller/UserController";
import { UserCreateModels } from "../model";

export const RouteUsers = (app: Elysia) =>
app.group("/user", (user) => {
user.post(
"/",
async ({ body }) =>
UserController.addUser({
body: body,
}),
{
body: UserCreateModels,
}
);

user.get("/", (() => UserController.getUser()));

return user;
});
# src/index.ts
import { Elysia } from "elysia";
import { RouteUsers } from "../routes/RouteUser";

const app = new Elysia().get("/", () => "Hello Elysia");
app.use(RouteUsers);

app.listen(3000);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

8. Import my export collections from Insomnia to your Insomnia apps

You can download that project in my open mini-project to create a boilerplate for ElysiaJS. Click in here.

#typescript #bun #elysiajs

buymeacoffee.com/sadewawicak25

--

--

M Sadewa Wicaksana
M Sadewa Wicaksana

Written by M Sadewa Wicaksana

Artificial Intelligence and Fullstack Engineering Enthusiast and Still Learning

No responses yet