-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.html
242 lines (206 loc) · 5.55 KB
/
schema.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!doctype html>
<head>
<title>Mongo Schema</title>
<style>
svg {
background-color: white;
font-family: "Helvetic Neue", Helvetica, Arial, sans-serif;
font-size: small;
}
.node rect,
.node circle,
.node ellipse {
stroke: #333;
opacity: 0.8;
fill: #fff;
fill-opacity: 0.6;
}
.edgePath path {
stroke: #333;
fill: #333;
fill-opacity: 1;
stroke-opacity: 1;
}
.cluster {
stroke: #999;
fill: #888;
fill-opacity: 0.3;
stroke-opacity: 0.6;
}
.entity-name rect {
fill: #08f;
fill-opacity: 0.3;
}
</style>
</head>
<body>
<div class="background"></div>
<div class="container">
<svg></svg>
</div>
<canvas width="1024" height="1024" style="display:none"></canvas>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dagre-d3/0.4.17/dagre-d3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script>
var drawERM = function(data) {
var width = window.innerWidth,
initialHeight = window.innerHeight,
svg = d3.select("body div.container svg"),
inner = svg.append("g");
svg.attr('width', width).attr('height', initialHeight);
var resizeGraph = function(doCenter) {
var newWidth = window.innerWidth;
var newHeight = window.innerHeight;
if(newWidth < g.graph().width + 40) {
newWidth = g.graph().width + 40;
}
if(newHeight < g.graph().height + 40) {
newHeight = g.graph().height + 40;
}
svg.attr('width', newWidth).attr('height', newHeight);
// Center the graph
if(doCenter) {
zoom
.translate([(svg.attr("width") - g.graph().width * initialScale) / 2, 20])
.scale(initialScale)
.event(svg);
}
}
// Set up zoom support
var zoom = d3.behavior.zoom().on("zoom", function() {
inner.attr("transform", "translate(" + d3.event.translate + ")" +
"scale(" + d3.event.scale + ")");
});
svg.call(zoom);
// create graph
var g = new dagreD3.graphlib.Graph({
multigraph: false,
compound: true
}).setGraph({
rankdir: "LR",
edgesep: 25,
nodesep: 0
});
var links = [];
var addField = function(collectionName, fieldName, fieldInfo, parentFieldName) {
var nodeName = parentFieldName ? collectionName + "_" + parentFieldName + "_" + fieldName : collectionName + "_" + fieldName;
g.setNode(nodeName, {
label: (parentFieldName ? parentFieldName + "." + fieldName : fieldName) + ": " + fieldInfo.type,
width: 300
});
g.setParent(nodeName, "___" + collectionName + "_container");
if(fieldInfo.foreignKey && fieldInfo.references) {
links.push({
from: nodeName,
to: fieldInfo.references + "__id"
});
}
if(fieldInfo.type == "Object" && fieldInfo.structure) {
for(var subFieldName in fieldInfo.structure) {
addField(collectionName, subFieldName, fieldInfo.structure[subFieldName], parentFieldName ? parentFieldName + "." + fieldName : fieldName);
}
}
};
var addCollection = function(collectionName, collectionInfo) {
g.setNode("___" + collectionName + "_container", {
label: ""
});
g.setNode("___" + collectionName + "_title", {
label: collectionName,
class: 'entity-name',
labelStyle: 'font-weight:bold;',
width: 300
});
g.setParent("___" + collectionName + "_title", "___" + collectionName + "_container");
for(var fieldName in collectionInfo) {
addField(collectionName, fieldName, collectionInfo[fieldName]);
}
};
for(var collectionName in data) {
addCollection(collectionName, data[collectionName]);
}
links.map(function(link) {
g.setEdge(link.from, link.to, {
label: '',
lineInterpolate: 'monotone'
});
});
// Create the renderer
var render = dagreD3.render();
// Run the renderer. This is what draws the final graph.
render(inner, g);
// adjust height
var initialScale = 1; //0.75;
window.addEventListener('resize', function () {
resizeGraph();
});
resizeGraph(true);
};
var data = {
"users": {
"_id": {
"primaryKey": true,
"type": "Object",
"required": true
},
"balance": {
"type": "number",
"required": true
},
"name": {
"type": "string",
"required": true
},
"email": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"tokens": {
"type": "Array",
"required": true
},
"createdAt": {
"type": "Date",
"required": true
},
"updatedAt": {
"type": "Date",
"required": true
},
"__v": {
"type": "number",
"required": true
}
}
};
drawERM(data);
$("canvas").attr("width", $("svg").width());
$("canvas").attr("height", $("svg").height());
var st = $(document.createElement("style"));
st.text("/* <![CDATA[ */" + $("style").text() + "/* ]]> */");
$("svg").append(st);
var html = d3.select("svg")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML;
var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html);
var canvas = document.querySelector("canvas"),
context = canvas.getContext("2d");
var image = new Image;
image.src = imgsrc;
image.onload = function() {
context.drawImage(image, 0, 0);
var canvasdata = canvas.toDataURL("image/png");
var a = document.createElement("a");
a.download = "diagram.png";
a.href = canvasdata;
document.body.appendChild(a);
a.click();
};
</script>
</body>