I am creating a CRM that has 3 record types that a user can create - Accounts, Jobs, and Contacts. (Collections). In this example, let’s assume the account document contains references to 2 job documents and one contact document reference. Both job documents contain references to the account document, while one of the job documents contains a reference to a contact document. Lastly, the contact document contains a reference to a job document and a reference to the account document.
accountDoc = {
...data,
jobs: [ Ref(Collection("jobs"), "job-1"), Ref(Collection("jobs"), "job-2") ],
contacts: [ Ref(Collection("contacts"), "contact") ]
};
// job 1
jobsDoc = {
...data,
account: Ref(Collection("accounts"), "account"),
contacts: [ Ref(Collection("contacts"), "contact") ]
};
// job 2
jobsDoc = {
...data,
account: Ref(Collection("accounts"), "account"),
contacts: []
};
contactDoc = {
...data,
account: Ref(Collection("accounts"), "account"),
jobs: [ Ref(Collection("jobs"), "job-1") ]
};
When a user navigates to any of those 3 records (Account, Job, Contact), they can create a note associated with that record. Let’s assume the user creates a note on the job-1 record, account record, and contact record.
noteDoc = {
...data,
related: Ref(Collection("accounts"), "account")
};
noteDoc = {
...data,
related: Ref(Collection("jobs"), "job-1")
};
noteDoc = {
...data,
related: Ref(Collection("contacts"), "contact")
};
Since the account document, both job documents, and the contact document are all related to each other, I would like to query all note documents that are related to those records and sorted by most recent. To take it a step further, I would like to be able to paginate the note documents sorted by the most recent note documents. For example, I’d like to query 2 of the most recent note documents that are related to those records. Is this possible? Is there a better way to go about this? Please and thank you!