Code coverage report for bookshelf/lib/base/relation.js

Statements: 37.5% (6 / 16)      Branches: 0% (0 / 4)      Functions: 0% (0 / 4)      Lines: 37.5% (6 / 16)      Ignored: none     

All files » bookshelf/lib/base/ » relation.js
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          1 1       1                   1                                           1   1
// Base Relation
// ---------------
 
'use strict';
 
var _ = require('lodash');
var CollectionBase = require('./collection');
 
// Used internally, the `Relation` helps in simplifying the relationship building,
// centralizing all logic dealing with type & option handling.
function RelationBase(type, Target, options) {
  this.type = type;
  this.target = Target;
  if (this.target) {
    this.targetTableName = _.result(Target.prototype, 'tableName');
    this.targetIdAttribute = _.result(Target.prototype, 'idAttribute');
  }
  _.extend(this, options);
}
 
_.extend(RelationBase.prototype, {
 
  // Creates a new relation instance, used by the `Eager` relation in
  // dealing with `morphTo` cases, where the same relation is targeting multiple models.
  instance: function instance(type, Target, options) {
    return new this.constructor(type, Target, options);
  },
 
  // Creates a new, unparsed model, used internally in the eager fetch helper
  // methods. (Parsing may mutate information necessary for eager pairing.)
  createModel: function createModel(data) {
    if (this.target.prototype instanceof CollectionBase) {
      return new this.target.prototype.model(data)._reset();
    }
    return new this.target(data)._reset();
  },
 
  // Eager pair the models.
  eagerPair: function eagerPair() {}
 
});
 
RelationBase.extend = require('../extend');
 
module.exports = RelationBase;