relation.js 26.6 KB
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _inflection = require('inflection');

var _inflection2 = _interopRequireDefault(_inflection);

var _helpers = require('./helpers');

var _helpers2 = _interopRequireDefault(_helpers);

var _model = require('./base/model');

var _model2 = _interopRequireDefault(_model);

var _relation = require('./base/relation');

var _relation2 = _interopRequireDefault(_relation);

var _promise = require('./base/promise');

var _promise2 = _interopRequireDefault(_promise);

var _constants = require('./constants');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var push = Array.prototype.push; // Relation
// ---------------

var removePivotPrefix = function removePivotPrefix(key) {
  return key.slice(_constants.PIVOT_PREFIX.length);
};
var hasPivotPrefix = function hasPivotPrefix(key) {
  return _lodash2.default.startsWith(key, _constants.PIVOT_PREFIX);
};

exports.default = _relation2.default.extend({

  // Assembles the new model or collection we're creating an instance of,
  // gathering any relevant primitives from the parent object,
  // without keeping any hard references.
  init: function init(parent) {
    this.parentId = parent.id;
    this.parentTableName = _lodash2.default.result(parent, 'tableName');
    this.parentIdAttribute = this.attribute('parentIdAttribute', parent);

    // Use formatted attributes so that morphKey and foreignKey will match
    // attribute keys.
    this.parentAttributes = parent.format(_lodash2.default.clone(parent.attributes));

    if (this.type === 'morphTo' && !parent._isEager) {
      // If the parent object is eager loading, and it's a polymorphic `morphTo` relation,
      // we can't know what the target will be until the models are sorted and matched.
      this.target = _helpers2.default.morphCandidate(this.candidates, this.parentAttributes[this.key('morphKey')]);
      this.targetTableName = _lodash2.default.result(this.target.prototype, 'tableName');
    }

    this.targetIdAttribute = this.attribute('targetIdAttribute', parent);
    this.parentFk = this.attribute('parentFk');

    var target = this.target ? this.relatedInstance() : {};
    target.relatedData = this;

    if (this.type === 'belongsToMany') {
      _lodash2.default.extend(target, pivotHelpers);
    }

    return target;
  },

  // Initializes a `through` relation, setting the `Target` model and `options`,
  // which includes any additional keys for the relation.
  through: function through(source, Target, options) {
    var type = this.type;
    if (type !== 'hasOne' && type !== 'hasMany' && type !== 'belongsToMany' && type !== 'belongsTo') {
      throw new Error('`through` is only chainable from `hasOne`, `belongsTo`, `hasMany`, or `belongsToMany`');
    }

    this.throughTarget = Target;
    this.throughTableName = _lodash2.default.result(Target.prototype, 'tableName');

    _lodash2.default.extend(this, options);
    _lodash2.default.extend(source, pivotHelpers);

    this.parentIdAttribute = this.attribute('parentIdAttribute');
    this.targetIdAttribute = this.attribute('targetIdAttribute');
    this.throughIdAttribute = this.attribute('throughIdAttribute', Target);
    this.parentFk = this.attribute('parentFk');

    // Set the appropriate foreign key if we're doing a belongsToMany, for convenience.
    if (this.type === 'belongsToMany') {
      this.foreignKey = this.throughForeignKey;
    } else if (this.otherKey) {
      this.foreignKey = this.otherKey;
    }

    return source;
  },

  // Generates and returns a specified key, for convenience... one of
  // `foreignKey`, `otherKey`, `throughForeignKey`.
  key: function key(keyName) {
    if (this[keyName]) return this[keyName];
    switch (keyName) {
      case 'otherKey':
        this[keyName] = singularMemo(this.targetTableName) + '_' + this.targetIdAttribute;
        break;
      case 'throughForeignKey':
        this[keyName] = singularMemo(this.joinTable()) + '_' + this.throughIdAttribute;
        break;
      case 'foreignKey':
        switch (this.type) {
          case 'morphTo':
            {
              var idKeyName = this.columnNames && this.columnNames[1] ? this.columnNames[1] : this.morphName + '_id';
              this[keyName] = idKeyName;
              break;
            }
          case 'belongsTo':
            this[keyName] = singularMemo(this.targetTableName) + '_' + this.targetIdAttribute;
            break;
          default:
            if (this.isMorph()) {
              this[keyName] = this.columnNames && this.columnNames[1] ? this.columnNames[1] : this.morphName + '_id';
              break;
            }
            this[keyName] = singularMemo(this.parentTableName) + '_' + this.parentIdAttribute;
            break;
        }
        break;
      case 'morphKey':
        this[keyName] = this.columnNames && this.columnNames[0] ? this.columnNames[0] : this.morphName + '_type';
        break;
      case 'morphValue':
        this[keyName] = this.parentTableName || this.targetTableName;
        break;
    }
    return this[keyName];
  },

  // Get the correct value for the following attributes:
  // parentIdAttribute, targetIdAttribute, throughIdAttribute and parentFk.
  attribute: function attribute(_attribute, parent) {
    switch (_attribute) {
      case 'parentIdAttribute':
        if (this.isThrough()) {
          if (this.type === 'belongsTo' && this.throughForeignKey) {
            return this.throughForeignKey;
          }

          if (this.type === 'belongsToMany' && this.isThroughForeignKeyTargeted()) {
            return this.throughForeignKeyTarget;
          }

          if (this.isOtherKeyTargeted()) {
            return this.otherKeyTarget;
          }

          // Return attribute calculated on `init` by default.
          return this.parentIdAttribute;
        }

        if (this.type === 'belongsTo' && this.foreignKey) {
          return this.foreignKey;
        }

        if (this.type !== 'belongsTo' && this.isForeignKeyTargeted()) {
          return this.foreignKeyTarget;
        }

        return _lodash2.default.result(parent, 'idAttribute');

      case 'targetIdAttribute':
        if (this.isThrough()) {
          if ((this.type === 'belongsToMany' || this.type === 'belongsTo') && this.isOtherKeyTargeted()) {
            return this.otherKeyTarget;
          }

          // Return attribute calculated on `init` by default.
          return this.targetIdAttribute;
        }

        if (this.type === 'morphTo' && !parent._isEager) {
          return _lodash2.default.result(this.target.prototype, 'idAttribute');
        }

        if (this.type === 'belongsTo' && this.isForeignKeyTargeted()) {
          return this.foreignKeyTarget;
        }

        if (this.type === 'belongsToMany' && this.isOtherKeyTargeted()) {
          return this.otherKeyTarget;
        }

        return this.targetIdAttribute;

      case 'throughIdAttribute':
        if (this.type !== 'belongsToMany' && this.isThroughForeignKeyTargeted()) {
          return this.throughForeignKeyTarget;
        }

        if (this.type === 'belongsToMany' && this.throughForeignKey) {
          return this.throughForeignKey;
        }

        return _lodash2.default.result(parent.prototype, 'idAttribute');

      case 'parentFk':
        if (!this.hasParentAttributes()) {
          return;
        }

        if (this.isThrough()) {
          if (this.type === 'belongsToMany' && this.isThroughForeignKeyTargeted()) {
            return this.parentAttributes[this.throughForeignKeyTarget];
          }

          if (this.type === 'belongsTo') {
            return this.throughForeignKey ? this.parentAttributes[this.parentIdAttribute] : this.parentId;
          }

          if (this.isOtherKeyTargeted()) {
            return this.parentAttributes[this.otherKeyTarget];
          }

          // Return attribute calculated on `init` by default.
          return this.parentFk;
        }

        return this.parentAttributes[this.isInverse() ? this.key('foreignKey') : this.parentIdAttribute];
    }
  },


  // Injects the necessary `select` constraints into a `knex` query builder.
  selectConstraints: function selectConstraints(knex, options) {
    var resp = options.parentResponse;

    // The `belongsToMany` and `through` relations have joins & pivot columns.
    if (this.isJoined()) this.joinClauses(knex);

    // Call the function, if one exists, to constrain the eager loaded query.
    if (options._beforeFn) options._beforeFn.call(knex, knex);

    // The base select column
    if (_lodash2.default.isArray(options.columns)) {
      knex.columns(options.columns);
    }

    var currentColumns = _lodash2.default.find(knex._statements, { grouping: 'columns' });

    if (!currentColumns || currentColumns.length === 0) {
      knex.column(this.targetTableName + '.*');
    }

    if (this.isJoined()) this.joinColumns(knex);

    // If this is a single relation and we're not eager loading,
    // limit the query to a single item.
    if (this.isSingle() && !resp) knex.limit(1);

    // Finally, add (and validate) the where conditions, necessary for constraining the relation.
    this.whereClauses(knex, resp);
  },

  // Inject & validates necessary `through` constraints for the current model.
  joinColumns: function joinColumns(knex) {
    var columns = [];
    var joinTable = this.joinTable();
    if (this.isThrough()) columns.push(this.throughIdAttribute);
    columns.push(this.key('foreignKey'));
    if (this.type === 'belongsToMany') columns.push(this.key('otherKey'));
    push.apply(columns, this.pivotColumns);
    knex.columns(_lodash2.default.map(columns, function (col) {
      return joinTable + '.' + col + ' as _pivot_' + col;
    }));
  },

  // Generates the join clauses necessary for the current relation.
  joinClauses: function joinClauses(knex) {
    var joinTable = this.joinTable();

    if (this.type === 'belongsTo' || this.type === 'belongsToMany') {

      var targetKey = this.type === 'belongsTo' ? this.key('foreignKey') : this.key('otherKey');

      knex.join(joinTable, joinTable + '.' + targetKey, '=', this.targetTableName + '.' + this.targetIdAttribute);

      // A `belongsTo` -> `through` is currently the only relation with two joins.
      if (this.type === 'belongsTo') {
        knex.join(this.parentTableName, joinTable + '.' + this.throughIdAttribute, '=', this.parentTableName + '.' + this.key('throughForeignKey'));
      }
    } else {
      knex.join(joinTable, joinTable + '.' + this.throughIdAttribute, '=', this.targetTableName + '.' + this.key('throughForeignKey'));
    }
  },

  // Check that there isn't an incorrect foreign key set, vs. the one
  // passed in when the relation was formed.
  whereClauses: function whereClauses(knex, response) {
    var key = void 0;

    if (this.isJoined()) {
      var isBelongsTo = this.type === 'belongsTo';
      var targetTable = isBelongsTo ? this.parentTableName : this.joinTable();

      var column = isBelongsTo ? this.parentIdAttribute : this.key('foreignKey');

      key = targetTable + '.' + column;
    } else {
      var _column = this.isInverse() ? this.targetIdAttribute : this.key('foreignKey');

      key = this.targetTableName + '.' + _column;
    }

    var method = response ? 'whereIn' : 'where';
    var ids = response ? this.eagerKeys(response) : this.parentFk;
    knex[method](key, ids);

    if (this.isMorph()) {
      var table = this.targetTableName;
      var _key = this.key('morphKey');
      var value = this.key('morphValue');
      knex.where(table + '.' + _key, value);
    }
  },

  // Fetches all `eagerKeys` from the current relation.
  eagerKeys: function eagerKeys(response) {
    var key = this.isInverse() && !this.isThrough() ? this.key('foreignKey') : this.parentIdAttribute;
    return _lodash2.default.reject((0, _lodash2.default)(response).map(key).uniq().value(), _lodash2.default.isNil);
  },

  // Generates the appropriate standard join table.
  joinTable: function joinTable() {
    if (this.isThrough()) return this.throughTableName;
    return this.joinTableName || [this.parentTableName, this.targetTableName].sort().join('_');
  },

  // Creates a new model or collection instance, depending on
  // the `relatedData` settings and the models passed in.
  relatedInstance: function relatedInstance(models) {
    models = models || [];

    var Target = this.target;

    // If it's a single model, check whether there's already a model
    // we can pick from... otherwise create a new instance.
    if (this.isSingle()) {
      if (!(Target.prototype instanceof _model2.default)) {
        throw new Error('The ' + this.type + ' related object must be a Bookshelf.Model');
      }
      return models[0] || new Target();
    }

    // Allows us to just use a model, but create a temporary
    // collection for a "*-many" relation.
    if (Target.prototype instanceof _model2.default) {
      return Target.collection(models, { parse: true });
    }
    return new Target(models, { parse: true });
  },

  // Groups the related response according to the type of relationship
  // we're handling, for easy attachment to the parent models.
  eagerPair: function eagerPair(relationName, related, parentModels) {
    var _this = this;

    // If this is a morphTo, we only want to pair on the morphValue for the current relation.
    if (this.type === 'morphTo') {
      parentModels = _lodash2.default.filter(parentModels, function (m) {
        return m.get(_this.key('morphKey')) === _this.key('morphValue');
      });
    }

    // If this is a `through` or `belongsToMany` relation, we need to cleanup & setup the `interim` model.
    if (this.isJoined()) related = this.parsePivot(related);

    // Group all of the related models for easier association with their parent models.
    var grouped = _lodash2.default.groupBy(related, function (m) {
      if (m.pivot) {
        if (_this.isInverse() && _this.isThrough()) {
          return _this.isThroughForeignKeyTargeted() ? m.pivot.get(_this.throughForeignKeyTarget) : m.pivot.id;
        }

        return m.pivot.get(_this.key('foreignKey'));
      }

      if (_this.isInverse()) {
        return _this.isForeignKeyTargeted() ? m.get(_this.foreignKeyTarget) : m.id;
      }

      return m.get(_this.key('foreignKey'));
    });

    // Loop over the `parentModels` and attach the grouped sub-models,
    // keeping the `relatedData` on the new related instance.
    _lodash2.default.each(parentModels, function (model) {
      var groupedKey = void 0;
      if (!_this.isInverse()) {
        groupedKey = model.get(_this.parentIdAttribute);
      } else {
        var keyColumn = _this.key(_this.isThrough() ? 'throughForeignKey' : 'foreignKey');
        var formatted = model.format(_lodash2.default.clone(model.attributes));
        groupedKey = formatted[keyColumn];
      }
      if (groupedKey) {
        var relation = model.relations[relationName] = _this.relatedInstance(grouped[groupedKey]);
        relation.relatedData = _this;
        if (_this.isJoined()) _lodash2.default.extend(relation, pivotHelpers);
      }
    });

    // Now that related models have been successfully paired, update each with
    // its parsed attributes
    related.map(function (model) {
      model.attributes = model.parse(model.attributes);
    });

    return related;
  },


  parsePivot: function parsePivot(models) {
    var _this2 = this;

    return _lodash2.default.map(models, function (model) {

      // Separate pivot attributes.
      var grouped = _lodash2.default.reduce(model.attributes, function (acc, value, key) {
        if (hasPivotPrefix(key)) {
          acc.pivot[removePivotPrefix(key)] = value;
        } else {
          acc.model[key] = value;
        }
        return acc;
      }, { model: {}, pivot: {} });

      // Assign non-pivot attributes to model.
      model.attributes = grouped.model;

      // If there are any pivot attributes, create a new pivot model with these
      // attributes.
      if (!_lodash2.default.isEmpty(grouped.pivot)) {
        var Through = _this2.throughTarget;
        var tableName = _this2.joinTable();
        model.pivot = Through != null ? new Through(grouped.pivot) : new _this2.Model(grouped.pivot, { tableName: tableName });
      }

      return model;
    });
  },

  // A few predicates to help clarify some of the logic above.
  isThrough: function isThrough() {
    return this.throughTarget != null;
  },
  isJoined: function isJoined() {
    return this.type === 'belongsToMany' || this.isThrough();
  },
  isMorph: function isMorph() {
    return this.type === 'morphOne' || this.type === 'morphMany';
  },
  isSingle: function isSingle() {
    var type = this.type;
    return type === 'hasOne' || type === 'belongsTo' || type === 'morphOne' || type === 'morphTo';
  },
  isInverse: function isInverse() {
    return this.type === 'belongsTo' || this.type === 'morphTo';
  },
  isForeignKeyTargeted: function isForeignKeyTargeted() {
    return this.foreignKeyTarget != null;
  },
  isThroughForeignKeyTargeted: function isThroughForeignKeyTargeted() {
    return this.throughForeignKeyTarget != null;
  },
  isOtherKeyTargeted: function isOtherKeyTargeted() {
    return this.otherKeyTarget != null;
  },
  hasParentAttributes: function hasParentAttributes() {
    return this.parentAttributes != null;
  },


  // Sets the `pivotColumns` to be retrieved along with the current model.
  withPivot: function withPivot(columns) {
    if (!_lodash2.default.isArray(columns)) columns = [columns];
    this.pivotColumns = this.pivotColumns || [];
    push.apply(this.pivotColumns, columns);
  }

});

// Simple memoization of the singularize call.

var singularMemo = function () {
  var cache = Object.create(null);
  return function (arg) {
    if (!(arg in cache)) {
      cache[arg] = _inflection2.default.singularize(arg);
    }
    return cache[arg];
  };
}();

// Specific to many-to-many relationships, these methods are mixed
// into the `belongsToMany` relationships when they are created,
// providing helpers for attaching and detaching related models.
var pivotHelpers = {

  /**
   * Attaches one or more `ids` or models from a foreign table to the current
   * table, on a {@linkplain many-to-many} relation. Creates and saves a new
   * model and attaches the model with the related model.
   *
   *     var admin1 = new Admin({username: 'user1', password: 'test'});
   *     var admin2 = new Admin({username: 'user2', password: 'test'});
   *
   *     Promise.all([admin1.save(), admin2.save()])
   *       .then(function() {
   *         return Promise.all([
   *         new Site({id: 1}).admins().attach([admin1, admin2]),
   *         new Site({id: 2}).admins().attach(admin2)
   *       ]);
   *     })
   *
   * This method (along with {@link Collection#detach} and {@link
   * Collection#updatePivot}) are mixed in to a {@link Collection} when
   * returned by a {@link Model#belongsToMany belongsToMany} relation.
   *
   * @method Collection#attach
   * @param {mixed|mixed[]} ids
   *   One or more ID values or models to be attached to the relation.
   * @param {Object} options
   *   A hash of options.
   * @param {Transaction} options.transacting
   *   Optionally run the query in a transaction.
   * @returns {Promise<Collection>}
   *   A promise resolving to the updated Collection.
   */
  attach: function attach(ids, options) {
    var _this3 = this;

    return _promise2.default.try(function () {
      return _this3.triggerThen('attaching', _this3, ids, options);
    }).then(function () {
      return _this3._handler('insert', ids, options);
    }).then(function (response) {
      return _this3.triggerThen('attached', _this3, response, options);
    }).return(this);
  },


  /**
   * Detach one or more related objects from their pivot tables. If a model or
   * id is passed, it attempts to remove the pivot table based on that foreign
   * key. If no parameters are specified, we assume we will detach all related
   * associations.
   *
   * This method (along with {@link Collection#attach} and {@link
   * Collection#updatePivot}) are mixed in to a {@link Collection} when returned
   * by a {@link Model#belongsToMany belongsToMany} relation.
   *
   * @method Collection#detach
   * @param {mixed|mixed[]} [ids]
   *   One or more ID values or models to be detached from the relation.
   * @param {Object} options
   *   A hash of options.
   * @param {Transaction} options.transacting
   *   Optionally run the query in a transaction.
   * @returns {Promise<undefined>}
   *   A promise resolving to `undefined`.
   */
  detach: function detach(ids, options) {
    var _this4 = this;

    return _promise2.default.try(function () {
      return _this4.triggerThen('detaching', _this4, ids, options);
    }).then(function () {
      return _this4._handler('delete', ids, options);
    }).then(function (response) {
      return _this4.triggerThen('detached', _this4, response, options);
    }).return(this);
  },


  /**
   * The `updatePivot` method is used exclusively on {@link Model#belongsToMany
   * belongsToMany} relations, and allows for updating pivot rows on the joining
   * table.
   *
   * This method (along with {@link Collection#attach} and {@link
   * Collection#detach}) are mixed in to a {@link Collection} when returned
   * by a {@link Model#belongsToMany belongsToMany} relation.
   *
   * @method Collection#updatePivot
   * @param {Object} attributes
   *   Values to be set in the `update` query.
   * @param {Object} [options]
   *   A hash of options.
   * @param {function|Object} [options.query]
   *   Constrain the update query. Similar to the `method` argument to {@link
   *   Model#query}.
   * @param {bool} [options.require=false]
   *   Causes promise to be rejected with an Error if no rows were updated.
   * @param {Transaction} [options.transacting]
   *   Optionally run the query in a transaction.
   * @returns {Promise<Number>}
   *   A promise resolving to number of rows updated.
   */
  updatePivot: function updatePivot(attributes, options) {
    return this._handler('update', attributes, options);
  },

  /**
   * The `withPivot` method is used exclusively on {@link Model#belongsToMany
   * belongsToMany} relations, and allows for additional fields to be pulled
   * from the joining table.
   *
   *     var Tag = bookshelf.Model.extend({
   *       comments: function() {
   *         return this.belongsToMany(Comment).withPivot(['created_at', 'order']);
   *       }
   *     });
   *
   * @method Collection#withPivot
   * @param {string[]} columns
   *   Names of columns to be included when retrieving pivot table rows.
   * @returns {Collection}
   *   Self, this method is chainable.
   */
  withPivot: function withPivot(columns) {
    this.relatedData.withPivot(columns);
    return this;
  },

  // Helper for handling either the `attach` or `detach` call on
  // the `belongsToMany` or `hasOne` / `hasMany` :through relationship.
  _handler: _promise2.default.method(function (method, ids, options) {
    var _this5 = this;

    var pending = [];
    if (ids == null) {
      if (method === 'insert') return _promise2.default.resolve(this);
      if (method === 'delete') pending.push(this._processPivot(method, null, options));
    }
    if (!_lodash2.default.isArray(ids)) ids = ids ? [ids] : [];
    _lodash2.default.each(ids, function (id) {
      return pending.push(_this5._processPivot(method, id, options));
    });
    return _promise2.default.all(pending).return(this);
  }),

  // Handles preparing the appropriate constraints and then delegates
  // the database interaction to _processPlainPivot for non-.through()
  // pivot definitions, or _processModelPivot for .through() models.
  // Returns a promise.
  _processPivot: _promise2.default.method(function (method, item) {
    var relatedData = this.relatedData,
        args = Array.prototype.slice.call(arguments),
        fks = {},
        data = {};

    fks[relatedData.key('foreignKey')] = relatedData.parentFk;

    // If the item is an object, it's either a model
    // that we're looking to attach to this model, or
    // a hash of attributes to set in the relation.
    if (_lodash2.default.isObject(item)) {
      if (item instanceof _model2.default) {
        fks[relatedData.key('otherKey')] = item.id;
      } else if (method !== 'update') {
        _lodash2.default.extend(data, item);
      }
    } else if (item) {
      fks[relatedData.key('otherKey')] = item;
    }

    args.push(_lodash2.default.extend(data, fks), fks);

    if (this.relatedData.throughTarget) {
      return this._processModelPivot.apply(this, args);
    }

    return this._processPlainPivot.apply(this, args);
  }),

  // Applies constraints to the knex builder and handles shelling out
  // to either the `insert` or `delete` call for the current model,
  // returning a promise.
  _processPlainPivot: _promise2.default.method(function (method, item, options, data) {
    var relatedData = this.relatedData;

    // Grab the `knex` query builder for the current model, and
    // check if we have any additional constraints for the query.
    var builder = this._builder(relatedData.joinTable());
    if (options && options.query) {
      _helpers2.default.query.call(null, { _knex: builder }, [options.query]);
    }

    if (options) {
      if (options.transacting) builder.transacting(options.transacting);
      if (options.debug) builder.debug();
    }

    var collection = this;
    if (method === 'delete') {
      return builder.where(data).del().then(function () {
        if (!item) return collection.reset();
        var model = collection.get(data[relatedData.key('otherKey')]);
        if (model) {
          collection.remove(model);
        }
      });
    }
    if (method === 'update') {
      return builder.where(data).update(item).then(function (numUpdated) {
        if (options && options.require === true && numUpdated === 0) {
          throw new Error('No rows were updated');
        }
        return numUpdated;
      });
    }

    return this.triggerThen('creating', this, data, options).then(function () {
      return builder.insert(data).then(function () {
        collection.add(item);
      });
    });
  }),

  // Loads or prepares a pivot model based on the constraints and deals with
  // pivot model changes by calling the appropriate Bookshelf Model API
  // methods. Returns a promise.
  _processModelPivot: _promise2.default.method(function (method, item, options, data, fks) {
    var relatedData = this.relatedData,
        JoinModel = relatedData.throughTarget,
        joinModel = new JoinModel();

    fks = joinModel.parse(fks);
    data = joinModel.parse(data);

    if (method === 'insert') {
      return joinModel.set(data).save(null, options);
    }

    return joinModel.set(fks).fetch({
      require: true
    }).then(function (instance) {
      if (method === 'delete') {
        return instance.destroy(options);
      }
      return instance.save(item, options);
    });
  })

};