이런저런 IT 이야기

Django + Prisma(Postgres) + Docker 구축 본문

Django

Django + Prisma(Postgres) + Docker 구축

이런저런 IT 이야기 2023. 7. 8. 15:00
반응형

https://github.com/leeyonghe/DjangoPrismaProject

샘플코드입니다. 미리 살펴보세요.

 

GitHub - leeyonghe/DjangoPrismaProject

Contribute to leeyonghe/DjangoPrismaProject development by creating an account on GitHub.

github.com

 

1. Docker 구성

  • Python Server 1대
    • python:3.8 서버 이미지를 사용
    • 포트번호 8000번으로 적용
    • 서버 실행시 반드시 prisma migrate 및 generate 명령어 사용
      • npx prisma migrate dev && npx prisma generate --schema prisma/schema.prisma && python3 manage.py runserver 0:80
  • Postgres DB 1대
    • DB 사용자 및 패스워드 적용
      • POSTGRES_USER: postgres
      • POSTGRES_PASSWORD: postgres123!
  • docker-compose.yml 파일 내용
version: '3.7'
services:
  dp_project:
    tty: true
    container_name: dp_project
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - ./project/:/main
    command: sh -c "npx prisma migrate dev && npx prisma generate --schema prisma/schema.prisma && python3 manage.py runserver 0:80"
    depends_on:
      - "postgres"
  postgres:
    container_name: postgres
    tty: true
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres123!
      PGDATA: /data/postgres
    ports:
      - "5432:5432"
    restart: unless-stopped
  • Dockerfile 내용 (Django 서버용)
FROM python:3.8

RUN apt update

RUN apt install nodejs -y

RUN apt install npm -y

RUN npm install -g npx

RUN pip install --upgrade pip

COPY requirements.txt /requirements.txt

RUN pip install -r /requirements.txt

WORKDIR /main

       - npx 명령어를 사용하기 위해서 nodejs & npm & npx 단계적으로 설치
       - npx 명령어는 docker-compose command에서 사용


2. Django 생성

$django-admin startproject project
  • project 이름으로 프로젝트를 생성
  • requirements.txt 파일 생성
psycopg2==2.9.6
Django==4.2.1
requests==2.30.0
djangorestframework==3.14.0
django-cors-headers==4.1.0
django-extensions==3.2.3
prisma==0.9.1
prisma-client==0.2.1
  • setting.py에서 DB사용 주석 처리
    • prisma를 사용하므로 별도 필요가 없음.

  • setting.py에서 template 경로 추가

  • template폴더에 main.html 생성
    • 유저 정보를 표시

  • views.py파일 생성 및 main함수 생성
async def Init(name, email):
    db = Prisma(auto_register=False)
    await db.connect()
    await db.user.create(data = { 'name' : name, 'email' : email })
    await db.disconnect()

async def getUser(email):
    db = Prisma(auto_register=False)
    await db.connect()    
    user = await db.user.find_unique(where={ 'email' : email })
    await db.disconnect()
    return user

def main(request):

    email = "test@gmail.com"
    name = "test"

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(Init(name, email))
    loop.close()
    return render(request, 'main.html', {})

    # loop = asyncio.new_event_loop()
    # task = loop.create_task(getUser(email))
    # loop.run_until_complete(task)
    # print("{}".format(task.result()))    
    # return render(request, 'main.html', { 'user' : task.result()})

3. Prisma(Posgtgres)

  • prisma 파일 생성
generator client {
  provider             = "prisma-client-py"
  interface            = "asyncio"
  recursive_type_depth = 5
}

datasource db {
  provider = "postgresql"
  url      = "postgresql://postgres:postgres123!@postgres:5432/postgres?sslmode=disable"
}

model User {
  id               String            @id @default(cuid())
  name             String?
  email            String?           @unique
}
  • databasece db 설정은 docker-compose에 나와있는 db정보를 적용 해야 한다.
  • 간단히 User 테이블 생성

4. 폴더구조

  • 폴더구조는 개인적인 구조로 정리되었으므로 이 글을 읽는 개발자가 자유롭게 적용하기 바람

5. 프로젝트 실행

$ docker-compose up -d --build
[+] Building 4.1s (15/15) FINISHED
...
[+] Running 2/0
 ✔ Container postgres    Running                                                                                                  0.0s
 ✔ Container dp_project  Running
  • 현재 테이블이 비어있으므로 더미로 넣는 Init() 함수를 실행
from django.shortcuts import render
from prisma import Prisma
import asyncio


async def Init(name, email):
    db = Prisma(auto_register=False)
    await db.connect()
    await db.user.create(data = { 'name' : name, 'email' : email })
    await db.disconnect()

async def getUser(email):
    db = Prisma(auto_register=False)
    await db.connect()    
    user = await db.user.find_unique(where={ 'email' : email })
    await db.disconnect()
    return user

def main(request):

    email = "test@gmail.com"
    name = "test"

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(Init(name, email))
    loop.close()
    return render(request, 'main.html', {})

    # loop = asyncio.new_event_loop()
    # task = loop.create_task(getUser(email))
    # loop.run_until_complete(task)
    # print("{}".format(task.result()))    
    # return render(request, 'main.html', { 'user' : task.result()})
  • 다시 실행시 getUser() 함수를 호출하도록 주석을 변경하여 실행
from django.shortcuts import render
from prisma import Prisma
import asyncio


async def Init(name, email):
    db = Prisma(auto_register=False)
    await db.connect()
    await db.user.create(data = { 'name' : name, 'email' : email })
    await db.disconnect()

async def getUser(email):
    db = Prisma(auto_register=False)
    await db.connect()    
    user = await db.user.find_unique(where={ 'email' : email })
    await db.disconnect()
    return user

def main(request):

    email = "test@gmail.com"
    name = "test"

    # loop = asyncio.new_event_loop()
    # asyncio.set_event_loop(loop)
    # loop.run_until_complete(Init(name, email))
    # loop.close()
    # return render(request, 'main.html', {})

    loop = asyncio.new_event_loop()
    task = loop.create_task(getUser(email))
    loop.run_until_complete(task)
    print("{}".format(task.result()))
    
    return render(request, 'main.html', { 'user' : task.result()})
  • 아래와 같이 출력


6. 아래는 prisma 쿼리 작성 방법이 설명되어있는 링크

https://prisma-client-py.readthedocs.io/en/stable/

 

Prisma Client Python

What is Prisma Client Python? Prisma Client Python is a next-generation ORM built on top of Prisma that has been designed from the ground up for ease of use and correctness. Prisma is a TypeScript ORM with zero-cost type safety for your database, although

prisma-client-py.readthedocs.io

 

반응형