Skip to content

Commit

Permalink
Merge pull request #2 from jeremydaly/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
jeremydaly authored Sep 8, 2018
2 parents 212f2ca + 5f8fe46 commit 431683a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,24 @@ If you're not setting max `user_connections`, the user **MUST BE** granted the `
The `mysql` module allows you to specify a "[timeout](https://github.com/mysqljs/mysql#timeouts)" with each query. Typically this will disconnect the connection and prevent you from running additional queries. `serverless-mysql` handles timeouts a bit more elegantly by throwing an error and `destroy()`ing the connection. This will reset the connection completely, allowing you to run additional queries **AFTER** you catch the error.

## Transaction Support
Transaction support in `serverless-mysql` has been dramatically simplified. Start a new transaction using the `transaction()` method, and then chain queries using the `query()` method. The `query()` method supports all standard query options.
Transaction support in `serverless-mysql` has been dramatically simplified. Start a new transaction using the `transaction()` method, and then chain queries using the `query()` method. The `query()` method supports all standard query options. Alternatively, you can specify a function as the only argument in a `query()` method call and return the arguments as an array of values. The function receives two arguments, the result of the last query executed and an array containing all the previous query results. This is useful if you need values from a previous query as part of your transaction.

You can specify an optional `rollback()` method in the chain. This will receive the `error` object, allowing you to add additional logging or perform some other action. Call the `commit()` when you are ready to execute the queries.

```javascript
let results = await mysql.transaction()
.query('INSERT INTO table (x) VALUES(?)', [1])
.query('UPDATE table SET x = 1 ')
.query('SELECT * FROM table')
.query('UPDATE table SET x = 1')
.rollback(e => { /* do something with the error */ }) // optional
.commit() // execute the queries
```

With a function to get the insertId from the previous query:

```javascript
let results = await mysql.transaction()
.query('INSERT INTO table (x) VALUES(?)', [1])
.query((r) => ['UPDATE table SET x = 1 WHERE id = ?', r.insertId])
.rollback(e => { /* do something with the error */ }) // optional
.commit() // execute the queries
```
Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* More detail regarding the MySQL module can be found here:
* https://github.com/mysqljs/mysql
* @author Jeremy Daly <[email protected]>
* @version 1.0.0
* @version 1.1.0
* @license MIT
*/

Expand Down Expand Up @@ -293,13 +293,19 @@ const transaction = () => {
let rollback = () => {} // default rollback event

return {
query: function(...args) { queries.push([...args]); return this },
query: function(...args) {
if (typeof args[0] === 'function') {
queries.push(args[0])
} else {
queries.push(() => [...args])
}
return this
},
rollback: function(fn) {
if (typeof fn === 'function') { rollback = fn }
return this
},
commit: async function() { return await commit(queries,rollback) },
queries
commit: async function() { return await commit(queries,rollback) }
}
}

Expand All @@ -314,7 +320,9 @@ const commit = async (queries,rollback) => {
// Loop through queries
for (let i in queries) {
// Execute the queries, pass the rollback as context
results.push(await query.apply({rollback},queries[i]))
let result = await query.apply({rollback},queries[i](results[results.length-1],results))
// Add the result to the main results accumulator
results.push(result)
}

// Commit our transaction
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-mysql",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 431683a

Please sign in to comment.