Hacker Newsnew | past | comments | ask | show | jobs light | darkhn

@stopachka, sorry for late reply. I've mostly provided my ideal API in the posts above. I think my answer to transactions and forgetting save is to offer a few options, as in ActiveRecord. From what I recall, Rails gives a few ways to make persistent changes:

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" })
})

```


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact |

Search: