UNPKG

vitamin

Version:

Data Mapper library for Node.js applications

162 lines (125 loc) 5.98 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _belongsToMany = require('./belongs-to-many'); var _belongsToMany2 = _interopRequireDefault(_belongsToMany); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // exports var _class = function (_Relation) { _inherits(_class, _Relation); /** * MorphToManyRelation constructor * * @param {Model} parent model instance * @param {Model} target mdoel instance * @param {String} pivot table name * @param {String} type * @param {String} pfk parent model foreign key * @param {String} tfk target model foreign key * @constructor */ function _class(parent, target, pivot, type, pfk, tfk) { _classCallCheck(this, _class); var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, parent, target, pivot, pfk, tfk)); _this.morphType = type; _this.morphName = parent.name; return _this; } /** * Add constraints on the relation query * * @param {Model} model * @return this relation */ _createClass(_class, [{ key: 'addConstraints', value: function addConstraints(model) { this.addMorphTypeConstraint(); return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'addConstraints', this).call(this, model); } /** * Create a query for the pivot table * * @param {Boolean} constraints * @return query * @private */ }, { key: 'newPivotQuery', value: function newPivotQuery() { var constraints = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var query = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'newPivotQuery', this).call(this, constraints); if (constraints) query.where(this.morphType, this.morphName); return query; } /** * Create a record to insert into the pivot table * * @param {Any} id of the target model * @param {Object} pivots attributes * @return plain object * @private */ }, { key: 'createPivotRecord', value: function createPivotRecord(id) { var pivots = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var record = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'createPivotRecord', this).apply(this, arguments); record[this.morphType] = this.morphName; return record; } /** * Get the qualified morph type name * * @return string */ }, { key: 'getQualifiedTypeColumn', value: function getQualifiedTypeColumn() { return this._through.getQualifiedColumn(this.morphType); } /** * Add `morphType` constraint on the relation query * * @private */ }, { key: 'addMorphTypeConstraint', value: function addMorphTypeConstraint() { this.query.where(this.getQualifiedTypeColumn(), this.morphName); } /** * Add eager load constraints on the relation query * * @param {Array} models * @private */ }, { key: 'addEagerLoadConstraints', value: function addEagerLoadConstraints(models) { _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'addEagerLoadConstraints', this).call(this, models); this.addMorphTypeConstraint(); } /** * Add `join`constraints to the intermediate table * * @private */ }, { key: 'addPivotJoin', value: function addPivotJoin() { var _this2 = this; this.query.join(this.table + ' as ' + this._through.alias, function (qb) { qb.on(_this2.getQualifiedTypeColumn(), _this2.morphName).andOn(_this2.query.getQualifiedColumn(_this2.target.primaryKey), _this2._through.getQualifiedColumn(_this2.targetKey)); }); } }]); return _class; }(_belongsToMany2.default); exports.default = _class;