Save 37% off PRO during our Black Friday Sale! »

Realtime Database Design

F45851534f21f9469d296950653464be?s=47 1amageek
November 24, 2017

Realtime Database Design

A real-time database can be efficiently and functionally utilized by designing.

F45851534f21f9469d296950653464be?s=128

1amageek

November 24, 2017
Tweet

Transcript

  1. Firebase Realtime Database Design nori Nov 2017 @1amageek Hello. #firebase.yebisu

    How are you
  2. System Device Technology LSI design engineer iOS engineer iOS engineer

    2017.5 2015.10 2009.4 Bio About 1amageek
  3. Salada Firebase Model Framework Pring Firestore Model Framework Bleu Bluetooth

    Library https://qiita.com/1amageek OSS Library
  4. Why design is necessary?

  5. Because, We want to 1. Simple 2. Safe 3. Efficient

    4. Functional
  6. What is design in IT?

  7. Organize data Connect data and data together

  8. What is design for engineers?

  9. Easy to connect Making data and data easier to connect

  10. Why use Firebase? https://qiita.com/1amageek

  11. Design for Firebase Model Relationship Rule

  12. Model 1. Typesafe 2. Common understanding

  13. model id_0 property_0: “model” id_1 property_1: 0 property_0: “model” property_1:

    0 class Model { let id: String let property_0: String let property_1: Int } let model0: Model = Model() model.property_0 = "model" model.property_1 = 0 let model1: Model = Model() model.property_0 = "model" model.property_1 = 0
  14. Key points of model design ID let model: Model =

    Model() let model: Model = Model(id: “ID”)
  15. /// Initialize Object public override init() { self.createdAt = Date()

    self.updatedAt = Date() self._ref = type(of: self).databaseRef.childByAutoId() self.id = self._ref.key super.init() } /// Initialize Object from snapshot. convenience required public init?(snapshot: DataSnapshot) { self.init() _setSnapshot(snapshot) } /// Initialize the object with the specified ID. convenience required public init?(id: String) { self.init() self.id = id self._ref = type(of: self).databaseRef.child(id) }
  16. Relationship 1. Essential

  17. 1 : 1 1 : N N : N

  18. 1 : 1 1 : N N : N Just

    think about N : N
  19. 1 : 1

  20. class Company { let id: String let name: String let

    CEO: String } class People { let id: String let name: String let company: String } let tesla: Company = Company() tesla.name = "Tesla" let mask: People = People() mask.name = "Elon Musk" tesla.CEO = mask.id mask.company = tesla.id
  21. 1 : N

  22. class Company { let id: String let name: String let

    CEO: String } class People { let id: String let name: String let companies: [String] } let tesla: Company = Company() tesla.name = "Tesla" let mask: People = People() mask.name = "Elon Musk" tesla.CEO = mask.id mask.companies.append(tesla.id)
  23. class Company { let id: String let name: String let

    CEO: String } class People { let id: String let name: String let companies: [String] } let tesla: Company = Company() tesla.name = "Tesla" let mask: People = People() mask.name = "Elon Musk" tesla.CEO = mask.id mask.companies.append(tesla.id) Growth Property
  24. people id_0 name: “Elon Musk” companies id_1 id_2 ɾ ɾ

    id_ 0 becomes heavier
  25. people-company people_id company_id_0: true company_id_1: true Separate into separate nodes

    company company_id_0 people people_id
  26. N : N

  27. people-company people_id company_id_0: true company_id_1: true company company_id_0 people people_id

  28. Key points of relationship design N : N let userA:

    User = User() let userB: User = User() userA.follow(userB) userB.frined(userA)
  29. Security 1. Essential

  30. Use Firebase bolt

  31. /** User */ path /user { read() { isSignedIn() }

    index() { [ “name" ] } path /{id} is User { read() { isSignedIn() } write() { isUser(id) } path /property { read() { isSignedIn() } } } } type User { name: String } isSignedIn() { auth != null } isUser(uid) { isSignedIn() && auth.uid == uid }
  32. /** User */ path /user { read() { isSignedIn() }

    index() { [ “name" ] } path /{id} is User { read() { isSignedIn() } write() { isUser(id) } path /property { read() { isSignedIn() } } } } type User { name: String } isSignedIn() { auth != null } isUser(uid) { isSignedIn() && auth.uid == uid } Type Permissions
  33. Key points of security design Multiple permission management isSignedIn() {

    auth != null } isUser(uid) { isSignedIn() && auth.uid == uid } canRead() { data.val() == null || data.child('userID').val() === auth.uid } canWrite() { data.child('userID').val() == auth.uid || newData.child('userID').val() == auth.uid } createOnly(value) { prior(value) == null && value != null }
  34. Model is important for engineers

  35. Salada Firebase Model Framework Let's use Salada

  36. Sample code https://github.com/1amageek/SaladaSample

  37. Thank you %