Slide 1

Slide 1 text

Firestore Database Design Session 2 #DevFest Norikazu Muramoto @1amageek Firebase Japan User Group Organizer

Slide 2

Slide 2 text

Norikazu Muramoto @1amageek Cookpad

Slide 3

Slide 3 text

System Device Technology Ltd, 2017.5 2015.10 2009.4 2018.6 Timers Inc Cookpad Inc Freelance 2018.9 Stamp Inc LSI design engineer iOS developer Business development, Development Manager CEO Bio

Slide 4

Slide 4 text

Firebase Japan User Group

Slide 5

Slide 5 text

What is Cloud Firestore?

Slide 6

Slide 6 text

Cloud Firestore

Slide 7

Slide 7 text

Cloud Firestore • Flexibility • Expressive querying • Realtime updates • Offline support • Designed to scale SubCollection is an important feature of Cloud Firestore.

Slide 8

Slide 8 text

What is SubCollection?

Slide 9

Slide 9 text

Cloud Firestore Data Model • Collection • Document • Data

Slide 10

Slide 10 text

Cloud Firestore SubCollection A Document can have a SubCollection.

Slide 11

Slide 11 text

How do we design products using Firestore?

Slide 12

Slide 12 text

Relationship Design Important!

Slide 13

Slide 13 text

How to make a relationship in Cloud Firestore? • Key • Reference • Query • SubCollection • Same ID We need to understand Client Side Join to understand the NoSQL relationship. There are several ways to create a relationship in Cloud Firestore.

Slide 14

Slide 14 text

What is Client Side Join?

Slide 15

Slide 15 text

Server Side Join Server Data Data Data JSON In RDB, join on the server side.

Slide 16

Slide 16 text

Client Side Join Cloud Firestore Data Client Side Join loads necessary data each time. Snapshot Data

Slide 17

Slide 17 text

Types of Relationship

Slide 18

Slide 18 text

Relationship by Key ID: xxx child: yyy ID: yyy parent: xxx Key Relationship is used for cross-referencing. However, since Document only knows the ID of the reference destination, the developer must manage the path. /parent/xxx /child/yyy

Slide 19

Slide 19 text

Relationship by Reference ID: xxx child: yyy ID: yyy parent: xxx Reference Relationship is used for cross-referencing. It holds the reference path. /parent/xxx /child/yyy /child/yyy /parent/xxx

Slide 20

Slide 20 text

Relationship by Query, SubCollection • Reference The snapshot acquired by the query does not contain data. • Value It contains data in the snapshot acquired by the query. There are two kinds of relationship by Query, SubCollection.

Slide 21

Slide 21 text

Relationship by Query ID: 0 parent: yyy ID: 1 parent: xxx ID: 2 parent: xxx ID: 3 parent: yyy /child/ Here, the relationship between parent and child is associated by ID. firestore .collection(“child”) .where(equalTo: “xxx”)

Slide 22

Slide 22 text

Relationship by Query ID: yyy data: { } /junction/ ID: 0 parent: yyy child: xxx ID: xxx data: { } /parent/ /child/ This expresses relationships through junction collections.

Slide 23

Slide 23 text

ID: xxx data: { } ID: yyy data: { } /parent/ /parent/child/ Relationship by SubCollection The SubCollection itself represents a relationship.

Slide 24

Slide 24 text

ID: xxx data: { } ID: yyy data: { } ID: yyy /parent/ /child/ /parent/child/ Reference Relationship by SubCollection Create a relationship by having the reference ID in SubCollection.

Slide 25

Slide 25 text

ID: xxx data: { } ID: xxx data: { } /parent/:same_id /child/:same_id Relationship by Same ID By using the same ID for another pass, we express the relationship between the two documents.

Slide 26

Slide 26 text

How to use it?

Slide 27

Slide 27 text

Security Rules To understand how to use, we need to know about security rules.

Slide 28

Slide 28 text

Security Rules • It is not possible to limit individual fields. It is necessary to separate documents between secure information and non-secure information. • When reading limits are applied, the query cannot be used. It is necessary to use SubCollection to create a secure collection. I will explain only the necessary points here. It takes time to explain security rules in detail.

Slide 29

Slide 29 text

Key Key is good for representing relations with a single object.

Slide 30

Slide 30 text

Reference Reference is suitable for a relationship having the same ID and having a different path.

Slide 31

Slide 31 text

Same ID It has a different structure with the same ID. Same ID is suitable for separating public information from private information.

Slide 32

Slide 32 text

Field Query It is the same as `where` Query of RDB. Field Query is suitable for expressing relationships of published information.

Slide 33

Slide 33 text

SubCollection SubCollection is suitable for expressing relationships of information that we want to maintain security.

Slide 34

Slide 34 text

Junction Collection Junction Collection is suitable for the relationship when there are two information roles.

Slide 35

Slide 35 text

Reference Collection The Reference Collection is suitable for the relationship between the followee and follower.

Slide 36

Slide 36 text

NoSQL has some techniques that you should not forget. Duplicate

Slide 37

Slide 37 text

What is Duplicate?

Slide 38

Slide 38 text

Duplicated Data ID: yyy data: { } /parent/item/ ID: 0 parent: yyy child: xxx ID: xxx data: { } /parent/ /child/ Writes exactly the same data to the two references.Writes exactly the same data to the two references. By doing this, JOIN becomes unnecessary, and data can be accessed at high speed. /child/item/ ID: 0 parent: yyy child: xxx

Slide 39

Slide 39 text

Duplicated Collection Duplicated Collection is suitable for relationships that are read frequently, but are less frequent to update.

Slide 40

Slide 40 text

1 : 1 1 : N N : N Key Reference Field Query SubCollection ( Nested Collection ) Junction Collection Reference Collection Same ID Cloud Firestore Relationships Duplicated Collection

Slide 41

Slide 41 text

Thank you.