TypeOrm 기본개념

TypeOrm GraphQL

TypeOrm (object Relational Mapping)

TypeORM 에서 ORM 기본, NativeScript, 박람회 및 전자 플랫폼 반응, NodeJS, 브라우저, 코르도바, 폰갭, 이온 성에서 실행할 수 있으며, 타이프 라이터와 자바 스크립트 (ES5, ES6, ES7, ES8)와 함께 사용할 수 있습니다. 이 솔루션의 목표는 항상 최신 JavaScript 기능을 지원하고 몇 가지 테이블이있는 소규모 응용 프로그램부터 여러 데이터베이스가있는 대규모 엔터프라이즈 응용 프로그램에 이르기까지 데이터베이스를 사용하는 모든 종류의 응용 프로그램을 개발하는 데 도움이되는 추가 기능을 제공하는 것입니다.

ORM 이란?

Object Relational Mapping, 객체-관계 매핑

  • 객체와 테이블 시스템(RDBMSs)을 변형 및 연결해주는 작업이라 말 할 수 있다. ORM을 이용한 개발은 객체와 데이터베이스의 변형에 유연하게 대처할 수 있도록 해준다. ORM을 객체 지향 프로그래밍 관점에서 생각해보면, 관계형 데이터베이스에 제약을 최대한 받지 않으면서, 객체를 클래스로 표현하는 것과 같이 관계형 데이터베이스를 객체처럼 쉽게 표현 또는 사용하자는 것이다.

ORM 의 장단점

장점

  • 선언 , 할당 , 종료 같은 부수적인 코드가 없거나 급격히 줄어든다.
  • 각종 객체에 대한 코드를 별도로 작성하기 떄문에 코드의 가독성을 올려준다.
  • SQL 의 절차 , 순차적인 접근 방식이 아닌 객체 접근 방식이다.

단점

  • 완벽하게 ORM 서비스구현이 어려울수있다.
  • 사용하기에 편리함은 있지만 설계가 복잡하다.
  • 프로젝트 복잡성과난이도레따라 퍼포먼스의 큰차이가 있다.

TypeOrm 환경설정 및 사용법

  1. Connection

    데이터베이스와의 상호 작용은 일단 연결을 설정 한 후에 만 ​​가능합니다. TypeORM Connection은 데이터베이스 연결을 설정하지 않고 연결 풀을 설정합니다.

    1-1. CreateConnection

    연결을 생성하는 방법에는 여러 가지가 있습니다. 가장 간단하고 일반적인 방법은 사용하는 것입니다 createConnectioncreateConnections기능을합니다.

    createConnection 단일연결 예제

         import {createConnection, Connection} from "typeorm";
    
         const connection = await createConnection({
             type: "postgres",
             host: "localhost",
             port: 5432,
             username: "test",
             password: "test",
             database: "test"
         });
    

    1-2. CreateConnections 다중연결 예제

         import {createConnections, Connection} from "typeorm";
    
             const connections = await createConnections([{
                 name: "default",
                 type: "mysql",
                 host: "localhost",
                 port: 3306,
                 username: "test",
                 password: "test",
                 database: "test"
             }, {
                 name: "test2-connection",
                 type: "mysql",
                 host: "localhost",
                 port: 3306,
                 username: "test",
                 password: "test",
                 database: "test2"
             }]);
    

    이 두 함수는 Connection전달하고 connect메서드를 호출하는 연결 옵션을 기반으로 만듭니다 . 프로젝트의 루트에 ormconfig.json 파일을 작성할 수 있으며 이러한 옵션을 사용하여 연결 옵션을 자동으로이 파일에서 읽을 수 있습니다.

    1-3. 연결성공시 getConnection함수를 사용하여 앱에서 어디에서나 연결할 수 있습니다.

         import {getConnection} from "typeorm";
    
         const connection = getConnection();
         const secondConnection = getConnection("test2-connection");
    
  2. Entity

    Entity는 데이터베이스 테이블 (또는 MongoDB를 사용할 때 컬렉션)에 매핑되는 클래스입니다. 새로운 클래스를 정의하여 Entity를 생성하고이를 다음과 @Entity()같이 표시 할 수 있습니다.

    example

     import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
     @Entity()
     export class User {
    
         @PrimaryGeneratedColumn()
         id: number;
    
         @Column()
         firstName: string;
    
         @Column()
         lastName: string;
    
         @Column()
         isActive: boolean;
    
     }
    

    위와같은 코드를 입력 할 경우 다음과 같은 데이터베이스 테이블이 생성됩니다.

     +-------------+--------------+----------------------------+
     |                          user                           |
     +-------------+--------------+----------------------------+
     | id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
     | firstName   | varchar(255) |                            |
     | lastName    | varchar(255) |                            |
     | isActive    | boolean      |                            |
     +-------------+--------------+----------------------------+
    

    기본 엔티티는 열과 관계로 구성됩니다. 각 엔티티는 반드시 (MongoDB를 사용중인 경우 또는 ObjectId가 열) 주 열을 갖는다. 각 개체는 연결 옵션에 등록해야합니다.

    2-1. 연결옵션 설정

     import {createConnection, Connection} from "typeorm";
     import {User} from "./entity/User";
    
     const connection: Connection = await createConnection({
         type: "mysql",
         host: "localhost",
         port: 3306,
         username: "test",
         password: "test",
         database: "test",
         entities: [User]
     });
    

    또는 모든 Entity가 포함 된 디렉토리 전체를 지정할 수 있으며 모든 Entity가로드됩니다.

     import {createConnection, Connection} from "typeorm";
     const connection: Connection = await createConnection({
         type: "mysql",
         host: "localhost",
         port: 3306,
         username: "test",
         password: "test",
         database: "test",
         entities: ["entity/*.js"]
     });
    

    2-2. 기본열

    Entity에는 최소한 하나의 기본 열이 있어야합니다. 기본 열의 몇 가지 유형이 있습니다.

     import {Entity, PrimaryColumn} from "typeorm";
     @Entity()
     export class User {
     @PrimaryColumn()
     id: number;
     }
    

    @PrimaryColumn()모든 유형의 값을 취하는 기본 열을 만듭니다. 열 유형을 지정할 수 있습니다. 열 유형을 지정하지 않으면 특성 유형에서 유추됩니다. 아래 예제는 int저장하기 전에 수동으로 지정해야하는 유형으로 ID를 만듭니다.

     import {Entity, PrimaryGeneratedColumn} from "typeorm";
     @Entity()
     export class User {
         @PrimaryGeneratedColumn()
         id: number;
     }
    

    @PrimaryGeneratedColumn(“uuid”)값이 자동으로 생성되는 기본 열을 만듭니다uuid.Uuid는 고유 한 문자열 ID입니다. 저장하기 전에 값을 수동으로 지정할 필요가 없습니다. 값이 자동으로 생성됩니다

     import {Entity, PrimaryColumn} from "typeorm";
     @Entity()
     export class User {
         @PrimaryColumn()
         firstName: string;
    
         @PrimaryColumn()
         lastName: string;
     }
    

    Entity를 사용하여 Entity를 저장 save하면 항상 주어진 Entity ID (또는 ID)로 엔티티를 데이터베이스에서 찾습니다. id / id가 발견되면 데이터베이스에서이 행을 갱신합니다. id / ids 행이없는 경우 새 행이 삽입된다.

  3. 기타설정

    대부분의 경우 연결 옵션을 편리하고 관리하기 용이하게, 별도의 구성 파일에 저장하려고합니다. TypeORM은 여러 구성 소스를 지원하며, ormconfig.[format] 파일 을 만들고 createConnection()구성을 전달하지 않고 응용 프로그램 호출에 구성을 저장하기 만하면됩니다.

연결 방법

    import {createConnection} from "typeorm";
    
    const connection = await createConnection();

사용법ormconfig.json

    {
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "test",
    "password": "test",
    "database": "test"
    }

이것으로 typeorm 의 간단한 개념과 설정방법을 알아보았다.

예제

src/entity/User.ts

    import { Entity, BaseEntity, PrimaryGeneratedColumn, Column } from 'typeorm';
    @Entity()
    export class User extends BaseEntity { 
        @PrimaryGeneratedColumn()
        id: number;
        
        @Column({
            length: 20
        })
        name: string;

        @Column()
        age: number;

        @Column()
        date: Date;
    }

src/api/list/index.ts

    import * as express from 'express';
    import listCtrl from './list.controller';

    const router = express.Router();

    router.get('/lists', listCtrl.getList);
    router.get('/insert', listCtrl.write);

    export default router;

src/api/list/list.controller.ts

    import { Request, Response } from 'express';
    import { getRepository } from 'typeorm';
    import { List } from 'entity/List';

    class ListController {
        // select
        public getList = async (req: Request, res: Response) => {
            try {
                const list = await getRepository(List).find();
                res.json(list);
            } catch (e) {
                res.state(404).json({message: e.message});
                throw new Error(e);
            }
        }

        // insert
        public write = async (req: Request, res: Response) => {
            const lists = await getRepository(List).find();

            const list = new List();
            list.subject = `@ title . ${lists.length}`;
            list.contents = example,
            list.name = 'Sung Jin, Park',
            list.regdate = new Date()
            
            try {
                list.save();
                res.json({ state: 200 })
            } catch (e) {
                throw new Error(e);
            }
        }
    }

    export default new ListController();

src/api/index.ts

    import * as express from 'express';
    import user from './user';
    import list from './list';

    const router = express.Router();

    router.use('/user', user);
    router.use('/list', list);

    export default router;

src/Server.ts

    import routes from './api';
    /* ... */
    constructor () {
        this.App = express();
        this.App.use('/api', routes);
    }

실행결과확인 TypeOrm result 화면