I am preparing a Firebase application. This application will be relational data. Author information, book information and sales information you want to save separately. Record structures will be following.
authors
books
sales
How do I create a data structure to achieve the required information by using the child values.
For example, the last book selling easily for the show.
Thank you in advance for your help, I wish you a good day.
Firebase has no tables or schema (as in SQL) and is not relational so you need to structure your data in a way to get the data you want to retrieve.
Note all node Keys (starts with Y) are created with childByAutoId.
authors
-Y89j809ja
author: "Heinlein"
-Yi9jasas0
author: "Clarke"
-Yojoos0sd
author: "Bradbury"
books
-Ykkao90sj
book_name: "Starship Troopers"
author_id: "-Y89j809ja"
-Yom9sjwppp
book_name: "2001: A Space Odyssey"
author_id: "-Ji9jasas0"
-Yowk90dj9s
book_name: "The Martian Chronicles"
author_id: "-Yojoos0sd"
sales
-Yijajisijd
sales_timestamp: "20160916130510"
book_id: "-Ykkao90sj"
-Yioj900wwo
sales_timestamp: "20160917084722"
book_id: "-Yojoos0sd"
With this structure, you can query the sales node for the last 3 sales (queryLimitedToLast by sales_timestamp). That will return the data needed to retrieve the book by it's book_id and in turn author by the author_id.
There are 100 other ways to structure the data but this will be the most 'relational'
It will require one query to get the sales from the sales node (.Value) and then iterate over those results to query for the book and author. You can nest those queries so they don't go against the asynchronous nature of Firebase.
Another option is to flatten your data and just keep each transaction stored with all of the needed data.
sales
-Yijajisijd
sales_timestamp: "20160916130510"
book_name: "Starship Troopers"
author: "Heinlein"
price: "$19.95"
-Yikiasomn
sales_timestamp: "20160917084722"
book_name: "2001: A Space Odyssey"
author: "Clarke"
price: "$14.95"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments