$30 off During Our Annual Pro Sale. View Details »

Nest アプリケーションの基盤となる仕組みを見直す

lightnet328
September 30, 2022

Nest アプリケーションの基盤となる仕組みを見直す

lightnet328

September 30, 2022
Tweet

More Decks by lightnet328

Other Decks in Programming

Transcript

  1. Nest ΞϓϦέʔγϣϯͷ
 ج൫ͱͳΔ࢓૊ΈΛݟ௚͢ Yutaro Kido @lightnet328 NestJS meetup Online #4

    2022೥9݄30೔
  2. ࣗݾ঺հ 2 • Yutaro Kido @lightnet328 • גࣜձࣾ dinii Tech

    Lead ◦ ػೳ։ൃΛ࣠ʹސ٬ఏڙՁ஋Λ࠷େԽ͢ΔऔΓ૊ΈΛ͍ͯ͠·͢ • TypeScript, React, GraphQL ͕޷͖ ◦ ܕɾؔ਺ʹΑΔએݴతͳ։ൃͱ޿େͳΤίγεςϜ͕޷͖ • େ༿΍ക͕ೖ͍ͬͯΔ৯΂෺͕޷͖ ◦ ֶੜ࣌୅͸͓Ζ͠ϙϯਣ͕৐ͬͨ৯΂෺͕޷͖Ͱͨ͠
  3. 3 ҿ৯ళͷ All in One Platform “μΠχʔ”

  4. 4 toB SaaS ͡Όͳͯ͘ɺ “͢΂ͯͷফඅऀ” ͕࢖͏ toC ΤϯλϝαʔϏε

  5. 5 ফඅऀΛΦϯϥΠϯԽ͠ɺΦϑϥΠϯʹ຾͍ͬͯͨσʔλΛूΊɺ toB ͱ toC ͷ૒ํʹ͓͍ͯ׆༻͢Δ͜ͱͰ ҿ৯ͷʮจ໌ʯͱʮจԽʯΛͭ͘ΔϓϥοτϑΥʔϜ

  6. dinii ͷٕज़ελοΫ 6 • ଟ਺ͷϓϩμΫτΛগਓ਺Ͱߴ଎͔ͭ҆ఆͯ͠։ൃ͚ͭͮ͠ΔͨΊͷٕज़બఆ • ݴޠͷεΠονϯάίετΛݮΒͤΔͨΊɺ࣮૷ޮ཰͕ྑ͘ͳΔ • εΩϧΞοϓ͕શͯͷϓϩμΫτͷ։ൃͷੜ࢈ੑ޲্ʹͭͳ͕Δ •

    ϑϩϯτΤϯυͱόοΫΤϯυͷ྆ํΛ୲౰͠΍͍ͨ͢Ίɺػೳ։ൃʹΦʔφʔγοϓΛ࣋ͪ΍͍͢ Ϩδ ΩΦεΫ ΩονϯσΟεϓϨΠ μογϡϘʔυ ϋϯσΟ ϞόΠϧΦʔμʔ ϓϦϯλ࿈ܞ ࣗಈ௼મػ࿈ܞ ϓϦϯλ࿈ܞ LINE Mini App όοΫΤϯυ
  7. dinii ͷγεςϜΞʔΩςΫνϟ 7 Dashboard Hasura
 Cloud Run Handy Cash
 Register

    Mobile
 Order NestJS
 Cloud Run Cloud
 SQL Kitchen
 Display • Cloud Run ʹΑΔΦʔτεέʔϦϯά • Hasura Ͱ PostgreSQL εΩʔϚͷ GraphQL εΩʔϚม׵ • Hasura ͱ NestJS ͷ GraphQL εΩʔϚͷ Schema Stiching
  8. NestJS ͱ dinii • NestJS ͸ dinii ʹ REST API

    ͕͋ͬͨࠒ͔Β࢖͍ଓ͚͍ͯΔ • NestJS Λ࢖͍͚ͭͮͯ REST API ͔Β GraphQL ʹҠߦͨ͠ • ΞϓϦέʔγϣϯͷॻ͖ຯΛେ͖͘ม͑Δ͜ͱແ͘ஈ֊తʹҠߦͰ͖ͨ ◦ NestJS ͸ॊೈੑͱ֦ுੑʹ༏Ε͍ͯΔ • طଘͷ Module ΍ Interceptor ͷ࢓૊ΈΛ࢖͏͚ͩͰΞϓϦέʔγϣϯతʹे෼ ◦ ҟͳΔ࢓૊Έಉ࢜ͷ૊Έ߹Θͤʹ͍ͭͯߟྀ͠ͳͯ͘ྑ͍ ◦ ΞϓϦέʔγϣϯͷ࣮૷ʹूதͰ͖ͯ࠷ߴ • dinii ͷϓϩμΫτશମ͕ Node.js ͷΤίγεςϜʹࢧ͑ΒΕ͍ͯΔ ◦ 4 ݄ΑΓ open collective ܦ༝Ͱ NestJS ΁ͷຖ݄ͷد෇Λଓ͚͍ͯ·͢ ◦ https://opencollective.com/nest 8
  9. ΞϓϦέʔγϣϯͷ࣮૷͚ͩʹूத͍ͯͯ͠Α͍ͷ͔ʁ 9

  10. ΞϓϦέʔγϣϯͷ࣮૷͚ͩʹूத͍ͯͯ͠Α͍ͷ͔ʁ 10 → Α͘ͳ͍͔΋

  11. ςʔϚ 11 • Nest ΞϓϦέʔγϣϯͷج൫ͱͳΔ࢓૊ΈΛݟ௚͢ ◦ ΞϓϦέʔγϣϯҎ֎ͷ෦෼ͬͯͲ͏΍ͬͯಈ͍͍ͯΔΜ͚ͩͬʁͱ͍͏࿩Ͱ͢ ◦ ͳͷͰ TypeORM

    ͷ࿩΋ Cloud Run ͷ࿩΋͠·͢ • ΞδΣϯμ ◦ NestJS ͷϥΠϑαΠΫϧ ◦ TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ
  12. NestJS ͷϥΠϑαΠΫϧͷ֓ཁ 12 • લఏ ◦ Nest ͷΞϓϦέʔγϣϯͷߏ଄͸ Module ʹΑͬͯ੔ཧ͞ΕΔ

    ◦ Module ͸ΞϓϦέʔγϣϯͷϥΠϑαΠΫϧϑοΫΛ࣋ͯΔ • onModuleInit ϝιου ◦ ϗετϞδϡʔϧͷґଘؔ܎͕ղܾ͞Εͨޙʹݺͼग़͞ΕΔ • onModuleDestroy ϝιου ◦ ϓϩηεͷऴྃγάφϧ*Λड͚औͬͨޙʹݺͼग़͞ΕΔ ◦ * SIGTERM, SIGINT ͳͲͷγάφϧ • beforeApplicationShutdown ◦ શͯͷ onModuleDestroy ͕׬ྃͨ͠ޙʹݺͼग़͞ΕΔ
  13. NestJS ͷϥΠϑαΠΫϧͷϢʔεέʔε 13 • onModuleInit ϝιου ◦ ϛυϧ΢ΣΞͱͷίωΫγϣϯཱ֬ ▪ σʔλϕʔεɺϝοηʔδϯάΩϡʔɺݕࡧΤϯδϯ…

    • onModuleDestroy ϝιου ◦ ϛυϧ΢ΣΞͱͷίωΫγϣϯ੾அ
  14. NestJS ͷϥΠϑαΠΫϧͷ஫ҙ఺ 14 • onModuleInit ϝιου ◦ ϗετϞδϡʔϧͷґଘؔ܎͕ղܾ͞Εͨޙʹݺͼग़͞ΕΔ ◦ INestApplication#init

    Λݺͼग़͞ͳ͍ͱݺͼग़͞Εͳ͍ • onModuleDestroy ϝιου ◦ ϓϩηεͷऴྃγάφϧ*Λड͚औͬͨޙʹݺͼग़͞ΕΔ ◦ * SIGTERM, SIGINT ͳͲͷγάφϧ ◦ INestApplication#enableShutdownHooks Λݺͼग़͓ͯ͘͠ඞཁ͕͋Δ ▪ γεςϜϦιʔεͷফඅ(EventEmitter ͷϦεφʔ૿Ճ)Λආ͚ΔͨΊʹσϑΥϧτ Ͱ͸ແޮʹͳ͍ͬͯΔ • beforeApplicationShutdown ◦ શͯͷ onModuleDestroy ͕׬ྃͨ͠ޙʹݺͼग़͞ΕΔ
  15. খωλ: NestJS ͷ໊લͷ༝དྷ 15 Modules A module is a class

    annotated with a @Module() decorator.
 The @Module() decorator provides metadata that Nest makes use of to organize the application structure.
 ग़య: Modules | NestJS
 https://docs.nestjs.com/modules ͜Ε·Ͱਂ͘ߟ͍͑ͯͳ͔ͬͨΜͰ͕͢ɺΞϓϦέʔγϣϯ͕Ϟδϡʔϧ͕ωετ͢Δߏ੒͔ͩΒ NestJS ͷΑ͏Ͱ͢ɻ
  16. ίωΫγϣϯͷΦϓγϣϯͷܕ෇͚ • TypeORM ͸ PostgreSQL Λ࢖༻͢Δ৔߹ɺίωΫγϣϯϓʔϧͷ؅ཧ͸ pg.Pool ʹ೚͍ͤͯΔ • σϑΥϧτͷϓʔϧαΠζ͸

    10 ͕ͩɺ͜Ε͸ pg.Pool ͷσϑΥϧτ஋͕ 10 ͳͨΊ • ίωΫγϣϯϓʔϧͷઃఆ͸ any ܕͷ DataSourceOptions[‘extra’] ʹద౰ͳΦϒδΣΫτΛ౉͢ඞཁ͕͋Δ ◦ ࣮ࡍʹ͸ @types/pg Ͱݴ͏ PoolConfig Λड͚औΕΔͷͰࣗ෼ͰܕΛ෇͚ͯ͋͛Δͱྑͦ͞͏ TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ 16
  17. TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ 17 ΞϓϦέʔγϣϯऴྃ࣌ͷίωΫγϣϯ։์ • TypeORMModule ͸ onApplicationShutdown ͰίωΫγϣϯΛด͡ΔΑ͏ʹͳ͍ͬͯΔ ◦

    TypeORMModule ͳͷͰ͜Ε͸ @nestjs/typeorm ͷ࿩ ◦ ͨͩ͠ɺݹ͍όʔδϣϯͩͱ keepConnectionAlive ͕༗ޮͳ৔߹ʹด͡ΒΕͳ͍ ▪ @nestjs/typeorm@8.1.0 Ͱ TypeORM v0.3 Ҏ্Λαϙʔτͨ͠ͱ͖ ʹมΘ͍ͬͯΔ ▪ ΞϓϦέʔγϣϯͷγϟοτμ΢ϯ࣌ʹίωΫγϣϯ͕੾அ͞Εͳ͍ةݥੑ͕͋ΔͷͰ஫ҙ
  18. TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ 18 ίωΫγϣϯϓʔϧͷαΠζઃܭ • PostgreSQL ࢖༻࣌ͷ TypeORM ͷσϑΥϧτͷίωΫγϣϯϓʔϧͷαΠζ͸ 10

    • Cloud Run ͸ 1 ͭͷΠϯελϯεʹ͖ͭɺCloud SQL ͷίωΫγϣϯΛ࠷େͰ΋ 100 ݸ·Ͱ͔࣋ͯ͠ͳ͍ • Cloud Run ͸ 1 ͭͷΠϯελϯε͕ಉ࣌ʹॲཧͰ͖Δ࠷େͷϦΫΤετ਺Λಉ࣮࣌ߦ਺ͱͯ͠ઃఆͰ͖Δ • ίωΫγϣϯ਺͕ϓʔϧαΠζͱಉ͡਺ʹୡ͢ΔͱɺΫΤϦ͕ऴྃ͢Δ·Ͱ࣍ͷΫΤϦ͸଴ػ͞ΕΔ ◦ ͭ·ΓɺϦΫΤετ͕٧·ΔΑ͏ʹͳΔ
  19. TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ 19 ίωΫγϣϯϓʔϧͷαΠζઃܭ

  20. TypeORM ͷίωΫγϣϯϓʔϧͷ؅ཧ 20 ίωΫγϣϯϓʔϧͷαΠζઃܭ • PostgreSQL ࢖༻࣌ͷ TypeORM ͷσϑΥϧτͷίωΫγϣϯϓʔϧͷαΠζ͸ 10

    • Cloud Run ͸ 1 ͭͷΠϯελϯεʹ͖ͭɺCloud SQL ͷίωΫγϣϯΛ࠷େͰ΋ 100 ݸ·Ͱ͔࣋ͯ͠ͳ͍ • Cloud Run ͸ 1 ͭͷΠϯελϯε͕ಉ࣌ʹॲཧͰ͖Δ࠷େͷϦΫΤετ਺Λಉ࣮࣌ߦ਺ͱͯ͠ઃఆͰ͖Δ • ίωΫγϣϯ਺͕ϓʔϧαΠζͱಉ͡਺ʹୡ͢ΔͱɺΫΤϦ͕ऴྃ͢Δ·Ͱ࣍ͷΫΤϦ͸଴ػ͞ΕΔ ◦ ͭ·ΓɺϦΫΤετ͕٧·ΔΑ͏ʹͳΔ • ϓʔϧαΠζ͸ಉ࣮࣌ߦ਺Ҏ্͔ͭ 100 ҎԼʹอͭඞཁ͕͋Δ ◦ 1 ϦΫΤετͰෳ਺ίωΫγϣϯফඅ͢Δ৔߹͕͋ΔͷͰϓʔϧαΠζ͸ಉ࣮࣌ߦ਺Λ্ճΓ͍ͨ • ಉ࣮࣌ߦ਺͔ϓʔϧαΠζΛௐ੔ͯ͠ Cloud SQL ͷίωΫγϣϯ਺Λ 100 ҎԼʹอͭඞཁ͕͋Δ
  21. ࠓ͙͢Ͱ͖Δ͜ͱ • جຊతͳ͜ͱͰ͸͋Γ·͕͢…… • Nest ΞϓϦέʔγϣϯͷϥΠϑαΠΫϧͷϩάΛ࢒͓ͯ͘͠ ◦ ࠔͬͨͱ͖ͷௐࠪʹ໾ཱͭ͸ͣ • ϦϦʔεϊʔτΛ஫ҙਂ͘ಡΉ

    • ΞϓϦέʔγϣϯҎԼͷϨΠϠʔʹΑΓؔ৺Λ࣋ͭ 21
  22. ·ͱΊ 22 • NestJS ͷॊೈੑͱ֦ுੑͷ͓͔͛ͰΞϓϦέʔγϣϯ։ൃʹूத͠΍͍͢ ◦ ͍ͭ΋͋Γ͕ͱ͏ • ҰํͰϑϨʔϜϫʔΫ͕Ӆṭͯ͘͠Ε͍ͯΔΞϓϦέʔγϣϯͷҎԼͷϨΠϠʔʹରͯ͠ೝ஌ಧ͖ʹ͍͘ ◦

    NestJS ͕ྑ͍ɾѱ͍ͱ͍͏࿩Ͱ͸ͳ͘Ұൠ࿦ͩͱࢥ͍ͬͯ·͢ • ΞϓϦέʔγϣϯҎԼͷϨΠϠʔʹΑΓؔ৺Λ࣋ͭ or εϖγϟϦετʹϨϏϡʔͯ͠΋Β͏ ◦ ࠓճͷൃද͕ͦͷ͖͔͚ͬʹͳͬͨͷͰ͋Ε͹޾͍Ͱ͢