Sequelize: Object Relation Mapping, 그리고 Collation

Ryu·2022년 5월 3일
0

2020-archive

목록 보기
7/7

2020년에 작성한 노트를 옮겨 적은 것입니다.

Concepts

  • SQL 쿼리 작성 없이 javascript의 클래스를 정의하듯 DB 스키마를 정의하고 사용하고 싶다.
  • Objects --< mapping >-- Relations
  • 장점
    • 기존 mysql 모듈을 사용하는 것보다 자유도가 높다 (Bluebird promise, 여러 DBMS 지원 )
    • Driver를 변경해도 쿼리 변경해 줄 필요가 없다.
    • Mysql의 사소한 문법을 지키는데에 드는 오버헤드 x
    • 쿼리(모델)를 위한 파일, data access를 위한 파일을 나누어 관리할 필요가 없다(!)
  • 단점
    • Sequelize를 모르는 상태에서 코드 이해하기가 낯설수도?
    • 결국 SQL을 안쓰기때문에 SQL로서 procedure나 schema 공유가 안됨.

API Docks

새로 알게된 것들

  • inflection: string을 목적에 맞게 parsing해줌

  • Charset과 collation

    • 문제: Schema 생성 시 에러 발생

       Specified key was too long; max key length is 767 bytes
    • 원인

      • InnoDB의 prefix length limit이 767byte 이다.
      • 그런데 DB의 VARCHAR 가 utf8mb4 인코딩이면 ? 767 / 4 = 191 Byte. 즉 VARCHAR(191)까지만 설정 가능
      MariaDB [bee_test]> select maxlen, character_set_name from information_schema.character_sets where character_set_name in('latin1', 'utf8', 'utf8mb4');
      +--------+--------------------+
      | maxlen | character_set_name |
      +--------+--------------------+
      |      1 | latin1             |
      |      3 | utf8               |
      |      4 | utf8mb4            |
      +--------+--------------------+
    • 해결

      • MariaDB의 default charset을 latin1에서 utf8로 변경
      • Sequelize의 define 옵션에 charset, collation 설정
      • DB status
        MariaDB [test]> status
        --------------
        mysql Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
      
        Connection id: 9
        Current database: test
        Current user: root@localhost
        SSL: Not in use
        Current pager: stdout
        Using outfile: ''
        Using delimiter: ;
        Server: MariaDB
        Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
        Protocol version: 10
        Connection: Localhost via UNIX socket
        Server characterset: utf8
        Db characterset: utf8
        Client characterset: utf8
        Conn. characterset: utf8
        UNIX socket: /var/run/mysqld/mysqld.sock
        Uptime: 26 min 4 sec
      [mysqld]
      character-set-server = utf8
      collation-server = utf8_general_ci
      • Sequelize option
      define: {
        charset: 'utf8',
        collate: 'utf8_general_ci'
      }
    • Collation

      • DB엔진이 Character 데이터를 어떻게 처리할 것인가?
      • 대부분 European language는 글자 하나 당 1byte, 그런데 Asian인 경우 3, 4byte가 필요하기 때문.
    • 결국, MariaDB에서 VARCHAR는 몇 바이트까지 처리가능하지? 에 대한 의문에 대한 대답은

    [NATIONAL] VARCHAR(M) [CHARACTER SET charset_name] [COLLATE collation_name]

    A variable-length string. M represents the maximum column length in characters. The range of M is 0 to 65,532. The effective maximum length of a VARCHAR is subject to the maximum row size and the character set used. For example, utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters.
    MariaDB stores VARCHAR values as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A VARCHAR column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

0개의 댓글

Powered by GraphCDN, the GraphQL CDN