mlaanderson / database-js

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

Hack for drivers be tested properly #15

Closed thiagodp closed 6 years ago

thiagodp commented 6 years ago

Database-js has drivers as separated projects. It does not need to include them as dev dependencies.

On the other side, the drivers should include database-js as a dev dependency, for testing purposes (or they have to me maintained inside database-js, but never committed). However, database-js loads the drivers according to the driverName, including (with require) the respective file. Since the driver's index.js is not inside database-js, it is not able to find it and, thus, to load the driver.

There is a little hack, however, to solve this problem. I have used it in database-js-json, inside its mocha test file:

    var obj = {
        // hack to load the index of the driver as a module of database-js
        driverName: '../../../index', 
        . . .
    };
   var conn = new dbjs.Connection( obj );
   . . .

It would be nice having another solution to the driver loading problem. I the mean time, this hack can be useful to add proper tests in each driver project. @mlaanderson What do you think?

mlaanderson commented 6 years ago

The drivers can be instantiated apart from database-js and tested that way. However it's probably best to test with database-js for completeness.

You can also load like this:

var jsondB = require('.'); // or the root to the driver project
var Connection = require('database-js');

var conn = new Connection('json:///mydata.json', jsondB);

If the second argument is passed, then the Connection constructor ignores the driver name in the connection URL or in a connection object.

thiagodp commented 6 years ago

Better using your way ;) (test updated)

thiagodp commented 6 years ago

It may worth it to make the drivers have only dev dependencies on database-js. I will open issues about that.