I am using nodejs and mysql client in nodejs I am trying to use pool function od the mysql module.
"use strict";
var mysqlClient = require('mysql')
, dbPool = mysqlClient.createPool(require('../config/database'));
function MyModel(params) {
this.tbl = params.tbl;
this.primary_key = params.primary_key;
this.primary_name = params.primary_name;
this.dbPool = dbPool;
}
module.exports = MyModel;
//primary key
MyModel.prototype.fromPK = function fromPK(pk, callback) {
this.dbPool.getConnection(function (err, connection) {
var query = "SELECT * FROM " + this.tbl + " WHERE " + this.primary_key + " = " + connection.escape(pk);
connection.query(query, callback);
});
};
I know I cannot access this inside getConnection and I could simply set var t = this outside it and access it with var t, but is there any other way to access this var in this condition. Should I define var t = this in each prototype function I write?
I have following detailed gist https://gist.github.com/yalamber/6bd1df0cc27849eb09b3