Skip to content

Commit 742e63a

Browse files
update README
1 parent 16a396d commit 742e63a

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,41 @@ function getConnection () {
101101
Request a connection pair. `release` should be called when the connection is no
102102
longer necessary.
103103

104+
#### `db.transaction(Function → Promise<T>) → Function`
105+
106+
Wrap a function as requiring a transaction.
107+
108+
```javascript
109+
const updateUser = db.atomic(function _updateUser(userId, name) {
110+
const getPair = db.getConnection()
111+
const queryDB = getPair.get('connection').then(conn => {
112+
return Promise.promisify(conn.query, {context: conn})(
113+
'UPDATE users SET name = $1 WHERE id = $2', [name, userId]
114+
)
115+
})
116+
const releaseConn = queryDB.return(getPair.get('release'))
117+
.then(release => release())
118+
return releaseConn.return(queryDB)
119+
})
120+
121+
// from inside an active session:
122+
updateUser(1313, 'gary').then(results => {
123+
124+
})
125+
```
126+
104127
#### `db.atomic(Function → Promise<T>) → Function`
105128

106-
Wrap a function as an atomic.
129+
Wrap a function as an atomic. This groups all pending connection requests made by
130+
the function and all subsequent events the function calls together, such that they
131+
are resolved before any other pending requests. This is useful for operations that
132+
stretch multiple queries, for example if you had to:
107133

108-
#### `db.transaction(Function → Promise<T>) → Function`
134+
1. Fetch some data,
135+
2. then insert a row in one table,
136+
3. and then insert a row in another table,
109137

110-
Wrap a function as requiring a transaction.
138+
One might write that as an atomic function so that the three operations are grouped
139+
despite being spaced out temporally.
111140

112141
[node-postgres]: https://github.com/brianc/node-postgres

0 commit comments

Comments
 (0)