mlaanderson / database-js

Common Database Interface for Node
MIT License
74 stars 16 forks source link

Accept connection parameters from a plain object #4

Closed thiagodp closed 6 years ago

thiagodp commented 6 years ago

Currently, Connection currently receives a connectionString:

class Connection {
    constructor(connectionString, driver) {
        this.__base = ParseConnection(connectionString, driver);
        . . .
    }
  . . .
}

Sometimes it is desirable to create a connection passing parameters, such as in ConnectionObject, or maybe using a plain object.

So, to adjusting Connection's constructor to accept both a string or a ConnectionObject would be:

class Connection {
    constructor(conn, driver) {
        this.__base = 'string' === typeof conn ? ParseConnection(conn, driver) : conn;
        . . .
   }
   . . .
}

To receiving a plain object with connection properties, I think it would be better changing ConnectionObject instead of Connection. In this case, ConnectionObject could have a static constructor-like method:

class ConnectionObject {
   ...
   static fromPlain( obj, driver ) {
      return new ConnectionObject(
        obj[ 'driverName' ], 
        obj[ 'username' ],
        obj[ 'password' ],
        obj[ 'hostname' ],
        obj[ 'port' ],
        obj[ 'database' ],
        obj[ 'parameters' ],
        driver
      );
   }
  . . .
}

And then we could create a connection from a plain object:

let p = require( 'mydb.json' ); // a JSON file with database connection properties
let c = new Connection( ConnectionObject.fromPlain( p, driver ), driver );
// or just
let c = new Connection( ConnectionObject.fromPlain( require( 'mydb.json' ), driver ) );

Wouldn't it be nice?

thiagodp commented 6 years ago

@mlaanderson , I've added a Pull Request.

mlaanderson commented 6 years ago

@thiagodp , those changes look good. Thanks.

I'm not a software developer, I'm a hardware engineer, but I needed a flexible tool for my test lab. Bear with me as I figure git and versioning out.

mlaanderson commented 6 years ago

Since you got it working, I'm going to close this issue.

thiagodp commented 6 years ago

I'm not a software developer, I'm a hardware engineer, but I needed a flexible tool for my test lab. Bear with me as I figure git and versioning out.

@mlaanderson Hey, you're doing a great job with database-js! Many thanks!