-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from nao1215/introduce-er
Introduce mermaid entity relationship diagram
- Loading branch information
Showing
11 changed files
with
849 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## Entity Relationship Diagram | ||
```mermaid | ||
erDiagram | ||
teachers ||--o{ students : "Teacher has many students" | ||
teachers }|..|| schools : "School has many teachers" | ||
schools { | ||
int id PK,UK "School ID" | ||
string name "School Name" | ||
int teacher_id FK,UK "Teacher ID" | ||
} | ||
students { | ||
int id PK,UK "Student ID" | ||
string name "Student Name" | ||
int teacher_id FK,UK "Teacher ID" | ||
} | ||
teachers { | ||
int id PK,UK "Teacher ID" | ||
string name "Teacher Name" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//go:build linux || darwin | ||
|
||
// Package main is generating entity relationship diagram. | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/nao1215/markdown" | ||
"github.com/nao1215/markdown/mermaid/er" | ||
) | ||
|
||
//go:generate go run main.go | ||
|
||
func main() { | ||
f, err := os.Create("generated.md") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
teachers := er.NewEntity( | ||
"teachers", | ||
[]*er.Attribute{ | ||
{ | ||
Type: "int", | ||
Name: "id", | ||
IsPrimaryKey: true, | ||
IsForeignKey: false, | ||
IsUniqueKey: true, | ||
Comment: "Teacher ID", | ||
}, | ||
{ | ||
Type: "string", | ||
Name: "name", | ||
IsPrimaryKey: false, | ||
IsForeignKey: false, | ||
IsUniqueKey: false, | ||
Comment: "Teacher Name", | ||
}, | ||
}, | ||
) | ||
students := er.NewEntity( | ||
"students", | ||
[]*er.Attribute{ | ||
{ | ||
Type: "int", | ||
Name: "id", | ||
IsPrimaryKey: true, | ||
IsForeignKey: false, | ||
IsUniqueKey: true, | ||
Comment: "Student ID", | ||
}, | ||
{ | ||
Type: "string", | ||
Name: "name", | ||
IsPrimaryKey: false, | ||
IsForeignKey: false, | ||
IsUniqueKey: false, | ||
Comment: "Student Name", | ||
}, | ||
{ | ||
Type: "int", | ||
Name: "teacher_id", | ||
IsPrimaryKey: false, | ||
IsForeignKey: true, | ||
IsUniqueKey: true, | ||
Comment: "Teacher ID", | ||
}, | ||
}, | ||
) | ||
schools := er.NewEntity( | ||
"schools", | ||
[]*er.Attribute{ | ||
{ | ||
Type: "int", | ||
Name: "id", | ||
IsPrimaryKey: true, | ||
IsForeignKey: false, | ||
IsUniqueKey: true, | ||
Comment: "School ID", | ||
}, | ||
{ | ||
Type: "string", | ||
Name: "name", | ||
IsPrimaryKey: false, | ||
IsForeignKey: false, | ||
IsUniqueKey: false, | ||
Comment: "School Name", | ||
}, | ||
{ | ||
Type: "int", | ||
Name: "teacher_id", | ||
IsPrimaryKey: false, | ||
IsForeignKey: true, | ||
IsUniqueKey: true, | ||
Comment: "Teacher ID", | ||
}, | ||
}, | ||
) | ||
|
||
erString := er.NewDiagram(f). | ||
Relationship( | ||
teachers, | ||
students, | ||
er.ExactlyOneRelationship, // "||" | ||
er.ZeroToMoreRelationship, // "}o" | ||
er.Identifying, // "--" | ||
"Teacher has many students", | ||
). | ||
Relationship( | ||
teachers, | ||
schools, | ||
er.OneToMoreRelationship, // "|}" | ||
er.ExactlyOneRelationship, // "||" | ||
er.NonIdentifying, // ".." | ||
"School has many teachers", | ||
). | ||
String() | ||
|
||
err = markdown.NewMarkdown(f). | ||
H2("Entity Relationship Diagram"). | ||
CodeBlocks(markdown.SyntaxHighlightMermaid, erString). | ||
Build() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package er | ||
|
||
// config is the configuration for the entity relationship diagram. | ||
type config struct{} | ||
|
||
// newConfig returns a new config. | ||
func newConfig() *config { | ||
return &config{} | ||
} | ||
|
||
// Option sets the options for the PieChart struct. | ||
type Option func(*config) |
Oops, something went wrong.