2

In my project I have several classes that look like this:

"use strict";
var exports = module.exports = {};
var SystemsDAO = require('./dao/systems/SystemsDAO.js');
var aop = require('./dbAOPUtils.js');
var Proxy = require('harmony-proxy');
var sqlite3 = require('sqlite3').verbose();

/* Wraps a SystemServiceObject and passes in a constructed
 * DAO object as an argument to specified functions. */
exports.SystemsDAOIntercepter = function(obj) {

    let handler = {
        get(target, propKey, receiver) {
           const origMethod = target[propKey];
           return function(...args) {
            console.log('systemDAOIntercepter: BEGIN');

            // Create a reportsdao object and proxy it through an dbSQLiteConnectionIntercepter
            // (So we don't have to create it for every single method) 
            var systemdao = new SystemsDAO('blah');
            var proxSystemDAO = aop.dbSQLiteConnectionIntercepter(systemdao, sqlite3.OPEN_READONLY);

            args.push(proxSystemDAO);

            console.log('propKey: ' + target[propKey]);

            let result = null;

            result = origMethod.apply(this, args);

            console.log('systemsDAOIntercepter: END');
            return result;
           };
        }
    };

    return new Proxy(obj, handler);
};

Is it possible to pass in a type for an argument so that I only need a single DAOIntercepter class, and not one for each data access object?

I can see the part where I require() the SystemsDAO working for this by passing the file name, but as for instantiation of that class, I can't really see how it would be possible to do that.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • Are you trying to make the `new SystemsDAO()` call dynamic / use different objects? If so, yes you can do that with ES6 classes (going to assume that's what you mean by "type"). Just pass a pointer / variable for the class around like you would any other variable, and you can call `new VariableName()` on it. – dvlsg Aug 23 '16 at 18:54
  • @dvlsg Correct. I want to be able to swap that out for other DAOs, and trim down the size of my code base by doing so. – leeand00 Aug 23 '16 at 18:55

1 Answers1

2

Sure - you can pass classes around like any other variable in javascript.

Note that DAOType is provided and instantiated inside the method, now.

"use strict";
var exports = module.exports = {};
var aop = require('./dbAOPUtils.js');
var Proxy = require('harmony-proxy');
var sqlite3 = require('sqlite3').verbose();

/* Wraps a SystemServiceObject and passes in a constructed
 * DAO object as an argument to specified functions. */
exports.makeInterceptor = function(DAOType, obj) {

    let handler = {
        get(target, propKey, receiver) {
           const origMethod = target[propKey];
           return function(...args) {
            console.log('systemDAOIntercepter: BEGIN');

            // Create a reportsdao object and proxy it through an dbSQLiteConnectionIntercepter
            // (So we don't have to create it for every single method) 
            var dao = new DAOType('blah');
            var proxSystemDAO = aop.dbSQLiteConnectionIntercepter(dao, sqlite3.OPEN_READONLY);

            args.push(proxSystemDAO);

            console.log('propKey: ' + target[propKey]);

            let result = null;

            result = origMethod.apply(this, args);

            console.log('makeInterceptor: END');
            return result;
           };
        }
    };

    return new Proxy(obj, handler);
};

I have to admit that I would (personally) prefer to see the class instantiated externally and injected in through the arguments, however, instead of creating it inline.

dvlsg
  • 5,378
  • 2
  • 29
  • 34
  • Just an observation...probably untreated, but anyway...the `sqlite3.OPEN_READONLY` should also be passed in as an argument, just in case they need something different. – leeand00 Aug 23 '16 at 18:59