1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/db/tag-type-store.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

86 lines
2.2 KiB
JavaScript

'use strict';
const metricsHelper = require('../metrics-helper');
const { DB_TIME } = require('../events');
const NotFoundError = require('../error/notfound-error');
const COLUMNS = ['name', 'description', 'icon'];
const TABLE = 'tag_types';
class TagTypeStore {
constructor(db, eventBus, getLogger) {
this.db = db;
this.logger = getLogger('tag-type-store.js');
this.timer = action =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'tag-type',
action,
});
}
async getAll() {
const stopTimer = this.timer('getTagTypes');
const rows = await this.db.select(COLUMNS).from(TABLE);
stopTimer();
return rows.map(this.rowToTagType);
}
async getTagType(name) {
const stopTimer = this.timer('getTagTypeByName');
return this.db
.first(COLUMNS)
.from(TABLE)
.where({ name })
.then(row => {
stopTimer();
if (!row) {
throw new NotFoundError('Could not find tag-type');
} else {
return this.rowToTagType(row);
}
});
}
async exists(name) {
const stopTimer = this.timer('exists');
const row = await this.db
.first(COLUMNS)
.from(TABLE)
.where({ name });
stopTimer();
return row;
}
async createTagType(newTagType) {
const stopTimer = this.timer('createTagType');
await this.db(TABLE).insert(newTagType);
stopTimer();
}
async deleteTagType(name) {
const stopTimer = this.timer('deleteTagType');
await this.db(TABLE)
.where({ name })
.del();
stopTimer();
}
async updateTagType({ name, description, icon }) {
const stopTimer = this.timer('updateTagType');
await this.db(TABLE)
.where({ name })
.update({ description, icon });
stopTimer();
}
rowToTagType(row) {
return {
name: row.name,
description: row.description,
icon: row.icon,
};
}
}
module.exports = TagTypeStore;