|
9 | 9 | ## Generating Mermaid erDiagram |
10 | 10 | * see [samples](./samples/readme.md) for more information |
11 | 11 |
|
| 12 | +## Ala Cart |
| 13 | +1. `npm install --save @funktechno/little-mermaid-2-the-sql` |
| 14 | +1. Example |
| 15 | + * from markdown content |
| 16 | + ```typescript |
| 17 | + import { GenerateSqlFromMermaid } from "@funktechno/little-mermaid-2-the-sql/lib/src/Library"; |
| 18 | + import { MarkdownContentResponseI } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; |
| 19 | + |
| 20 | + |
| 21 | + const markdownContent:MarkdownContentResponseI ={ |
| 22 | + settings: { |
| 23 | + database: "postgres", |
| 24 | + outputName: "result", |
| 25 | + isRaw: false, |
| 26 | + src: "" |
| 27 | + }, |
| 28 | + content:` |
| 29 | + #Test |
| 30 | + \`\`\`mermaid |
| 31 | + erDiagram |
| 32 | + %% comment 1 |
| 33 | + Persons { |
| 34 | + int PersonID PK "NOT NULL" |
| 35 | + varchar255 LastName |
| 36 | + varchar255 FirstName |
| 37 | + varchar255 Address |
| 38 | + varchar255 City |
| 39 | + } |
| 40 | +
|
| 41 | + %% comment 2 |
| 42 | + Orders { |
| 43 | + int OrderID PK "NOT NULL" |
| 44 | + int PersonID FK "NOT NULL" |
| 45 | + } |
| 46 | + |
| 47 | + Persons ||--o{ Orders : "[Persons.PersonId] to [Orders.PersonId]" |
| 48 | + \`\`\` |
| 49 | + ` |
| 50 | + }; |
| 51 | + |
| 52 | + const sqlOutputs = GenerateSqlFromMermaid(markdownContent); |
| 53 | + ``` |
| 54 | + |
| 55 | + * manual mermaid db model |
| 56 | + ```typescript |
| 57 | + import { DbParser } from "@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl"; |
| 58 | + import erDb from "@funktechno/little-mermaid-2-the-sql/lib/src/mermaid/src/diagrams/er/erDb"; |
| 59 | +
|
| 60 | + const db = erDb; |
| 61 | +
|
| 62 | + let entityName = "Persons"; |
| 63 | + // load mermaid db with entities |
| 64 | + db.addEntity(entityName); |
| 65 | + let attributes:DbEntityAttributesDefinition[] = [ |
| 66 | + { |
| 67 | + attributeName: "PersonID", |
| 68 | + attributeKeyType: "PK", |
| 69 | + attributeType: "int", |
| 70 | + attributeComment: "NOT NULL" |
| 71 | + }, |
| 72 | + { |
| 73 | + attributeName: "LastName", |
| 74 | + attributeType: "varchar255" |
| 75 | + }, |
| 76 | + { |
| 77 | + attributeName: "FirstName", |
| 78 | + attributeType: "varchar255" |
| 79 | + }, |
| 80 | + { |
| 81 | + attributeName: "Address", |
| 82 | + attributeType: "varchar255" |
| 83 | + }, |
| 84 | + { |
| 85 | + attributeName: "City", |
| 86 | + attributeType: "varchar255" |
| 87 | + } |
| 88 | + ]; |
| 89 | + db.addAttributes(entityName,attributes); |
| 90 | +
|
| 91 | + entityName = "Orders"; |
| 92 | + db.addEntity(entityName); |
| 93 | + attributes = [ |
| 94 | + { |
| 95 | + attributeName: "OrderID", |
| 96 | + attributeKeyType: "PK", |
| 97 | + attributeType: "int", |
| 98 | + attributeComment: "NOT NULL" |
| 99 | + }, |
| 100 | + { |
| 101 | + attributeName: "PersonID", |
| 102 | + attributeKeyType: "FK", |
| 103 | + attributeType: "int", |
| 104 | + attributeComment: "NOT NULL" |
| 105 | + } |
| 106 | + ]; |
| 107 | + db.addAttributes(entityName,attributes); |
| 108 | +
|
| 109 | + const relSpec:DbRelSpec= { |
| 110 | + cardA: "ZERO_OR_MORE", |
| 111 | + cardB: "ONLY_ONE", |
| 112 | + relType: "IDENTIFYING" |
| 113 | + }; |
| 114 | +
|
| 115 | + db.addRelationship("Persons", `[Persons.PersonId] to [${entityName}.PersonId]`, entityName, relSpec); |
| 116 | + const ddlSyntax = new DbParser('sqlite', db).getSQLDataDefinition(); |
| 117 | + ``` |
| 118 | + * sql output |
| 119 | + ```sql |
| 120 | + CREATE TABLE "Persons" ( |
| 121 | + "City" varchar(255), |
| 122 | + "Address" varchar(255), |
| 123 | + "FirstName" varchar(255), |
| 124 | + "LastName" varchar(255), |
| 125 | + "PersonID" int NOT NULL, |
| 126 | + PRIMARY KEY("PersonID") |
| 127 | + ); |
| 128 | +
|
| 129 | + CREATE TABLE "Orders" ( |
| 130 | + "PersonID" int NOT NULL, |
| 131 | + "OrderID" int NOT NULL, |
| 132 | + PRIMARY KEY("OrderID"), |
| 133 | + FOREIGN KEY ("PersonId") REFERENCES "Persons"("PersonId") |
| 134 | + ); |
| 135 | + ``` |
| 136 | + |
12 | 137 | ## Development |
13 | 138 |
|
14 | 139 | 1. git clone repo |
|
0 commit comments