0
votes

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

2

2 Answers

1
votes

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.

0
votes

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.