* For any package and it's installation or how to use you might need to search on internet

To get technical help check this URL for sample code
https://github.com/loopbackio/loopback-next/tree/master

To get loopback CLI help
https://loopback.io/doc/en/lb4/Command-line-interface.html

To get loopback concept click below link
https://loopback.io/doc/en/lb4/Loopback-application-layout.html
https://loopback.io/doc/en/lb4/Concepts.html

To get idea about database relation and models 
https://loopback.io/doc/en/lb4/Model.html
https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html#specify-the-foreign-key-constraints-in-todo-model

To get idea about data validation
https://loopback.io/doc/en/lb4/Validation.html

To get idea about authentication and authorization
https://loopback.io/doc/en/lb4/Authentication-overview.html

For server configuration detailed settings
https://loopback.io/doc/en/lb4/Customizing-server-configuration.html

For database related operation in models need to refere below chapter
https://loopback.io/doc/en/lb4/MySQL-connector.html


@]
Create loopback 4 application
1st goto location where you want to create your application
Make sure you do not create application folder as it will be created by lb4 cli
# lb4 app

@]
Install all packages from package.json
# npm install

@]
Run your project
# npm start

@]
Build project
# npm run build

@]
Need to enable debug mode in applciation so all error details are included on the response, including a full stack trace.
https://stackoverflow.com/questions/55308813/how-do-i-return-an-error-from-a-controller-in-loopback-4
To do that need to add following coe in application.ts
in Production mode need to set it to false

// Enable debug mode in your application
this.bind(RestBindings.ERROR_WRITER_OPTIONS).to({debug: true});

@]
Understand error code 
https://loopback.io/doc/en/lb4/Error-codes.html#missing_required_parameter

@]
Need to find a way to through user defined errors in programming code.
Such as in program we might check for some validation and upon rejection we need to show error
In loopback there are certain way to do this
return Promise.reject()
throw HttpErrors.method(argument);
RestHttpErrors.method(argument)
Need to find exact and new way for this as it is important for error management.

@]
Automatically refresh upon changes (build & watch with nodemon)
Watch for file changes in Loopback 4 during run time
Install package tsc-watch
# npm install -D tsc-watch
Add below line under "scripts" tag in package.json
"start:watch": "tsc-watch --target es2017 --outDir ./dist --onSuccess \"node .\"",
And use
# npm run start:watch instead of ~ npm run start.
It helps automatically detect any source-code changes and restart the server as well.

@]
Setup data source connection for any type of database
https://loopback.io/doc/en/lb4/Connecting-to-MySQL.html#3-create-a-data-source
# lb4 datasource
Host: localhost / 127.0.0.1 (depending on language and/or connection method used)
Port: 8889
Username: root
Password: root

If we are using database config from .env file or any kind of cod which uses .env variable then we need to import dotenv in datasource
This is because datasource is directly used for various terminal based operation

@]
Handle environment configuration
https://stackoverflow.com/questions/52858986/how-to-specify-environment-variables-in-loopback-4
# npm install --save dotenv

Once installed need to create new file .env on root directory
After need to add a code in application.ts at top in root folder
This is important as we need to .env variable at every place

// Addition of dotenv for access to process.env (environment variables)
import {config as dotEnvConfig} from 'dotenv';
dotEnvConfig();

@]
When you build your project using 
npm run build
does not keep the .env file in the ./dist folder if you need this then need to create a command to do this
in package.json file we need to add below
"build-env": "cp .env ./dist/.env && tsc",
and after use 
# npm run build-env

Need to find whether this is required or not


@]
Setup API authetication
There is package in loopback4 we need to install 
# npm i --save @loopback/authentication 
# npm i --save @loopback/authentication-jwt
# npm install --save @loopback/security

Protect unauthorized API access using JWT verification
To do that install below packages
After follow steps on below URL
<https://loopback.io/doc/en/lb4/Authentication-tutorial.html#before-we-begin>

@]
Setup API authorisation
# npm install --save @loopback/authorization
You can read more at open source libaray https://casbin.org
https://loopback.io/doc/en/lb4/RBAC-with-authorization.html
https://loopback.io/doc/en/lb4/migration-auth-access-control-example.html
https://loopback.io/blog/building-an-online-game-with-loopback-4-pt4
https://strongloop.com/strongblog/building-an-online-game-with-loopback-4-pt4/


@]
First setup API authentication using JWT using above packages.
JWT comes with inbuilt data tabes which we need to modify as per our database and need to set up API security
We can also use or database tables in this case we need to extend jwt package

@]
Now build the application
# npm run build

@]
Setup cron job related services
# npm install --save @loopback/cron

@]
Install a library to help you hash passwords. This is required at many place.
https://www.npmjs.com/package/bcrypt?activeTab=readme
# npm install --save bcrypt

@]
Encrypt and Decrypt package for any kind of data with private key
https://www.npmjs.com/package/crypto-js?activeTab=readme
https://cryptojs.gitbook.io/docs/
# npm install --save crypto-js
# npm install --save @types/crypto-js

@]
Setup API versioning process
URL based on Header based

@]
Setup mail sending functionality
https://stackoverflow.com/questions/57182231/send-email-with-loopback-4
https://nodemailer.com/about/
# npm install --save nodemailer

@]
A node.js module for interfacing with all type of Push Notification. All in one.
# npm install --save node-pushnotifications 

There are few more options available as below 

https://www.npmjs.com/package/loopback4-notifications
# npm install loopback4-notifications

Setup push notification to desktop device MAC, Windows, Linux, Growl 
https://www.npmjs.com/package/node-notifier
# npm install --save node-notifier

Setup push notification to IOS and Android 
IOS: https://github.com/node-apn/node-apn
ANDROID: https://github.com/ToothlessGear/node-gcm

@]
Soft delete functionality package
https://www.npmjs.com/package/loopback4-soft-delete
This is important to protect data when delete operation performed accidentally
# npm install loopback4-soft-delete

@]
OpenAI node package
https://github.com/openai/openai-node


@]
Every time first generate model or discover it from already created database table
https://loopback.io/doc/en/lb4/Connecting-to-MySQL.html#2-create-models
# lb4 model

@]
Create model from database table
https://loopback.io/doc/en/lb4/Connecting-to-MySQL.html#model-discovery
# lb4 discover --relations --schema your_database_name

@]
Generate repository for model
https://loopback.io/doc/en/lb4/Connecting-to-MySQL.html#4-create-repositories
# lb4 repository

@]
Generate controller
https://loopback.io/doc/en/lb4/Getting-started.html#adding-your-own-controller
# lb4 controller

@]
To migrate database from modal to mysql database
First need to update migrate.ts file as per your migration requirement
Like add your models which are required to migrate
.ts > await app.migrateSchema({existingSchema, models: ['DepartmentDivision']});
# npm run migrate

@]
Set relation between two models
# lb4 relation

@]
For JWT json web token we need to use below package
https://www.npmjs.com/package/jsonwebtoken
# npm install --save jsonwebtoken

@]
Need to hide API explorar in production mode and anyone can get access of it.
There are 2 things to do
1st side signup process which can allow you to get API access token this will found in respected controller 
Need to add 'x-visibility': 'undocumented', in controller decorater such as @post().
In this project check for /signup in api-acess.controller.ts file
2nd need to hide explorar by commenting code in application.ts

@]
Enabling HTTPS
https://loopback.io/doc/en/lb4/Enabling-https.html
For production mode need to set config for https 
This can be done in root index.js in const config.

@]
If you don't want to use request data to be passed in URL then we need to use post method then we need to tweak lb4.
We need to use @post only but need to change the url pattern as below and pass data to post
Also need to write a mechanism to to check data validation
Need to find and set some standard way for this as it's not very clear at this moment

GET, 
@post('/controller/get')
@post('/controller/get/by-id')
@post('/controller/get/all')

POST, 
@post('/controller/post')

PUT, 
@post('/controller/put')

PATCH, 
@post('/controller/patch')

DELETE
@post('/controller/delete')

@]
We need to know API response code properly which are global
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
There are 5 categories and each has multiple code for various purpose

@]
ISSUES I FOUND

1]
During migrate process migrate.ts script do not set unsigned attribute on table field name.
This functionality should work but somehow not working so due to this migrate process interrupted



@]
Working demo of loopback 4 
https://strongloop.com/strongblog/building-an-online-game-with-loopback-4-pt7/


@]
Importnat links
https://www.youtube.com/watch?v=s2yDaKiNYCg
https://medium.com/@iqbaldjulfri/role-based-authentication-with-jwt-in-loopback-4-4f9ab63daa52