Skip to content

Commit

Permalink
added prisma and connected to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
chandanck22 committed Mar 23, 2024
1 parent 1263e34 commit e098173
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 22 deletions.
15 changes: 14 additions & 1 deletion 0-1/week13/blogosphere/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ npm install
cd ../backend
npm install
```
4. Set up your `PostgreSQL` database and configure the Prisma connection in backend/prisma/schema.prisma.
4. Create a `.env` and `wrangler.toml` file inside backend.

- inside `.env` - Use [AIVEN](https://aiven.io/) postgres database
```
DATABASE_URL="PASTE DATABASE URL"
```
- inside `wrangler.toml` - Use [PRISMA](https://www.prisma.io/data-platform/accelerate)
```
name = "backend"
compatibility_date = "2023-12-01"
[vars]
DATABASE_URL="PASTE the PRISMA URL"
```

5. Start the `backend` server using Cloudflare Workers:

Expand Down
3 changes: 2 additions & 1 deletion 0-1/week13/blogosphere/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
node_modules
dist
.wrangler
.dev.vars
.env
wrangler.toml

# Change them to your taste:
package-lock.json
Expand Down
5 changes: 4 additions & 1 deletion 0-1/week13/blogosphere/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"hono": "^4.1.3"
"@prisma/client": "^5.11.0",
"@prisma/extension-accelerate": "^1.0.0",
"hono": "^4.1.3",
"prisma": "^5.11.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240208.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"email" TEXT NOT NULL,
"name" TEXT,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Post" (
"id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"content" TEXT NOT NULL,
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" TEXT NOT NULL,

CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
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"
31 changes: 31 additions & 0 deletions 0-1/week13/blogosphere/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id String @id @default(uuid())
email String @unique
name String?
password String
posts Post[]
}

model Post {
id String @id @default(uuid())
title String
content String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
}
18 changes: 14 additions & 4 deletions 0-1/week13/blogosphere/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { Hono } from 'hono'
import { Hono } from 'hono';
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

const app = new Hono()
const app = new Hono<{
Bindings: {
DATABASE_URL: string;
}
}>()

app.get('/', (c) => {
return c.text('Hello Hono!')
})

app.post('/api/v1/signup', (c) => {
return c.text('signup route')
const prisma = new PrismaClient({
datasourceUrl: c.env.DATABASE_URL,
}).$extends(withAccelerate());
return c.text('signup route');
})

app.post('/api/v1/signin', (c) => {
return c.text('signin route')
return c.text('signin route');
})

app.get('/api/v1/blog/:id', (c) => {
Expand All @@ -30,3 +39,4 @@ app.put('/api/v1/blog', (c) => {
})

export default app

17 changes: 2 additions & 15 deletions 0-1/week13/blogosphere/backend/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
name = "backend"
compatibility_date = "2023-12-01"

# [vars]
# MY_VAR = "my-variable"

# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"

# [[d1_databases]]
# binding = "DB"
# database_name = "my-database"
# database_id = ""
[vars]
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiZTYxNDdkZTYtMDBiNi00MmZlLTk2ZGYtOTQ2ZjNkNDM5OGFmIiwidGVuYW50X2lkIjoiOWZlMGJlMjM2ZTk5ZGNjOTRhMzk0ZmI5ZTg2YWZkOTM0N2FjNDAyMjFhYjU1ZDg4NGQwZDgxNGY0ZWM5NGQ4YiIsImludGVybmFsX3NlY3JldCI6IjAwNDNjOGVhLTc0NDQtNDlmZS04OTAwLTMxMzNkNGIwNmY4ZiJ9.nzyOiAlvKo4cu1DamwHJOmKlr_QvQvzHjvrFwES-LnQ"

0 comments on commit e098173

Please sign in to comment.