mysql-transactions
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,3 +0,7 @@ | ||
### august 13th, 2013 / 0.0.1 | ||
### august 15th, 2013 / 0.0.2 | ||
* simplified the readme | ||
### august 14th, 2013 / 0.0.1 | ||
* initial commit | ||
{ | ||
"name": "mysql-transactions", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Use SQL transactions with node-mysql", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
129
readme.md
@@ -1,6 +0,6 @@ | ||
### node-mysql-transactions | ||
## node-mysql-transactions | ||
#### dependencies | ||
### dependencies | ||
`npm install mysql` | ||
@@ -10,3 +10,3 @@ | ||
#### install | ||
### install | ||
`npm install mysql-transactions` | ||
@@ -16,127 +16,42 @@ | ||
#### tutorial | ||
Seeing as how you're viewing a module named `mysql-transactions` I'm going to assume you understand the little bits and | ||
jump right into coding a simple demo. Cool? Cool. | ||
##### the schema | ||
Before we get started this is the structure we're going to be using for this demo. | ||
```sql | ||
CREATE TABLE users ( | ||
id INT PRIMARY KEY AUTO_INCREMENT, | ||
name VARCHAR(40) NOT NULL | ||
); | ||
``` | ||
##### game time | ||
### quick start | ||
```javascript | ||
var async = require('async'); | ||
var transactions = require('mysql-transactions')({ | ||
user: 'hamburglar', | ||
password: 'omnomnomnomnom', | ||
password: 'omnomnomnomnomnom', | ||
database: 'playhouse' | ||
}); | ||
``` | ||
As you can see, we're using the `async` module as well. But the section to note is `require('mysql-transactions')({ ... });`. | ||
Here we must pass in our `connection` options (again?). These options are the same ones used in your non-transactional queries | ||
i.e. the options/string supplied to [mysql.createConnection](https://github.com/felixge/node-mysql#connection-options). | ||
```javascript | ||
async.series( | ||
[ | ||
function(done) { | ||
transactions.begin(done); | ||
}, | ||
``` | ||
transactions.begin(); | ||
Okay, so now that we have our connection options ready we need to kick off the transactions process which we do by | ||
calling `transactions.begin(done)`. Note that we passed in `done` to continue the `async` flow. | ||
transactions.query('INSERT INTO users SET ?', { name: 'Lord Voldemort' }, function(err, result) { | ||
```javascript | ||
function(done) { | ||
transactions.query('insert into users set ?', { name: 'John Smith' }, function(err, result) { | ||
if (err) return done(err); | ||
done(null, result.affectedRows ? true : false); | ||
}); | ||
if (err) { | ||
transactions.rollback(); | ||
return console.log('Rolled back.'); | ||
} | ||
``` | ||
With the `transactions` process open we can query the database like | ||
[normal](https://github.com/felixge/node-mysql#escaping-query-values). | ||
transactions.commit(function(err, result) { | ||
console.log('Committed.'); | ||
}); | ||
```javascript | ||
], | ||
function(err, results) { | ||
if (err) { | ||
transactions.rollback(); | ||
return console.log(err); | ||
} | ||
transactions.commit(function(err, result) { | ||
if (err) return console.log(err); | ||
console.log('done'); | ||
}); | ||
} | ||
); | ||
}); | ||
``` | ||
The last step in the process is to either `commit` or `rollback` the `transactions`. One _must_ be called to close the | ||
`connection`. While this example shows `transactions.commit(function...)`, it is not necessary to provide a function to | ||
`commit`/`rollback` or `begin` for that matter. | ||
**Note**: After calling `.begin()`, `.commit()` or `.rollback()` **must** be called so that the | ||
connection is closed. | ||
#### full demo | ||
```javascript | ||
var async = require('async'); | ||
### api | ||
Each `[fn]` is a `callback` for a [mysql.query](https://github.com/felixge/node-mysql#introduction). | ||
var transactions = require('mysql-transactions')({ | ||
user: 'hamburglar', | ||
password: 'omnomnomnomnom', | ||
database: 'playhouse' | ||
}); | ||
* `begin([fn])`: start the transaction process. | ||
* `query(String [, Object/Array] [, fn])`: traditional query. | ||
* `rollback([fn])`: rollback the transaction. | ||
* `commit([fn])`: commit the transaction. | ||
async.series( | ||
[ | ||
function(done) { | ||
transactions.begin(done); | ||
}, | ||
function(done) { | ||
transactions.query('insert into users set ?', { name: 'John Smith' }, function(err, result) { | ||
if (err) return done(err); | ||
done(null, result.affectedRows ? true : false); | ||
}); | ||
} | ||
], | ||
function(err, results) { | ||
if (err) { | ||
transactions.rollback(); | ||
return console.log(err); | ||
} | ||
transactions.commit(function(err, result) { | ||
if (err) return console.log(err); | ||
console.log('done'); | ||
}); | ||
} | ||
); | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3866
56