简体   繁体   中英

Firebase Structure Database and Queries

I'm developing an employee time management app in swift, I would be happy to help you with the structure of the data and how to extract some queries. The user database is like this:

https://i.stack.imgur.com/hfSXM.jpg

{"Database":
  {"users"
   {"3iHTIn1MicMdPbgEV6nnMy5ijHq1": 
           { "company" : "My Company",
             "email" : "edel@gmail.com",
             "name" : "Tom",
             "type" : "Employee"
           }
   }
  }
 }

The user will clock in and out when start and finish job. Is better to open new child in the project for the dates and the hours or to add to the user child with the date? (please see the queries that I want to pull before answer) Help with the queries:

  1. Pull all the dates and the hours for this month for user.

  2. Pull all the user that have same company name and work now (the field clock in are full and the field clock out are empty).

  3. Pull list all the company names that have for all users and delete duplicate (is better to make another child in the project with only company's names?)

Thanks!!

A few thoughts. I would probably denormalize the data because you have several ways you want to query it. I'm using some pseudo code to keep the answer short(er)

Pull all the dates and the hours for this month for user.

the challenge here is querying by two child nodes; one of them being a certain user and the other being a range of dates. The solution is a compound query of uid and YYMM (year month)

here's the structure:

jobs
  job_0
    start: 170518090135
    end: 170518170515
    uid_yymm: uid_0_1705
    uid: uid_0
    company_id: company_0
    comp_status: company_0_true
  job_1
    start: 1705190835
    end: 17051910430
    uid_yymm: uid_0_1705
    uid: uid_1
    company_id: company_0
    comp_status: company_0_false

queryOrdered(byChild: "uid_yymm").queryEqual(to value: "uid_0_1705")

This will query all of the jobs for uid_0 with the year being 2017 and month being 05.

Pull all the user that have same company name and work now (the field clock in are full and the field clock out are empty).

Searching for no data can make things more complex with NoSQL so we'll just avoid that completely. Again, a compound query will do the job with the above structure on the comp_status node.

queryOrdered(byChild: "comp_status").queryEqual(to value: "company_0_true")

this query will return all of the users that work for company_0 that are (true) on the clock. when they go off the clock, as in uid_1's case, set to company_0_false.

Pull list all the company names that have for all users and delete duplicate (is better to make another child in the project with only company's names?)

This is a little vague but I think the answer is yes, have a separate company names node but keep a reference to the company in the users node.

Database
   users
      3iHTIn1MicMdPbgEV6nnMy5ijHq1 
         company: "company_0"
         email: "edel@gmail.com"
         name: "Tom"
         type: "Employee"
      6889js9i99s9ksij8asd88as8d8 
         company: "company_1"
         email: "bill@gmail.com"
         name: "bill"
         type: "Employee"

   companies
      company_0
        comp_name: "Electric Company"
      company_1
        comp_name: "Bad Company"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM