Last active
April 18, 2018 08:44
-
-
Save bajtos/cafb7c9688b4f9b332bec661d088d7f7 to your computer and use it in GitHub Desktop.
Discover LoopBack models
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
'use strict'; | |
const loopback = require('loopback'); | |
const promisify = require('util').promisify; | |
const fs = require('fs'); | |
const writeFile = promisify(fs.writeFile); | |
const mkdirp = promisify(require('mkdirp')); | |
const app = loopback(); | |
const db = app.dataSource('db', { | |
connector: 'mysql', | |
user: 'root', | |
}); | |
discover().then( | |
success => process.exit(), | |
error => { console.error('UNHANDLED ERROR:\n', error); process.exit(1); }, | |
); | |
async function discover() { | |
const schema = await db.discoverSchema( | |
'salaries', // the table name | |
{ | |
schema: 'employees' // the database/schema name | |
}, | |
); | |
await mkdirp('common/models'); | |
await writeFile( | |
'common/models/salaries.json', | |
JSON.stringify(schema, null, 2) | |
); | |
} | |
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
{ | |
"name": "discover-models", | |
"version": "1.0.0", | |
"license": "MIT", | |
"dependencies": { | |
"loopback": "^3.19.0", | |
"loopback-connector-mysql": "^5.2.0", | |
"mkdirp": "^0.5.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment