improve config saving; fix pairing bug
This commit is contained in:
parent
67e12b19cb
commit
f3f46f7b5b
4 changed files with 81 additions and 9 deletions
|
|
@ -5,6 +5,7 @@ const _ = require('lodash/fp')
|
|||
const argv = require('minimist')(process.argv.slice(2))
|
||||
const pify = require('pify')
|
||||
|
||||
const pgp = require('pg-promise')()
|
||||
const db = require('./db')
|
||||
const configValidate = require('./config-validate')
|
||||
|
||||
|
|
@ -39,7 +40,7 @@ function isEquivalentField (a, b) {
|
|||
|
||||
// b overrides a
|
||||
function mergeValues (a, b) {
|
||||
return _.unionWith(isEquivalentField, b, a)
|
||||
return _.reject(r => _.isNil(r.fieldValue), _.unionWith(isEquivalentField, b, a))
|
||||
}
|
||||
|
||||
function load (versionId) {
|
||||
|
|
@ -125,6 +126,50 @@ function save (config) {
|
|||
.catch(() => db.none(sql, ['config', {config}, false]))
|
||||
}
|
||||
|
||||
function configAddField (scope, fieldCode, value) {
|
||||
return {
|
||||
fieldLocator: {
|
||||
fieldScope: {
|
||||
crypto: scope.crypto,
|
||||
machine: scope.machine
|
||||
},
|
||||
code: fieldCode
|
||||
},
|
||||
fieldValue: {value}
|
||||
}
|
||||
}
|
||||
|
||||
function configDeleteField (scope, fieldCode) {
|
||||
return {
|
||||
fieldLocator: {
|
||||
fieldScope: {
|
||||
crypto: scope.crypto,
|
||||
machine: scope.machine
|
||||
},
|
||||
code: fieldCode
|
||||
},
|
||||
fieldValue: null
|
||||
}
|
||||
}
|
||||
|
||||
function modifyConfig (newFields) {
|
||||
const TransactionMode = pgp.txMode.TransactionMode
|
||||
const isolationLevel = pgp.txMode.isolationLevel
|
||||
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
|
||||
|
||||
function transaction (t) {
|
||||
return loadLatest()
|
||||
.then(settings => {
|
||||
const oldConfig = settings.config
|
||||
return save(mergeValues(oldConfig, newFields))
|
||||
})
|
||||
}
|
||||
|
||||
transaction.txMode = tmSRD
|
||||
|
||||
return db.tx(transaction)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
settings,
|
||||
loadConfig,
|
||||
|
|
@ -132,5 +177,8 @@ module.exports = {
|
|||
loadLatest,
|
||||
save,
|
||||
loadFixture,
|
||||
mergeValues
|
||||
mergeValues,
|
||||
modifyConfig,
|
||||
configAddField,
|
||||
configDeleteField
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue