Skip to content
Permalink
Browse files

add documentation around null transaction queries

  • Loading branch information...
jeremydaly committed Jul 17, 2019
1 parent 6a80cc7 commit 3de167ae2da9e6d06d952b2c8f34eafaf7a32c32
Showing with 18 additions and 0 deletions.
  1. +18 −0 README.md
@@ -243,6 +243,24 @@ let results = await mysql.transaction()
.commit() // execute the queries
```

You can also return a `null` or empty response from `.query()` calls within a transaction. This lets you perform conditional transactions like this:

```javascript
let results = await mysql.transaction()
.query('DELETE FROM table WHERE id = ?', [someVar])
.query((r) => {
if (r.affectedRows > 0) {
['UPDATE anotherTable SET x = 1 WHERE id = ?', [someVar]]
} else {
return null
}
})
.rollback(e => { /* do something with the error */ }) // optional
.commit() // execute the queries
```

If the record to `DELETE` doesn't exist, the `UPDATE` will not be performed. If the `UPDATE` fails, the `DELETE` will be rolled back.

**NOTE:** Transaction support is designed for InnoDB tables (default). Other table types may not behave as expected.

## Reusing Persistent Connections

0 comments on commit 3de167a

Please sign in to comment.
You can’t perform that action at this time.