2016-11-02 23:17:28 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const COLUMNS = ['app_name', 'strategies'];
|
|
|
|
const TABLE = 'client_strategies';
|
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
const mapRow = (row) => ({
|
|
|
|
appName: row.app_name,
|
|
|
|
strategies: row.strategies,
|
|
|
|
});
|
|
|
|
|
|
|
|
class ClientStrategyStore {
|
|
|
|
constructor (db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRow (appName, strategies) {
|
|
|
|
return this.db(TABLE)
|
2016-11-02 23:17:28 +01:00
|
|
|
.where('app_name', appName) // eslint-disable-line
|
|
|
|
.update({
|
|
|
|
strategies: JSON.stringify(strategies),
|
|
|
|
updated_at: 'now()', // eslint-disable-line
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
insertNewRow (appName, strategies) {
|
|
|
|
return this.db(TABLE).insert({
|
2016-11-02 23:17:28 +01:00
|
|
|
app_name: appName, // eslint-disable-line
|
|
|
|
strategies: JSON.stringify(strategies),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
insert (appName, strategies) {
|
|
|
|
return this.db(TABLE)
|
2016-11-02 23:17:28 +01:00
|
|
|
.count('*')
|
|
|
|
.where('app_name', appName)
|
|
|
|
.map(row => ({ count: row.count }))
|
|
|
|
.then(rows => {
|
2016-11-02 23:24:23 +01:00
|
|
|
if (rows[0].count > 0) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.updateRow(appName, strategies);
|
2016-11-02 23:24:23 +01:00
|
|
|
} else {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.insertNewRow(appName, strategies);
|
2016-11-02 23:24:23 +01:00
|
|
|
}
|
2016-11-02 23:17:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
getAll () {
|
|
|
|
return this.db
|
2016-11-02 23:17:28 +01:00
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.map(mapRow);
|
|
|
|
}
|
2016-11-28 17:11:11 +01:00
|
|
|
|
|
|
|
getByAppName (appName) {
|
|
|
|
return this.db
|
|
|
|
.select(COLUMNS)
|
|
|
|
.where('app_name', appName)
|
|
|
|
.from(TABLE)
|
|
|
|
.map(mapRow);
|
|
|
|
}
|
2016-11-02 23:17:28 +01:00
|
|
|
};
|
2016-11-05 14:08:47 +01:00
|
|
|
|
|
|
|
module.exports = ClientStrategyStore;
|