'NoSQL Schema - Calendar App with Appointments
I have an app that allows users to track appointments. There is a calendar component that displays appointments for a day, similar to the native calendar app in iOS. I'm trying to figure out the best way to store appointment data in firestore so it can be displayed on the calendar.
Current Schema
Each user has a single userAppointments document that holds all the necessary data to display the appointment on the calendar for every appointment. If the user has 10 appointments or 1000 appointments, they're all on the same document.
When the user clicks on the appointment, a new call is made to pull all the details for that appointment.
The advantage of this schema is there's only one query necessary to get all the calendar data. The downside is that the document could eventually become too big if the user keeps adding appointments.
Below is the current schema:
//collections
userAppointments: {
//document
userId1: {
appointmentId1: {
title: "Hair cut at Barber",
start: 5/17/2022 2:00 PM,
end: 5/17/2022 3:00 PM
},
appointmentId2: {
title: "Change tires",
start: 5/22/2022 1:00 PM,
end: 5/22/2022 1:30 PM
}
...
}
...
}
//collections
appointments: {
//documents
appointmentId1: {
title: "Hair cut at Barber",
start: 5/17/2022 2:00 PM,
end: 5/17/2022 3:00 PM,
description: ...,
location:...,
...
//more appointment fields
}
...
}
Alternative
Create a calendarAppointments collection with a document for each individual appointment and request only the appointments for that day. This still seems like it would be making a lot of calls to firestore and my documents would be tiny.
//collections
userId1: {
//collections
calendarAppointments : {
//documents
appointmentId1: {
title: "Hair cut at Barber",
start: 5/17/2022 2:00 PM,
end: 5/17/2022 3:00 PM
},
appointmentId2: {
title: "Change tires",
start: 5/22/2022 1:00 PM,
end: 5/22/2022 1:30 PM
},
...
}
}
//collections
appointments: {
//documents
appointmentId1: {
title: "Hair cut at Barber",
start: 5/17/2022 2:00 PM,
end: 5/17/2022 3:00 PM,
description: ...,
location:...,
...
//more appointment fields
}
...
}
Is there some kind of happy medium between the two? I considered having one document that holds all the appointments for a single day or a single week. However, with timezones that's tricky because depending on the timezone, an appointment could appear on a different day (if the user travels this becomes an issue).
Edit - Resolved Since my calendar has a weekly view, I'm going to have a separate document for each week that stores all the appointments for that week according to UTC. Each document will have a timestamp field in UTC. My app will query the database for documents between the start and end of the week. Unless the user is in the GMT time zone, the request will always retrieve two documents for a given week and I will combine the data from both of these documents to build my weekly events.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
