Data model dump tool for genieacs-sim
This commit is contained in:
1 parent
1e9aff2fdc
commit
396a687fa2
1 file changed
+73
Executable
+73
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const outputFilename = path.resolve(`data_model_${process.argv[2]}.json`);
|
||||
process.chdir(path.resolve(path.dirname(fs.realpathSync(__filename)), ".."));
|
||||
|
||||
const db = require("../lib/db");
|
||||
|
||||
const dataModel = {};
|
||||
|
||||
|
||||
function recursive(object, path) {
|
||||
if (!path)
|
||||
path = "";
|
||||
|
||||
for (let key in object) {
|
||||
if (key[0] === "_" || !object[key])
|
||||
continue;
|
||||
|
||||
let isObject = object[key]["_object"];
|
||||
let isValue = "_value" in object[key];
|
||||
|
||||
if ((isObject && isValue) || (!isObject && !isValue))
|
||||
throw new Error("Unexpected object");
|
||||
|
||||
if (isValue) {
|
||||
let value = object[key]["_value"];
|
||||
if (typeof value === "boolean")
|
||||
value = +value;
|
||||
|
||||
let values = [object[key]["_writable"] || false, value.toString()];
|
||||
if (object[key]["_type"])
|
||||
values.push(object[key]["_type"]);
|
||||
|
||||
dataModel[`${path}${key}`] = values;
|
||||
}
|
||||
|
||||
if (isObject) {
|
||||
let values = [object[key]["_writable"] || false];
|
||||
dataModel[`${path}${key}.`] = values;
|
||||
recursive(object[key], `${path}${key}.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
db.connect(function(err) {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
return db.devicesCollection.findOne({
|
||||
_id: process.argv[2]
|
||||
}, {}, function(err, device) {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
if (!device)
|
||||
throw new Error("Device not found");
|
||||
|
||||
recursive(device);
|
||||
|
||||
fs.writeFile(outputFilename, JSON.stringify(dataModel, null, 2), function() {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
console.log(`${outputFilename} saved`);
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user