Recommended way to sort and getLatest of children
See original GitHub issueHi! I have one question. I’m building a small chat app. My database has users and messages (relates to each other). The app has a Contactlist which shows all the users and Chatscreens with the messages.
I want to sort my contactlist by the last message for each contact. So the contact with the newest message will be the first one. The model of the contact looks like this now:
import { Model } from "@nozbe/watermelondb";
import { field, children, lazy } from "@nozbe/watermelondb/decorators";
import { Q } from "@nozbe/watermelondb";
import { action } from "@nozbe/watermelondb/decorators";
export default class Contact extends Model {
static table = "contacts";
static associations = {
messages: { type: "has_many", foreignKey: "contact_id" }
};
@field("name") name;
@field("image") image;
@children("messages") messages;
@lazy unreadMessages = this.messages.extend(Q.where("is_read", false));
@action async markMessagesAsRead() {
const unreadMessages = await this.unreadMessages.fetch();
await unreadMessages.map(
async message => await this.subAction(() => message.markAsRead())
);
}
}
```.
I'm wondering how could I sort the messages by a specific key (send date) and get the latest of it.
So I only can call contact.lastMessage or something like that. What is the recommended way to sort the messages in the model and get the latest?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How To Organize Your Life (With Kids!) In 12 Simple Steps
Want the secrets to staying organized as a busy mom? These 11 actionable steps will help you declutter, organize and destress your home...
Read more >10 Tips to Help Get Your Child Organized - Understood.org
5. Introduce idea organizers. Show kids how to use outlines, graphic organizers, or concept webs to organize ideas for school projects.
Read more >13 Simple Sorting Activities for Preschoolers
Sorting is an important cognitive skill. Use these sorting activities for preschoolers to teach the concepts of classifying and grouping.
Read more >Secrets of organized families: Insider strategies for getting ...
Get useful tips for organizing your household room by room from real families and professional organizers.
Read more >10 Ways to Help Your Kid Get Organized for the New School ...
Use dividers to separate class notes, or color-code notebooks. Separate "to do" and "done" folders help organize worksheets, notices and items to be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Activity!
I think it’s okey to close this issue. Just watch https://github.com/Nozbe/WatermelonDB/pull/199 and wait till it’s fixed.