all files / lib/offshore/adapter/ stream.js

77.78% Statements 14/18
57.14% Branches 8/14
100% Functions 1/1
76.47% Lines 13/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                 
/**
 * Module Dependencies
 */
 
var normalize = require('../utils/normalize');
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
 
/**
 * Stream Normalization
 */
 
module.exports = {
 
  // stream.write() is used to send data
  // Must call stream.end() to complete stream
  stream: function(criteria, stream, metaContainer) {
 
    // Normalize Arguments
    criteria = normalize.criteria(criteria);
 
    // Build Default Error Message
    var err = 'No stream() method defined in adapter!';
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    Iif (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
 
    Eif (!hasOwnProperty(adapter, 'stream')) return stream.end(new Error(err));
    adapter.stream(connName, this.collection, criteria, stream, metaContainer);
  }
 
};