UNPKG

1.31 kBJavaScriptView Raw
1/**
2 * Created by mac on 16/8/10.
3 */
4const fs = require('fs');
5const func = require('../common/func');
6
7module.exports = function (tables, markdown) {
8 var text = '# 数据库文档\n\n';
9 const tableHeader =
10 '|字段|类型|允许为空|是否主键|是否自增|说明|\n' +
11 '|:---:|:---:|:---:|:---:|:---:|:---:|\n';
12
13 //对表名字进行排序
14 var tablesArray = Object.keys(tables);
15 func.quickSort(tablesArray, 0, tablesArray.length - 1);
16
17 //分别处理每个表
18 for (var table of tablesArray) {
19 text = text + '## ' + table + '\n';
20 text += tableHeader;
21 //分别处理表的每个字段
22 for (var field in tables[table]) {
23 text = text + '|' + field;
24 //分别处理字段的每个属性
25 var property = tables[table][field];
26 text = text +
27 '|' + func.typeTransform(property.type) +
28 '|' + func.spaceToFalse(property.allowNull) +
29 '|' + func.spaceToFalse(property.primaryKey) +
30 '|' + func.spaceToFalse(property.autoIncrement) +
31 '|' + func.spaceToFalse(property.description);
32 text += '|\n'
33 }
34 text += '\n'
35 }
36
37 //写入文件
38 fs.writeFile(markdown.path + markdown.file , text, function (err) {
39 if (err) throw err;
40 console.log('It\'s saved!'); //文件被保存
41 return true;
42 });
43};
44
45