src/transaction.js
function makeTxClient(trx, client, connection) {
  const trxClient = Object.create(client.constructor.prototype);
  trxClient.version = client.version;
  trxClient.config = client.config;
  trxClient.driver = client.driver;
  trxClient.connectionSettings = client.connectionSettings;
  trxClient.transacting = true;
  trxClient.valueForUndefined = client.valueForUndefined;
  trxClient.logger = client.logger;

  trxClient.on('query', function(arg) {
    trx.emit('query', arg);
    client.emit('query', arg);
  });

  trxClient.on('query-error', function(err, obj) {
    trx.emit('query-error', err, obj);
    client.emit('query-error', err, obj);
  });

  trxClient.on('query-response', function(response, obj, builder) {
    trx.emit('query-response', response, obj, builder);
    client.emit('query-response', response, obj, builder);
  });

  const _query = trxClient.query;
  trxClient.query = function(conn, obj) {
    const completed = trx.isCompleted();
    return Promise.try(function() {
      if (conn !== connection)
        throw new Error('Invalid connection for transaction query.');
      if (completed) completedError(trx, obj);
      return _query.call(trxClient, conn, obj);
    });
  };
  const _stream = trxClient.stream;
  trxClient.stream = function(conn, obj, stream, options) {
    const completed = trx.isCompleted();
    return Promise.try(function() {
      if (conn !== connection)
        throw new Error('Invalid connection for transaction query.');
      if (completed) completedError(trx, obj);
      return _stream.call(trxClient, conn, obj, stream, options);
    });
  };
  trxClient.acquireConnection = function() {
    return Promise.resolve(connection);
  };
  trxClient.releaseConnection = function() {
    return Promise.resolve();
  };

  return trxClient;
}
function makeTxClient(trx, client, connection) {
  const trxClient = Object.create(client.constructor.prototype);
  trxClient.version = client.version;
  trxClient.config = client.config;
  trxClient.driver = client.driver;
  trxClient.connectionSettings = client.connectionSettings;
  trxClient.transacting = true;
  trxClient.valueForUndefined = client.valueForUndefined;
  trxClient.logger = client.logger;

  trxClient.on('query', function(arg) {
    trx.emit('query', arg);
    client.emit('query', arg);
  });

  trxClient.on('query-error', function(err, obj) {
    trx.emit('query-error', err, obj);
    client.emit('query-error', err, obj);
  });

  trxClient.on('query-response', function(response, obj, builder) {
    trx.emit('query-response', response, obj, builder);
    client.emit('query-response', response, obj, builder);
  });

  const _query = trxClient.query;
  trxClient.query = function(conn, obj) {
    const completed = trx.isCompleted();
    return Promise.try(function() {
      if (conn !== connection)
        throw new Error('Invalid connection for transaction query.');
      if (completed) completedError(trx, obj);
      return _query.call(trxClient, conn, obj);
    });
  };
  const _stream = trxClient.stream;
  trxClient.stream = function(conn, obj, stream, options) {
    const completed = trx.isCompleted();
    return Promise.try(function() {
      if (conn !== connection)
        throw new Error('Invalid connection for transaction query.');
      if (completed) completedError(trx, obj);
      return _stream.call(trxClient, conn, obj, stream, options);
    });
  };
  trxClient.acquireConnection = function() {
    return Promise.resolve(connection);
  };
  trxClient.releaseConnection = function() {
    return Promise.resolve();
  };

  return trxClient;
}
function makeTxClient ( trx , client , connection ) { const trxClient = Object . create ( client . constructor . prototype ) ; trxClient . version = client . version ; trxClient . config = client . config ; trxClient . driver = client . driver ; trxClient . connectionSettings = client . connectionSettings ; trxClient . transacting = true ; trxClient . valueForUndefined = client . valueForUndefined ; trxClient . logger = client . logger ; trxClient . on ( 'query' , function ( arg ) { trx . emit ( 'query' , arg ) ; client . emit ( 'query' , arg ) ; } ) ; trxClient . on ( 'query-error' , function ( err , obj ) { trx . emit ( 'query-error' , err , obj ) ; client . emit ( 'query-error' , err , obj ) ; } ) ; trxClient . on ( 'query-response' , function ( response , obj , builder ) { trx . emit ( 'query-response' , response , obj , builder ) ; client . emit ( 'query-response' , response , obj , builder ) ; } ) ; const _query = trxClient . query ; trxClient . query = function ( conn , obj ) { const completed = trx . isCompleted ( ) ; return Promise . try ( function ( ) { if ( conn !== connection ) throw new Error ( 'Invalid connection for transaction query.' ) ; if ( completed ) completedError ( trx , obj ) ; return _query . call ( trxClient , conn , obj ) ; } ) ; } ; const _stream = trxClient . stream ; trxClient . stream = function ( conn , obj , stream , options ) { const completed = trx . isCompleted ( ) ; return Promise . try ( function ( ) { if ( conn !== connection ) throw new Error ( 'Invalid connection for transaction query.' ) ; if ( completed ) completedError ( trx , obj ) ; return _stream . call ( trxClient , conn , obj , stream , options ) ; } ) ; } ; trxClient . acquireConnection = function ( ) { return Promise . resolve ( connection ) ; } ; trxClient . releaseConnection = function ( ) { return Promise . resolve ( ) ; } ; return trxClient ; }
We need to make a client object which always acquires the same connection and does not release back into the pool.
We need to make a client object which always acquires the same connection and does not release back into the pool .