question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommended way to sort and getLatest of children

See original GitHub issue

Hi! 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Bessonovcommented, Oct 16, 2019

Activity!

0reactions
vdlindenmarkcommented, Oct 23, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found