Observable Data Service implementation with 3 levels dependencies

Multi tool use
Observable Data Service implementation with 3 levels dependencies
In my current project I am using the Observable Data Services following the implementation of theses tutorials:
How to build Angular apps using Observable Data Services - Pitfalls to avoid and Angular Observable Data Services.
Everything was ok until the change of my data models.
Now I have 3 levels of dependencies between my models :
The Thread model
export class Thread {
_id: string;
category: Category;
type: string;
title: string;
messages: Message;
The Message model
export class Message {
_id: string;
author: User;
text: string;
comments: Comment;
The Comment model
export class Comment {
_id: string;
author: User;
text: string;
Until now, I implemented the Observable Data Service by maintaining a data store of the parent model (Thread in this case) :
export class ThreadsStoreService {
private _threads: BehaviorSubject<Thread>;
public dataStore: {
threads: Thread
};
constructor(private threadsBackendService: ThreadsBackendService) {
this.dataStore = {
threads:
};
this._threads = new BehaviorSubject();
}
get threads() {
return this._threads.asObservable();
}
And modifying this data store during the change of the children states like this :
Message in this case : Add a new message to the thread
addThreadMessage(threadId: string, body: any) {
this.messagesBackendService.addThreadMessage(threadId, body)
.subscribe(
res => {
const newMessage = new Message({
text: body.text
});
this.threadsStoreService.dataStore.threads.forEach((item, index) => {
if (item._id === threadId) {
this.threadsStoreService.dataStore.threads[index].messages.push(newMessage);
this._messages.next(
Object.assign({}, this.threadsStoreService.dataStore.threads[index]).messages
);
}
});
},
err => console.log('Error creating the message to the thread')
);
}
Now I need to handle the Comment
models but If I follow this approach, I will work with "array of array of array" updating an array which is in the Thread data store and I feel it's not right.
Comment
Maybe there is a better solution with the implementation of Observable Data Services?
Thanks in advance for your answers.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.