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

実践 Cloud Functions for Firebase

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

star__hoshi

February 20, 2018
Tweet

More Decks by star__hoshi

Other Decks in Technology

Transcript

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

    ࣄۀ෦ • iOS ΤϯδχΞ • ࠷ۙ͸ TypeScript
  2. Cloud Functions ͍͢͝؆୯ʹ͍͏ͱ • αʔόαΠυॲཧ͕Ͱ͖Δ΍ͭ • ྫ͑͹ Firestore ͳͲͷॻ͖ࠐΈΠϕϯτΛτϦΨʔͯ͠ॲཧ •

    ͍͍Ͷ͞Ε͔ͨΒ Push ௨஌ૹΔͳͲ • JavaScript (TypeScript) Ͱॻ͘ • AWS Lambda Έ͍ͨͳ΋Μ
  3. 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() } }
  4. 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() } }
  5. Cloud Functions • Algolia ࿈ܞ (ݕࡧ) • EC • ࡏݿ؅ཧ

    • ܾࡁ (ΫϨΧܾࡁͳͲ) • ૹۚ (ग़඼ऀͷۜߦޱ࠲ʹૹΔ) • ... • ݱ࣌఺Ͱ 15 ͜
  6. ϥΠϒϥϦͷґଘؔ܎ 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 ͔΋ }
  7. Cloud Functions ͷτϦΨʔ • HTTP • https:/ /us-central1-myapp.cloudfunctions.net/user?id=1 • ී௨ʹ

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

    ͷΠϕϯτߋ৽ͷํ͕ηΩϡΞ • Authorized HTTPS Endpoint ΋͋Δ͕ • https:/ /github.com/firebase/functions-samples/tree/master/ authorized-https-endpoint
  9. ݁ہ΍Γ͍ͨ͜ͱ͸ HTTP 1. Request ౤͛Δ 2. αʔόαΠυॲཧ͢Δ 3. Response ؼͬͯ͘Δ

    4. ΫϥΠΞϯτଆͰϋϯυϦϯά͢Δ • OK(200) • BadReqeust(400) • Internal(500)
  10. 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
  11. Τϥʔ͸ Reject Λ ฦͦ͏ • GCP ͷ Stackdriver ͰΤϥʔ؂ࢹͰ͖ Δ

    • Promise.reject() Λฦͨ͠΍ͭ or throw Error ͨ͠΍ͭه࿥ • Τϥʔϝʔϧૹ৴ͱ͔΋Ͱ͖Δ
  12. ѲΓͭͿͪ͠Όμϝ 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 })
  13. 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 ͠ͳ͍΋͋Γ
  14. ରࡦ - 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 }) } }) })
  15. 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
  16. σϓϩΠ͕஗͍ͷ͸ Ͳ͏͠Α͏΋ͳ͍ • deploy ճ਺ΛݮΒ͢ • ૄ݁߹ʹͯ͠Ͱ͖Δͱ͜Ζ͔ΒϩʔΧϧͰςετ • ϞοΫͯ͠ςετ •

    Cloud Functions ͷ୯ମςετ • https:/ /firebase.google.com/docs/functions/unit-testing
  17. ଞͷϥΠϒϥϦ • 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 ...
  18. ଞͷϥΠϒϥϦ • https:/ /github.com/starhoshi/Shana • ςετϔϧύʔ • σʔλͷมߋΛ observe ͠ଓ͚Δ

    • https:/ /github.com/starhoshi/orderable.ts • ܾࡁॲཧΛߦ͏ Cloud Functions