-
-
Save mattdanielbrown/e411ea4d0b8b08c114be5c7c17d0f2e1 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "dbname": { | |
| "host": "localhost", | |
| "port": 5432, | |
| "database": "mydatabase", | |
| "password": "user123", | |
| "name": "dbname", | |
| "user": "user", | |
| "connector": "postgresql" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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