chore: setup prisma
testing: add user registration example base
This commit is contained in:
parent
756fc472ec
commit
5b03696ed4
9 changed files with 74 additions and 13 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -173,3 +173,6 @@ dist
|
||||||
|
|
||||||
# Finder (MacOS) folder config
|
# Finder (MacOS) folder config
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# Prisma DB
|
||||||
|
prisma/dev.db
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -1 +1,2 @@
|
||||||
PORT=4300
|
PORT=4300
|
||||||
|
DATABASE_URL="file:./dev.db" # Prisma database URL
|
|
@ -7,6 +7,8 @@
|
||||||
"dev": "bun --watch ."
|
"dev": "bun --watch ."
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"hono": "^4.5.11"
|
"@prisma/client": "5.19.1",
|
||||||
|
"hono": "^4.5.11",
|
||||||
|
"prisma": "^5.19.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
9
prisma/migrations/20240911211516_clear/migration.sql
Normal file
9
prisma/migrations/20240911211516_clear/migration.sql
Normal file
|
@ -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");
|
3
prisma/migrations/migration_lock.toml
Normal file
3
prisma/migrations/migration_lock.toml
Normal file
|
@ -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"
|
17
prisma/schema.prisma
Normal file
17
prisma/schema.prisma
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { logger } from "hono/logger";
|
import { logger } from "hono/logger";
|
||||||
|
|
||||||
import userRoute from "./routes/v1/users";
|
|
||||||
const app = new Hono().use(logger());
|
const app = new Hono().use(logger());
|
||||||
|
|
||||||
|
import userRoute from "./routes/v1/users";
|
||||||
app.route("/users", userRoute);
|
app.route("/users", userRoute);
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
|
@ -1,21 +1,47 @@
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
//====================================================>
|
prisma.$connect().then(() => {
|
||||||
|
console.log("Successfully connected to database");
|
||||||
// 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" }
|
|
||||||
];
|
|
||||||
|
|
||||||
//====================================================>
|
//====================================================>
|
||||||
|
|
||||||
// Fetch user information by ID
|
// Fetch user information by ID
|
||||||
app.get("/:id", (ctx) => {
|
app.get("/:id", async (ctx) => {
|
||||||
return ctx.json(userArray.filter(user => user.id === parseInt(ctx.req.param("id"))));
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
//====================================================>
|
//====================================================>
|
||||||
|
|
Loading…
Reference in a new issue