UNPKG

vitamin

Version:

Data Mapper library for Node.js applications

120 lines (93 loc) 4.79 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 _hasOneOrMany = require('./has-one-or-many'); var _hasOneOrMany2 = _interopRequireDefault(_hasOneOrMany); 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); /** * MorphOneOrManyRelation constructor * * @param {Model} parent mapper instance * @param {Model} target mapper instance * @param {String} type * @param {String} fk target model foreign key * @param {String} pk parent model primary key * @constructor */ function _class(parent, target, type, fk, pk) { _classCallCheck(this, _class); var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, parent, target, fk, pk)); _this.morphType = type; _this.morphName = parent.name; return _this; } /** * Save a related model instance into the database * * @param {Model} model * @param {Array} returning * @return Promise */ _createClass(_class, [{ key: 'save', value: function save(model) { var returning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['*']; return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'save', this).call(this, model.set(this.morphType, this.morphName), returning); } /** * Add constraints on the relation query * * @param {Model} model * @return this relation */ }, { key: 'addConstraints', value: function addConstraints(model) { this.addMorphTypeConstraint(); return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'addConstraints', this).call(this, model); } /** * Get the qualified morph type name * * @return string */ }, { key: 'getQualifiedTypeColumn', value: function getQualifiedTypeColumn() { return this.query.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(); } }]); return _class; }(_hasOneOrMany2.default); exports.default = _class;