Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Firestoreを実装してみた話
Search
keinuma
January 05, 2021
68
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Firestoreを実装してみた話
keinuma
January 05, 2021
Transcript
16 slides
Firestore を してみた 2021/1/5 沼⽥ 1 / 16
⽇ すこと Firestore とは クライアントによるFirestore の使い Firestore を 慮したクライアントの 2
/ 16
Firestore とは NoSQL データベース 他のNoSQL に べてリレーション、クエリが柔 Web, Mobile 3
/ 16
Firestore のデータ構 4 / 16
Firestore のデータ構 Collection がRDB のテーブル Document がレコード Collection をネストしたSubcollection 作れる
5 / 16
Firestore の使い 照 Collection と 件を指 して 索 更 を
知できる ・更 Document 単 で 6 / 16
照 import * as firebase from 'firebase/app' const firestore =
firebase.firestore() const collection = firestore.collection('messages') collection .where('uids', 'array-contains', 'xxxx-xxx-xxx') .get() .then((document) => { const data = document.data() console.log(data) }) 7 / 16
照( 更を 知) const firestore = firebase.firestore() const collection =
firestore.collection('messages') const unsubscribe = collection .where('uids', 'array-contains', 'xxxx-xxx-xxx') .onSnapshot() .then((snapshot) => { const data = snapshot.data() console.log(data) }) window.addEventListener('unload', unsubscribe) 8 / 16
const firestore = firebase.firestore() const collection = firestore.collection('messages') collection.doc('xxxx-xxx-xxx').set({ text:
'hogehoge' }) 9 / 16
Firestore からデータ構 を する Collection はFirestore のデータとアプリのデータを できる データの は双
向で 義する 10 / 16
const converter = { toFirestore(messageModel) { return { ...messageModel, updatedAt:
firestore.Timestamp.fromDate(messageModel.updatedAt) } }, fromFirestore(snapshot) { const messageData = snapshot.data() return new MessageModel({ ...messageData, updatedAt: messageData.updatedAt.toDate() }) } } const convertCollection = collection.withConverter(converter) 11 / 16
Firestore の Firestore に するメソッドをラップしてClass Converter はアプリのデータ構 とFirestore 知って いるため
けて 12 / 16
13 / 16
Firestore のメソッドをラップ class FirestoreRepository { private readonly collection constructor(path, converter)
{ this.collection = db.collection(path) .withConverter(converter) } subscribeById(id, callback) { return this.collection.doc(id).onSnapshot( (snapshot) => callback(snapshot.data()) ) } } 14 / 16
ドメイン別に を 義 const converter = { toFirestore(messageModel) { ...
}, fromFirestore(snapshot) { ... } } const MessagesRepository = new FirestoreRepository( 'messages', converter ) 15 / 16
まとめ Firestore はデータ のオプション(Converter) がある Firestore のメソッドをRepository してConverter を け
て すると責 を 離できる 16 / 16
More Decks by keinuma
See All by keinuma
品質と新規開発のバランス / Quality and new development
keinuma
0
2.5k
Flutter or React Native
keinuma
1
150
GoによるGraphQLの実装
keinuma
0
3.4k
Atomic Design手法
keinuma
0
110
なぜAtomic Designが生まれたか
keinuma
0
98
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
790
Building Adaptive Systems
keathley
44
3.3k
What's in a price? How to price your products and services
michaelherold
247
13k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Code Review Best Practice
trishagee
74
20k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
590
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
370
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
490
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
560
Git: the NoSQL Database
bkeepers
PRO
433
67k
Between Models and Reality
mayunak
4
610
Mind Mapping
helmedeiros
1
460