2
votes

I'm trying to save 2 related models and would like to use a database transaction for this so that in case of a failure nothing is written to the database.

In Vapor 3 you could use the following:

req.transaction(on: .<#dbid#>) { conn in
    // use conn as your connection
}

How can this be done in Vapor 4? Unfortunately, the documentation for transactions doesn't exist for version 4.

1

1 Answers

3
votes

It's req.db.transaction to create a new transaction for use. The tests are the best place to look to find examples - e.g. here

(Also an issue on GH at vapor/docs would be great to ensure the docs get written!)