Skip to content

Commit a6599c9

Browse files
committed
Merge pull request #7 from arangodb/transactions
Added support for transactions
2 parents 0cc53bc + 921da7b commit a6599c9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ Fetches all databases from the server and passes an array of new *Database* inst
137137

138138
Deletes the database with the given *databaseName* from the server.
139139

140+
### Transactions
141+
142+
#### database.transaction(collections, action, [params,] [lockTimeout,] callback)
143+
144+
Performs a server-side transaction and passes the *action*'s return value to the callback.
145+
146+
*Parameter*
147+
148+
* *collections*: an object with the following properties:
149+
* *read*: an array of names (or a single name) of collections that will be read from during the transaction.
150+
* *write*: an array of names (or a single name) of collections that will be written to or read from during the transaction.
151+
* *action*: a string evaluating to a JavaScript function to be executed on the server.
152+
* *params* (optional): parameters that will be passed to the function.
153+
* *lockTimeout* (optional): determines how long the database will wait while attemping to gain locks on collections used by the transaction before timing out.
154+
155+
If *collections* is an array or string, it will be used as *collections.write*.
156+
157+
Please note that while *action* should be a string evaluating to a well-formed JavaScript function, it's not possible to pass in a JavaScript function directly because the function needs to be evaluated on the server and will be transmitted in plain text.
158+
140159
### Queries
141160

142161
#### database.query(query, [bindVars,] callback)

lib/database.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,33 @@ extend(Database.prototype, {
194194
}
195195
});
196196
},
197+
transaction: function (collections, action, params, lockTimeout, callback) {
198+
if (typeof lockTimeout === 'function') {
199+
callback = lockTimeout;
200+
lockTimeout = undefined;
201+
}
202+
if (typeof params === 'function') {
203+
callback = params;
204+
params = undefined;
205+
}
206+
if (typeof params === 'number') {
207+
lockTimeout = params;
208+
params = undefined;
209+
}
210+
if (typeof collections === 'string' || Array.isArray(collections)) {
211+
collections = {write: collections};
212+
}
213+
if (!callback) callback = noop;
214+
this._api.post('transaction', {
215+
collections: collections,
216+
action: action,
217+
params: params,
218+
lockTimeout: lockTimeout
219+
}, function (err, body) {
220+
if (err) callback(err);
221+
else callback(null, body.result);
222+
});
223+
},
197224
query: function (query, bindVars, callback) {
198225
if (typeof bindVars === 'function') {
199226
callback = bindVars;

0 commit comments

Comments
 (0)