Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week03 - NoSQL, MongoDB, Mongoose, 3-Tier Architecure #4

Open
hyejungg opened this issue Apr 26, 2022 · 1 comment
Open

week03 - NoSQL, MongoDB, Mongoose, 3-Tier Architecure #4

hyejungg opened this issue Apr 26, 2022 · 1 comment
Assignees
Labels

Comments

@hyejungg
Copy link
Member

๐Ÿ”ฅ ์•Œ๊ฒŒ๋œ ์ 

  1. NoSQL
  2. MongoDB?!
  3. Mongoose
  4. ๊ฒฌ๊ณ ํ•œ Node.js ํ”„๋กœ์ ํŠธ ์„ค๊ณ„
  5. CRUD ์‹ค์Šต

๐Ÿ“š ๋ฐฐ์šด ์ 

NoSQL, SQL?!

SQL : RDBMS์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ํŠน์ˆ˜ ๋ชฉ์ ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด

NoSQL : ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐํ•œ ์Šคํ‚ค๋งˆ ์ œ๊ณต
// Not-Only SQL (sql๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค~)

SQL๊ณผ NoSQL์ฐจ์ด

3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_sql:nosqlแ„Žแ…กแ„‹แ…ต

  • NoSQL์€ DB์ž์ฒด๊ฐ€ ํ™•์žฅ๋˜๊ณ  ๋ถ„์‚ฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ(์ˆ˜ํ‰์  ํ™•์žฅ)์— ์ ํ•ฉํ•˜๊ฒŒ ๋˜์–ด ์žˆ์Œ

NoSQL ํŠน์ง•

  1. ํ‘œ์ค€ํ™”๋œ ๊ตฌ์กฐ์  ์งˆ์˜ ์–ธ์–ด(SQL)์ด ์—†์Œ
  2. ๊ด€๊ณ„๋ฅผ ๊ฐ–์ง€ ์•Š์Œ

์–ธ์ œ ์“ฐ์ง€?

  • ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ • < ์กฐํšŒ ๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ
  • ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ -> ์œ ์—ฐํ•œ ๊ตฌ์กฐ
  • ๋ง‰๋Œ€ํ•œ ์–‘์˜ ํŠธ๋ž˜ํ”ฝ์„ ๋‹ค๋ฃฐ ๊ฒฝ์šฐ -> ์ˆ˜ํ‰์  ํ™•์žฅ

ex.

์ผ๊ด€์„ฑ์ด ์ค‘์š”ํ•œ ์€ํ–‰์‹œ์Šคํ…œ : SQL (ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ํ•„์š”ํ•˜๋ฏ€๋กœ) / ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์กฐํšŒ๊ฐ€ ๋งŽ์€ SNS : NoSQL

3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_sqlแ„Œแ…ฉแ†ผแ„…แ…ฒ

MongoDB?

  • document ์ง€ํ–ฅ NoSQL
  • JSON ํ˜•ํƒœ์˜ ๋™์  ์Šคํ‚ค๋งˆํ˜• document ์‚ฌ์šฉ

Document

  • Database > Collection > Document > Field ๊ณ„์ธต์œผ๋กœ ์ด๋ค„์ง

3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_document?
3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_collection?
3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_feild?

  • MongoDB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ตœ์†Œ ๋‹จ์œ„
  • ํ•„๋“œ์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ
  • ๋ฐ์ดํ„ฐ ์ž…์ถœ๋ ฅ ์‹œ JSON ํ˜•์‹์„ ์‚ฌ์šฉ, ์ €์žฅ ์‹œ์—๋Š” BSON(Binary JSON) ๋ณ€ํ™˜
  • ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ ์ค‘์ฒฉ document, ๋ฐฐ์—ด, ์ปฌ๋ ‰์…˜ ๋ถ„๋ฆฌ ๋“ฑ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Œ

BASE

  • Basically Available (๊ฐ€์šฉ์„ฑ)
  • Soft State (์†Œํ”„ํŠธ ์ƒํƒœ)
  • Eventually Consistent (๊ฒฐ๊ณผ์  ์ผ๊ด€์„ฑ)

MongoDB ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ์–ด๋–ป๊ฒŒ?

Document ์„ค๊ณ„ -> Collection ์„ค๊ณ„ ์ˆœ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ํŽธํ•จ!

  • Embedded ๋ฐฉ์‹ : 1๊ฐœ์˜ document์•ˆ์— ๋‹ค ๋„ฃ๋Š” ๋ฐฉ์‹.
  • References ๋ฐฉ์‹ : RDBMS์˜ ์ •๊ทœํ™”ํ•œ ๋Š๋‚Œ. (๊ฐ document๋Š” ์™ธ๋ž˜ํ‚ค ๊ฐ™์€ ๊ฑธ๋กœ ์ฐธ์กฐํ•˜๋Š” ํ˜•ํƒœ)
    • ์ž์‹ ์ฐธ์กฐ : ๋ถ€๋ชจ๊ฐ€ ์ž์‹์˜ id๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๊ฐ–๋Š” ํ˜•ํƒœ
    • ๋ถ€๋ชจ ์ฐธ์กฐ : ์ž์‹์—์„œ ๋ถ€๋ชจ์˜ id๋ฅผ ๊ฐ–๋Š” ํ˜•ํƒœ
    • ์ƒํ˜ธ ์ฐธ์กฐ : ๊ฐ๊ฐ์˜ document๊ฐ€ id๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํ˜•ํƒœ

mongoDB๋Š” ์ฐธ์กฐ๊ฐ€ ๋งŽ์œผ๋ฉด ์กฐํšŒ ์‹œ๋‚˜ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆด ๋•Œ, ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์ง€ ์•Š์Œ. join์ด ์•ˆ๋˜๋‹ˆ๊นŒ!!

์–ด๋–ค ๊ฑธ ์“ฐ๋Š”๊ฒŒ ์ข‹์„๊นŒ?
3แ„Žแ…กแ„‰แ…ฆแ„†แ…ตแ„‚แ…ก_แ„‹แ…ฅแ†ซแ„Œแ…ฆแ„‰แ…กแ„‹แ…ญแ†ผ?

MongoDB ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๋ง ํŒจํ„ด

  1. Extended Reference : ์„œ๋กœ ๋‹ค๋ฅธ Collection์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ์ผ๋ถ€๋ฅผ ๋‚ด๋ถ€์— ์ž„๋ฒ ๋“œํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ํŒจํ„ด
  • ์ฟผ๋ฆฌ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
  1. Subset : ์„œ๋กœ ๋‹ค๋ฅธ Collection์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ์ผ๋ถ€๋ฅผ ๋‚ด๋ถ€์— ์ž„๋ฒ ๋“œํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ํŒจํ„ด
    • ๋‹ค๋ฅธ Collection์— ์žˆ๋Š” ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ์ด๋ฏ€๋กœ Join์ด ์–ด๋ ค์›€.
      • Join ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฟผ๋ฆฌ ์กฐํšŒ ์„ฑ๋Šฅ์ด ๋œ์–ด์งˆ ์ˆ˜ ์žˆ์Œ
    • ์กฐํšŒ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋ฏธ๋ฆฌ ๋ณด์—ฌ์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ถ€์— ์ €์žฅํ•˜๋Š” ํŒจํ„ด
      • ๋”๋ณด๊ธฐ๋ฅผ ๋ˆ„๋ฅด๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋“ค์„ ๋ฏธ๋ฆฌ ์ž„๋ฒ ๋“œ ์‹œํ‚ค๋Š” ํ˜•ํƒœ
    • update ์‹œ ์–‘์ชฝ ๋ชจ๋‘ ์ˆ˜์ •ํ•ด์•ผ ํ•จ

Mongoose

ORM vs ODM

ORM : Object Relational Mapping (๊ฐ์ฒด์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งคํ•‘) //์‹œํ€„๋ผ์ด์ฆˆ, typeorm

ODM : Object Document Mapping (๊ฐ์ฒด์™€ Document ๋งคํ•‘) //๋ชฝ๊ตฌ์Šค

๊ฒฌ๊ณ ํ•œ NodeJs Project โ‰๏ธ

3 Layer Architecture

controller <-> service <-> data access(Mongoose)
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ Controller์—์„œ ๋ถ„๋ฆฌ

์™œ ๋ถ„๋ฆฌ?

๋…๋ฆฝ์ ์ธ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ / ์œ ์ง€๋ณด์ˆ˜ / ํ™•์žฅ์„ฑ, ์œ ์—ฐ์„ฑ์„ ์œ„ํ•จ

์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌ?

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง -> Service Layer๋กœ
Serivce Layer๋Š” req, res ๊ฐ์ฒด๋ฅผ ๋ฐ›์ง€ ์•Š์Œ
req, res๋Š” Controller Layer์—์„œ

Contorller

  • Model ์ฒ˜๋ฆฌ ์ „ 1์ฐจ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ˆ˜ํ–‰ ํ›„ ๋‹ค์‹œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ Response

Service

  • Controller์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ๊ฐ€๊ณตํ•˜์—ฌ DB์— ์ ‘๊ทผ -> ๊ฒฐ๊ณผ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ๊ฐ€๊ณต
  • ์—ฌ๊ธฐ์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต ๊ณผ์ •์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง

DTO (Data Transfer Object)

  • ๊ณ„์ธต ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด
  • Typescript์—์„œ๋Š” Interface๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-23 แ„‹แ…ฉแ„’แ…ฎ 3 24 12

@hyejungg hyejungg self-assigned this Apr 26, 2022
@hyejungg
Copy link
Member Author

๋””ํŽœ๋˜์‹œ์—์„œ @types ๊ฐ€ ๋ถ™์–ด์•ผ typescript ์‚ฌ์šฉ ๊ฐ€๋Šฅ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant