Add a way to enforce collection mutations via actions disallowing inline mutations #996
Send to my agentIt would be great if we had a way to enforce only being able to use mutations like someCollection.insert inside of actions.
e.g.
This is allowed
const addTodo = createOptimisticAction<string>({
onMutate: (text) => {
todoCollection.insert({
id: uuid(),
text,
completed: false
})
},
....
})
This should be forbidden
<Button onClick={() =>
todoCollection.insert({
id: uuid(),
text,
completed: false
})}>
</Button
Why#
The more I have used Tanstack DB the more I have realized almost all mutations I do have some sort of side effects, making single mutations quite useless. I started only using optimistic actions but coding agents and teammates often fall into the trap on using inline muations again. a way to enforce to enforce mutations through actions would be great
kevin-dp(GitHub profile, opens in a new tab) added labelsenhancement
Hi @kevin-dp, I created a PR https://github.com/TanStack/db/pull/1296 to have an example on using lint rules to enforce how we should mutate the collection, can you check it out
I do think that inline mutations (insert/update/delete) are useful in some simple cases. Forcing those simple cases to use actions would be overkill.
Yeah, I totally agree. We shouldn’t always enforce or restrict how TanStack DB is used. This PR is not proposing an official lint rule or anything like that; it’s just an example of how a team can use ESLint to add conventions-based restrictions when they need that level of enforcement.
Yeah, I totally agree. We shouldn’t always enforce or restrict how TanStack DB is used. This PR is not proposing an official lint rule or anything like that; it’s just an example of how a team can use ESLint to add conventions-based restrictions when they need that level of enforcement.
Yep, thanks for that. Very useful!
#1296 added an optional action-enforcement example with a local ESLint rule, which matches the convention agreed in this discussion. I think this can be closed.
Sign in to join this conversation.
One option I've been exploring is grouping actions in a directory, collections in a different directory, and using a modularity enforcer (like jsboundaries or feature sliced design) to enforce that components can only call actions (not collections).
I do a similar thing for user actions that must be appended to an undo/redo tree.