Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

any-db-transaction

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-db-transaction - npm Package Compare versions

Comparing version 2.1.0 to 2.1.1

2

package.json
{
"name": "any-db-transaction",
"version": "2.1.0",
"version": "2.1.1",
"description": "Transaction object for Any-DB adapters",

@@ -5,0 +5,0 @@ "main": "transaction.js",

@@ -14,8 +14,7 @@ # any-db-transaction

var connection = anyDB.createConnection(...)
var pool = anyDB.createPool(...)
// Callback-style
begin(connection, function (err, transaction) {
if (err) return console.error(err)
// Do work using transaction
if (err) return console.error(err)
// Do work using transaction
transaction.query(...)

@@ -32,2 +31,3 @@ transaction.commit()

// Or use a connection pool
var pool = anyDB.createPool(...)
var transaction = begin(pool)

@@ -232,37 +232,38 @@ ```

var pool = require('any-db').createPool(...)
```javascript
var pool = require('any-db').createPool(...)
var tx = begin(pool)
tx.on('error', finished)
// this is our external service
var abuseService = require('./services').abuseService()
/*
Why query with the pool and not the transaction?
Because it allows the transaction queries to begin executing immediately,
rather than queueing them all up behind the initial SELECT.
*/
pool.query('SELECT id FROM users').on('row', function (user) {
if (tx.state().match('rollback')) return
abuseService.checkUser(user.id, function (err, result) {
if (err) return tx.handleError(err)
// Errors from these queries will propagate up to the transaction object
if (result.flag) {
tx.query('UPDATE users SET abuse_flag = 1 WHERE id = $1', [user.id])
} else if (result.destroy) {
tx.query('DELETE FROM users WHERE id = $1', [user.id])
}
})
}).on('error', function (err) {
tx.handleError(err)
}).on('end', function () {
tx.commit(finished)
})
}).on('error', function (err) {
tx.handleError(err)
}).on('end', function () {
tx.commit(finished)
})
var tx = begin(pool)
tx.on('error', finished)
/*
Why query with the pool and not the transaction?
Because it allows the transaction queries to begin executing immediately,
rather than queueing them all up behind the initial SELECT.
*/
pool.query('SELECT id FROM users')
.on('data', function (user) {
if (tx.state() == 'closed') {
// Do not make unneccessary requests
return
}
abuseService.checkUser(user.id, function (err, result) {
if (err) return tx.handleError(err)
// Errors from these queries will propagate up to the transaction object
if (result.flag) {
tx.query('UPDATE users SET abuse_flag = 1 WHERE id = $1', [user.id])
} else if (result.destroy) {
tx.query('DELETE FROM users WHERE id = $1', [user.id])
}
})
}).on('end', function () {
tx.commit(finished)
})
function finished (err) {
if (err) console.error(err)
else console.log('All done!')
if (err) console.error(err)
else console.log('All done!')
}

@@ -275,7 +276,10 @@ ```

[any-db]: https://github.com/grncdr/node-any-db
[begin]: #begin
[Connection]: https://github.com/grncdr/any-db-adapter-spec#connection
[Queryable]: https://github.com/grncdr/any-db-adapter-spec#queryable
[Queryable.query]: https://github.com/grncdr/any-db-adapter-spec#queryablequery
[Query]: https://github.com/grncdr/any-db-adapter-spec#query
[ConnectionPool]: https://github.com/grncdr/any-db-pool#connectionpool
[Connection]: https://github.com/grncdr/node-any-db-adapter-spec#connection
[Queryable]: https://github.com/grncdr/node-any-db-adapter-spec#queryable
[Queryable.query]: https://github.com/grncdr/node-any-db-adapter-spec#queryablequery
[Query]: https://github.com/grncdr/node-any-db-adapter-spec#query
[ConnectionPool]: https://github.com/grncdr/node-any-db-pool#connectionpool
[connect]: https://npm.im/connect
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc