Skip to content

Commit 910915e

Browse files
committed
integrate prisma
1 parent 19c466e commit 910915e

File tree

9 files changed

+69
-24
lines changed

9 files changed

+69
-24
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MONGO_URI="mongodb url"
1+
DATABASE_URL="mongodb url"
22
BEARER_TOKEN="256-bit WEP secret key"

bun.lockb

7.66 KB
Binary file not shown.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
"deploy": "wrangler deploy --minify src/index.ts"
55
},
66
"dependencies": {
7+
"@prisma/client": "^5.15.0",
78
"@types/bun": "^1.1.3",
89
"hono": "^4.4.4",
910
"mongoose": "^8.4.1"
1011
},
1112
"devDependencies": {
1213
"@cloudflare/workers-types": "^4.20240529.0",
14+
"@types/node": "^20.14.2",
1315
"bun-types": "^1.1.12",
16+
"prisma": "^5.15.0",
17+
"ts-node": "^10.9.2",
18+
"typescript": "^5.4.5",
1419
"wrangler": "^3.57.2"
1520
}
1621
}

prisma/schema.prisma

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client-js"
9+
}
10+
11+
datasource db {
12+
provider = "mongodb"
13+
url = env("DATABASE_URL")
14+
}
15+
16+
model Store {
17+
id String @id @default(auto()) @map("_id") @db.ObjectId
18+
name String
19+
storeKey String
20+
data Data[]
21+
}
22+
23+
model Data {
24+
id String @id @default(auto()) @map("_id") @db.ObjectId
25+
storeId String @db.ObjectId
26+
value String
27+
store Store @relation(fields: [storeId], references: [id])
28+
}

src/config/db.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/controllers/storeController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Context } from 'hono'
2+
3+
export const createStore = async (c: Context) => {
4+
return c.json({ message: "Store created" })
5+
}

src/db/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PrismaClient } from '@prisma/client'
2+
3+
const prismaClientSingleton = () => {
4+
return new PrismaClient()
5+
}
6+
7+
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>
8+
9+
const globalForPrisma = globalThis as unknown as {
10+
prisma: PrismaClientSingleton | undefined
11+
}
12+
13+
const prisma = globalForPrisma.prisma ?? prismaClientSingleton()
14+
15+
export default prisma
16+
17+
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { Hono } from 'hono'
2-
import connectDB from "./config/db"
32
import { bearerAuth } from 'hono/bearer-auth'
43

4+
import storeRouter from './routes/store'
5+
56
const app = new Hono()
67

78
const token = Bun.env.BEARER_TOKEN || ""
89

910
app.use('/api/*', bearerAuth({ token }))
1011

12+
app.route('/api/store', storeRouter)
13+
1114
app.get('/', (c) => {
12-
connectDB()
1315
return c.text("Hello world")
1416
})
1517

16-
app.get('/api/page', (c) => {
17-
return c.json({ message: 'You are authorized' })
18-
})
19-
2018
export default app

src/routes/store.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Hono } from 'hono'
2+
import { createStore } from '../controllers/storeController'
3+
4+
const storeRouter = new Hono()
5+
6+
// create a store
7+
storeRouter.post('/', (c) => createStore(c))
8+
9+
export default storeRouter

0 commit comments

Comments
 (0)