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

Realtime Database Design

1amageek
November 24, 2017

Realtime Database Design

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

1amageek

November 24, 2017
Tweet

More Decks by 1amageek

Other Decks in Design

Transcript

  1. 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
  2. Key points of model design ID let model: Model =

    Model() let model: Model = Model(id: “ID”)
  3. /// 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) }
  4. 1 : 1 1 : N N : N Just

    think about N : N
  5. 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
  6. 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)
  7. 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
  8. Key points of relationship design N : N let userA:

    User = User() let userB: User = User() userA.follow(userB) userB.frined(userA)
  9. /** 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 }
  10. /** 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
  11. 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 }