vitamin
Version:
Data Mapper library for Node.js applications
293 lines (229 loc) • 8.08 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 _modelNotFound = require('./errors/model-not-found');
var _modelNotFound2 = _interopRequireDefault(_modelNotFound);
var _underscore = require('underscore');
var _vitaminModel = require('vitamin-model');
var _vitaminModel2 = _interopRequireDefault(_vitaminModel);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
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; }
/**
* @class Model
*/
var _class = function (_BaseModel) {
_inherits(_class, _BaseModel);
/**
* Model constructor
*
* @param {Object} data
* @param {Boolean} exists
* @constructor
*/
function _class() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var exists = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
_classCallCheck(this, _class);
var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
_this.related = {};
return _this;
}
/**
* Begin querying the model
*
* @return query instance
*/
_createClass(_class, [{
key: 'toJSON',
/**
* Returns a JSON representation of this model
*
* @return plain object
*/
value: function toJSON() {
var json = (0, _underscore.mapObject)(this.related, function (related, name) {
return !related ? related : related.toJSON();
});
return (0, _underscore.extend)(_get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'toJSON', this).call(this), json);
}
/**
* Save the model in the database
*
* @param {Array} returning
* @return promise
*/
}, {
key: 'save',
value: function save() {
var returning = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['*'];
if (!this.isDirty()) return _bluebird2.default.resolve(this);
return this.mapper.save(this, returning);
}
/**
* Update the model in the database
*
* @param {Object} data
* @param {Array} returning
* @return promise
*/
}, {
key: 'update',
value: function update(data) {
var returning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['*'];
if (!this.exists) return _bluebird2.default.reject(new _modelNotFound2.default());
return this.fill(data).save(returning);
}
/**
* Delete the model from the database
*
* @return promise
*/
}, {
key: 'destroy',
value: function destroy() {
if (!this.exists) return _bluebird2.default.reject(new _modelNotFound2.default());
return this.mapper.destroy(this);
}
/**
* Update the model's update timestamp
*
* @return promise
*/
}, {
key: 'touch',
value: function touch() {
return this.mapper.touch(this);
}
/**
* Load the given relationships
*
* @param {Array} relations
* @return promise
*/
}, {
key: 'load',
value: function load(relations) {
var _mapper;
return (_mapper = this.mapper).load.apply(_mapper, [[this]].concat(Array.prototype.slice.call(arguments))).return(this);
}
/**
* Set the relationship value in the model
*
* @param {String} name
* @param {Any} value
* @return this model
*/
}, {
key: 'setRelated',
value: function setRelated(name, value) {
this.related[name] = value;
return this;
}
/**
* Unset the relationship value in the model
*
* @param {String} name
* @return this model
*/
}, {
key: 'unsetRelated',
value: function unsetRelated(name) {
delete this.related[name];
return this;
}
/**
* Get the relationship value
*
* @return any
*/
}, {
key: 'getRelated',
value: function getRelated(name) {
return this.related[name];
}
/**
* Determine if the given relationship is loaded
*
* @param {String} name
* @return boolean
*/
}, {
key: 'hasRelated',
value: function hasRelated(name) {
return !!this.related[name];
}
/**
* Trigger an event with arguments
*
* @param {String} event
* @param {Array} args
* @return promise
*/
}, {
key: 'emit',
value: function emit(event) {
var _mapper2;
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return (_mapper2 = this.mapper).emit.apply(_mapper2, arguments);
}
}], [{
key: 'query',
value: function query() {
return this.prototype.mapper.newQuery();
}
/**
* Save a new model in the database
*
* @param {Object} data
* @param {Array} returning
* @return promise
*/
}, {
key: 'create',
value: function create(data) {
var returning = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['*'];
return this.make(data).save(returning);
}
/**
* Add a listener for the given event
*
* @param {String} event
* @param {Function} fn
* @return model
* @static
*/
}, {
key: 'on',
value: function on(event, fn) {
var _prototype$mapper$emi;
(_prototype$mapper$emi = this.prototype.mapper.emitter).on.apply(_prototype$mapper$emi, arguments);
return this;
}
/**
* Remove an event listener
*
* @param {String} event
* @param {Function} fn
* @return model
* @static
*/
}, {
key: 'off',
value: function off(event, fn) {
var _prototype$mapper$emi2;
(_prototype$mapper$emi2 = this.prototype.mapper.emitter).off.apply(_prototype$mapper$emi2, arguments);
return this;
}
}]);
return _class;
}(_vitaminModel2.default);
exports.default = _class;