five-bells-shared
Advanced tools
Comparing version
@@ -9,2 +9,3 @@ exports.Config = require('./lib/config') | ||
exports.DB = require('./lib/db') | ||
exports.NotificationScheduler = require('./lib/notificationScheduler') | ||
@@ -11,0 +12,0 @@ exports.BaseError = require('./errors/base-error') |
@@ -6,3 +6,6 @@ 'use strict' | ||
const url = require('url') | ||
const retry = require('retry-as-promised') | ||
const DEFAULT_MAX_TRANSACTION_RETRIES = 5 | ||
module.exports = function (Sequelize, log) { | ||
@@ -13,6 +16,9 @@ if (typeof log === 'object') { | ||
return class DB extends Sequelize { | ||
// options.maxTransactionRetries is the number of times to retry transactions that fail due to serialization / transaction isolation errors | ||
constructor (uri, options) { | ||
options = _.merge({ | ||
logging: typeof log === 'object' ? log.debug : false, | ||
omitNull: true | ||
omitNull: true, | ||
// All transactions should be done with isolation level SERIALIZABLE | ||
isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE | ||
}, options) | ||
@@ -32,2 +38,4 @@ const dbParts = url.parse(uri) | ||
super(uri, options) | ||
this.maxTransactionRetries = options.maxTransactionRetries || DEFAULT_MAX_TRANSACTION_RETRIES | ||
} | ||
@@ -66,10 +74,21 @@ | ||
// Turn the generator into a promise, then call upstream transaction(). | ||
return super.transaction(function (tr) { | ||
const generator = transactionFn.call(this, tr) | ||
if (typeof generator === 'object' && | ||
typeof generator.next === 'function') { | ||
return co(generator) | ||
} | ||
// Transactions will be retried if they fail with an error due to serialization / transaction isolation | ||
return retry(() => { | ||
return super.transaction((tr) => { | ||
const generator = transactionFn.call(this, tr) | ||
if (typeof generator === 'object' && | ||
typeof generator.next === 'function') { | ||
return co(generator) | ||
} | ||
return generator | ||
return generator | ||
}) | ||
}, { | ||
max: this.maxTransactionRetries, | ||
match: [ | ||
'could not serialize access due to concurrent update', | ||
'could not serialize access due to read/write dependencies among transactions' | ||
], | ||
backoffBase: 100, | ||
backoffExponent: 1.1 | ||
}) | ||
@@ -76,0 +95,0 @@ } |
@@ -31,9 +31,14 @@ 'use strict' | ||
Model.prototype.save = function * (options) { | ||
const myKnex = knexOrTransaction(knex, options) | ||
return myKnex.update(this.getDataPersistent()).into(Model.tableName).where('id', this.id).then() | ||
const myKnex = knexOrTransaction(knex, options)(Model.tableName) | ||
const data = this.getDataPersistent() | ||
if (this.id) { | ||
return myKnex.update(data).where('id', this.id).then() | ||
} else { | ||
return myKnex.insert(data).then() | ||
} | ||
} | ||
// Delete row based on where options | ||
Model.prototype.destroy = function (selectOptions) { | ||
return knex(Model.tableName).where(selectOptions).del() | ||
Model.prototype.destroy = function (options) { | ||
const myKnex = knexOrTransaction(knex, options) | ||
return myKnex(Model.tableName).where('id', this.id).del().then() | ||
} | ||
@@ -40,0 +45,0 @@ |
{ | ||
"name": "five-bells-shared", | ||
"version": "9.0.1", | ||
"version": "9.1.0", | ||
"description": "Shared components for Five Bells projects.", | ||
@@ -31,2 +31,3 @@ "keywords": [ | ||
"co-body": "^1.0.0", | ||
"co-defer": "^1.0.0", | ||
"immutable": "^3.7.6", | ||
@@ -38,2 +39,3 @@ "lodash": "^3.8.0", | ||
"path-to-regexp": "^1.2.0", | ||
"retry-as-promised": "^2.0.1", | ||
"through2": "^0.6.3", | ||
@@ -40,0 +42,0 @@ "tv4": "^1.2.7", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1729039
0.87%117
1.74%3533
2.94%18
12.5%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added