-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (51 loc) · 1.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var mongo = require('mongodb').MongoClient;
var Transaction = require('./transaction');
mongo.connect('mongodb://localhost:27017/transactions', function(err, db) {
if (err) return console.log(err);
db.dropDatabase(function(err) {
if (err) return console.log(err);
db.collection('posts').insert([
{title: 'Hello', cat: 'World'},
{title: 'Hi', cat: 'World'},
{title: 'Bye', cat: 'Planet'},
], function(err, results) {
console.log(results);
Transaction(db)
.step(function(t, context, next) {
t.collection('posts').findAndModify({ cat: 'World' }, [], { $set: { cat: 'Me' } }, function(err, result) {
if (err) return console.log(err);
context.post1 = result;
next();
});
})
.step(function(t, context, next) {
t.collection('posts').findAndModify({ cat: 'World' }, [], { $set: { cat: 'Me' } }, function(err, result) {
if (err) return console.log(err);
context.posts2 = result;
next();
});
})
.step(function(t, context, next) {
t.collection('posts').findAndModify({ cat: 'World' }, [], { $set: { cat: 'Me' } }, function(err, result) {
if (err) return console.log(err);
context.posts3 = result;
next('Testing an Error, will roll all transactions back');
// next();
});
})
.commit(function(err, context) {
if (err) {
console.log('Done with errors\n', err);
db.collection('posts').find().toArray(function(err, results) {
console.log(results);
});
} else {
console.log('Done\n', context);
db.collection('posts').find().toArray(function(err, results) {
console.log(results);
});
}
});
});
});
});