UNPKG

vitamin

Version:

Data Mapper library for Node.js applications

78 lines (57 loc) 3.04 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 _base = require('./base'); var _base2 = _interopRequireDefault(_base); var _oneToOne = require('./mixins/one-to-one'); var _oneToOne2 = _interopRequireDefault(_oneToOne); 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 (_mixin) { _inherits(_class, _mixin); /** * BelongsToRelation constructor * * @param {Model} parent model instance * @param {Model} target model instance * @param {String} fk parent model foreign key * @param {String} pk target model primary key * @constructor */ function _class(parent, target, fk, pk) { _classCallCheck(this, _class); var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, parent, target)); _this.localKey = fk; _this.otherKey = pk; 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 this.model.set(this.localKey, model.get(this.otherKey)); } /** * Dissociate the parent model from the current model * * @return parent model */ }, { key: 'dissociate', value: function dissociate() { return this.model.set(this.localKey, null); } }]); return _class; }((0, _oneToOne2.default)(_base2.default)); exports.default = _class;