You are on page 1of 9

03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

My moto

Node js,Loopback 3 complete installation,with


model generation script-:by_hemantham
hemantham
Oct 15, 2018 · 5 min read

Hello, world..!! I’m always, Happy to write…Living to Code. I would like to share my
experience with Loopback, One among the best Nodejs frameworks so far.

Prerequisites

1. Node js version above 8.(I would recommend you to use nvm)

2. Ofcource text editor (VScode is simply an amazing tool)

3. Patience and Interest to learn

Step_1

# npm install -g loopback


https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 1/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

Step_2

# npm install -g loopback-cli

Step_3

open command prompt and type..

# lb <project_name>

In my case it is..look at the pic below

here, helloloopback is my project name. lb is a command key, provided by loopback’s


cli to generate files that we would need to run the project.

Step_4

Now it’s time to move faster. Because it is it, Just hit enter, in response to the command
prompts. Remember to choose loopback 3.x , Since the title sounds it. sorry for guys
who looking for loopback 4.x, and congratulations to guys who are looking for
loopback 2.x, because of the installation steps which are almost the same.

Now, when loopback asks you

what kind of application do you have in mind?

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 2/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

Simply select empty-server(An empty loopback API, without any configured models or
datasources…..as shown in the above picture)

Step_4

Relaaaax..!!! Smell the cup of co ee, let npm do its duty.

Step_5

When you see the above screen, it’s a clean installation of loopback project and you are set to go o
further.

Okay, no more screenshots…

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 3/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

Step_6

Feel free to explore the project directory, And create a folder bin inside the folder
server. And inside bin folder create a js file discover_dbmodels.js

server/bin/discover_dbmodels.js
# Paste the below code in it ..

. . .

// ** script starts here**


‘use strict’;

const loopback = require(‘loopback’);

const promisify = require(‘util’).promisify;

const fs = require(‘fs’);

const writeFile = promisify(fs.writeFile);

const readFile = promisify(fs.readFile);

const mkdirp = promisify(require(‘mkdirp’));

const args = require(‘minimist’)(process.argv.slice(2));

const DATASOURCE_NAME = ‘mysql’;

const dataSourceConfig = require(‘../datasources.json’);

const db1 = new loopback.DataSource(dataSourceConfig[DATASOURCE_NAME]);

var createIfNotExist = require(“create-if-not-exist”);

var path = require(‘path’);

var outputPath = path.resolve(__dirname, ‘../../common/models’);

const lower = args.t

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 4/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

const cap_Fir_Let_ = lower.charAt(0).toUpperCase() + lower.substr(1);

discover().then(

success => {

console.log(‘\n’)

console.log(‘# table ->’ + args.t + ‘ from # database ->’ + args.d + ‘ has been loaded
succesfully..!!’)

process.exit()

},

error => {

console.error(‘\n UNHANDLED ERROR:\n\n’, error);

process.exit(1);

);

async function discover() {

const options = {

relations: true

};

const dbtable = await db1.discoverSchemas(args.t, options);

await mkdirp(‘common/models’);

const key = args.d + ‘.’ + args.t

dbtable[key].properties.id.required = false;

await writeFile(‘common/models/’ + cap_Fir_Let_ + ‘.json’,


JSON.stringify(dbtable[key], null, 2));
https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 5/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

const configJson = await readFile(‘server/model-config.json’, ‘utf-8’);

const config = JSON.parse(configJson);

config[cap_Fir_Let_] = {

dataSource: DATASOURCE_NAME,

public: true

};

await writeFile(

‘server/model-config.json’,

JSON.stringify(config, null, 2)

);

var model_JS_file = path.join(outputPath + ‘/’ + cap_Fir_Let_ + ‘.js’);

await createIfNotExist(

model_JS_file,

“‘use strict’;module.exports=function(“ + cap_Fir_Let_ + “) {};”, ‘utf8’

);

// ** script ends here**

Step_7

Go back to cmd and type # lb datasource ,and in my example i have choosen mysql


database to drag tables as models into loopback, and to do so, i need to to first get
connected with the database, and This is the step where i’m doing it..THE
CONNECTION WITH MYSQL DATABASE

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 6/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

Don’t forget to choose mysql connector from the prompts.Use arrow keys to move up and down

this is my datasource, just answer to the prompts with correct values and cli generates datasource.json for
you in server/datasource.json

Okay no more screenshots for the second time ;)

Step_8

Before running the project we are lagging 2 more steps

1. run this command # npm install create-if-not-exist — — save. This should


automatically include in package.json by npm, if that does not happen include it
manullay in package.json under “dependencies” and run npm install.

2. Include the following line in package.json under “scripts”

“discover-table”: “node server/bin/discover_dbmodels.js”


This is because, to generate a model(table) from the mysql database. To run the above
script run the following command in the cmd

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 7/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

where table_name and database_name are command line arguments sent to discover_dbmodels.js le
for generating models

Step_9

Generate all the tables in your database one by one by running the above script. You
guys might have got a doubt ,What if there are hundreds of tables in my database,should i
have to run all of them one by one? And that’s an amazing doubt checkout my another
article in which i added a script for auto-generating all the database tables at once with
one command. The name of the article follows the same this article has but comes with
auto-generate_allTables.

once u are done with models generation ur done with the command line with this last
step

# npm start
You would see the following result in the cmd

try browsing the link at http://localhost:30002/explorer in your browser

You are all done with loopback installation…now try the magics provided by loopback
at this screen below

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 8/9
03/04/2020 Node js,Loopback 3 complete installation,with model generation script-:by_hemantham

When you see your models here..then it’s a clean end of installation

That’s it guys… if you want line by line explaination of what the above
discover_dbmodels.js script does, check out my next article. It’s name would be
probably

Complete walkthough over Model GeneratorScript-


loopback3.
See you guys there…i know you are curious to know…and i’m curious enough to expalin.
Kudos to all …!!!!

Bye ..byee… semicolons ;

JavaScript Loopback Nodejs Loopback3 MySQL

About Help Legal

https://medium.com/@hemantham002/node-js-loopback-3-complete-installation-with-model-generation-script-eafcbc68f1b2 9/9

You might also like