diff --git a/.gitignore b/.gitignore index 9b1ee42..9af116e 100644 --- a/.gitignore +++ b/.gitignore @@ -173,3 +173,6 @@ dist # Finder (MacOS) folder config .DS_Store + +# Prisma DB +prisma/dev.db \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index edc9ee6..f3a76d5 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/example.env b/example.env index 08369fd..9a264d5 100644 --- a/example.env +++ b/example.env @@ -1 +1,2 @@ -PORT=4300 \ No newline at end of file +PORT=4300 +DATABASE_URL="file:./dev.db" # Prisma database URL \ No newline at end of file diff --git a/package.json b/package.json index 3b1b3ac..6ec969c 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "dev": "bun --watch ." }, "dependencies": { - "hono": "^4.5.11" + "@prisma/client": "5.19.1", + "hono": "^4.5.11", + "prisma": "^5.19.1" } } \ No newline at end of file diff --git a/prisma/migrations/20240911211516_clear/migration.sql b/prisma/migrations/20240911211516_clear/migration.sql new file mode 100644 index 0000000..3c7feac --- /dev/null +++ b/prisma/migrations/20240911211516_clear/migration.sql @@ -0,0 +1,9 @@ +-- CreateTable +CREATE TABLE "User" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "email" TEXT NOT NULL, + "name" TEXT NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..e5e5c47 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..009033a --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,17 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "sqlite" + url = env("DATABASE_URL") +} + +model User { + id Int @id @default(autoincrement()) + email String @unique + name String +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a493eda..60ae9d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import { Hono } from "hono"; import { logger } from "hono/logger"; -import userRoute from "./routes/v1/users"; const app = new Hono().use(logger()); +import userRoute from "./routes/v1/users"; app.route("/users", userRoute); export default { diff --git a/src/routes/v1/users/index.ts b/src/routes/v1/users/index.ts index 37986c2..35a297d 100644 --- a/src/routes/v1/users/index.ts +++ b/src/routes/v1/users/index.ts @@ -1,21 +1,47 @@ import { Hono } from "hono"; +import { PrismaClient } from '@prisma/client' const app = new Hono(); +const prisma = new PrismaClient(); -//====================================================> - -// Demo Data for usage before integrating Prisma ORM -const userArray = [ - { id: 1, name: "Jo", email: "jo@thevoid.cafe", hashedPassword: "jo-hash" }, - { id: 2, name: "Danil", email: "semiko@thevoid.cafe", hashedPassword: "semiko-hash" }, - { id: 3, name: "Rhys", email: "rhys@thevoid.cafe", passwordAsHash: "rhys-hash" } -]; +prisma.$connect().then(() => { + console.log("Successfully connected to database"); +}) //====================================================> // Fetch user information by ID -app.get("/:id", (ctx) => { - return ctx.json(userArray.filter(user => user.id === parseInt(ctx.req.param("id")))); +app.get("/:id", async (ctx) => { + const user = await prisma.user.findFirst({ where: { id: parseInt(ctx.req.param("id")) } }); + + console.log(user); + + return ctx.json(user); +}); + +app.post("/create", async (ctx) => { + const body = await ctx.req.json(); + const { name, email } = body; + + if (!name || !email || !body) { + ctx.status(422); + return ctx.json({ msg: "You are missing some of the required parameters! If you didn't know -name- and -email- are expected :3" }); + }; + + if (typeof name !== "string" || typeof email !== "string") { + ctx.status(422); + return ctx.json({ msg: "The provided parameters should be of type String!" }); + }; + + const newUser = await prisma.user.create({ + data: { + name: name, + email: email, + }, + }) + + console.log(newUser); + return ctx.json(newUser); }); //====================================================>