0

EDIT - The question title has been edited to reflect the true nature of the issue

I'm attempting to refactor my node.js application code, and make better use of OOP principles. Prior to refactoring, the mysql driver and connection (pool) lived at the top of the main app.js file above the routing logic etc. I've now created the file below and in the main app.js file i'm importing this and attempting to use the class. However the class fails to instantiate. I'm new with JavaScript classes so i'm probably making a simple mistake. Can anyone see a mistake here?

const mysql = require('mysql');

class Database {

    constructor(maxConnections) {
        this._pool = mysql.createPool({
            connectionLimit: maxConnections,
            host: '',
            user: '',
            password: '',
            database: ''
        });
    }

    get Pool() {
        return this._pool;
    }

    // static ExecuteSQL(sql, callback) {
    //     this.Pool.getConnection(function (err, connection) {
    //         connection.query(sql, function (err, rows, fields) {
    //             if (err) {
    //                 callback(err, null, null);
    //             } else {
    //                 callback(null, rows, fields);
    //             }
    //             connection.release();
    //         });
    //     });
    // }

    static Format(sql, args) {
        return mysql.format(sql, args);
    }
}

module.exports = {
    Database,
}

The database logic worked prior to refactoring the code so i assume this is a mistake relating to JavaScript classes.

Michael
  • 177
  • 3
  • 11
  • What's the error that you are getting? – alexhenkel Aug 23 '17 at 16:27
  • I don't know, its very awkward to debug - i'm deploying it and then running it (HTTP 500). I've tried wrapping the instantiation code in a try...catch block but it does not catch the error – Michael Aug 23 '17 at 16:30
  • And how are you instantiating the class? – alexhenkel Aug 23 '17 at 16:33
  • const dbdriver = require('./db.js'), db = new dbdriver.Database(100); – Michael Aug 23 '17 at 16:35
  • Also tried... try { var context = new dbdriver.Database(10); sql = context.Format(sql, e) } catch (err) { reply(err) } – Michael Aug 23 '17 at 16:36
  • 1
    What about `module.exports = Database` and import as `Database`? – alexhenkel Aug 23 '17 at 16:46
  • I can't see how it would help, just a different style way of doing the same thing? It seems to me that the class itself is failing – Michael Aug 23 '17 at 16:51
  • if i change the constructor content to this._pool = 123; and then instantiate the object and call db.Pool it works fine. So it looks like the problem lies with the storing the mysql Pool object in the class – Michael Aug 23 '17 at 17:16
  • @alexhenkel I've solved the problem and i'll add the solution now. I apologise the question was not very accurate - the constructor was fine although i was attempting to call a static property from an object instance (i just realised JS doesn't allow this) – Michael Aug 23 '17 at 17:29
  • @Michael that is arguably a [design flaw](https://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co) in Java. JavaScript actually does the correct thing here (for once). – Jared Smith Aug 23 '17 at 17:38
  • @JaredSmith thanks for the link! I've never really given it much thought, but it certainly makes sense the way JS does it – Michael Aug 23 '17 at 17:47
  • @Michael also with one exception (`Object.create(null)`) every object can access it's constructor through the propery lookup chain: `foo.constructor.someStaticMethod()`. Which is arguably less clear than using the class name, but certainly better than having the method called out of nowhere. – Jared Smith Aug 23 '17 at 17:53

1 Answers1

0

The question wasn't accurate, nonetheless here is the solution:

The problem was that I was attempting to call db.Format() where db was an instance of Database. It seems that with JavaScript you cannot call a static member from an instance. The fix was to call Database.Format() where Database is the 'static' class definition

Michael
  • 177
  • 3
  • 11