0

I'm trying to connect mongodb and mysql on Loopback4

How to config datasource.json and model as Content's model conecter mongoDB, User's model conecter mysql

Jee Mok
  • 6,157
  • 8
  • 47
  • 80

2 Answers2

1

If you are using lookback 4 , then you can just use the cli for this purpose.

# lb4 datasource

Then answer the wizard questions such as:

  • datasourceName - Here specify your datasource name, this will be used to reference this connection across the looback4 application

Then loopback4 will show you the list of drivers you can choose from, in this case choose mongodb driver.

  • Url - You can leave it blank and answer the following questions
  • Host:
  • Port:
  • User:
  • Password:
  • Database

that´s all, loopback4 will install the chosen driver and will create a json format configuration file located at src/datasources/datasource.json where you can change the values entered during the configuration.

I hope that helps.

Mario Estrada
  • 311
  • 2
  • 4
  • PS: You can run lb4 datasource again to configure the mysql datasource in the same way, the only thing different is the driver in this case will be mysql driver. – Mario Estrada Jun 30 '18 at 20:17
0

You can take these examples to try to connect:

To MongoDB

{
  "name": "dbname",
  "connector": "mongodb",
  "url": "",
  "host": "localhost",
  "port": 27017,
  "user": "",
  "password": "",
  "database": "dbname",
  "useNewUrlParser": true
}

To MySQL

{
  "name": "dbname",
  "connector": "mysql",
  "host": "localhost",
  "port": 3306,
  "user": "",
  "password": "",
  "database": "dbname"
}

I've used both of them and they work correctly. Of course those values have been taken from my local development environment, and they are not recommended for production.

Daniel Duarte
  • 464
  • 4
  • 12