onyxframework / sql

A delightful SQL ORM ☺️
https://api.onyxframework.com/sql
MIT License
91 stars 7 forks source link

Transactions #30

Closed greenbigfrog closed 6 years ago

greenbigfrog commented 6 years ago

Not sure if I'm missing it, or if core.cr doesn't support transactions.

vladfaust commented 6 years ago

Hello, @greenbigfrog,

I suppose that should do the case:

db = DB.open(ENV["DATABASE_URL"])
db.transaction do |tx|
  repo = Core::Repository.new(tx.connection, query_logger)
  repo.insert(something)
  repo.query(something)
end

You can read more about transactions here (though it's terribly documented). A working example of Crystal DB transaction could be found here.

Do not hesitate asking more questions or suggesting better implementation!

greenbigfrog commented 6 years ago

Thanks. That should do the job! For now I'll try to go plain SQL... Never a bad idea to know how to do it without help...

greenbigfrog commented 6 years ago

Since you seam to know your way around transactions I've still got a question for you. How can I send different log messages, depending on if a transaction was successfull or not?

db.transaction do |tx|
  tx.exec(sth)
end
# if successfull log `tada` else log `error`
vladfaust commented 6 years ago

@greenbigfrog see this, whatever it means 😅

In a nutshell - begin 'n rescue are youre friends here...

greenbigfrog commented 6 years ago

Hm. I guess I'll just have to do some testing... Thanks

vladfaust commented 6 years ago

@greenbigfrog please share your thoughts after testing!