References: getting a user's todos #2131
-
I want to understand why MST handles references in a way that's different from what I expected (and, possibly, what's advertised), and is that a limitation, bug, or user error. In the docs, MST describes identifiers and references:
The docs stop short of showing something like a User having a one-to-many relationship with something like a Todo, so I put together a demo based on one of the examples to try to better understand that setup. Here are the important bits: const Todo = types.model("Todo", {
id: types.identifier,
title: types.string,
finished: false,
user: types.maybe(types.reference(types.late(() => User))),
}) const User = types.model("User", {
id: types.identifier,
name: "",
todos: types.array(
types.safeReference(
types.late(() => Todo),
{ acceptsUndefined: false }
)
),
}) const TodoStore = types.model("TodoStore", {
todos: types.array(Todo),
users: types.array(User),
}) My expectation was that if there's a list of users and a list of todos, when a todo that refers to a user (e.g. an assignee) is added to Puzzled, I looked for relevant discussions...#1953 maybe touches on this? I took one of the replies to mean that I'd have to update the user's todos reference when adding a todo item. In my demo, that's: const TodoStore = types
// ...
.actions((self) => ({
addTodo(title, _user) {
const id =
self.todos
.map((todo) => parseInt(todo.id, 10))
.sort()
.at(-1) + 1;
const todo = { id: `${id}`, title, user: _user };
self.todos.push(todo);
// Expected ☝️ this update to propagate to the user referenced in the Todo
// Instead, 👇 have to update the user's Todos manually
const user = self.users.find((u) => u.id === _user);
user.todos.push(id);
}, This feels like it should be a simple matter of state changes being observed in the usual MobX way, but that doesn't happen here, and I haven't yet made sense of why. Having to update references manually seems counter to what I understand about MobX and MST. What am I missing from the docs, examples, etc. that addresses how this (seemingly very common) use case would be accomplished with MST? Ultimately, I want to get this right for my project in the simplest, most maintainable way possible. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @mossymaker - thanks for the detailed question and example. I think the issue here is unclear documentation. It makes a lot of sense that you would expect the data to be updated automatically baed on the documentation you quoted. I'm sorry for the lack of clarity. As you found in the earlier discussion, it is actually on you to make sure your data stays normalized. The idea is that we "make it possible" to keep data normalized, but we aren't necessarily going to do it on your behalf. I'm not one of the original authors, so I can't speak to the actual intention behind that decision, but I think the idea is that we give you the tools to make your own trees work how you want them. My recommendation is:
Again, really sorry for the lack of clarity on this. We know the docs need a little extra love, and it's on the list of things to do in 2024. I'd be happy to review any docs clarification PR you want to make. But if that's not interesting to you or you don't have time, I'll just make a note of this discussion as one of the items we need to make sure to improve. I hope this helps! |
Beta Was this translation helpful? Give feedback.
Hey @mossymaker - thanks for the detailed question and example.
I think the issue here is unclear documentation. It makes a lot of sense that you would expect the data to be updated automatically baed on the documentation you quoted. I'm sorry for the lack of clarity.
As you found in the earlier discussion, it is actually on you to make sure your data stays normalized. The idea is that we "make it possible" to keep data normalized, but we aren't necessarily going to do it on your behalf.
I'm not one of the original authors, so I can't speak to the actual intention behind that decision, but I think the idea is that we give you the tools to make your own trees work how you want them. My rec…