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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @file 数据库脚本
* @Author wangjie19
* @Date 2020-07-23 15:56:39
* @Last Modified by: wangjie19
* @Last Modified time: 2020-07-27 17:49:26
*/
import path from 'path';
import fs from 'fs';
import store from './redux/store';
import Table from "./table";
import { IConfig } from './interface';
class DB {
constructor() {
}
/**
* 设置表集合
* @return 表集合
*/
private getTables(): string[] {
const state = store.getState();
const tablePath = path.resolve(state.root, state.db);
const tables = fs.readdirSync(tablePath);
return tables.map(tb => tb.replace(path.extname(tb), ''));
}
/**
* 创建表
* @param name 表名
* @param columns 字段集合
* @return 表对象
*/
create(name: string = ''): Table {
const state = store.getState();
const tablePath = path.resolve(state.root, state.db, `${name}.json`);
fs.writeFileSync(tablePath, '[]');
return new Table(name);
}
/**
* 删除表
* @param name 表名
* @return 删除的表结果
*/
drop(name = ''): boolean {
const state = store.getState();
const tablePath = path.resolve(state.root, state.db, `${name}.json`);
fs.unlinkSync(tablePath);
return true;
}
/**
* 获取表
* @param name 表名
* @return 对应表
*/
table(name: string = ''): Table | Error {
const tables = this.getTables();
if (tables.includes(name)) {
return new Table(name);
}
else {
return new Error('表不存在');
}
}
}
export default DB;
|