Skip to content

Commit

Permalink
Merge pull request #1 from dalmatele/excel
Browse files Browse the repository at this point in the history
Support xlsx format
  • Loading branch information
dalmatele authored Jan 2, 2021
2 parents 23ebe63 + 91bd058 commit ebdeecf
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 3 deletions.
Binary file added .DS_Store
Binary file not shown.
44 changes: 43 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const commandLineArgs = require('command-line-args');
const fs = require('fs');
const path = require('path');
const extractMongoSchema = require('./extract-mongo-schema');
const xlsx = require("xlsx");

const optionDefinitions = [
{ name: 'database', alias: 'd', type: String },
Expand Down Expand Up @@ -37,7 +38,7 @@ const printUsage = function () {
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-f, --format string\tOutput file format. Can be "json", "html-diagram" or "xlsx".');
console.log('\t\t-i, --inputJson string\tInput JSON file, to be used instead of --database. NOTE: this will ignore the remainder of input params and use a previously generated JSON file to generate the 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".');
Expand Down Expand Up @@ -175,6 +176,47 @@ const opts = {
process.exit(1);
}
}
if(outputFormat == "xlsx"){
console.log(args.output);
if(!args.output.endsWith(".xlsx")){
console.log("Wrong output format [xlsx]");
process.exit(1);
}
//get all collections
var collections = Object.keys(schema);
var wb = xlsx.utils.book_new();
//one worksheet per collection
collections.forEach(element => {
var wsName = element;

// console.log(element);
var wsData = [["Collection", "primaryKey", "type", "structure", "require"]];
var items = Object.keys(schema[element]);//items in collection
items.forEach( item => {
var props = Object.keys(schema[element][item]);
var itemProperties = {
primaryKey: schema[element][item]["primaryKey"] != "undefined" ? schema[element][item]["primaryKey"] == "undefined" : false,
type: schema[element][item]["type"] != "undefined" ? schema[element][item]["type"] : "undefined",
structure: schema[element][item]["structure"] != "undefined" ? schema[element][item]["structure"] : "undefined",
require: schema[element][item]["required"] != "undefined" ? schema[element][item]["required"] : "undefined"
};
if(itemProperties.type != "undefined" && itemProperties.type == "Object"){
itemProperties.structure = JSON.stringify(itemProperties.structure);
}
var data = [];
data.push(item);
data.push(itemProperties.primaryKey);
data.push(itemProperties.type);
data.push(itemProperties.structure);
data.push(itemProperties.require);
wsData.push(data);
});
// console.log(wsData);
var ws = xlsx.utils.aoa_to_sheet(wsData);
xlsx.utils.book_append_sheet(wb, ws, wsName);
});
xlsx.writeFile(wb, args.output);
}

console.log('Success.');
console.log('');
Expand Down
106 changes: 105 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"command-line-args": "^3.0.5",
"fs": "0.0.1-security",
"mongodb": "^3.5.8",
"path": "^0.12.7"
"path": "^0.12.7",
"xlsx": "^0.16.9"
},
"devDependencies": {
"eslint": "^5.14.0",
Expand Down
8 changes: 8 additions & 0 deletions tests/createXlsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const xlsx = require("xlsx");

var wb = xlsx.utils.book_new();
var wsName = "SheetJS";
var wsData = [["a", "b", "c"], ["1", "2", "3"]];
var ws = xlsx.utils.aoa_to_sheet(wsData);
xlsx.utils.book_append_sheet(wb, ws, wsName);
xlsx.writeFile(wb, "out.xlsx");
Binary file added ~$text.xlsx
Binary file not shown.

0 comments on commit ebdeecf

Please sign in to comment.