refactor: use a single DB task to load settings
This commit is contained in:
parent
c233aa9aff
commit
593f7fe949
1 changed files with 22 additions and 16 deletions
|
|
@ -91,7 +91,7 @@ function saveAccounts(accounts) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadAccounts(schemaVersion) {
|
function _loadAccounts(db, schemaVersion) {
|
||||||
const sql = `SELECT data
|
const sql = `SELECT data
|
||||||
FROM user_config
|
FROM user_config
|
||||||
WHERE type = $1
|
WHERE type = $1
|
||||||
|
|
@ -100,14 +100,15 @@ function loadAccounts(schemaVersion) {
|
||||||
ORDER BY id DESC
|
ORDER BY id DESC
|
||||||
LIMIT 1`
|
LIMIT 1`
|
||||||
|
|
||||||
return db
|
return db.oneOrNone(
|
||||||
.oneOrNone(sql, [
|
sql,
|
||||||
'accounts',
|
['accounts', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION],
|
||||||
schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION,
|
row => row?.data?.accounts ?? {},
|
||||||
])
|
)
|
||||||
.then(_.compose(_.defaultTo({}), _.get('data.accounts')))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const loadAccounts = schemaVersion => _loadAccounts(db, schemaVersion)
|
||||||
|
|
||||||
function hideSecretFields(accounts) {
|
function hideSecretFields(accounts) {
|
||||||
return _.flow(
|
return _.flow(
|
||||||
_.filter(path => !_.isEmpty(_.get(path, accounts))),
|
_.filter(path => !_.isEmpty(_.get(path, accounts))),
|
||||||
|
|
@ -222,7 +223,7 @@ function loadLatestConfigOrNone(schemaVersion) {
|
||||||
.then(row => (row ? row.data.config : {}))
|
.then(row => (row ? row.data.config : {}))
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadConfig(versionId) {
|
function loadConfig(db, versionId) {
|
||||||
const sql = `SELECT data
|
const sql = `SELECT data
|
||||||
FROM user_config
|
FROM user_config
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
|
|
@ -231,8 +232,11 @@ function loadConfig(versionId) {
|
||||||
AND valid`
|
AND valid`
|
||||||
|
|
||||||
return db
|
return db
|
||||||
.one(sql, [versionId, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
.one(
|
||||||
.then(row => row.data.config)
|
sql,
|
||||||
|
[versionId, NEW_SETTINGS_LOADER_SCHEMA_VERSION],
|
||||||
|
({ data: { config } }) => config,
|
||||||
|
)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err.name === 'QueryResultError') {
|
if (err.name === 'QueryResultError') {
|
||||||
throw new Error('No such config version: ' + versionId)
|
throw new Error('No such config version: ' + versionId)
|
||||||
|
|
@ -245,12 +249,14 @@ function loadConfig(versionId) {
|
||||||
function load(versionId) {
|
function load(versionId) {
|
||||||
if (!versionId) Promise.reject('versionId is required')
|
if (!versionId) Promise.reject('versionId is required')
|
||||||
|
|
||||||
return Promise.all([loadConfig(versionId), loadAccounts()]).then(
|
return db.task(t => {
|
||||||
([config, accounts]) => ({
|
t.batch([loadConfig(t, versionId), _loadAccounts(t)]).then(
|
||||||
config,
|
([config, accounts]) => ({
|
||||||
accounts,
|
config,
|
||||||
}),
|
accounts,
|
||||||
)
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchCurrentConfigVersion = () => {
|
const fetchCurrentConfigVersion = () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue