1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/db/setting-store.js
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

52 lines
1.1 KiB
JavaScript

/* eslint camelcase: "off" */
'use strict';
const TABLE = 'settings';
class SettingStore {
constructor(db, getLogger) {
this.db = db;
this.logger = getLogger('settings-store.js');
}
updateRow(name, content) {
return this.db(TABLE)
.where('name', name)
.update({
content: JSON.stringify(content),
});
}
insertNewRow(name, content) {
return this.db(TABLE).insert({ name, content });
}
insert(name, content) {
return this.db(TABLE)
.count('*')
.where('name', name)
.map(row => ({ count: row.count }))
.then(rows => {
if (rows[0].count > 0) {
return this.updateRow(name, content);
}
return this.insertNewRow(name, content);
});
}
async get(name) {
const result = await this.db
.select()
.from(TABLE)
.where('name', name);
if (result.length > 0) {
return result[0].content;
}
return undefined;
}
}
module.exports = SettingStore;