Sequelize事务实现

Managed transaction (auto-callback)

  • 事务将自动提交
  • sequelize.transaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
return models.sequelize.transaction(function(t) {
// chain all your queries here. make sure you return them.
return User.create({
firstName: 'Abraham',
lastName: 'Lincoln'
}, {
transaction: t
}).then(function(user) {
return user.setShooter({
firstName: 'John',
lastName: 'Boothe'
}, {
transaction: t
});
});
}).then(function(result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
}).catch(function(err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback
});

Unmanaged transaction (then-callback)

  • 非自动提交事务
  • sequelize.transaction()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
return sequelize.transaction().then(function (t) {
return User.create({
firstName: 'Homer',
lastName: 'Simpson'
}, {transaction: t}).then(function (user) {
return user.addSibling({
firstName: 'Lisa',
lastName: 'Simpson'
}, {transaction: t});
}).then(function () {
t.commit();
}).catch(function (err) {
t.rollback();
});
});

Automatically pass transactions to all queries

  • 自动添加事务
  • 无需{ transaction: t }
  • require(‘continuation-local-storage’)
1
2
3
4
5
6
7
8
9
10
var cls = require('continuation-local-storage'),
namespace = cls.createNamespace('my-very-own-namespace');
sequelize.transaction(function (t1) {
namespace.get('transaction') === t1; // true
});
sequelize.transaction(function (t2) {
namespace.get('transaction') === t2; // true
});

Array transaction

  • 事务并行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
models.sequelize.transaction(function(t) {
return models.Products.create(args, {
transaction: t
}).then(function(result) {
return Promise.all([models.user.create({
name: 'test'
}, {
transaction: t
}), models.address.create({
id: '1'
}, {
transaction: t
})]).then(function() {})
})
})

push + promise.all 实现循环读取、写入

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
models.sequelize.transaction(function(t) {
var tranArray = [];
if (!_.isUndefined(args.categories_id)) {
var categories_idArray = args.categories_id.split(',');
tranArray.push(models.Categories_product.destroy({
where: {
product_id: product_id
}
}, {
transaction: t
}));
categories_idArray.forEach(function(categories_id) {
tranArray.push(models.Categories_product.create({
product_id: product_id,
categories_id: categories_id
}, {
transaction: t
}));
});
}
if (_.isUndefined(areacode)) {
tranArray.push(models.Products.update(args, {
where: {
id: product_id
}
}));
} else {
tranArray.push(models.Product_lang_details.update(args,
where: {
product_id: product_id,
areacode: areacode
}));
tranArray.push(models.Product_shipping_to.update({
freezeTableName: args.freezeTableName
},
where: {
product_id: product_id,
areacode: areacode
}));
}
return Promise.all(tranArray);
}).then(function() {
return utils.response(res, 200, {
'message': 'update product successful!'
});
}).catch(function(error) {
logger.error('product update fall', error);
return utils.responseError(res, 500, {
'message': 'format error'
});
});
###