All files / lib table.ts

81.44% Statements 79/97
56% Branches 14/25
91.67% Functions 11/12
81.44% Lines 79/97

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 981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 1x 1x 2x 2x 2x 2x   1x 1x 1x 1x 1x 1x 12x 12x 12x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x   1x 1x 1x  
/**
 * @file 表脚本
 * @Author wangjie19
 * @Date 2020-07-23 15:21:19
 * @Last Modified by: wangjie19
 * @Last Modified time: 2020-07-28 20:31:43
 */
 
import fs from "fs";
import path from 'path';
import store from './redux/store';
 
class Table {
    protected name: string;
    protected list: Array<object>
    constructor(name: string) {
        this.name = name;
        this.list = this.data();
    }
 
    private write(list: object[]): Table {
        const state = store.getState();
        const tablePath = path.resolve(state.root, state.db, `${this.name}.json`);
        fs.writeFileSync(tablePath, JSON.stringify(list), {encoding: 'utf-8'});
        return this;
    }
 
    /**
     * 表数据
     * @return 表数据集合
     */
    data(): object[] {
        const state = store.getState();
        const tablePath = path.resolve(state.root, state.db, `${this.name}.json`);
        return JSON.parse(fs.readFileSync(tablePath, {encoding: 'utf-8'}));
    }
 
    /**
     * 条件查询
     * @param field 查询字段
     * @return 查询结果对象
     */
    where(field: any): object[] {
        const list = this.data();
        return list.filter((item: any) => {
            return Object.keys(field).every(key => {
                return item[key] === field[key];
            });
        });
    }
 
    /**
     * 插入数据
     * @param row 行数据
     * @return 表对象
     */
    insert(row: object = {}): Table {
        const list = this.data();
        list.push(row);
        this.write(list);
        return this;
    }
 
    /**
     * 删除数据
     * @return 删除后的表对象
     */
    deleterow(field: any): Table {
        const list = this.data();
        const newList = list.filter((item: any) => {
            return !Object.keys(field).every(key => {
                return item[key] === field[key];
            });
        });
        this.write(newList);
        return this;
    }
 
    /**
     * 更新表数据
     * @param row 行数据
     */
    update(field: any, row: object = {}): Table {
        const list = this.data();
        const newList = list.map((item: any, index) => {
            const isSome = Object.keys(field).every(key => {
                return item[key] === field[key];
            });
            item = isSome ? row : item;
            return item;
        });
        this.write(newList);
        return this;
    }
}
 
export default Table;