Slide 1

Slide 1 text

࣮ફ Cloud Functions for Firebase Firebase.yebisu #2

Slide 2

Slide 2 text

ࣗݾ঺հ • ੕઒݈հ • @star__hoshi • Cookpad Inc. • Komerco ࣄۀ෦ • iOS ΤϯδχΞ • ࠷ۙ͸ TypeScript

Slide 3

Slide 3 text

Virtual Youtuber ϥΠϒίʔσΟϯάͯ͠·͢ https:/ /www.youtube.com/channel/UCxkUH6lnXfpC35h0ApDz_yA

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Cloud Functions ͍͢͝؆୯ʹ͍͏ͱ • αʔόαΠυॲཧ͕Ͱ͖Δ΍ͭ • ྫ͑͹ Firestore ͳͲͷॻ͖ࠐΈΠϕϯτΛτϦΨʔͯ͠ॲཧ • ͍͍Ͷ͞Ε͔ͨΒ Push ௨஌ૹΔͳͲ • JavaScript (TypeScript) Ͱॻ͘ • AWS Lambda Έ͍ͨͳ΋Μ

Slide 6

Slide 6 text

https:/ /speakerdeck.com/1amageek/dong-kichu-sitakutukupatudofalsectocbizinesu

Slide 7

Slide 7 text

Cloud Firestore ɹɹɹ+ Cloud Functions

Slide 8

Slide 8 text

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() } }

Slide 9

Slide 9 text

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() } }

Slide 10

Slide 10 text

Komerco ͸ Cloud Functions Ͱ ΍Δ͜ͱ͕ଟ͍

Slide 11

Slide 11 text

Cloud Functions • Algolia ࿈ܞ (ݕࡧ) • EC • ࡏݿ؅ཧ • ܾࡁ (ΫϨΧܾࡁͳͲ) • ૹۚ (ग़඼ऀͷۜߦޱ࠲ʹૹΔ) • ... • ݱ࣌఺Ͱ 15 ͜

Slide 12

Slide 12 text

ࠔΓͲ͜ΖͱରԠࡦ • ϥΠϒϥϦͷґଘؔ܎ • Cloud Functions ͷτϦΨʔ • Πϕϯτ ΤϥʔϋϯυϦϯά • ଟॏىಈ • σϓϩΠ͕஗͍

Slide 13

Slide 13 text

ϥΠϒϥϦͷґଘؔ܎ 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 ͔΋ }

Slide 14

Slide 14 text

Cloud Functions ͷτϦΨʔ • HTTP • https:/ /us-central1-myapp.cloudfunctions.net/user?id=1 • ී௨ʹ curl Ͱୟ͚Δɺศར • Event • .document('user/{userID}').onCreate(async event => { ... }) • σʔλͷ write ΍ delete Πϕϯτ͕τϦΨʔ

Slide 15

Slide 15 text

HTTP ͸ศར͕ͩ • HTTP ͸ެ։͞ΕΔɺୟ͖์୊ • Cookie ͳͲͰೝূ͞ΕΔΘ͚Ͱ͸ͳ͍ • Firestore ͷΠϕϯτߋ৽ͷํ͕ηΩϡΞ • Authorized HTTPS Endpoint ΋͋Δ͕ • https:/ /github.com/firebase/functions-samples/tree/master/ authorized-https-endpoint

Slide 16

Slide 16 text

Πϕϯτ ΤϥʔϋϯυϦϯά

Slide 17

Slide 17 text

Τϥʔ͕ى͖ͨΒʁ

Slide 18

Slide 18 text

ࣦഊཧ༝Λอଘ͢Δ

Slide 19

Slide 19 text

݁ہ΍Γ͍ͨ͜ͱ͸ HTTP 1. Request ౤͛Δ 2. αʔόαΠυॲཧ͢Δ 3. Response ؼͬͯ͘Δ 4. ΫϥΠΞϯτଆͰϋϯυϦϯά͢Δ • OK(200) • BadReqeust(400) • Internal(500)

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Τϥʔ͸ Reject Λ ฦͦ͏ • GCP ͷ Stackdriver ͰΤϥʔ؂ࢹͰ͖ Δ • Promise.reject() Λฦͨ͠΍ͭ or throw Error ͨ͠΍ͭه࿥ • Τϥʔϝʔϧૹ৴ͱ͔΋Ͱ͖Δ

Slide 22

Slide 22 text

ѲΓͭͿͪ͠Όμϝ 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 })

Slide 23

Slide 23 text

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 ͠ͳ͍΋͋Γ

Slide 24

Slide 24 text

ଟॏىಈ https:/ /cloud.google.com/functions/docs/concepts/events-triggers#triggers • τϦΨʔΠϕϯτ͸كʹଟॏىಈ͠·͢ • ଟॏܾࡁ... • ࡏݿ਺͕ڰ͏...

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

ରࡦ - 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 }) } }) })

Slide 27

Slide 27 text

mission-completed ଟॏىಈ๷ࢭ͢ΔॲཧΛϥΠϒϥϦʹͨ͠ https:/ /github.com/starhoshi/mission-completed

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

σϓϩΠ͕஗͍ͷ͸ Ͳ͏͠Α͏΋ͳ͍ • deploy ճ਺ΛݮΒ͢ • ૄ݁߹ʹͯ͠Ͱ͖Δͱ͜Ζ͔ΒϩʔΧϧͰςετ • ϞοΫͯ͠ςετ • Cloud Functions ͷ୯ମςετ • https:/ /firebase.google.com/docs/functions/unit-testing

Slide 32

Slide 32 text

ଞͷϥΠϒϥϦ • 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 ...

Slide 33

Slide 33 text

ଞͷϥΠϒϥϦ • https:/ /github.com/starhoshi/Shana • ςετϔϧύʔ • σʔλͷมߋΛ observe ͠ଓ͚Δ • https:/ /github.com/starhoshi/orderable.ts • ܾࡁॲཧΛߦ͏ Cloud Functions

Slide 34

Slide 34 text

͓ΘΓ ·ͩ·ͩϊ΢ϋ΢͕ཷ·ͬͯͳ͍ͷͰ ΈΜͳͰ͕Μ͹͍͖ͬͯ·͠ΐ͏