forked from perak/extract-mongo-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
150 lines (123 loc) · 4.24 KB
/
cli.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
#! /usr/bin/env node
const commandLineArgs = require("command-line-args");
const fs = require("fs");
const path = require("path");
const replaceExt = require("replace-ext");
const extractMongoSchema = require("./extract-mongo-schema");
const optionDefinitions = [
{ name: "database", alias: "d", type: String },
{ name: "output", alias: "o", type: String },
{ name: "format", alias: "f", type: String },
{ name: "collection", alias: "c", type: String },
{ name: "array", alias: "a", type: String },
{ name: "raw", alias: "r", type: Boolean, defaultValue: false },
{ name: "limit", alias: "l", type: Number, defaultValue: 100 },
{ name: "dont-follow-fk", alias: "n", multiple: true, type: String }
];
const args = commandLineArgs(optionDefinitions);
console.log("");
console.log("Extract schema from Mongo database (including foreign keys)");
var printUsage = function() {
console.log("");
console.log("Usage:");
console.log("\textract-mongo-schema -d connection_string -o schema.json");
console.log("\t\t-d, --database string\tDatabase connection string. Example: \"mongodb://localhost:3001/meteor\".");
console.log("\t\t-o, --output string\tOutput file");
console.log("\t\t-f, --format string\tOutput file format. Can be \"json\" or \"html-diagram\".");
console.log("\t\t-c, --collection\tComma separated list of collections to analyze. Example: \"collection1,collection2\".");
console.log("\t\t-a, --array\tComma separated list of types of arrays to analyze. Example: \"Uint8Array,ArrayBuffer,Array\".");
console.log("\t\t-r, --raw\tShows the exact list of types with frequency instead of the most frequent type only.");
console.log("\t\t-l, --limit\tChanges the amount of items to parse from the collections. Default is 100.");
console.log("\t\t-n, --dont-follow-fk string\tDon't follow specified foreign key. Can be simply \"fieldName\" (all collections) or \"collectionName:fieldName\" (only for given collection).");
console.log("");
console.log("Enjoy! :)");
console.log("");
};
if(!args.database) {
console.log("");
console.log("Database connection string is missing.");
printUsage();
process.exit(1);
}
if(!args.output) {
console.log("");
console.log("Output path is missing.");
printUsage();
process.exit(1);
}
if(fs.existsSync(args.output)) {
var outputStat = fs.lstatSync(args.output);
if(outputStat.isDirectory()) {
console.log("Error: output \"" + args.output + "\" is not a file.");
process.exit(1);
}
}
var collectionList = null;
if(args.collection) {
collectionList = args.collection.split(",");
}
var arrayList = null;
if(args.array) {
arrayList = args.array.split(",");
}
var outputFormat = args.format || "json";
var dontFollowTMP = args["dont-follow-fk"] || [];
var dontFollowFK = {
__ANY__: {}
};
dontFollowTMP.map(function(df) {
var dfArray = df.split(":");
var collection = "";
var field = "";
if(dfArray.length > 1) {
collection = dfArray[0];
field = dfArray[1];
} else {
collection = "__ANY__";
field = dfArray[0];
}
dontFollowFK[collection][field] = true;
});
console.log("");
console.log("Extracting...");
var opts = {
collectionList: collectionList,
arrayList:arrayList,
raw: args.raw,
limit: args.limit,
dontFollowFK: dontFollowFK
};
extractMongoSchema.extractMongoSchema(args.database, opts, function(err, schema) {
if(err) {
console.log(err);
process.exit(1);
}
if(outputFormat === "json") {
try {
fs.writeFileSync(args.output, JSON.stringify(schema, null, "\t"), "utf8");
} catch(e) {
console.log("Error: cannot write output \"" + args.output + "\". " + e.message);
process.exit(1);
}
}
if(outputFormat === "html-diagram") {
var templateFileName = path.join(__dirname, "/template-html-diagram.html");
// read input file
var templateHTML = "";
try {
templateHTML = fs.readFileSync(templateFileName, "utf8");
} catch(e) {
console.log("Error: cannot read template file \"" + templateFileName + "\". " + e.message);
process.exit(1);
}
templateHTML = templateHTML.replace("{/*DATA_HERE*/}", JSON.stringify(schema, null, "\t"));
try {
fs.writeFileSync(args.output, templateHTML, "utf8");
} catch(e) {
console.log("Error: cannot write output \"" + args.output + "\". " + e.message);
process.exit(1);
}
}
console.log("Success.");
console.log("");
});