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

Admin site as data

Admin site as data

Lisp meetup #40の発表資料です。

OHTA Shogo

May 30, 2016
Tweet

More Decks by OHTA Shogo

Other Decks in Programming

Transcript

  1. "ENJOTJUFBTEBUB
    Lisp meetup #40

    2016/05/30

    @athos0220

    View full-size slide

  2. ؅ཧαΠτͱ͸

    View full-size slide

  3. ؅ཧαΠτͱ͸
    ‣ 8FCΞϓϦͷ؅ཧऀ޲͚ʹ%#΁ͷجຊతͳ$36%
    ૢ࡞ͷΠϯλʔϑΣʔεΛఏڙ͢Δ
    ‣ ʮ؅ཧը໘ʯʮ؅ཧϖʔδʯͳͲͱ΋
    ‣ %KBOHPͳͲͷ8FCϑϨʔϜϫʔΫ͸Ϟσϧఆ͔ٛ
    Βࣗಈతʹੜ੒Ͱ͖Δ͜ͱ͕ಛ௃

    View full-size slide

  4. యܕతͳߏ੒
    ‣ %KBOHP΍3BJMT౳ɺଟ͘ͷ؅ཧ
    αΠτ͸03.ͷଘࡏΛԾఆ
    ‣ 03.͕3%#΁ͷ$36%ૢ࡞Λ
    ఏڙ͢Δ
    ‣ ͞Βʹ03.ͷϞσϧఆ͔ٛΒ
    ฤूϑΥʔϜΛࣗಈੜ੒͢Δ
    Admin site
    ORM
    RDBMS

    View full-size slide

  5. 5IF$MPKVSF8BZ

    View full-size slide

  6. γϯϓϧ͞
    ‣ $MPKVSFͷجຊతཧ೦ͷͻͱͭ
    ‣ ΋ͭΕͷͳ͍͜ͱ 㱻DPNQMFDU

    ‣ ͭҎ্ͷ΋ͷΛབྷΈ߹ΘͤΔͱෳࡶ
    ͕͞ੜ͡Δ
    ‣ γϯϓϧ͞͸෭࣍తʹಘΒΕΔ΋ͷ
    Ͱͳ͘ɺੵۃతʹબͿ΋ͷ

    View full-size slide

  7. σʔλࢦ޲
    ‣ $MPKVSFͰ͸૊ࠐΈσʔλܕΛଟ༻͢Δ
    ‣ σʔλ͸ੜདྷγϯϓϧͰ͋Γ૊Έ߹ΘͤՄೳ
    ‣ σʔλʼؔ਺ʼϚΫϩͷॱͰ૊Έ߹ΘͤՄೳੑ͕ߴ·Δ
    ‣ ҆қʹϚΫϩͰ%4-Λ࡞ΔͷͰ͸ͳ͘ɺσʔλ΍ؔ਺ͱ
    ͯ͠දݱͰ͖ͳ͍͔Λߟ͑Δ

    View full-size slide

  8. ϑϨʔϜϫʔΫΑΓϥΠϒϥϦ
    ‣ ϞϊϦγοΫͳϑϨʔϜϫʔΫ͸ϑϨʔϜϫʔΫࣗମͷݶ
    քʹୡͨ͠ͱ͖ͷରॲ͕೉͍͠
    ‣ ୯ػೳͷϥΠϒϥϦΛ૊Έ߹Θͤͯ࢖͏ํ͕ࣗ༝౓͕ߴ͍
    ‣ ͜ͷߟ͔͑Β$MPKVSFʹ͸ϑϨʔϜϫʔΫ ಛʹϑϧελοΫ
    ͳ8"'
    ͕΄ͱΜͲͳ͍

    View full-size slide

  9. ʮϑϨʔϜϫʔΫΑΓϥΠϒϥϦʯ
    ͷ೉͠͞
    ‣ ϥΠϒϥϦಉ࢜Λ૊Έ߹ΘͤΔͷ͸Ϣʔβͷ੹೚Ͱɺ
    ͏·͘ڞଘͤ͞ΒΕͳ͍૊Έ߹Θͤ΋͋Δ
    -FJOJOHFOςϯϓϨʔτͰయܕతͳߏ੒Λ࡞ͬͯ͘ΕΔ
    ΋ͷ΋͋Δ͕ςϯϓϨʔτࣗମͷอक͕೉
    ‣ ্ͷϨΠϠʔͷϥΠϒϥϦʹͳΔ΄ͲԼͷϨΠϠʔ
    ʹͲΜͳϥΠϒϥϦ͕དྷΔ͔Λ૝ఆ͖͠Εͳ͍

    View full-size slide

  10. $MPKVSFʹ͓͚Δ
    3%#ϥΠϒϥϦͷมભ

    View full-size slide

  11. $MPKVSFʹ͓͚Δ
    3%#ϥΠϒϥϦͷมભ
    ‣ 3%#पล͸$MPKVSFϥΠϒϥϦͷதͰ΋৘੎͕มԽ͠
    ΍͍͢෼໺ͷͻͱͭ
    ‣ େ͖ͳྲྀΕͱͯ͠ΑΓγϯϓϧͳํ޲ʹ޲͔͍ͬͯΔ
    ,PSNB
    )POFZ42-
    :FTRM)VH42-

    View full-size slide

  12. ,PSNB
    ςʔϒϧఆٛɺΫΤϦఆٛ౳ͷ%4-Λ΋ͭ
    (defentity address)
    (defentity user
    (has-one address))
    (select user
    (with address)
    (fields :firstName :lastName :address.state)
    (where {:email "[email protected]"}))

    View full-size slide

  13. )POFZ42-
    ‣ Ϛοϓ͔ΒΫΤϦΛ࡞ΔΫΤϦϏϧμʔ
    ‣ ςʔϒϧఆٛͷखஈ͸͍࣋ͬͯͳ͍
    (-> (select :*)
    (from :foo)
    (where [:= :f.a "baz"]))
    ;=> {:from [:foo], :where [:= :f.a "baz"], :select (:*)}
    (-> (select :*)
    (from :foo)
    (where [:= :f.a "baz"])
    sql/format)
    ;=> ["SELECT * FROM foo WHERE f.a = ?" "baz"]

    View full-size slide

  14. :FTRM)VH42-
    ໊લ౳ΛΞϊςʔτ͞Εͨ42-͔Βؔ਺Λಈతʹੜ੒͢Δ
    -- name: users-by-country
    SELECT *
    FROM users
    WHERE country_code = :country_code
    (defqueries "users_by_country.sql"
    {:connection db-spec})
    (users-by-country {:country_code "GB"})
    users_by_country.sql
    users_by_country.clj

    View full-size slide

  15. 42-ϑΝʔετΞϓϩʔν
    ‣ 42-͸ͦΕࣗ਎͕%4-
    ‣ %4-ͷ্ʹ%4-Λߏங͢Δͷ͸ҙຯ͕͋Δͷ͔ʁ
    ‣ 42-Λ௚઀ॻ͘͜ͱͰ3%#͝ͱʹదͨ͠ΫΤϦΛ
    هड़Ͱ͖Δ

    View full-size slide

  16. ͦͯ͠୭΋஌Βͳ͘ͳͬͨ
    ‣ 42-ϑΝʔετΞϓϩʔνΛͱΔ͜ͱͰɺ$MPKVSF
    ϨϕϧͰ͸ςʔϒϧఆ͚ٛͩͰͳ͘ΫΤϦʹ͍ͭͯ
    ΋୭΋஌Βͳ͘ͳΔ
    ‣ 42-ϑΝʔετΞϓϩʔνΛͱΔϥΠϒϥϦ͕͋Δ
    ͜ͱΛ૝ఆ্ͨ͠Ͱ؅ཧαΠτͷΑ͏ͳػೳΛఏڙ
    ͢ΔϥΠϒϥϦΛߏஙͰ͖Δͷ͔ʁ

    View full-size slide

  17. BENJOLJU
    "ENJOTJUFBTEBUB

    View full-size slide

  18. BENJOLJU
    ‣ 3JOHϋϯυϥͱͯ͠࡞ΒΕͨ؅ཧαΠτϥΠϒϥϦ
    ‣ ґଘੑͷٯసʹΑͬͯ%#ʹඇґଘ
    ‣ $MPKVSFʹΑΔ"1*αʔόͱ$MPKVSF4DSJQUʹΑΔ
    41"ͷߏ੒

    View full-size slide

  19. ґଘੑٯసͷݪଇ
    ্ҐϞδϡʔϧ͸ԼҐϞδϡʔϧʹґଘ͢΂͖Ͱ͸ͳ͍ɻ
    ͲͪΒͷϞδϡʔϧ΋ந৅ʹґଘ͢΂͖Ͱ͋Δɻ
    https://en.wikipedia.org/wiki/Dependency_inversion_principle ΑΓ຋༁
    high level
    module
    low level
    module
    high level
    module
    abstraction
    low level
    module
    ௚઀ͷґଘ͕ͳ͘ͳΓ
    ૄ݁߹ʹͳΔ

    View full-size slide

  20. ґଘੑͷٯస
    Ξμϓλʔͱը໘ఆٛ QBHFTQFD
    ͱ͍͏ந৅Λ௥Ճ
    Admin site
    ORM
    RDBMS
    Admin site
    DB wrapper
    RDBMS
    adapter/spec
    యܕతͳ؅ཧαΠτͷߏ੒ BENJOLJUͷߏ੒

    View full-size slide

  21. ߏ੒ཁૉ
    ‣ Ξμϓλʔ
    $36%ૢ࡞Λ3%#ϥΠϒϥϦ΁ҕৡ͢Δ
    ‣ ը໘ఆٛ QBHFTQFD

    ฤूϑΥʔϜΛϨΠΞ΢τ͠ɺ஋ΛͲ͏දࣔ͢Δ͔Λ
    نఆ͢Δ
    εΩʔϚఆٛͰ͸ͳ͋͘͘·Ͱσʔλͷݟͨ໨Λنఆ

    View full-size slide

  22. Ξμϓλʔ :FTRMΛ࢖͏ྫ

    -- name: all-products
    SELECT *
    FROM products
    (require '[admin-kit.adapter :as adapter])
    (defqueries "products.sql"
    {:connection db-spec})
    (adapter/make-adapter {:create …
    :read all-products
    :update …
    :delete …})
    products.sql
    products_adapter.clj

    View full-size slide

  23. ը໘ఆٛ QBHFTQFD

    (def products-spec
    {:title "঎඼"
    :fields [{:name :_id
    :label "ID"
    :format #(format "%03d" %)
    :detail? true}
    {:name :name
    :label "঎඼໊"
    :type :text}
    {:name :furigana
    :label "ϑϦΨφ"
    :type :text
    :sortable? true}

    ]})
    Ұཡը໘
    ฤूը໘

    View full-size slide

  24. ࢖͍ํ
    ͨͩͷ3JOHϋϯυϥͳͷͰɺ3JOHϛυϧ΢ΣΞ౳ͱ
    ૊Έ߹Θͤͯ࢖͏͜ͱ͕Ͱ͖Δ
    (require '[admin-kit.handler :as handler])
    (def site-spec
    {:products
    {:spec products-spec, :adapter products-adapter} …})
    (def app
    (handler/make-admin-site-handler site-spec))
    (jetty/run-jetty app {:port 8080})

    View full-size slide

  25. ར఺
    ‣ ଞͷ8FCΞϓϦ΁ͷ૊ࠐΈͷ༰қ͞
    ‣ σʔλιʔεඇґଘ
    3%#ʹݶΒͣɺ/P42-Ͱ΋ϑΝΠϧγεςϜͰ΋Ξμ
    ϓλ͑͞ॻ͚͹ରԠͰ͖Δ
    ‣ ΧελϚΠζੑͷߴ͞
    ը໘ఆٛΛࣗ෼Ͱॻͨ͘ΊΧελϚΠζ͕༰қ
    ϑΥʔϜͷछྨ΋ϚϧνϝιουΛॻ͘͜ͱͰ૿΍ͤΔ

    View full-size slide

  26. ૝ఆ͞ΕΔ໰୊఺
    ‣ ΞμϓλΛखͰॻ͘ͷ͕μϧ͍ʜ
    ϚΫϩΛॻ͜͏ɻϥΠϒϥϦϨϕϧΑΓ΋ΞϓϦϨϕ
    ϧͰͷந৅Խ͕΍Γ΍͍͢৔߹͸ଟ͍ɻ
    ‣ ը໘ఆٛΛखͰॻ͘ͷ͕μϧ͍ʜ
    ࣮༻తͳΞϓϦͷ৔߹ɺࣗಈੜ੒͞Εͨը໘Ͱ͸ෆे
    ෼ͳ͜ͱ͸ଟ͘ͲͷΈͪΧελϚΠζ͕ඞཁʹͳΔ
    ը໘ఆٛ͸σʔλͳͷͰɺεΩʔϚఆ͔ٛΒࣗಈੜ੒
    Λ͢ΔπʔϧΛ࡞Δ͜ͱ΋ෆՄೳͰ͸ͳ͍

    View full-size slide

  27. ࠓޙͷ՝୊
    ‣ ଟରଟؔ࿈ϑΟʔϧυରԠ
    ‣ ϑΟϧλػೳ௥Ճ
    ‣ ϑϩϯτΤϯυଆΛ3FBDUίϯϙʔωϯτʹ෼ׂ͠ɺ
    Ϣʔβ͕ࣗ༝ʹ૊Έ߹ΘͤΒΕΔΑ͏ʹ͢Δ
    ‣ υΩϡϝϯτ੔උ

    View full-size slide

  28. ૊Έ߹ΘͤՄೳͳ
    ϥΠϒϥϦΛ࡞Δख͕͔Γ ࢑ఆ

    View full-size slide

  29. ૊Έ߹ΘͤՄೳͳ
    ϥΠϒϥϦΛ࡞Δख͕͔Γ ࢑ఆ

    ‣ ͜Ε·ͰͷBENJOLJUͷ։ൃ͔Βಘͨڭ܇
    ‣ ݸʑʹ͸໨৽͍͠΋ͷ͸ͳ͍
    ػೳΛ๊͑ࠐ·ͳ͍
    طଘͷ૊Έ߹ΘͤՄೳͳ࢓૊Έʹ৐͔ͬΔ
    ػೳΛϞϊϦγοΫʹ࡞Γࠐ·ͳ͍
    ந৅ʹґଘ͢Δ

    View full-size slide

  30. ػೳΛ๊͑ࠐ·ͳ͍
    ‣ ػೳΛ࣋ͭͱଞͷϥΠϒϥϦͱڝ߹ͯ͠࢖͑ͳ͍Մ
    ೳੑ͕ߴ·Δ
    ‣ ଞͷϥΠϒϥϦʹ೚ͤΒΕΔػೳͰ͋Ε͹ɺ҆қʹ
    औΓࠐ·ͳ͍

    View full-size slide

  31. طଘͷ૊Έ߹ΘͤՄೳͳ
    ࢓૊Έʹ৐͔ͬΔ
    ‣ ଞͷϥΠϒϥϦͱ૊Έ߹ΘͤՄೳʹ͢ΔͯͬͱΓૣ͍
    ํ๏͸طଘͷ࢓૊Έʹ৐͔ͬΔ͜ͱ
    ‣ طଘͷ૊Έ߹ΘͤՄೳͳ࢓૊Έͷྫɿ
    3JOHͷϋϯυϥ ̋
    ɾϛυϧ΢ΣΞ ˕

    -FJOJOHFOͷλεΫ ˚
    ɾߴ֊λεΫ ̋

    View full-size slide

  32. ػೳΛϞϊϦγοΫʹ࡞Γࠐ·ͳ͍
    ‣ Ϣʔβ͕ϥΠϒϥϦͷఏڙ͢ΔػೳΛ෦෼తʹ͔͠
    ࢖Θͳ͍ɺ·ͨ͸Ұ෦ΛࣗલͰஔ͖׵͑ͯ࢖͏৔߹
    Λ૝ఆ
    ‣ ػೳ͕ΑΓϓϦϛςΟϒͳ෦෼ʹ෼ׂՄೳͳઃܭʹ
    ͳ͍ͬͯΕ͹Ϣʔβ͕࢖༻͢Δ෦෼Λબ୒Ͱ͖Δ

    View full-size slide

  33. ந৅ʹґଘ͢Δ
    ‣ ଞͷϥΠϒϥϦʹґଘ͢Δ৔߹͸Ұ୴ந৅ΛڬΜͰ
    ϥΠϒϥϦͱͷ݁߹Λૄʹอͭ
    ‣ ந৅͕ଟա͗Δͱ࢖͍ʹ͘͘ͳΔͷͰɺͲ͜ʹந৅
    Λஔ͔͘͸τϨʔυΦϑΛߟ͑Δඞཁ͕͋Δ

    View full-size slide

  34. ·ͱΊ
    ‣ ʮϑϨʔϜϫʔΫΑΓϥΠϒϥϦʯΛਪ͠ਐΊΔͱɺΑΓ
    ্ҐͷػೳΛϥΠϒϥϦͱͯ͠ఏڙ͢Δͷ͕೉͘͠ͳΔ
    ‣ BENJOLJUͰ؅ཧαΠτػೳΛϥΠϒϥϦͱͯ͠ఏڙ͢Δ
    ͜ͱΛ໛ࡧ
    ‣ ґଘੑٯసͷݪଇ౳Λ͏·͘࢖͏͜ͱͰଞͷϥΠϒϥϦ
    ͱ૊Έ߹Θ͕ͤ༰қͳϥΠϒϥϦΛ࡞Δ͜ͱ͕Մೳ

    View full-size slide

  35. ࢀߟ
    ‣ 4JNQMJDJUZ.BUUFST<:PV5VCF>
    ‣ $MPKVSFOFFETBXFCGSBNFXPSLXJUINPSF
    NPNFOUVN
    ‣ :FTRM42-JO42-JO$MPKVSF
    ‣ BENJOLJU<(JU)VC>

    View full-size slide