0

We are looking for a mechanism that allows users to purchase digital content using Angular 10.

I'm thinking about how to create 1:N relationship with reference type using Angular 10 and Cloud Firestore by referring to the following in digitalcontents.service.ts, but by nesting in subcollection or by reference I didn't know how to add or get to the users collection by referencing the root collection (digitalContents).

DB Structure

-root-

users<collection>
 uid<document>
  name:string<field>
  digitalContents:map[{
   digitalId:reference
   purchaceDate:timeStamp
  }]

digitalContents<collection>
  digitalId<document>
    name:string<field>
    price:number

Is the DB design good for how to add or get to the above users collection, or how should I do this? I would appreciate it if you could teach me. You can DI the AngularFireStore and get the collection.

We apologize for the inconvenience, but we would appreciate it if you could teach us.

1 Answers1

1

One of the best/worst things related with the use of NoSQL databases, more specifically with Firestore, is that there aren't any written rules of a right/wrong way to structure your data since the correct answer depends on your specific implementation. The closest thing to a rule is a recommendation to avoid multiple reads on different collections as you're being charged for each read/write. That said for your specific case I think that you would like to follow a process called denormalization:

Database denormalization is the process of optimizing your database for reads by creating redundant data

The structure I would recommend to follow would be something like:

users (collection)
 |-- userid (document)
       |-- name
       |-- purchases (subcollection)
             |-- purchaseid (document)
                   |-- digitalID (reference)
                   |-- digitalName
                   |-- price
                   |-- purchasedDate

As you can see I'm repeating the information from the digitalCollections inside each user, there are two reasons behind this structure:

  1. (The most obvious one) I'm pretty sure that you want to save the price of the item purchased, this is due to some changes on the price that would be inconsistent with the price of the item purchased when the user did it or even if the item was purchased with a discount, which wouldn't be consistent with the catalog price.

  2. By saving all the purchases on each user you can simply query the purchases subcollection and get all of them without going into a different collection:

var purchases_by_user1 = db.collection('users/userid').collection('purchases');

Or query by specific purchase:

var purchase_x_by_user1 = db.collection('users/userid').collection('purchases').doc('purchaseid');

There's also an excellent video that explains some of the premises of how to structure your data, I hope this makes it clear and helps you to choose the correct design for your use case.

Emmanuel
  • 1,436
  • 1
  • 11
  • 17
  • Thank you! It was so easy to understand! I also understood the video. Is it possible to add a className or another element to the one I bought when displaying a list of all characters? – project sixth Aug 30 '20 at 04:20
  • I'm sorry I don't understand the question, what element do you want to add? – Emmanuel Aug 31 '20 at 15:02