우리가 부여받은 데이터베이스에
table도 넣고 데이터도 넣기 위한 백엔드 서버가 구축되어야함.
백엔드 서버를 구축하기 위해서는 먼저
💡데이터와 테이블을 넣는 작업을 도와주는 typeORM을 설치
📌백엔드 서버구축 파일 기본 설치내용
- 백엔드 서버 구축을 위한 새 폴더 만들기
yarn init
을 해서 빈package.json
생성
- 터미널에 설치목록들 설치
1)typeorm
2)pg
(postgres에 접속하기 위한 보조 라이브러리)
3)typescript
4)tsconfig.json
파일 내용 채우기(docs의 recommended)
💡데이터베이스와 백엔드를 연결해주는건 typeORM에서 제공
import { DataSource } from ‘typeorm’
const AppDataSource = new DataSource ({
type : "postgres",
host : "호스트 주소",
port : 할당받은 포트번호, // 할당받은 포트 번호
username : "postgres",
password : "postgres2022",
database : "postgres", // 어떤 테이블이 들어갈 것 인가
entities : [ ], // 파일경로
logging : true,
// entities 들어간 것들을 데이터베이스와 동기화
synchronize : true
})
AppDatasource.initialize()
.then(()⇒{
//성공시 실행
console.log(”접속완료”)
})
.catch(()⇒{
//실패시 실행
console.log(”접속 실패”)
})
entities의 파일경로
entities["./*.postgres.ts"]
해당 위치의 postgres.ts로 끝나는 모든 파일을 데이터베이스와 연결시켜줘
라는 뜻
📌entities 만들기(실제 데이터)
import { Entity } from ‘typeorm’
// @ -> 데코레이터(Type ORM에 테이블임을 알려줌. 데코레이터는 함수)
@Entity()
export class Board {
// 자동으로 생성되는 번호
@primaryGenerateColumn(’increment’)
number: number;
// 데이터베이스의 타입을 임의로 바꾸고 싶다면 아래와 같이 안에 적으면 됨
@column({type : “text”})
writer: string;
@column()
title: string;
@column()
age: number;
}