unleash-server
Version:
Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.
81 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const COLUMNS = [
'name',
'description',
'stickiness',
'sort_order',
'legal_values',
'created_at',
];
const TABLE = 'context_fields';
const mapRow = (row) => ({
name: row.name,
description: row.description,
stickiness: row.stickiness,
sortOrder: row.sort_order,
legalValues: row.legal_values || [],
createdAt: row.created_at,
});
class ContextFieldStore {
constructor(db, getLogger) {
this.db = db;
this.logger = getLogger('context-field-store.ts');
}
fieldToRow(data) {
return {
name: data.name,
description: data.description,
stickiness: data.stickiness,
sort_order: data.sortOrder,
legal_values: JSON.stringify(data.legalValues || []),
};
}
async getAll() {
const rows = await this.db
.select(COLUMNS)
.from(TABLE)
.orderBy('name', 'asc');
return rows.map(mapRow);
}
async get(key) {
return this.db
.first(COLUMNS)
.from(TABLE)
.where({ name: key })
.then(mapRow);
}
async deleteAll() {
await this.db(TABLE).del();
}
destroy() { }
async exists(key) {
const result = await this.db.raw(`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`, [key]);
const { present } = result.rows[0];
return present;
}
// TODO: write tests for the changes you made here?
async create(contextField) {
const [row] = await this.db(TABLE)
.insert(this.fieldToRow(contextField))
.returning('*');
return mapRow(row);
}
async update(data) {
const [row] = await this.db(TABLE)
.where({ name: data.name })
.update(this.fieldToRow(data))
.returning('*');
return mapRow(row);
}
async delete(name) {
return this.db(TABLE).where({ name }).del();
}
async count() {
return this.db(TABLE)
.count('*')
.then((res) => Number(res[0].count));
}
}
exports.default = ContextFieldStore;
//# sourceMappingURL=context-field-store.js.map