-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server and prisma config; create poll route
- Loading branch information
1 parent
17a9fff
commit de1ef64
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- CreateTable | ||
CREATE TABLE "Poll" ( | ||
"id" TEXT NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "Poll_pkey" PRIMARY KEY ("id") | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Poll { | ||
id String @id @default(uuid()) | ||
title String | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @default(now()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fastify, { FastifyReply, FastifyRequest } from "fastify"; | ||
import { PrismaClient } from "@prisma/client"; | ||
import { z } from "zod"; | ||
|
||
const app = fastify(); | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
app.post("/polls", async (request: FastifyRequest, reply: FastifyReply) => { | ||
const createPollBody = z.object({ | ||
title: z.string(), | ||
}); | ||
|
||
const { title } = createPollBody.parse(request.body); | ||
|
||
const poll = await prisma.poll.create({ | ||
data: { | ||
title, | ||
}, | ||
}); | ||
|
||
return reply.status(201).send({ | ||
pollId: poll.id, | ||
}); | ||
}); | ||
|
||
app.listen({ port: 3333 }).then(() => { | ||
console.log("HTTP server running!"); | ||
}); |