:::: 개발 ::::/::: JSCRIPT :::

node js , sequelize , postgres 개발 세팅 1

nayha 2020. 3. 5. 23:57

node js
-express
-router
-sequelize

docker
   DB 설치
   docker run -p 5432:5432 -e   POSTGRES_PASSWORD=1234## -e   POSTGRES_USER=dev -e   POSTGRES_DB=DEVDB --name postgresCon -d postgres

   #프로세스 러닝 확인
   docker ps

   #postgresSql 서버 접속
   docker exec -i -t postgresCon bash

   # 유저 postgres 로 변경
   su - postgres

 

   # 변경 후 postgres cli 접속   

   psql --username dev --dbname DEVDB


   #DB 목록조회
   \l
   #테이블 조회
   \dt
   #postgresql 종료
   \q
   #docker 컨테이너에서 나가기
   exit

 POSTGRES CLI 확인



   postgresql 구성 종료
   https://055055.tistory.com/39

Nodejs 설치
   디렉토리 생성 후 ex) mkdir NodePrj

​   npm init
   
   ## 시퀄라이즈 cli 로 여러가지 기본 생성 진행 할수 있다 (글로벌)
   npm install -g sequelize-cli 

 

   sequelize init 
Created "config\config.json"
Successfully created models folder at "D:\nodePrj\models".
Successfully created migrations folder at "D:\nodePrj\migrations".
Successfully created seeders folder at "D:\nodePrj\seeders".

 

config.json

{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  }
}




   ## 시퀄라이즈 ,포스트그레스 설치
   npm install sequelize pg --save

 

 완료 후 폴더 구조

기본 구조



   시퀄라이즈(sequelize)
    자바스크립트로 간단히 DB 제어가능하다
   ORM(Object-Relational Mapping) 이용
    객체와 관계형 데이터베이스의 관계를 매핑 해주는 도구
   Model Define
    Table의 Schema를 표현하는 수단
    모델을 정의한 이후에 실제로 올려 캐쉬하려면 import 해야된다.
    예)
    module.export = (sequlize, DataTypes) =>
       (
           sequelize.definde('user',{
                   email:{
                       type:DataTypes.STRING(40),
                       allowNull:false,
                       unique:true,
                   },
                   nick:{
                       type:DataTypes.STRING(15),
                       allowNull:false,
                   },
                   password:{
                       type:DataTypes.STRING(100),
                       allowNull:true,
                   },
                   provider:{
                       type:DataTypes.STRING(10),
                       allowNull:false,
                       defaultValue:'local',
                   },
                   snsId:{
                       type:DataTypes.STRING(30),
                       allowNull:true,
                   },
               },
               {
                   //테이블생성 후 자동으로 createdAt,updatedAt 컬럼생성
                   //생성된 시간과 수정된 시간 알수있다
                   timestamps: true,
                   // deletedAt 컬럼 생성
                   // 실제로 삭제하지 않고 삭제된 날짜가 deletedAt 추가
                   // find 작업해 deletedAt row 에 데이터가 있으면 제외 된다
                   // timestamps: true 일때만 사용가능
                   paranoid: true,
               }
           )
       );
​​

반응형