From 3de167ae2da9e6d06d952b2c8f34eafaf7a32c32 Mon Sep 17 00:00:00 2001 From: Jeremy Daly Date: Wed, 17 Jul 2019 10:56:52 -0400 Subject: [PATCH] add documentation around null transaction queries --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 69efb4c..77b7e7d 100644 --- a/README.md +++ b/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