all files / lib/offshore/collection/ index.js

100% Statements 23/23
83.33% Branches 5/6
100% Functions 2/2
100% Lines 23/23
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                  410×     410×     410×     410×     410×     410×   410×       5493× 5491×            
/**
 * Dependencies
 */
 
var _ = require('lodash');
var extend = require('../utils/extend');
var inherits = require('util').inherits;
 
// Various Pieces
var Core = require('../core');
var Query = require('../query');
 
/**
 * Collection
 *
 * A prototype for managing a collection of database
 * records.
 *
 * This file is the prototype for collections defined using Offshore.
 * It contains the entry point for all ORM methods (e.g. User.find())
 *
 * Methods in this file defer to the adapter for their true implementation:
 * the implementation here just validates and normalizes the parameters.
 *
 * @param {Object} offshore, reference to parent
 * @param {Object} options
 * @param {Function} callback
 */
 
var Collection = module.exports = function(offshore, connections, cb) {
 
  var self = this;
 
  // Set the connections
  this.connections = connections || {};
 
  // Cache reference to the parent
  this.offshore = offshore;
 
  // Default Attributes
  this.attributes = this.attributes || {};
 
  // Instantiate Base Collection
  Core.call(this);
 
  // Instantiate Query Language
  Query.call(this);
 
  return this;
};
 
inherits(Collection, Core);
inherits(Collection, Query);
 
Collection.prototype._loadQuery = function(query) {
  if (!query) {
    return this;
  }
 
  var obj = Object.create(this);
  obj._query = query;
  obj.adapter = this.adapter._loadQuery(query);
  return obj;
};
 
// Make Extendable
Collection.extend = extend;