Skip to content

Instantly share code, notes, and snippets.

@officer-rosmarino
Last active September 12, 2018 13:33
Show Gist options
  • Save officer-rosmarino/a09f8a625aa1b142d31b62f22fef747b to your computer and use it in GitHub Desktop.
Save officer-rosmarino/a09f8a625aa1b142d31b62f22fef747b to your computer and use it in GitHub Desktop.
Loopback: Discover a schema in a db.
/*
* Discovers a table schema and outputs it into a file: run this script via:
* $ node discover-schema.js
*/
var path = require('path');
var fs = require('fs');
var app = require('loopback');
var output_directory = path.resolve(__dirname, '..', '..', 'common', 'models');
function callback(err, schema) {
if (err) {
console.error(err);
return;
}
if (typeof schema != 'object') {
throw 'schema object not defined';
}
console.log("Auto discovery for schema " + schema.name);
/*
* Convert schema name from CamelCase to dashed lowercase (loopback format
* for json files describing models), for example: CamelCase -> camel-case.
*/
//var model_name = schema.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
var model_name = mapName(null, schema.name);
console.log('Writing model JSON file..');
// write model definition JSON file
var now_ms = Date.now();
var model_JSON_file = path.join(output_directory, model_name + '.json');
// if JSON file exists
if (fs.existsSync(model_JSON_file)) {
// save a backup copy of the JSON file
let bkp_file = path.join(output_directory, model_name + ".json" + '.bkp_' + now_ms);
fs.renameSync(model_JSON_file, bkp_file);
console.log("Backing up old JSON file..");
}
// write the new JSON file
fs.writeFileSync(
model_JSON_file,
JSON.stringify(schema, null, 2)
);
console.log("JSON saved to " + model_JSON_file);
console.log('Writing model JS file..');
// write model JS file, useful to extend a model with custom methods
var model_JS_file = path.join(output_directory, model_name + '.js');
// if JS file exists
if (fs.existsSync(model_JS_file)) {
// save a backup copy of the JS file
let bkp_file = path.join(output_directory, model_name + ".js" + '.bkp_' + now_ms)
fs.renameSync(model_JS_file, bkp_file);
console.log("Backing up old JS file..");
}
// write the new JS file
fs.writeFileSync(
model_JS_file,
"'use strict';module.exports=function(" + model_name + ") {};"
);
console.log("JSON saved to " + model_JSON_file);
// Append model to model-config.json
var model_config_file = path.resolve(__dirname, '../model-config.json');
var model_config_obj = JSON.parse(fs.readFileSync(model_config_file, 'utf8'));
if (typeof model_config_obj[model_name] === 'undefined') {
let datasource = process.argv[3];
model_config_obj[model_name] = { 'dataSource': datasource, 'public': false };
let json_content = JSON.stringify(model_config_obj, null, 2);
fs.writeFileSync(model_config_file, JSON.stringify(model_config_obj, null, 2));
}
}
function printUsage() {
console.log("\nUsage: node discover-schema.js [-ds datasource -sn db_schema_name]\n" +
"\t-ds datasource: name of the datasource as specified in datasources.json\n" +
"\t-tn db_table_name: name of the table in the db\n");
}
// custom name mapper
function mapName(type, name) {
return name;
};
function main() {
switch (process.argv.length) {
/*
* if there are 6 params (first and second are execPath and JS file being executed)
*/
case 6:
// should be datasource
var param11 = process.argv[2];
var datasource = process.argv[3];
// should be db schema name
var param21 = process.argv[4];
var table_name = process.argv[5];
var datasource_json = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', 'datasources.json'), 'utf8'));
if (param11 === '-ds' && param21 === '-tn' && datasource_json.hasOwnProperty(datasource)) {
options = {};
options.nameMapper = mapName;
var ds = app.createDataSource(datasource, datasource_json[datasource]);
ds.discoverSchema(table_name, options, callback);
} else {
printUsage();
}
break;
default:
printUsage();
}
}
// main function
main();
@artcoders27
Copy link

Thanks.
But, this is what i got : "
Usage: node discover-schema.js [-ds datasource -sn db_schema_name]
-ds datasource: name of the datasource as specified in datasources.json
-tn db_table_name: name of the table in the db"

@prstraderdev
Copy link

Thanks. But it seems like hasOwnProperty returns false everytime.

@jpmyob
Copy link

jpmyob commented Feb 16, 2018

@honestserpent - thx dude. I think this is gonna do what I needed, but im getting

error: '[node-ibm_db] SQL_ERROR',
message: '[IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "LIVE.TBLADDRESS" is not valid in the context where it is used. SQLSTATE=42703\r\n',
state: '42S22'

Where I'm connecting to a DB2 where "live" is the schema and "tbladdress" is the table name.

I have tried w/ and w/o the schema so "live.tbladdress" "tbladdress" and all caps, all lower "LIVE.TBLADDRESS" "TBLADDRESS" - no luck

Any Ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment