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

83.33% Statements 15/18
100% Branches 0/0
66.67% Functions 6/9
83.33% Lines 15/18

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              69x 69x                             87x 87x       9x 9x       45x                           1x 1x       1x                   1x       1x 1x     69x 69x  
import { Knex } from 'knex';
import { Logger, LogProvider } from '../logger';
import {
    IFeatureType,
    IFeatureTypeStore,
} from '../types/stores/feature-type-store';
 
const COLUMNS = ['id', 'name', 'description', 'lifetime_days'];
const TABLE = 'feature_types';
 
interface IFeatureTypeRow {
    id: number;
    name: string;
    description: string;
    lifetime_days: number;
}
 
class FeatureTypeStore implements IFeatureTypeStore {
    private db: Knex;
 
    private logger: Logger;
 
    constructor(db: Knex, getLogger: LogProvider) {
        this.db = db;
        this.logger = getLogger('feature-type-store.ts');
    }
 
    async getAll(): Promise<IFeatureType[]> {
        const rows = await this.db.select(COLUMNS).from(TABLE);
        return rows.map(this.rowToFeatureType);
    }
 
    private rowToFeatureType(row: IFeatureTypeRow): IFeatureType {
        return {
            id: row.id,
            name: row.name,
            description: row.description,
            lifetimeDays: row.lifetime_days,
        };
    }
 
    async get(id: number): Promise<IFeatureType | undefined> {
        const row = await this.db(TABLE).where({ id }).first();
        return this.rowToFeatureType(row);
    }
 
    async getByName(name: string): Promise<IFeatureType> {
        const row = await this.db(TABLE).where({ name }).first();
        return this.rowToFeatureType(row);
    }
 
    async delete(key: number): Promise<void> {
        await this.db(TABLE).where({ id: key }).del();
    }
 
    async deleteAll(): Promise<void> {
        await this.db(TABLE).del();
    }
 
    destroy(): void {}
 
    async exists(key: number): Promise<boolean> {
        const result = await this.db.raw(
            `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
            [key],
        );
        const { present } = result.rows[0];
        return present;
    }
}
export default FeatureTypeStore;
module.exports = FeatureTypeStore;