vitamin
Version:
Data Mapper library for Node.js applications
166 lines (123 loc) • 5.87 kB
JavaScript
'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 _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
var _query = require('./query');
var _query2 = _interopRequireDefault(_query);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _registry = require('../registry');
var _registry2 = _interopRequireDefault(_registry);
var _belongsTo = require('./belongs-to');
var _belongsTo2 = _interopRequireDefault(_belongsTo);
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);
/**
* MorphToRelation constructor
*
* @param {Model} parent model instance
* @param {String} type
* @param {String} fk parent model foreign key
* @param {String} pk target model primary key
* @constructor
*/
function _class(parent, type, fk, pk) {
_classCallCheck(this, _class);
var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, parent, null, fk, pk));
_this.morphType = type;
return _this;
}
/**
* Associate the model instance to the current model
*
* @param {Model} model
* @return parent model
*/
_createClass(_class, [{
key: 'associate',
value: function associate(model) {
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'associate', this).call(this, model).set(this.morphType, model.mapper.name);
}
/**
* Dissociate the parent model from the current model
*
* @return parent model
*/
}, {
key: 'dissociate',
value: function dissociate() {
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'dissociate', this).call(this).set(this.morphType, null);
}
/**
* Get the relation query
*
* @return query
*/
}, {
key: 'getQuery',
value: function getQuery() {
if (!this.target) {
var target = _registry2.default.get(this.model.get(this.morphType));
this.setTarget(target);
}
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'getQuery', this).call(this);
}
/**
* Load and populate the related models
*
* @param {Array} models
* @return promise
*/
}, {
key: 'eagerLoad',
value: function eagerLoad(models) {
var _this2 = this;
var groups = _underscore2.default.groupBy(models, function (m) {
return m.get(_this2.morphType);
});
return _bluebird2.default.map(_underscore2.default.keys(groups), function (type) {
// we define the current target mapper object
_this2.setTarget(_registry2.default.get(type));
// run a separate query for each model type
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'eagerLoad', _this2).call(_this2, groups[type]);
}).return(models);
}
/**
* Add constraints on the relation query
*
* @param {Model} model
* @return this relation
*/
}, {
key: 'addConstraints',
value: function addConstraints(model) {
this.model = model;
this.getQuery().where(this.getCompareKey(), model.get(this.localKey));
return this;
}
/**
* Set the relation target model
*
* @param {Model} target model instance
* @return this relation
* @private
*/
}, {
key: 'setTarget',
value: function setTarget(target) {
this.otherKey = target.primaryKey;
return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'setTarget', this).call(this, target);
}
}]);
return _class;
}(_belongsTo2.default);
exports.default = _class;