I was having some problem when trying to design for my firebase database structure. Basically, 1 account can have many receipts, 1 receipts can have many items. Here is the JSON:
receipts {
accountID1 : {
receiptID1 : {
date : "07/07/2017"
store : {
storeName : "store1"
storeAddr : "addr1"
}
currency : {
currencyName : "currency1"
currentcySymbol : "$"
}
totalAmount : "50.00"
items : {
itemID1 : true,
itemID2 : true,
}
}
receiptID2 : {
date : "08/07/2017"
store : {
storeName : "store1"
storeAddr : "addr1"
}
currency : {
currencyName : "currency1"
currentcySymbol : "$"
}
totalAmount : "20.00"
items : {
itemID3 : true,
itemID4 : true,
}
}
}
},
items {
itemID1 : {
type : "food"
name : "snack"
unitprice : "10.00"
quantity : "2"
}
itemID2 : {
type : "entertainment"
name : "gaming equipment"
unitprice : "150.00"
quantity : "1"
}
itemID3 : {
type : "food"
name : "fruit juice"
unitprice : "4.00"
quantity : "1"
}
itemID4 : {
type : "entertainment"
name : "gaming equipment"
unitprice : "150.00"
quantity : "1"
}
},
itemIDsByType {
food : {
itemID1 : true,
itemID3 : true,
}
entertainment: {
itemID2 : true,
itemID4 : true,
}
}
I realized there is a duplication problem under the items child. For instance, account A purchase item A in receipt 1. Then, account A purchase the same item again in receipt 2. Under the receipts child, yes that will not cause any interference.
However, by looking at items child, specifically for itemID2 and itemID4, they are same item but belonged to different receipt. These two records are duplicated, and let's say for large set of data, I think this design might cause a problem.
Any ideas on how to restructure the database design in order to remove the duplication problem mentioned above?
I have actually come out with another design but it is less-flatten:
receipts {
accountID1 : {
receiptID1 : {
date : "07/07/2017"
merchantName : "NTUC"
branch : {
branchName : "Marsiling"
branchAddress : "Blk 167, Marsiling"
}
currency : {
currencyName : "currency1"
currencySymbol : "$"
}
totalAmount : "50.00"
}
receiptID2 : {
date : "08/07/2017"
merchantName : "NTUC"
branch : {
branchName : "Marsiling"
branchAddress : "Blk 167, Marsiling"
}
currency : {
currencyName : "currency1"
currencySymbol : "$"
}
totalAmount : "20.00"
}
}
},
itemLists {
receiptID1 : {
items : {
itemID1 : {
type : "food"
name : "snack"
unitprice : "10.00"
quantity : "2"
}
itemID2 : {
type : "entertainment"
name : "gaming equipment"
unitprice : "150.00"
quantity : "1"
}
itemID3 : {
type : "food"
name : "fruit juice"
unitprice : "4.00"
quantity : "1"
}
}
}
receiptID2 : {
items : {
itemID4 : {
type : "entertainment"
name : "gaming equipment"
unitprice : "150.00"
quantity : "1"
}
}
}
},
itemIDsByType {
food : {
itemID1 : true,
itemID3 : true,
}
entertainment: {
itemID2 : true,
itemID4 : true,
}
},
merchants {
merchantID1 : {
merchantName : "NTUC"
branches : {
branchID1 : {
branchName : "Marsiling"
branchAddress : "Blk 167, Marsiling"
}
branchID2 : {
branchName : "Woodlands"
branchAddress : "Blk 161, Woodlands"
}
}
}
}
As for this design, each of the items are grouped under each receiptID so that could eliminate the duplication problem mentioned above. But I find it less-flatten, I am trying to juggle between flatten design with duplicate data or less-flatten design with no duplicated data. Which one would be better for a large set of data?