by stopachka · 2y ago · 0 comments
Writing a `user.save()` could be a good idea, but it opens up a question about how to do transactions. For example, saving _both_ user and post together).
I could see a variant where we return proxied objects from `useQuery`.
What would your ideal API look like?
Imagine that you support the model discussed above where it’s possible to update the local store optimistically without syncing back to the db. Now you’re one missing .save() away from having everything look like it’s working in the frontend when really nothing is persisting. It’s the sort of foot gun that you might regret supporting.
Our model is slightly different in that we require the .save() on objects to create the mutation for the sync. The primary reason is that we’re syncing back to real tables in Postgres and require referential integrity etc to be maintained.
tx((db) => {
const author = new Author(db)
author.save()
article.name = “New name”
article.author = author
article.save()
}
Mutating an object outside of a transaction is a hard error. Doing the mutation in a transaction but failing to call save within the same transaction is a hard error too.Mark (our team member) has advocated for a callback-based API that looks a lot like what you landed on. It has the advantage of removing an import too!
Question: how do you solve the 'draft' state issue that remolacha mentioned?
Forms just inherently can have partially-finished/invalid states, and it feels wrong to try and kraal model objects into carrying intermediary/invalid data for them (and in some cases won’t work at all, eg if a single form field is parsed into structured data in the model)
We have controllers that all the users actions are funnelled through. The top level functions in there are wrapped in transactions so in practice it’s not something you manually wrangle.
author.save()
.then(() => {
const article = new Article(db);
article.name = "New name";
article.author = author;
return article.save(); // could be used to chain the next save
})
.then(() => {
console.log("Both author and article saved successfully.");
})
.catch((error) => {
console.error("rollback, no changes");
});
but I confess I might have said that without having the same understanding of the problem as you so might be nonsense. it just happen that I decided to implement transactions this way in a side project of mine.1. Assign, then save. AFAIK, this is effectively transactional if you're saving a single object, since it's a single `UPDATE` statement in sql. If you assigned to a related object, you need to save that separately.
2. Use ActiveRecord functions like `post.update({title: "foo", content: "Lorem ipsum"})`. This assigns to the in-memory object and also kicks off a request to the DB. This is basically syntax sugar over assigning and then calling `save()`, but addresses the issue around devs forgetting to call `save()` after assigning. In Rails, this is used in 90% of cases.
3. I can also choose to wrap mutations in a transaction if I'm mutating multiple proxy objects, and I need them to succeed/fail as a group. This is rarely used, but sometimes necessary. For example, in Rails, I can write something along the lines of this:
```rb
ActiveRecord.transaction do
post.title = "Foo"
post.author.name = "John Smith"
post.save()
post.author.save()
end# Alternatively, using the `update()` syntax
ActiveRecord.transaction do
post.update({ title: "Foo" })
post.author.update( { name: "John Smith" })
end```
This gives transactional semantics around anything happening inside of the `do` block. I think the syntax would look very similar in javascript, for example:
```js
transaction(() => {
post.update({ title: "Foo" })
post.author.update( { name: "John Smith" })
})```
He gave an example in the first post in this chain xD