1+ import { dbToTypes } from "../../data/datatypes" ;
2+ import { jsonToMermaid } from "./mermaid" ;
3+
4+ export function jsonToDocumentation ( obj ) {
5+
6+ const documentationSummary = obj . tables
7+ . map ( ( table ) => {
8+ return `\t- [${ table . name } ](#${ table . name } )\n` ;
9+ } ) . join ( "" ) ;
10+
11+ const documentationEntities = obj . tables
12+ . map ( ( table ) => {
13+ const fields = table . fields
14+ . map ( ( field ) => {
15+ const fieldType =
16+ field . type +
17+ ( ( dbToTypes [ obj . database ] [ field . type ] . isSized ||
18+ dbToTypes [ obj . database ] [ field . type ] . hasPrecision ) &&
19+ field . size &&
20+ field . size !== ""
21+ ? "(" + field . size + ")"
22+ : "" ) ;
23+ return `| **${ field . name } ** | ${ fieldType } | ${ field . primary ? "🔑 PK, " : "" } ` +
24+ `${ field . nullable ? "null " : "not null " } ${ field . unique ? ", unique" : "" } ${ field . increment ?", autoincrement" :"" } ` +
25+ `${ field . default ? `, default: ${ field . default } ` : "" } | |${ field . comment ? field . comment : "" } |` ;
26+
27+ } ) . join ( "\n" ) ;
28+ return `### ${ table . name } \n${ table . comment ? table . comment : "" } \n` +
29+ `| Name | Type | Settings | References | Note |\n` +
30+ `|-------------|---------------|-------------------------------|-------------------------------|--------------------------------|\n` +
31+ `${ fields } \n\n` ;
32+ } ) . join ( "" ) ;
33+
34+ const documentationRelationships = obj . relationships ?. length
35+ ? obj . relationships
36+ . map ( ( r ) => {
37+ console . log ( r ) ;
38+ const startTable = obj . tables [ r . startTableId ] . name ;
39+ const endTable = obj . tables [ r . endTableId ] . name ;
40+ return `- **${ startTable } to ${ endTable } **: ${ r . cardinality } (${ r . comment ? r . comment : "" } )\n` ;
41+ } ) . join ( "" ) : "" ;
42+
43+ return `# ${ obj . title } Database Documentation\n## Summary\n- [Introduction](#introduction)\n- [Database Type](#database-type)\n` +
44+ `- [Table Structure](#table-structure)\n${ documentationSummary } \n- [Relationships](#relationships)\n- [Database Diagram](#database-Diagram)\n` +
45+ `## Introduction\n${ obj . notes } \n## Database type\n- **Database system:** ` +
46+ `${ obj . database . type } \n## Table structure\n\n${ documentationEntities } ` +
47+ `\n\n## Relationships\n${ documentationRelationships } \n\n` +
48+ `## Database Diagram\n\`\`\`${ jsonToMermaid ( obj ) } \`\`\`` ;
49+ }
0 commit comments