Skip to content

Commit

Permalink
feat: 完成 prisma 初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhana committed Nov 16, 2024
1 parent 4ab6a3a commit 7d0d512
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NODE_ENV = "development"

HANA_DATABASE_URL = "postgresql://your_username:your_password@localhost:5432/grey_flowers?schema=public"
18 changes: 18 additions & 0 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import process from 'node:process'
import { PrismaClient } from '@prisma/client'

function prismaClientSingleton() {
return new PrismaClient()
}

declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>
// eslint-disable-next-line no-restricted-globals
} & typeof global

const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()

export default prisma

if (process.env.NODE_ENV !== 'production')
globalThis.prismaGlobal = prisma
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default defineNuxtConfig({
'@nuxtjs/seo',
'@vueuse/nuxt',
'@pinia/nuxt',
'@prisma/nuxt',
],
plugins: ['~/plugins/directives.ts', '~/plugins/formik.client.ts'],
devtools: { enabled: true },
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"postinstall": "nuxt prepare",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"new": "node ./scripts/newArticle.js"
"new": "node ./scripts/newArticle.js",
"generate:prisma": "prisma generate",
"migrate:dev": "prisma migrate dev"
},
"dependencies": {
"nuxt": "^3.13.0",
Expand All @@ -33,6 +35,8 @@
"@nuxtjs/seo": "2.0.0-rc.23",
"@nuxtjs/tailwindcss": "^6.12.1",
"@pinia/nuxt": "^0.6.1",
"@prisma/client": "^5.22.0",
"@prisma/nuxt": "^0.0.35",
"@tailwindcss/forms": "^0.5.9",
"@tailwindcss/typography": "^0.5.15",
"@types/node": "^22.7.4",
Expand All @@ -45,6 +49,7 @@
"gsap": "^3.12.5",
"nuxt-lodash": "^2.5.3",
"pinia": "^2.2.5",
"prisma": "5.22.0",
"sass": "^1.79.4",
"sharp": "^0.33.5",
"typescript": "~5.5.4",
Expand Down
129 changes: 109 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 = "postgresql"
url = env("HANA_DATABASE_URL")
}

model Article {
id Int @id @default(autoincrement())
title String @unique
description String?
cover String?
alt String @unique
ogImage String?
publishedAt DateTime @default(now())
editedAt DateTime @updatedAt
published Boolean @default(false)
wordCount Int @default(0)
tags Tag[] @relation("ArticleTags")
category Category? @relation(fields: [categoryId], references: [id])
categoryId Int?
comments Comment[] // 文章的评论
}

model Tag {
id Int @id @default(autoincrement())
name String @unique
articleCount Int @default(0)
articles Article[] @relation("ArticleTags")
}

model Category {
id Int @id @default(autoincrement())
name String @unique
cover String?
articleCount Int @default(0)
articles Article[]
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String? @default("佚名")
site String?
avatar String?
comments Comment[]
replies Comment[] @relation("ReplyToUser")
messages Message[]
}

model Comment {
id Int @id @default(autoincrement())
content String
level CommentLevel @default(PARENT)
parentId Int?
parent Comment? @relation("ParentChild", fields: [parentId], references: [id])
children Comment[] @relation("ParentChild")
article Article? @relation(fields: [articleId], references: [id])
articleId Int?
author User? @relation(fields: [authorId], references: [id])
authorId Int?
replyToUser User? @relation("ReplyToUser", fields: [replyToUserId], references: [id])
replyToUserId Int?
publishedAt DateTime @default(now())
editedAt DateTime @updatedAt
}

model Message {
id Int @id @default(autoincrement())
content String
parentId Int?
parent Message? @relation("ParentChild", fields: [parentId], references: [id])
children Message[] @relation("ParentChild")
author User? @relation(fields: [authorId], references: [id])
authorId Int?
publishedAt DateTime @default(now())
editedAt DateTime @updatedAt
}

enum CommentLevel {
PARENT
CHILD
}
6 changes: 6 additions & 0 deletions server/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default defineEventHandler((event) => {
return {
hello: 'world',
eventInfo: event,
}
})

0 comments on commit 7d0d512

Please sign in to comment.