Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

lightnet328
September 30, 2022

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

lightnet328

September 30, 2022
Tweet

More Decks by lightnet328

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ 2 • Yutaro Kido @lightnet328 • גࣜձࣾ dinii Tech

    Lead ◦ ػೳ։ൃΛ࣠ʹސ٬ఏڙՁ஋Λ࠷େԽ͢ΔऔΓ૊ΈΛ͍ͯ͠·͢ • TypeScript, React, GraphQL ͕޷͖ ◦ ܕɾؔ਺ʹΑΔએݴతͳ։ൃͱ޿େͳΤίγεςϜ͕޷͖ • େ༿΍ക͕ೖ͍ͬͯΔ৯΂෺͕޷͖ ◦ ֶੜ࣌୅͸͓Ζ͠ϙϯਣ͕৐ͬͨ৯΂෺͕޷͖Ͱͨ͠
  2. dinii ͷٕज़ελοΫ 6 • ଟ਺ͷϓϩμΫτΛগਓ਺Ͱߴ଎͔ͭ҆ఆͯ͠։ൃ͚ͭͮ͠ΔͨΊͷٕज़બఆ • ݴޠͷεΠονϯάίετΛݮΒͤΔͨΊɺ࣮૷ޮ཰͕ྑ͘ͳΔ • εΩϧΞοϓ͕શͯͷϓϩμΫτͷ։ൃͷੜ࢈ੑ޲্ʹͭͳ͕Δ •

    ϑϩϯτΤϯυͱόοΫΤϯυͷ྆ํΛ୲౰͠΍͍ͨ͢Ίɺػೳ։ൃʹΦʔφʔγοϓΛ࣋ͪ΍͍͢ Ϩδ ΩΦεΫ ΩονϯσΟεϓϨΠ μογϡϘʔυ ϋϯσΟ ϞόΠϧΦʔμʔ ϓϦϯλ࿈ܞ ࣗಈ௼મػ࿈ܞ ϓϦϯλ࿈ܞ LINE Mini App όοΫΤϯυ
  3. 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
  4. 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
  5. ςʔϚ 11 • Nest ΞϓϦέʔγϣϯͷج൫ͱͳΔ࢓૊ΈΛݟ௚͢ ◦ ΞϓϦέʔγϣϯҎ֎ͷ෦෼ͬͯͲ͏΍ͬͯಈ͍͍ͯΔΜ͚ͩͬʁͱ͍͏࿩Ͱ͢ ◦ ͳͷͰ TypeORM

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

    ◦ Module ͸ΞϓϦέʔγϣϯͷϥΠϑαΠΫϧϑοΫΛ࣋ͯΔ • onModuleInit ϝιου ◦ ϗετϞδϡʔϧͷґଘؔ܎͕ղܾ͞Εͨޙʹݺͼग़͞ΕΔ • onModuleDestroy ϝιου ◦ ϓϩηεͷऴྃγάφϧ*Λड͚औͬͨޙʹݺͼग़͞ΕΔ ◦ * SIGTERM, SIGINT ͳͲͷγάφϧ • beforeApplicationShutdown ◦ શͯͷ onModuleDestroy ͕׬ྃͨ͠ޙʹݺͼग़͞ΕΔ
  7. NestJS ͷϥΠϑαΠΫϧͷ஫ҙ఺ 14 • onModuleInit ϝιου ◦ ϗετϞδϡʔϧͷґଘؔ܎͕ղܾ͞Εͨޙʹݺͼग़͞ΕΔ ◦ INestApplication#init

    Λݺͼग़͞ͳ͍ͱݺͼग़͞Εͳ͍ • onModuleDestroy ϝιου ◦ ϓϩηεͷऴྃγάφϧ*Λड͚औͬͨޙʹݺͼग़͞ΕΔ ◦ * SIGTERM, SIGINT ͳͲͷγάφϧ ◦ INestApplication#enableShutdownHooks Λݺͼग़͓ͯ͘͠ඞཁ͕͋Δ ▪ γεςϜϦιʔεͷফඅ(EventEmitter ͷϦεφʔ૿Ճ)Λආ͚ΔͨΊʹσϑΥϧτ Ͱ͸ແޮʹͳ͍ͬͯΔ • beforeApplicationShutdown ◦ શͯͷ onModuleDestroy ͕׬ྃͨ͠ޙʹݺͼग़͞ΕΔ
  8. খωλ: 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 ͷΑ͏Ͱ͢ɻ
  9. ίωΫγϣϯͷΦϓγϣϯͷܕ෇͚ • TypeORM ͸ PostgreSQL Λ࢖༻͢Δ৔߹ɺίωΫγϣϯϓʔϧͷ؅ཧ͸ pg.Pool ʹ೚͍ͤͯΔ • σϑΥϧτͷϓʔϧαΠζ͸

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

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

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

    • Cloud Run ͸ 1 ͭͷΠϯελϯεʹ͖ͭɺCloud SQL ͷίωΫγϣϯΛ࠷େͰ΋ 100 ݸ·Ͱ͔࣋ͯ͠ͳ͍ • Cloud Run ͸ 1 ͭͷΠϯελϯε͕ಉ࣌ʹॲཧͰ͖Δ࠷େͷϦΫΤετ਺Λಉ࣮࣌ߦ਺ͱͯ͠ઃఆͰ͖Δ • ίωΫγϣϯ਺͕ϓʔϧαΠζͱಉ͡਺ʹୡ͢ΔͱɺΫΤϦ͕ऴྃ͢Δ·Ͱ࣍ͷΫΤϦ͸଴ػ͞ΕΔ ◦ ͭ·ΓɺϦΫΤετ͕٧·ΔΑ͏ʹͳΔ • ϓʔϧαΠζ͸ಉ࣮࣌ߦ਺Ҏ্͔ͭ 100 ҎԼʹอͭඞཁ͕͋Δ ◦ 1 ϦΫΤετͰෳ਺ίωΫγϣϯফඅ͢Δ৔߹͕͋ΔͷͰϓʔϧαΠζ͸ಉ࣮࣌ߦ਺Λ্ճΓ͍ͨ • ಉ࣮࣌ߦ਺͔ϓʔϧαΠζΛௐ੔ͯ͠ Cloud SQL ͷίωΫγϣϯ਺Λ 100 ҎԼʹอͭඞཁ͕͋Δ
  13. ·ͱΊ 22 • NestJS ͷॊೈੑͱ֦ுੑͷ͓͔͛ͰΞϓϦέʔγϣϯ։ൃʹूத͠΍͍͢ ◦ ͍ͭ΋͋Γ͕ͱ͏ • ҰํͰϑϨʔϜϫʔΫ͕Ӆṭͯ͘͠Ε͍ͯΔΞϓϦέʔγϣϯͷҎԼͷϨΠϠʔʹରͯ͠ೝ஌ಧ͖ʹ͍͘ ◦

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