loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Support connection strings (settings.url) in all connectors

Open bajtos opened this issue 10 years ago • 9 comments

We should enhance all of our connectors to support connection strings passed via settings.url. This makes it easier to configure datasources via environment variables provided by example by BlueMix or Heroku.

{
  "db": {
    "connector": "mysql",
    "url": "mysql://localhost/test"
  }
}

Note that configuring the url value from environment variables is out of scope of this issue, see https://github.com/strongloop/loopback-boot/issues/4 for that part.

bajtos avatar Feb 19 '16 12:02 bajtos

I think it would be good to have the url override all other settings like host and user. That way it's possible to specify development settings via individual settings and then let the URL from the environment variable override everything when running in staging/production.

bajtos avatar Feb 19 '16 12:02 bajtos

  • [x] change in loopback-workspace https://github.com/strongloop/loopback-workspace/pull/270 (should be merged as the last one, after all connectors support url properly, and description added in generator-loopback)
  • [x] Unit test for connection in each connector
  • [x] add description fields to notify users that url would override other settings
  • [x] update apiccli automation after prompt url added to all connectors

Checklist of all datasource connectors we have: The connetors already handles url would be checked.

  • [x] Mongodb
  • [x] Mysql https://github.com/strongloop/loopback-connector-mysql/pull/156
  • [x] Postgresql
  • [x] Mssql https://github.com/strongloop/loopback-connector-mssql/pull/75
  • [x] DB2 https://github.com/strongloop/loopback-connector-db2/pull/23
  • [x] Cloudant
  • [x] Oracle
  • [x] Redis
  • [ ] sqlite3 (N/A in this story)

jannyHou avatar Mar 24 '16 14:03 jannyHou

VERIFY STEPS

This feature enables datasource connection using connection strings. So there are two things to verify for each connector:

  • [ ] The connection string will override other config(could be verified by making them invalid, but the connection should succeed due to correct url)
  • [ ] Config object without connection string still works(could be verified by make url empty)

And please check https://github.com/strongloop-internal/scrum-loopback/pull/831#issuecomment-209156739 to see the details of the format of each connector's url.


Feel free to use the script to migrate and create sample data if automigrate function is available for that datasource. Then if endpoint GET /ModelNames returns the data, that means connection works:

module.exports = function(app, done) {
    var Urldemo = app.models.urldemo;
    var sample = [{name: 'First'}, {name: 'Second'}, {name: 'Third'}];

    app.datasources.mysqlDS.automigrate('urldemo', function(err) {
      if (err) {
        console.error("Automigrate Error:", err);
        done(err);
      } else {
        Urldemo.create(sample, function(err, data) {
          done(err, data);
        });
      }
    });
}

jannyHou avatar Apr 18 '16 17:04 jannyHou

Once this is verified, we should make a new release for all relevant modules (juggler, connectors, etc.)

bajtos avatar May 02 '16 15:05 bajtos

@laven26 @ssh24 can you verify this please?

cgole avatar May 03 '16 16:05 cgole

Verified.

  • [x] MongoDB
  • [x] MySQL
  • [x] MSSQL
  • [x] Cloudant
  • [x] Oracle
  • [x] Postgresql
  • [x] DB2 : there is no issue from our QA developer side. DB2 connector does not work properly for which I have created an issue https://github.com/strongloop/loopback-connector-db2/issues/32

ssh24 avatar May 06 '16 20:05 ssh24

What about sqlite3? Any other way to set foreign keys=true in connection steing?

sm-g avatar May 20 '16 02:05 sm-g

@sm-g I am not quite familiar with database sqlite3, I think @raymondfeng or @bajtos might be able to give you a quick answer of

Any other way to set foreign keys=true in connection string?

And anyway I will look into it then.

What about sqlite3?

The feature in this pr is a short-term solution. Actually we have a long-term discussion about implementing a unified NDBC interface for relational databases. I will bring "support sqlite3 connection string" into plan, and we will discuss what's the proper way to do it.

jannyHou avatar May 27 '16 00:05 jannyHou

I believ all related modules have been already published to npmjs.org, I am closing this issue as done.

bajtos avatar Sep 09 '16 08:09 bajtos