Skip to content

Commit 7057be3

Browse files
committed
improved documentation
1 parent 11b8757 commit 7057be3

File tree

6 files changed

+188
-3
lines changed

6 files changed

+188
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@funktechno/little-mermaid-2-the-sql",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

readme.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,131 @@
99
## Generating Mermaid erDiagram
1010
* see [samples](./samples/readme.md) for more information
1111

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+
12137
## Development
13138

14139
1. git clone repo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"Generate DDL examples Should generate create table SQL syntax": "CREATE TABLE \"Persons\" (\n\t\"City\" varchar(255),\n\t\"Address\" varchar(255),\n\t\"FirstName\" varchar(255),\n\t\"LastName\" varchar(255),\n\t\"PersonID\" int NOT NULL,\n\tPRIMARY KEY(\"PersonID\")\n);\n\nCREATE TABLE \"Orders\" (\n\t\"PersonID\" int NOT NULL,\n\t\"OrderID\" int NOT NULL,\n\tPRIMARY KEY(\"OrderID\")\n);\n\n"
2+
"Generate DDL examples Should generate create table SQL syntax": "CREATE TABLE \"Persons\" (\n\t\"City\" varchar(255),\n\t\"Address\" varchar(255),\n\t\"FirstName\" varchar(255),\n\t\"LastName\" varchar(255),\n\t\"PersonID\" int NOT NULL,\n\tPRIMARY KEY(\"PersonID\")\n);\n\nCREATE TABLE \"Orders\" (\n\t\"PersonID\" int NOT NULL,\n\t\"OrderID\" int NOT NULL,\n\tPRIMARY KEY(\"OrderID\"),\n\tFOREIGN KEY (\"PersonId\") REFERENCES \"Persons\"(\"PersonId\")\n);\n\n"
33
}

tests/src/Library.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { getExpected, updateExpected } from "../utils/helper";
2+
import { GenerateSqlFromMermaid } from "../../src/Library";
3+
import { MarkdownContentResponseI } from "../../src/types";
4+
5+
const dataSource = "GenerateSqlFromMermaid";
6+
7+
describe("Generate DDL examples from markdown", () => {
8+
it("Create DLL", async () => {
9+
const markdownContent:MarkdownContentResponseI ={
10+
settings: {
11+
database: "postgres",
12+
outputName: "result",
13+
isRaw: false,
14+
src: ""
15+
},
16+
content:`
17+
#Test
18+
\`\`\`mermaid
19+
erDiagram
20+
%% comment 1
21+
Persons {
22+
int PersonID PK "NOT NULL"
23+
varchar255 LastName
24+
varchar255 FirstName
25+
varchar255 Address
26+
varchar255 City
27+
}
28+
29+
%% comment 2
30+
Orders {
31+
int OrderID PK "NOT NULL"
32+
int PersonID FK "NOT NULL"
33+
}
34+
35+
Persons ||--o{ Orders : "[Persons.PersonId] to [Orders.PersonId]"
36+
\`\`\`
37+
`
38+
};
39+
// junit compile error for require parser.js export
40+
// const sqlOutputs = GenerateSqlFromMermaid(markdownContent);
41+
42+
// const dataKey = expect.getState().currentTestName || "unknown";
43+
44+
// const expectedResult = await getExpected(dataSource, dataKey);
45+
46+
// if (sqlOutputs != expectedResult) {
47+
// await updateExpected(dataSource, dataKey, sqlOutputs);
48+
// }
49+
50+
// expect(sqlOutputs).toStrictEqual(expectedResult);
51+
});
52+
});
53+

tests/src/generate-sql-ddl.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DbParser } from "../../src/generate-sql-ddl";
22
import erDb from "../../src/mermaid/src/diagrams/er/erDb";
33
import { DbEntityAttributesDefinition, DbRelSpec } from "../../src/types";
44
import { getExpected, updateExpected } from "../utils/helper";
5+
import * as fs from "fs";
56

67
const dataSource = "getSQLDataDefinition";
78
describe("Generate DDL examples", () => {
@@ -59,7 +60,7 @@ describe("Generate DDL examples", () => {
5960
relType: "IDENTIFYING"
6061
};
6162

62-
db.addRelationship("Persons", entityName,`[Persons.PersonId] to [${entityName}.PersonId]`, relSpec);
63+
db.addRelationship("Persons", `[Persons.PersonId] to [${entityName}.PersonId]`,entityName, relSpec);
6364
// `erDiagram artists { }`;
6465
const ddlSyntax = new DbParser("sqlite", db).getSQLDataDefinition();
6566

@@ -74,5 +75,10 @@ describe("Generate DDL examples", () => {
7475
// console.log(result);
7576
expect(ddlSyntax).toStrictEqual(expectedResult);
7677
// expect(ddlSyntax).toBe('CREATE TABLE artists');
78+
79+
// await fs.writeFileSync(
80+
// "output-sqlite.sql",
81+
// ddlSyntax
82+
// );
7783
});
7884
});

0 commit comments

Comments
 (0)