diff --git a/.vscode/settings.json b/.vscode/settings.json index c7c1623b..2c63c085 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,2 @@ { - "typescript.tsdk": "./node_modules/typescript/lib" -} \ No newline at end of file +} diff --git a/lib/admin/accounts.js b/lib/admin/accounts.js index 163a5435..f0390057 100644 --- a/lib/admin/accounts.js +++ b/lib/admin/accounts.js @@ -4,6 +4,7 @@ const path = require('path') const options = require('../options') const db = require('../db') +const config = require('./config') const accountRoot = options.pluginPath const schemas = {} @@ -37,8 +38,7 @@ function selectedAccounts () { v.fieldValue.value const mapSchema = code => schemas[code] - return db.oneOrNone('select data from user_config where type=$1', ['config']) - .then(row => row && row.data) + return config.fetchConfig() .then(data => { if (!data) return [] diff --git a/lib/admin/config.js b/lib/admin/config.js index 602b327b..e4569301 100644 --- a/lib/admin/config.js +++ b/lib/admin/config.js @@ -19,8 +19,11 @@ function fetchSchema () { .then(JSON.parse) } -function dbFetchConfig () { - return db.oneOrNone('select data from user_config where type=$1', ['config']) +function fetchConfig () { + const sql = `select data from user_config where type=$1 + order by id desc limit 1` + + return db.oneOrNone(sql, ['config']) .then(row => row && row.data) } @@ -108,7 +111,7 @@ const fetchMachines = () => machines.getMachines() .then(machineList => machineList.map(r => r.deviceId)) function validateConfig () { - return Promise.all([fetchSchema(), dbFetchConfig(), fetchMachines()]) + return Promise.all([fetchSchema(), fetchConfig(), fetchMachines()]) .then(([schema, configRec, machineList]) => { const config = configRec ? configRec.config : [] const cryptos = getCryptos(config, machineList) @@ -127,7 +130,7 @@ function validateConfig () { function fetchConfigGroup (code) { const fieldLocatorCodeEq = R.pathEq(['fieldLocator', 'code']) - return Promise.all([fetchSchema(), fetchData(), dbFetchConfig(), fetchMachines()]) + return Promise.all([fetchSchema(), fetchData(), fetchConfig(), fetchMachines()]) .then(([schema, data, config, machineList]) => { const configValues = config ? config.config : [] const groupSchema = schema.groups.find(r => r.code === code) @@ -211,18 +214,16 @@ function fetchData () { } function dbSaveConfig (config) { - return db.none('update user_config set data=$1 where type=$2', [config, 'config']) + const sql = 'insert into user_config (type, data) values ($1, $2)' + return db.none(sql, ['config', config]) } function saveConfigGroup (results) { - return dbFetchConfig() - .then(config => { - return config - ? Promise.resolve(config) - : db.none('insert into user_config (type, data) values ($1, $2)', ['config', {config: []}]) - .then(dbFetchConfig) - }) + if (results.values.length === 0) return fetchConfigGroup(results.groupCode) + + return fetchConfig() .then(config => { + if (!config) config = {config: []} const oldValues = config.config results.values.forEach(newValue => { @@ -258,5 +259,6 @@ function saveConfigGroup (results) { module.exports = { fetchConfigGroup, saveConfigGroup, - validateConfig + validateConfig, + fetchConfig } diff --git a/lib/settings-loader.js b/lib/settings-loader.js index 2b5ceae4..57c55177 100644 --- a/lib/settings-loader.js +++ b/lib/settings-loader.js @@ -58,8 +58,8 @@ function settings () { } function save (config) { - const sql = 'update user_config set data=$1 where type=$2' - return db.none(sql, [{config}, 'config']) + const sql = 'insert into user_config (type, data) values ($1, $2)' + return db.none(sql, ['config', config]) } module.exports = { diff --git a/migrations/021-config-version-id.js b/migrations/021-config-version-id.js index 7a1ba1ce..44c01949 100644 --- a/migrations/021-config-version-id.js +++ b/migrations/021-config-version-id.js @@ -3,6 +3,7 @@ var db = require('./db') exports.up = function (next) { var sql = [ 'alter table devices add column user_config_id int', + 'alter table user_config add column created timestamptz NOT NULL default now()', `ALTER TABLE devices ADD CONSTRAINT user_config_id FOREIGN KEY (user_config_id) REFERENCES user_config (id)` diff --git a/tools/show.js b/tools/show.js index 260adb77..daca1433 100644 --- a/tools/show.js +++ b/tools/show.js @@ -7,7 +7,10 @@ function pp (o) { } function dbFetchConfig () { - return db.oneOrNone('select data from user_config where type=$1', ['config']) + return db.oneOrNone( + 'select data from user_config where type=$1 order by id desc limit 1', + ['config'] + ) .then(row => row && row.data) }