Code Monkey home page Code Monkey logo

nest-graphql-typeorm's Introduction

Nest - TypeOrm 인증 서버 예시

사용 기술

추가 설치 라이브러리

코드 실행 및 api 테스트 방법

npm i
cp .env.sample .env.dev

// env.sample
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=
DB_PASSWORD=
DB_NAME=
SECRET_KEY=
  • 패키지 설치 후, cp 명령어로 env 파일 복사
  • 먼저 postgreSQL DB를 생성하셔야 합니다.
  • DB_NAME은 생성한 DB 이름, DB_USERNAME은 관리자 이름, DB_PASSWORD은 임의로 설정한 비밀번호입니다. 비밀번호는 localhost에서 실행할 경우, 아무 값이나 넣으셔도 무방합니다.
  • 현재 편의를 위해 임시 키 값을 넣어놨으므로, .env.devDB_NAME DB_USERNAME DB_PASSWORD 값만 넣어주면 됩니다.
  • 아래 명령어로 오류 없이 앱이 실행된다면 테이블은 자동으로 생성됩니다. (User 테이블과 Child 테이블)
npm run start:dev
  • 접속 하신 로컬의 뒤에 /graphql을 붙이면 플레이그라운드로 접속 가능합니다.
  • 플레이그라운드에서 쿼리와 뮤테이션 명세 확인 및 api 테스트가 가능합니다. 예시 :https://hocalhost:3000/graphql
  • createAccountOfParent createAccountOfSitter login 뮤테이션 외에는 token이 필요하므로, 플레이그라운드 하단의 HTTP HEADERS에 로그인 실행 후 받아온 token 값을 넣어주어야 합니다. 토큰의 키값은 반드시 jwt여야만 합니다.
  • 예시 image

DB 테이블

유저 테이블

  • Child 테이블과 OneToMany 관계
Name Type required Constraint Description
id number yes Primary key
created_at Date yes Not Null
updated_at Date yes Not Null
member_number number yes Unique, Not Null 회원번호 자동생성
name string yes Not Null 회원 이름
birthday Date yes Not Null 생년원일
gender enum yes Not Null 성별
account_id number yes Unique, Not Null 회원가입 아이디
password string yes Not Null 비밀번호
email string yes Not Null 이메일
roles enum yes Not Null 회원 역할
parent_description string 부모 역할만 생성가능
sitter_description string 시터 역할만 생성 가능
care_range enum 시터 역할만 생성 가능

아이 테이블

  • User 테이블과 ManyToOne 관계
Name Type required Constraint Description
id number yes Primary key
created_at Date yes Not Null
updated_at Date yes Not Null
birthday Date yes Not Null 생년월일
gender enum yes Not Null 성별
parentId number yes Not Null User와 릴레이션

쿼리

me

  • 리턴 타입 : 유저 엔티티의 모든 필드
  • 로그인한 누구나 쿼리 요청 가능

userProfile

  • 인풋 타입 : 유저 Id
  • 리턴 타입 : 유저 엔티티의 필드와 성공 여부를 가진 객체
  • 로그인한 누구나 쿼리 가능

뮤테이션

createAccountOfParent

  • 인풋 타입 : User 엔티티의 필수 필드와 child엔티티 필드, 추가로 부모 역할을 위한 parentDescription 필드
  • 리턴 타입 : 성공 여부 객체
  • 권한 검사 없이 뮤테이션 요청 가능

createAccountOfSitter

  • 인풋 타입 : User 엔티티의 필수 필드와 시터 역할을 위한 careRangeparentDescription 필드
  • 리턴 타입 : 성공 여부 객체
  • 권한 검사 없이 뮤테이션 요청 가능

login

  • 인풋 타입 : accountIdpassword
  • 리턴 타입 : 성공 여부 객체와 jwt 토큰 반환
  • 권한 검사 없이 뮤테이션 요청 가능

updateProfile

  • 인풋 타입 : 업데이트를 원하는 유저 객체의 필드, http header에 jwt 토큰 넣어주어야 함
  • 리턴 타입 : 성공 여부 객체
  • jwt 토큰 기반 로그인된 유저는 누구나 요청 가능

changePassword

  • 인풋 타입 : 변경할 패스워드, http header에 jwt 토큰 넣어주어야 함
  • 리턴 타입 : 성공 여부 객체
  • 비고 : 비밀번호는 생성과 업데이트 전에 비밀번호 정책 검사 후, 해쉬 함수를 적용
  • jwt 토큰 기반 로그인된 유저는 누구나 요청 가능

addParentRole

  • 인풋 타입 : 부모 역할로서의 요청사항, Child 엔티티 필드
  • 리턴 타입 : 성공 여부 객체
  • 비고 : 내부에서 실제 roles 필드에 들어있는 값을 확인해서 이미 모든 역할을 가지고 있다면 요청을 취소함
  • jwt 토큰 기반 로그인된 유저 중 오직 Sitter 역할만 요청 가능

addSitterRole

  • 인풋 타입 : 시터 역할로서의 자기 소개 및 케어 가능 범위
  • 리턴 타입 : 성공 여부 객체
  • 비고 : 내부에서 실제 roles 필드에 들어있는 값을 확인해서 이미 모든 역할을 가지고 있다면 요청을 취소함
  • jwt 토큰 기반 로그인된 유저 중 오직 Parent 역할만 요청 가능

updateChildInfo

  • 인풋 타입 : 변경하고자 하는 Child 엔티티 필드
  • 리턴 타입 : 성공 여부 객체
  • 비고 : 부모는 여러 아이를 가질 수 있지만, 현재 모든 아이의 정보를 한번에 변경하지 못함
  • jwt 토큰 기반 로그인된 유저 중 오직 Parent 역할을 가지고 있는 유저만 요청 가능

기타 사항

  • User 엔티티와 Child 엔티티의 릴레이션이 매끄럽지 못하여, User 엔티티를 통한 Child 엔티티의 생성과 수정에 리팩토링이 필요함
  • 에러 처리는 error 객체를 반환하기 보다, 제어할 수 있는 부분은 text 메시지로 처리하고자 했음

nest-graphql-typeorm's People

Contributors

devyuns avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.