TypeORM도 공식문서가 있다.
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large scale enterprise applications with multiple databases.
우선 ORM은 Object-Relational Mapping의 약자로 객체와 관계형 데이터베이스의 데이터를 자동으로 연결해준다. 그리고 TypeORM의 문서를 읽어보니 첫문장에 바로 TypeORM is an ORM 이라고 설명하고있다. TpyeORM = ORM 이다. 자바스크립트와 타입스크립트와 함께 쓰이는 ORM이라고 설명하고 있고, 앞에 Tpye이 붙은걸보니 TpyeScript처럼 Tpye을 지정 해 줄수있는 ORM이지 않을까 하는 생각이 들었다.
우선적으로
Install the npm package:
npm install typeorm --save 설치를 해줘야한다.
그리고 아래는 공식문서의 data-source.ts 의 예제이다.
프로젝트 드렉토리 구성
MyProject
├── src // place of your TypeScript code
│ ├── entity // place where your entities (database models) are stored
│ │ └── User.ts // sample entity
│ ├── migration // place where your migrations are stored
│ ├── data-source.ts // data source and all connection configuration
│ └── index.ts // start point of your application
├── .gitignore // standard gitignore file
├── package.json // node module dependencies
├── README.md // simple readme file
└── tsconfig.json // TypeScript compiler options
After you have all dependencies installed, edit the data-source.ts file and put your own database connection configuration options in there:
export const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
username: "test",
password: "test",
database: "test",
synchronize: true,
logging: true,
entities: [Post, Category],
subscribers: [],
migrations: [],
})
어떤식으로 Type 지정해주는지는 공식문서의 예제를 통해 간단히 보자.
export class Photo {
id: number
name: string
description: string
filename: string
views: number
isPublished: boolean
}
이런식으로 type들을 지정해 줄수 있다.
And you want to store photos in your database.
To store things in the database, first, you need a database table, and database tables are created from your models.
Not all models, but only those you define as entities.
사진을 데이터베이스에 저장하려면 데이터베이스안에 우선 데이터 베이스 테이블이
필요하고 그 데이터베이스는 entities를 정의해야한다 라고 나온다. 아래에 다른 예시를 보자
Entity is your model decorated by an @Entity decorator. A database table will be created for such models. You work with entities everywhere in TypeORM. You can load/insert/update/remove and perform other operations with them.
Let's make our Photo model an entity:
import { Entity } from "typeorm"
@Entity()
export class Photo {
id: number
name: string
description: string
filename: string
views: number
isPublished: boolean
}
Now, a database table will be created for the Photo entity and we'll be able to work with it anywhere in our app. We have created a database table, however, what table can exist without columns? Let's create few columns in our database table. 그럼 데이터 베이스 테이블이 생성되어질것이고 Photo entity에 아무곳에서나 작업할수있다고한다. 그러나 데이터데이블에 Column이 없을 수있냐? 라는 말이고, colums를 추가해주자고 한다. 여기서 entity를 찾아보니 단어 그자체로의 뜻은 실재,존재자 또한
나무위키에서는
엔티티는 개체를 의미한다.
사람이 생각하는 개념이나 정보 단위와 같은 현실 세계의 대상체, 실세계에 존재하는 유형 혹은 무형 정보의 대상이며 서로 구별이 되는 하나하나의 대상을 의미한다.
개체는 하나 이상의 속성(정보)으로 구성된다. 예로 텔레비전, 공책, 책, 연필, 지우개, 컴퓨터 등등, 이런 것들이 다 개체라고 할 수 있다.
라고 설명하고있는데 몸에 확 와닿지는 않는 설명이다.
내가 다루고자하는? 만들고자하는? 정보들의 각각 하나하나를 개체 = entity라고 하는거 같은데 그 아래 엄청난 내용들이 추가적으로 설명되어있다.
보통 영어 단어가 한국어로 와닿지 않을때는 영영사전을 찾아보는 편인데 Oxford에 entity는 a things with distinct and independent existence 라는 의미가 컴퓨터 용어로 사용되어서 그런지 이것또한 비슷한의미를 내포하는듯하다 어떤 존재!라는 의미라는 정도만 와닿는다.
이렇게 공식문서에 차례차례 Column 만드는법 primary column 만드는법
auto-generated column만드는 법 차례대로 나오니 한번 직접 만들어보면서 익
히는게 좋을거 같다.