@@ -101,12 +101,41 @@ function getConnection () {
101
101
Request a connection pair. ` release ` should be called when the connection is no
102
102
longer necessary.
103
103
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
+
104
127
#### ` db.atomic(Function → Promise<T>) → Function `
105
128
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:
107
133
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,
109
137
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.
111
140
112
141
[ node-postgres ] : https://github.com/brianc/node-postgres
0 commit comments