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

Firestore Database Design

August 31, 2018

Firestore Database Design

This paper will explain the relationship of Cloud Firestore.


August 31, 2018

More Decks by 1amageek

Other Decks in Technology


  1. Firestore Database Design Session 2 #DevFest Norikazu Muramoto @1amageek Firebase

    Japan User Group Organizer
  2. Norikazu Muramoto @1amageek Cookpad

  3. 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
  4. Firebase Japan User Group

  5. What is Cloud Firestore?

  6. Cloud Firestore

  7. Cloud Firestore • Flexibility • Expressive querying • Realtime updates

    • Offline support • Designed to scale SubCollection is an important feature of Cloud Firestore.
  8. What is SubCollection?

  9. Cloud Firestore Data Model • Collection • Document • Data

  10. Cloud Firestore SubCollection A Document can have a SubCollection.

  11. How do we design products using Firestore?

  12. Relationship Design Important!

  13. 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.
  14. What is Client Side Join?

  15. Server Side Join Server Data Data Data JSON In RDB,

    join on the server side.
  16. Client Side Join Cloud Firestore Data Client Side Join loads

    necessary data each time. Snapshot Data
  17. Types of Relationship

  18. 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
  19. 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
  20. 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.
  21. 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”)
  22. Relationship by Query ID: yyy data: { } /junction/ ID:

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

    /parent/ /parent/child/ Relationship by SubCollection The SubCollection itself represents a relationship.
  24. 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.
  25. 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.
  26. How to use it?

  27. Security Rules To understand how to use, we need to

    know about security rules.
  28. 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.
  29. Key Key is good for representing relations with a single

  30. Reference Reference is suitable for a relationship having the same

    ID and having a different path.
  31. Same ID It has a different structure with the same

    ID. Same ID is suitable for separating public information from private information.
  32. Field Query It is the same as `where` Query of

    RDB. Field Query is suitable for expressing relationships of published information.
  33. SubCollection SubCollection is suitable for expressing relationships of information that

    we want to maintain security.
  34. Junction Collection Junction Collection is suitable for the relationship when

    there are two information roles.
  35. Reference Collection The Reference Collection is suitable for the relationship

    between the followee and follower.
  36. NoSQL has some techniques that you should not forget. Duplicate

  37. What is Duplicate?

  38. 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
  39. Duplicated Collection Duplicated Collection is suitable for relationships that are

    read frequently, but are less frequent to update.
  40. 1 : 1 1 : N N : N Key

    Reference Field Query SubCollection ( Nested Collection ) Junction Collection Reference Collection Same ID Cloud Firestore Relationships Duplicated Collection
  41. Thank you.