mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const COLUMNS = ['app_name', 'strategies'];
|
|
const TABLE = 'client_strategies';
|
|
|
|
module.exports = function (db) {
|
|
function update (appName, strategies) {
|
|
return db(TABLE)
|
|
.where('app_name', appName) // eslint-disable-line
|
|
.update({
|
|
strategies: JSON.stringify(strategies),
|
|
updated_at: 'now()', // eslint-disable-line
|
|
});
|
|
}
|
|
|
|
function insert (appName, strategies) {
|
|
return db(TABLE).insert({
|
|
app_name: appName, // eslint-disable-line
|
|
strategies: JSON.stringify(strategies),
|
|
});
|
|
}
|
|
|
|
function insertOrUpdate (appName, strategies) {
|
|
return db(TABLE)
|
|
.count('*')
|
|
.where('app_name', appName)
|
|
.map(row => ({ count: row.count }))
|
|
.then(rows => {
|
|
return rows[0].count > 0 ? update(appName, strategies) : insert(appName, strategies);
|
|
});
|
|
}
|
|
|
|
function getAll () {
|
|
return db
|
|
.select(COLUMNS)
|
|
.from(TABLE)
|
|
.map(mapRow);
|
|
}
|
|
|
|
function mapRow (row) {
|
|
return {
|
|
appName: row.app_name,
|
|
strategies: row.strategies,
|
|
};
|
|
}
|
|
|
|
return { insertOrUpdate, getAll };
|
|
};
|