All files / src/lib/db tag-type-store.ts

97.67% Statements 42/43
100% Branches 3/3
92.31% Functions 12/13
97.67% Lines 42/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117      69x 69x 69x     69x 69x               69x               87x 87x 87x 134x             14x 14x 14x 14x       7x 7x         7x 7x 2x   5x           9x 9x       9x 9x 9x       99x 99x 99x       1x 1x 1x       3x 3x 3x       3x         3x 3x           1x 1x 1x           19x               69x  
import { Knex } from 'knex';
import { EventEmitter } from 'events';
import { LogProvider, Logger } from '../logger';
import { DB_TIME } from '../metric-events';
import metricsHelper from '../util/metrics-helper';
import NotFoundError from '../error/notfound-error';
import { ITagType, ITagTypeStore } from '../types/stores/tag-type-store';
 
const COLUMNS = ['name', 'description', 'icon'];
const TABLE = 'tag_types';
 
interface ITagTypeTable {
    name: string;
    description?: string;
    icon?: string;
}
 
export default class TagTypeStore implements ITagTypeStore {
    private db: Knex;
 
    private logger: Logger;
 
    private readonly timer: Function;
 
    constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) {
        this.db = db;
        this.logger = getLogger('tag-type-store.ts');
        this.timer = (action) =>
            metricsHelper.wrapTimer(eventBus, DB_TIME, {
                store: 'tag-type',
                action,
            });
    }
 
    async getAll(): Promise<ITagType[]> {
        const stopTimer = this.timer('getTagTypes');
        const rows = await this.db.select(COLUMNS).from(TABLE);
        stopTimer();
        return rows.map(this.rowToTagType);
    }
 
    async get(name: string): Promise<ITagType> {
        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: string): Promise<boolean> {
        const stopTimer = this.timer('exists');
        const result = await this.db.raw(
            `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
            [name],
        );
        const { present } = result.rows[0];
        stopTimer();
        return present;
    }
 
    async createTagType(newTagType: ITagType): Promise<void> {
        const stopTimer = this.timer('createTagType');
        await this.db(TABLE).insert(newTagType);
        stopTimer();
    }
 
    async delete(name: string): Promise<void> {
        const stopTimer = this.timer('deleteTagType');
        await this.db(TABLE).where({ name }).del();
        stopTimer();
    }
 
    async deleteAll(): Promise<void> {
        const stopTimer = this.timer('deleteAll');
        await this.db(TABLE).del();
        stopTimer();
    }
 
    async bulkImport(tagTypes: ITagType[]): Promise<ITagType[]> {
        const rows = await this.db(TABLE)
            .insert(tagTypes)
            .returning(COLUMNS)
            .onConflict('name')
            .ignore();
        if (rows.length > 0) {
            return rows;
        }
        return [];
    }
 
    async updateTagType({ name, description, icon }: ITagType): Promise<void> {
        const stopTimer = this.timer('updateTagType');
        await this.db(TABLE).where({ name }).update({ description, icon });
        stopTimer();
    }
 
    destroy(): void {}
 
    rowToTagType(row: ITagTypeTable): ITagType {
        return {
            name: row.name,
            description: row.description,
            icon: row.icon,
        };
    }
}
 
module.exports = TagTypeStore;