Skip to content

Commit

Permalink
feat: add settings for db (#16)
Browse files Browse the repository at this point in the history
* feat: add prisma & basic settings

* chore: add .env to gitignore

* fix: add shadow database url in schema for solving migration issue

* feat: add @prisma/client and db schema file path

* chore: add migration files to gitignore

* chore: add scripts for prisma

* feat: add test api

* feat: add docker compose & actions

* fix: add checkout option & prod action

* fix: add migration files

* fix: remove edge option

* feat: add edge test api

* build(package): update nextjs

* fix: add runtime option

* fix: remove runtime option

* docs(github): add pr template

---------

Co-authored-by: Go Gwangseo <rufflain918@gmail.com>
  • Loading branch information
kanghyun98 and aube-dev authored Feb 4, 2023
1 parent 86df22c commit 643de27
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 134 deletions.
15 changes: 15 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Issue

Resolves #(이슈 번호)

## Description

## Check List

- [ ] PR 제목을 커밋 규칙에 맞게 작성
- [ ] 적절한 라벨 설정
- [ ] GitHub Projects에 연결
- [ ] 작업한 사람 모두를 Assign
- [ ] Code Review 요청
- [ ] DB Schema를 바꾼 경우, DB Schema를 바꾸는 다른 PR이 없는지 확인
- [ ] `main` 브랜치의 최신 상태를 반영하고 있는지 확인
16 changes: 14 additions & 2 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ on: push
jobs:
chromatic-deployment:
runs-on: ubuntu-latest
environment: Preview
steps:
- uses: actions/checkout@v1
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn
run: yarn install --frozen-lockfile

- name: Publish to Chromatic
uses: chromaui/action@v1
with:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/db-migration-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: 'DB Migration (Production)'
on:
push:
branches:
- main
paths:
- 'src/database/schema.prisma'

jobs:
migrate:
runs-on: ubuntu-latest
environment: Production
steps:
- uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Migrate
run: yarn db:prod
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
27 changes: 27 additions & 0 deletions .github/workflows/db-migration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'DB Migration'
on:
pull_request:
types: [opened, synchronize]
paths:
- 'src/database/schema.prisma'

jobs:
migrate:
runs-on: ubuntu-latest
environment: Preview
steps:
- uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Migrate
run: yarn db:prod
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand Down
1 change: 1 addition & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
'@/logics': path.resolve(__dirname, '../src/logics'),
'@/constants': path.resolve(__dirname, '../src/constants'),
'@/assets': path.resolve(__dirname, '../src/assets'),
'@/lib': path.resolve(__dirname, '../lib'),
};

return config;
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"source.fixAll.eslint": true
},
"editor.tabSize": 2,
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"git.pruneOnFetch": true,
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
postgres:
image: postgres:15.1
ports:
- '5466:5432'
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: ''
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
db_data:
12 changes: 12 additions & 0 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PrismaClient } from '@prisma/client';

/**
* @see https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices
*/
const globalForPrisma = global as unknown as { prisma: PrismaClient };

export const prisma = globalForPrisma.prisma || new PrismaClient();

if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}
3 changes: 0 additions & 3 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const withVanillaExtract = createVanillaExtractPlugin();
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
experimental: {
runtime: 'experimental-edge',
},
};

module.exports = withVanillaExtract(nextConfig);
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
"lint": "next lint",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"chromatic": "npx chromatic --exit-zero-on-changes"
"chromatic": "npx chromatic --exit-zero-on-changes",
"db:dev": "npx prisma migrate dev",
"db:push": "npx prisma db push",
"db:up": "docker compose up -d",
"db:nuke": "docker compose down --volumes --remove-orphans",
"db:prod": "npx prisma migrate deploy",
"postinstall": "npx prisma generate"
},
"dependencies": {
"@prisma/client": "4.9.0",
"@tanstack/react-query": "4.16.1",
"@types/node": "18.11.9",
"@types/react": "18.0.25",
"@types/react-dom": "18.0.9",
"@vanilla-extract/css": "1.9.2",
"eslint": "8.28.0",
"eslint-config-next": "13.0.4",
"eslint-config-next": "13.1.6",
"framer-motion": "7.6.7",
"jotai": "1.10.0",
"next": "13.0.4",
"next": "13.1.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "4.9.3"
Expand All @@ -43,7 +50,12 @@
"chromatic": "6.11.4",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-storybook": "0.6.7",
"prettier": "2.7.1"
"prettier": "2.7.1",
"prettier-plugin-prisma": "4.8.0",
"prisma": "4.9.0"
},
"prisma": {
"schema": "./src/database/schema.prisma"
},
"readme": "ERROR: No README data found!",
"_id": "gdsc-ssu-web@0.1.0"
Expand Down
8 changes: 8 additions & 0 deletions src/database/migrations/20230130114123_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- CreateTable
CREATE TABLE "test" (
"id" SERIAL NOT NULL,
"test" TEXT NOT NULL,
"test2" TEXT NOT NULL,

CONSTRAINT "test_pkey" PRIMARY KEY ("id")
);
3 changes: 3 additions & 0 deletions src/database/migrations/migration_lock.toml
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"
14 changes: 14 additions & 0 deletions src/database/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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

model test {
id Int @id @default(autoincrement())
test String
test2 String
}
19 changes: 19 additions & 0 deletions src/pages/api/edge-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { NextRequest } from 'next/server';

export const config = {
runtime: 'edge',
};

export default async function handler(req: NextRequest) {
return new Response(
JSON.stringify({
name: 'Jim Halpert',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
},
},
);
}
29 changes: 13 additions & 16 deletions src/pages/api/hello.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import type { NextRequest } from 'next/server';
import { prisma } from '@/lib/prisma';
import type { NextApiRequest, NextApiResponse } from 'next';

export const config = {
runtime: 'experimental-edge',
};

export default function handler(req: NextRequest) {
return new Response(
JSON.stringify({
name: 'Jim Halpert',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
},
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const newTest = await prisma.test.create({
data: {
test: '1234',
test2: '5678',
},
);
});

res.status(200).json(newTest);
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@/pages/*": ["src/pages/*"],
"@/logics/*": ["src/logics/*"],
"@/constants/*": ["src/constants/*"],
"@/assets/*": ["src/assets/*"]
"@/assets/*": ["src/assets/*"],
"@/lib/*": ["lib/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
Expand Down
Loading

1 comment on commit 643de27

@vercel
Copy link

@vercel vercel bot commented on 643de27 Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web – ./

web-xi-liart.vercel.app
web-gdsc-ssu.vercel.app
web-git-main-gdsc-ssu.vercel.app

Please sign in to comment.