All files / lib index.ts

85.25% Statements 52/61
64.29% Branches 9/14
85.71% Functions 6/7
85.25% Lines 52/61

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 621x 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 1x 1x 10x   1x 1x 1x  
/**
 * @file 文件系统数据库入口脚本
 * @Author wangjie19
 * @Date 2020-07-22 17:17:38
 * @Last Modified by: wangjie19
 * @Last Modified time: 2020-07-28 17:08:33
 */
 
import path from 'path';
import fs from 'fs';
import DB from './db';
import store from './redux/store';
import { deleteDir } from './util';
 
class FileDB {
    constructor() {}
 
    /**
     * 创建数据库
     * @param name 数据库名
     * @return 数据库对象
     */
    createDB(): DB {
        const state = store.getState();
        if (!fs.existsSync(path.resolve(state.root, state.db))) {
            fs.mkdirSync(path.resolve(state.root, state.db));
        }
        return new DB();
    }
 
    /**
     * 删除数据库
     * @param name 数据库名
     * @return 文件数据库主对象
     */
    deleteDB(name: string = ''): FileDB {
        const state = store.getState();
        deleteDir(path.resolve(state.root, name));
        return this;
    }
 
    /**
     * 获取所有数据库
     */
    dbs(): Array<string> {
        const state = store.getState();
        const dbPaths = fs.readdirSync(state.root);
        return dbPaths.filter(it => it !== '.gitkeep');
    }
 
    /**
     * 获取数据库
     * @param name 数据库名
     * @return 对应数据库对象/错误对象
     */
    db(): DB {
        return new DB();
    }
}
 
export default FileDB;