How do I create a data structure?

sustun

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

  • author_id
  • author_name

books

  • book_id
  • book_name
  • author_id

sales

  • sales_id
  • sales_date
  • price
  • author_id
  • book_id

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.

  • book_name
  • author_name
  • sales_date

Thank you in advance for your help, I wish you a good day.

Jay

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create a Linked List Data Structure in Java?

How do I create a dictionary structure from provided data recursively?

How do I create a collection like data structure in PHP?

How do I create a new structure in Screeps

How do I further optimize this Data Structure?

How do I get data on a hierarchical structure?

How do I deserialize this type of data structure

How do I subset a list with mixed data type and data structure?

How do I check the data structure of this imported data in React

How do you create this tree data structure one item at a time?

How do I create such a dynamic structure?HTML + PHP

Angular 2 How do I create a nested structure?

How do I create a structure for a dynamic SQL query?

How do I create a "spacer" in a C++ class memory structure?

Java: How do I create this JSON Array Structure using GSON

How do I create an HTML table with the following structure?

How do I create a new instance of a Structure in F#?

How can I create data structure with list of key, value pair?

How can I create a hierarchy structure for a array/object data in nodejs?

How do I create columated data with VIM?

How do I change data structure for use with google line chart

How do i visualise this Data structure written in C++?

How do I extract data in JSON in a Pandas column and structure it by stacking it?

How do I transform a data frame to the desired structure and form?

How do I structure data common to all users in Cloud Firestore?

list of lists of data structure - how do i access with linq?

How do I translate this google sheet data structure into an object?

How do I preserve the data frame structure after using paste()?

How do I modify a data structure and return it in OCaml?