I have the following models for a chat application using redux-orm
. Each Conversation
contains many Messages
, but one message can only belong to a single Conversation
:
export class Message extends Model {
static modelName = 'Message';
static fields = {
id: attr(),
text: attr(),
created: attr(),
from: fk('User'),
conversation: fk('Conversation', 'messages')
};
}
export class Conversation extends Model {
static modelName = 'Conversation';
static fields = {
id: attr(),
created: attr(),
};
}
I'm using the following selector to get a list of conversations with their respective messages.
export const getConversations = createSelector(
getOrm,
createOrmSelector(orm, session => {
return session.Conversation
.all()
.toModelArray()
})
);
The problem? The messages
property of each Conversation
instance is a QuerySet
, not an Array
, which makes it difficult to deal with when passing ti components.
Here's the solutions I've tried:
Mapping the
messages
property of everyConversation
model returned to an array ofMessages
withmessages.all().toModelArray()
. This gave me the errorCan't mutate a reverse many-to-one relation
, even when I tried cloning the object.Creating an entirely new plain old JavaScript object and copying all the properties over, then setting the correct value for
messages
. This worked, but creating all these new objects seems like a huge performance hog on an application with frequent state changes.
How should I achieve my goal here?