all files / lib/offshore/query/ adapters.js

100% Statements 17/17
83.33% Branches 10/12
100% Functions 4/4
100% Lines 15/15
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        410×   410×   411×   411×     3464×     3249×     1202× 388× 388×         814×                          
/**
 * Mixes Custom Non-CRUD Adapter Methods into the prototype.
 */
 
module.exports = function() {
  var self = this;
 
  Object.keys(this.connections).forEach(function(conn) {
 
    var adapter = self.connections[conn]._adapter || {};
 
    Object.keys(adapter).forEach(function(key) {
 
      // Ignore the Identity Property
      if (['identity', 'tableName'].indexOf(key) >= 0) return;
 
      // Don't override keys that already exists
      if (self[key]) return;
 
      // Don't override a property, only functions
      if (typeof adapter[key] != 'function') {
        self[key] = adapter[key];
        return;
      }
 
      // Apply the Function with passed in args and set this.identity as
      // the first argument
      self[key] = function() {
 
        var tableName = self.tableName || self.identity;
 
        // If this is the teardown method, just pass in the connection name,
        // otherwise pass the connection and the tableName
        var defaultArgs = key === 'teardown' ? [conn] : [conn, tableName];
 
        // Concat self.identity with args (must massage arguments into a proper array)
        // Use a normalized _tableName set in the core module.
        var args = defaultArgs.concat(Array.prototype.slice.call(arguments));
        return adapter[key].apply(self, args);
      };
    });
  });
 
};