Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Last active September 5, 2022 02:02
Show Gist options
  • Save Edudjr/995a119143441e723855cf6c1dff6188 to your computer and use it in GitHub Desktop.
Save Edudjr/995a119143441e723855cf6c1dff6188 to your computer and use it in GitHub Desktop.
A script for loopback that automatically creates the folders and models from a database in postgresql. It also exposes the models to the Explorer API.
{
"dbname": {
"host": "localhost",
"port": 5432,
"database": "mydatabase",
"password": "user123",
"name": "dbname",
"user": "user",
"connector": "postgresql"
}
}
/**
* Put this file in the root directory for your project and it should work.
* run: node loopback-discover-database.js
*
*/
var fs = require('fs');
var loopback = require('loopback');
var commonFolder = './common';
var modelsFolder = commonFolder+'/models';
//TODO: Change to the correct path for your server folder if needed
var serverPath = './server/';
var modelConfig = require(serverPath + 'model-config');
var dataSources = require(serverPath + 'datasources');
//TODO: dataSourceName should be the name of your datasource in server/datasources.json
var dataSourceName = 'dbname';
var ds = loopback.createDataSource('postgresql', dataSources[dataSourceName]);
initMain();
function initMain(){
// Check if common/models exists
// If not, create them and build models from database
if (!fs.existsSync(commonFolder)){
fs.mkdirSync(commonFolder);
}
if (!fs.existsSync(modelsFolder)){
fs.mkdirSync(modelsFolder);
}
discoverAndCreate();
}
function discoverAndCreate(callback){
//Will print the schema of the database
ds.discoverModelDefinitions(function (err, models) {
models.forEach(function (def, index, array) {
// def.name ~ the model name
ds.discoverSchema(def.name, null, function (err, schema) {
schema.name = schema.name.toLowerCase();
fs.writeFile('common/models/'+def.name+'.json', prettyJSON(schema), function(err){
if (err) throw err;
console.log('It\'s saved!');
//If last, then save
if(index === array.length - 1){
saveAndExposeSchemas();
}
});
addSchema(schema.name);
});
});
});
}
function addSchema(schema){
modelConfig[schema] = {
dataSource: dataSourceName,
public: true,
$promise: {},
$resolved: true
};
}
function saveAndExposeSchemas(){
fs.writeFile('server/model-config.json', prettyJSON(modelConfig), function(err){
if (err) throw err;
console.log('Schemas are exposed!');
});
}
function prettyJSON(str){
return JSON.stringify(str, null, ' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment