Save 37% off PRO during our Black Friday Sale! »

実践 Cloud Functions for Firebase

71ce746e94f84be1cef8e57e8277bd6d?s=47 star__hoshi
February 20, 2018

実践 Cloud Functions for Firebase

* Firebase.yebisu #2 - connpass https://connpass.com/event/75511/
* star__hoshi - YouTube - YouTube https://www.youtube.com/channel/UCxkUH6lnXfpC35h0ApDz_yA
* 1amageek/Pring: Cloud Firestore model framework for iOS - Google https://github.com/1amageek/Pring
* 1amageek/pring.ts: Cloud Firestore model framework for TypeScript - Google https://github.com/1amageek/pring.ts
* starhoshi/event-response: Handle the results of Cloud Functions like http status code and response. https://github.com/starhoshi/event-response
* Events and Triggers  |  Cloud Functions Documentation  |  Google Cloud Platform https://cloud.google.com/functions/docs/concepts/events-triggers#triggers
* starhoshi/mission-completed: Stop processing even if Cloud Functions fires multiple times. https://github.com/starhoshi/mission-completed
* Cloud Functions for Firebase を TDD で開発する - Qiita https://qiita.com/star__hoshi/items/572193910542be1863af
* Cloud Functions の単体テスト  |  Firebase https://firebase.google.com/docs/functions/unit-testing
* starhoshi/retrycf: Fire Cloud Functions again. https://github.com/starhoshi/retrycf
* starhoshi/fire-slack: Easy to send a slack notification from Cloud Functions https://github.com/starhoshi/fire-slack
* Cloud Functions の Slack 通知に Cloud Firestore データへの直リンクを貼る - Qiita https://qiita.com/star__hoshi/items/2fd87553988f5591ea84
* starhoshi/Shana: Testing utilities for Cloud Functions https://github.com/starhoshi/Shana
* starhoshi/orderable.ts: An order processing framework works with Cloud Functions for Firebase https://github.com/starhoshi/orderable.ts

71ce746e94f84be1cef8e57e8277bd6d?s=128

star__hoshi

February 20, 2018
Tweet

Transcript

  1. ࣮ફ Cloud Functions for Firebase Firebase.yebisu #2

  2. ࣗݾ঺հ • ੕઒݈հ • @star__hoshi • Cookpad Inc. • Komerco

    ࣄۀ෦ • iOS ΤϯδχΞ • ࠷ۙ͸ TypeScript
  3. Virtual Youtuber ϥΠϒίʔσΟϯάͯ͠·͢ https:/ /www.youtube.com/channel/UCxkUH6lnXfpC35h0ApDz_yA

  4. ࣮ફ Cloud Functions for Firebase ɹ !ೖ໳ͷ࿩͸͠·ͤΜ

  5. Cloud Functions ͍͢͝؆୯ʹ͍͏ͱ • αʔόαΠυॲཧ͕Ͱ͖Δ΍ͭ • ྫ͑͹ Firestore ͳͲͷॻ͖ࠐΈΠϕϯτΛτϦΨʔͯ͠ॲཧ •

    ͍͍Ͷ͞Ε͔ͨΒ Push ௨஌ૹΔͳͲ • JavaScript (TypeScript) Ͱॻ͘ • AWS Lambda Έ͍ͨͳ΋Μ
  6. https:/ /speakerdeck.com/1amageek/dong-kichu-sitakutukupatudofalsectocbizinesu

  7. Cloud Firestore ɹɹɹ+ Cloud Functions

  8. iOS • Swift + Pring import Pring class User: Object

    { @objc dynamic var name: String? static func create(name: String) { let user = User() user.name = name user.save() } }
  9. Cloud Functions • TypeScript + pring.ts import { Pring, property

    } from 'pring' export class User extends Pring.Base { @property name: string static async create(name: string) { const user = new User() user.name = name await user.save() } }
  10. Komerco ͸ Cloud Functions Ͱ ΍Δ͜ͱ͕ଟ͍

  11. Cloud Functions • Algolia ࿈ܞ (ݕࡧ) • EC • ࡏݿ؅ཧ

    • ܾࡁ (ΫϨΧܾࡁͳͲ) • ૹۚ (ग़඼ऀͷۜߦޱ࠲ʹૹΔ) • ... • ݱ࣌఺Ͱ 15 ͜
  12. ࠔΓͲ͜ΖͱରԠࡦ • ϥΠϒϥϦͷґଘؔ܎ • Cloud Functions ͷτϦΨʔ • Πϕϯτ ΤϥʔϋϯυϦϯά

    • ଟॏىಈ • σϓϩΠ͕஗͍
  13. ϥΠϒϥϦͷґଘؔ܎ package.json ͷ dependencies ͸ݻఆͨ͠ํ͕ແ೉ • Cloud Functions ͸ node

    6, package-lock.json ͸ node 8 ͔Β • yarn.lock ͸౰વಡΈࠐ·Εͳ͍ • deploy ͨ͠Βґଘઌ͕ഁյతมߋͯͯ͠Τϥʔ... "dependencies": { "uuid": "3.0.1" // "^3.0.1" ͩͱ 3.1.0 ͔΋ }
  14. Cloud Functions ͷτϦΨʔ • HTTP • https:/ /us-central1-myapp.cloudfunctions.net/user?id=1 • ී௨ʹ

    curl Ͱୟ͚Δɺศར • Event • .document('user/{userID}').onCreate(async event => { ... }) • σʔλͷ write ΍ delete Πϕϯτ͕τϦΨʔ
  15. HTTP ͸ศར͕ͩ • HTTP ͸ެ։͞ΕΔɺୟ͖์୊ • Cookie ͳͲͰೝূ͞ΕΔΘ͚Ͱ͸ͳ͍ • Firestore

    ͷΠϕϯτߋ৽ͷํ͕ηΩϡΞ • Authorized HTTPS Endpoint ΋͋Δ͕ • https:/ /github.com/firebase/functions-samples/tree/master/ authorized-https-endpoint
  16. Πϕϯτ ΤϥʔϋϯυϦϯά

  17. Τϥʔ͕ى͖ͨΒʁ

  18. ࣦഊཧ༝Λอଘ͢Δ

  19. ݁ہ΍Γ͍ͨ͜ͱ͸ HTTP 1. Request ౤͛Δ 2. αʔόαΠυॲཧ͢Δ 3. Response ؼͬͯ͘Δ

    4. ΫϥΠΞϯτଆͰϋϯυϦϯά͢Δ • OK(200) • BadReqeust(400) • Internal(500)
  20. event-response • Event τϦΨʔͷ݁ՌΛอଘ͢Δ • HTTP ͷ Response ͷΑ͏ʹ݁Ռ͕໌ࣔత •

    OK, BadRequest, Internal Λه࿥͢Δ • ϥΠϒϥϦԽͨ͠ new EventResponse.Result(reference).setOK() new EventResponse.Result(reference).setBadRequest('error_id', 'error reason') new EventResponse.Result(reference).setInternalError('error_id', 'error reason') https:/ /github.com/starhoshi/event-response
  21. Τϥʔ͸ Reject Λ ฦͦ͏ • GCP ͷ Stackdriver ͰΤϥʔ؂ࢹͰ͖ Δ

    • Promise.reject() Λฦͨ͠΍ͭ or throw Error ͨ͠΍ͭه࿥ • Τϥʔϝʔϧૹ৴ͱ͔΋Ͱ͖Δ
  22. ѲΓͭͿͪ͠Όμϝ return Promise.reject() ͯ͠ͳ͍͔ΒΤϥʔ͕ه࿥͞Εͳ͍ exports.updateUser = functions.firestore.document('users/{userId}') .onCreate(async event =>

    { try { await event.data.ref.update({name: undefined}) } catch (error) { console.error(error) } return undefined })
  23. catch ͨ͠Β reject Λฦ͢ exports.updateUser = functions.firestore.document('users/{userId}') .onCreate(async event =>

    { try { await event.data.ref.update({name: undefined}) } catch (error) { console.error(error) return Promise.reject(error) } return undefined }) ͦ΋ͦ΋ try ͠ͳ͍Ͱ͍͍৔߹͸ try ͠ͳ͍΋͋Γ
  24. ଟॏىಈ https:/ /cloud.google.com/functions/docs/concepts/events-triggers#triggers • τϦΨʔΠϕϯτ͸كʹଟॏىಈ͠·͢ • ଟॏܾࡁ... • ࡏݿ਺͕ڰ͏...

  25. كͱ͸ • ։ൃதͰ2िʹ1ճ͘Β͍ૺ۰͢Δ • ମײ͕ͩ·͋·͋ى͜Δ

  26. ରࡦ - transaction • transaction Λ࢖ͬͯॲཧ։࢝ϑϥάΛཱͯΔ • transaction ͸ॻ͖ࠐΈॲཧΛฒྻʹ࣮ߦͰ͖Δ •

    ͢Ͱʹϑϥάཱ͕͍ͬͯͨΒ throw Error ͢Δ await firestore.runTransaction(async (transaction) => { return transaction.get(reference).then(snapshot => { if (snapshot.data().completed)) { throw new CompletedError(id) } else { transaction.update(reference, { completed: true }) } }) })
  27. mission-completed ଟॏىಈ๷ࢭ͢ΔॲཧΛϥΠϒϥϦʹͨ͠ https:/ /github.com/starhoshi/mission-completed

  28. σϓϩΠ஗͍ • ૣͯ͘30ඵ • ී௨ʹ10෼଴ͬͨΓ͢Δ! • package.json ߋ৽࣌͸࣮֬ʹ஗͍

  29. σϓϩΠ͠ͳ͍Ͱ։ൃ͢Δ https:/ /qiita.com/star__hoshi/items/572193910542be1863af

  30. rescue-fire test('update name', async () => { // ςετσʔλͷ४උ const

    data = {name: 'name'} const user = await admin.firestore().collection('user').add(data) const event = Rescue.event(user, data) // ΠϕϯτΛ࡞੒ // Cloud Functions ΛൃՐ await changeName(event) // Ξοϓσʔτ͞ΕͨϢʔβΛऔಘ͠ɺ name ͕มߋ͞Ε͍ͯΔ͜ͱΛ֬ೝ const updatedUser = await admin.firestore().collection('user').doc(user.id).get() expect(updatedUser.data()!.name).toBe('new name') }) https:/ /github.com/starhoshi/rescue-fire
  31. σϓϩΠ͕஗͍ͷ͸ Ͳ͏͠Α͏΋ͳ͍ • deploy ճ਺ΛݮΒ͢ • ૄ݁߹ʹͯ͠Ͱ͖Δͱ͜Ζ͔ΒϩʔΧϧͰςετ • ϞοΫͯ͠ςετ •

    Cloud Functions ͷ୯ମςετ • https:/ /firebase.google.com/docs/functions/unit-testing
  32. ଞͷϥΠϒϥϦ • https:/ /github.com/starhoshi/retrycf • Cloud Functions Λ Retry ͢Δ

    • https:/ /github.com/starhoshi/fire-slack • Cloud Functions ͔Β slack ௨஌ΛศརʹૹΕΔ • https:/ /qiita.com/star__hoshi/items/2fd87553988f5591ea84 • Cloud Functions ͷ Slack ௨஌ʹ Cloud Firestore ...
  33. ଞͷϥΠϒϥϦ • https:/ /github.com/starhoshi/Shana • ςετϔϧύʔ • σʔλͷมߋΛ observe ͠ଓ͚Δ

    • https:/ /github.com/starhoshi/orderable.ts • ܾࡁॲཧΛߦ͏ Cloud Functions
  34. ͓ΘΓ ·ͩ·ͩϊ΢ϋ΢͕ཷ·ͬͯͳ͍ͷͰ ΈΜͳͰ͕Μ͹͍͖ͬͯ·͠ΐ͏