-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
213 lines (152 loc) · 5.49 KB
/
index.js
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
import { readFileSync, writeFileSync} from 'fs';
import {MongoClient, ObjectId, Long } from 'mongodb';
import { execFileSync } from "child_process";
import * as eta from "eta";
const template = readFileSync("./template.eta").toString();
async function listDatabases(client){
let databasesList = await client.db().admin().listDatabases();
let dbList = databasesList.databases.filter(function (val) {
if (val.name !== "local" && val.name !== "config" && val.name !== "admin" ) {
return val
}
})
console.log("Databases:");
dbList.forEach(db => console.log(` - ${db.name}`));
return dbList;
}
function reduceElement(element, schema) {
for (const key of Object.keys(element)) {
if (!(key in schema)) {
let type = fieldTypeOf(element[key.toString()])
if (type === 'Object') {
schema[key.toString()] = {}
reduceElement(element[key.toString()], schema[key.toString()] )
} else if (type === 'Array') {
if ( (element[key.toString()].length === 0 || element[key.toString()][0] === 'undefined') && !(schema[key.toString()] instanceof Object) ) {
schema[key.toString()] = type + ' - undefined';
} else if (fieldTypeOf(element[key.toString()][0]) === 'Object') {
schema[key.toString()] = {}
reduceDocuments(element[key.toString()], schema[key.toString()] )
} else {
schema[key.toString()] = type + ' - ' + fieldTypeOf(element[key.toString()][0]);
}
} else if (type === 'ObjectId' && key !== "_id" && key.endsWith("Id") && key.startsWith("_")){
// parse name and add to list to check if the table actually exists later
const parsedTableName = key.replace("_","").substring(0, key.length - 3)
if (schema['relations'] === undefined) {
schema['relations'] = []
}
if (! schema['relations'].includes(parsedTableName)) {
schema['relations'].push(parsedTableName)
schema['relations'].push(parsedTableName + "s")
}
} else {
schema[key.toString()] = type
}
} // if
}// for
}
function reduceDocuments(documentList, schema){
for (const doc of documentList) {
reduceElement(doc,schema)
}
}
function fieldTypeOf(thing) {
if (!arguments.length) { throw 'varietyTypeOf() requires an argument'; }
if (typeof thing === 'undefined') {
return 'undefined';
} else if (typeof thing !== 'object') {
// the messiness below capitalizes the first letter, so the output matches
// the other return values below. -JC
const typeofThing = typeof thing; // edgecase of JSHint's "singleGroups"
return typeofThing[0].toUpperCase() + typeofThing.slice(1);
} else {
if (thing && thing.constructor === Array) {
return 'Array';
} else if (thing === null) {
return 'null';
} else if (thing instanceof Date) {
return 'Date';
} else if(thing instanceof Long) {
return 'NumberLong';
} else if (thing instanceof ObjectId) {
return 'ObjectId';
// } else if (thing instanceof BSON.BSONType) {
// const binDataTypes = {};
// binDataTypes[0x00] = 'generic';
// binDataTypes[0x01] = 'function';
// binDataTypes[0x02] = 'old';
// binDataTypes[0x03] = 'UUID';
// binDataTypes[0x04] = 'UUID';
// binDataTypes[0x05] = 'MD5';
// binDataTypes[0x06] = 'Encrypted-BSON';
// binDataTypes[0x07] = 'time-series';
// binDataTypes[0x80] = 'user';
// return 'BinData-' + binDataTypes[thing.subtype()];
} else {
return 'Object'
}
}
}
function checkRelations(schema){
for (const db of Object.keys(schema)) {
for (const table of Object.keys(schema[db])) {
if (schema[db][table]['relations'] !== undefined) {
let removeList = []
for (const relation of schema[db][table]['relations']) {
if (!Object.keys(schema[db]).includes(relation)) {
removeList.push(relation)
}
}
for (const removeListElement of removeList) {
schema[db][table]['relations'].splice(schema[db][table]['relations'].indexOf(removeListElement), 1)
}
}// if
} // for
} // for
}
const args = process.argv.slice(2);
async function getSchema() {
let connectionString = "mongodb://localhost:27017"
if (args[0]){
connectionString = args[0]
}
const client = new MongoClient(connectionString);
await client.connect();
let dbList = await listDatabases(client);
if (dbList.length === 0 ){
console.log("Unable to find any databases to diagram")
process.exit(1);
}
let pipeline = [
{
$sample:
/**
* size: The number of documents to sample.
*/
{
size: 1000,
},
},
]
let schema = {}
for (const database of dbList) {
schema[database.name.toString()] = {};
let collectionList = (await client.db(database.name.toString()).listCollections().toArray()).filter(obj => obj.type === "collection");
for (const collection of collectionList) {
const samples = await client.db(database.name).collection(collection.name).aggregate(pipeline).toArray();
schema[database.name][collection.name] = {}
reduceDocuments(samples, schema[database.name][collection.name]);
}
}
checkRelations(schema)
return schema
}
async function main() {
const schema = await getSchema();
const output = await eta.render(template, {schema});
writeFileSync("output.d2", output);
execFileSync("d2", ["output.d2", "out.svg"]);
process.exit(0);
}
main();