var di = require('di'); var _ = require('lodash'); var _DillerDao = require('./DillerDao'); var _Diller = require('./Diller'); var _DillerConfig = require('./DillerConfig'); var pgpConstructor = require('pg-promise'); var pgp; function _DillerTx(config) { var log = config.log(); function queryLogger(e) { log.info("Query:", e.query); if (e.ctx) { // this query is executing inside a task or transaction, if (e.ctx.isTX) { // this query is inside a transaction; } else { // this query is inside a task; } } } function connectLogger(client) { //log.info('PGP: connect'); } function disconnectLogger(client) { //log.info('PGP: disconnect'); } if (!pgp) { var pgpOptions = {}; pgpOptions.query = (config.logQueries && queryLogger) || undefined; pgpOptions.connect = (config.logQueries && connectLogger) || undefined; pgpOptions.disconnect = (config.logQueries && disconnectLogger) || undefined; pgp = pgpConstructor(pgpOptions); } return function (action) { var con = pgp(config.postgresqlConfig); return con.tx(function (pg) { var dao = new _DillerDao(con); var diller = new _Diller(config, con, dao); return action(pg, dao, diller) }); }; } di.annotate(_DillerTx, new di.Inject(_DillerConfig)); module.exports = _DillerTx;