From 98798efaf1278fec024389788e096149090c60b6 Mon Sep 17 00:00:00 2001 From: nicosommi Date: Tue, 11 Aug 2015 18:36:39 -0300 Subject: [PATCH] weakmap implementation --- es5/lib/collection.js | 6 +- es5/lib/model/addAssociation.js | 9 ++- es5/lib/model/fetch.js | 16 ++-- es5/lib/model/index.js | 102 ++++++++++++-------------- es5/spec/model.spec.js | 58 ++++++++------- es5/spec/model/fetch.spec.js | 6 +- es5/spec/model/save.spec.js | 22 +++--- es5/spec/testClasses.js | 22 +++--- es5/spec/validation/isPresent.spec.js | 18 ++--- es6/lib/model/addAssociation.js | 3 +- es6/lib/model/fetch.js | 10 ++- es6/lib/model/index.js | 74 +++++++------------ 12 files changed, 166 insertions(+), 180 deletions(-) diff --git a/es5/lib/collection.js b/es5/lib/collection.js index 77f40af..7015f68 100644 --- a/es5/lib/collection.js +++ b/es5/lib/collection.js @@ -14,7 +14,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } var _jargon = require("jargon"); @@ -25,6 +25,8 @@ var _modelFinderJs = require("./modelFinder.js"); var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs); var Collection = (function (_Array) { + _inherits(Collection, _Array); + function Collection(initialData) { _classCallCheck(this, Collection); @@ -52,8 +54,6 @@ var Collection = (function (_Array) { }); } - _inherits(Collection, _Array); - _createClass(Collection, [{ key: "push", value: function push() { diff --git a/es5/lib/model/addAssociation.js b/es5/lib/model/addAssociation.js index 2b87312..719f01d 100644 --- a/es5/lib/model/addAssociation.js +++ b/es5/lib/model/addAssociation.js @@ -20,11 +20,16 @@ var _collectionJs = require("../collection.js"); var _collectionJs2 = _interopRequireDefault(_collectionJs); +var _incognito = require("incognito"); + +var _incognito2 = _interopRequireDefault(_incognito); + +//private functions + var _ = require("./"); var _2 = _interopRequireDefault(_); -//private functions function getSetterFunctionHasOne(association, privateAssociationName, privateImplicitAssociationName) { var _this = this; @@ -236,7 +241,7 @@ function addAssociation(associationDetails) { this[associationDetails.name] = associationDetails.value; - this._associations[associationDetails.name] = association; + (0, _incognito2["default"])(this)._associations[associationDetails.name] = association; return associationSetter; } diff --git a/es5/lib/model/fetch.js b/es5/lib/model/fetch.js index c49ec60..fb09aaa 100644 --- a/es5/lib/model/fetch.js +++ b/es5/lib/model/fetch.js @@ -17,6 +17,10 @@ var _jargon = require("jargon"); var _jargon2 = _interopRequireDefault(_jargon); +var _incognito = require("incognito"); + +var _incognito2 = _interopRequireDefault(_incognito); + var _modelFinderJs = require("../modelFinder.js"); var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs); @@ -181,8 +185,8 @@ function fetchByBelongsTo(associationName, associations, callback) { function fetchBy() { var _this5 = this; - var fields = arguments[0] === undefined ? [this.primaryKey] : arguments[0]; - var callback = arguments[1] === undefined ? undefined : arguments[1]; + var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0]; + var callback = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1]; if (!this.constructor.database) { throw new Error("Cannot fetch without Model.database set."); @@ -201,7 +205,9 @@ function fetchBy() { } }, this); - if (this._softDelete) { + var _ = (0, _incognito2["default"])(this); + + if (_._softDelete) { chain = chain.whereNull((0, _jargon2["default"])("deletedAt").snake.toString()); } @@ -211,14 +217,14 @@ function fetchBy() { } else { _this5[_symbols2["default"].parseAttributesFromFields](records[0]); - if (_this5._includeAssociations.length > 0) { + if (_._includeAssociations.length > 0) { (function () { var associations = _this5.associations; /* We'll be putting all of our Async tasks into this */ var fetchTasks = []; - _this5._includeAssociations.forEach(function (associationName) { + _._includeAssociations.forEach(function (associationName) { var association = associations[associationName]; diff --git a/es5/lib/model/index.js b/es5/lib/model/index.js index d6d1942..ea94401 100644 --- a/es5/lib/model/index.js +++ b/es5/lib/model/index.js @@ -28,10 +28,18 @@ var _quirk = require("quirk"); var _quirk2 = _interopRequireDefault(_quirk); +var _incognito = require("incognito"); + +var _incognito2 = _interopRequireDefault(_incognito); + var _symbols = require("./symbols"); var _symbols2 = _interopRequireDefault(_symbols); +/** + * @class Model + */ + //import additional methods from external files var _fetchJs = require("./fetch.js"); @@ -50,10 +58,6 @@ var _modelFinderJs = require("../modelFinder.js"); var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs); -/** - * @class Model - */ - var Model = (function () { /** * @param {Object.} [initialAttributes] Provide default values for attributes by passing a Key-Value Object. @@ -65,22 +69,18 @@ var Model = (function () { _classCallCheck(this, Model); - ["_validations", "_associations"].forEach(function (privatePropertyName) { - Object.defineProperty(_this, privatePropertyName, { - writable: true, - enumerable: false, - value: {} - }); - }); + var _ = (0, _incognito2["default"])(this); + _._validations = {}; + _._associations = {}; + _._includeAssociations = []; + _._tableName = null; + _._primaryKey = null; + _._softDelete = null; + /** * Define dynamic properties */ Object.defineProperties(this, { - "_includeAssociations": { - enumerable: false, - writable: true, - value: [] - }, "additionalAttributes": { enumerable: false, //this fix db related issues @@ -110,43 +110,27 @@ var Model = (function () { get: this[_symbols2["default"].validations] }, - "_tableName": { - enumerable: false, - writable: true - }, - "tableName": { get: function get() { - return _this._tableName || (0, _jargon2["default"])(_this.constructor.name).plural.snake.toString(); + return _._tableName || (0, _jargon2["default"])(_this.constructor.name).plural.snake.toString(); }, set: function set(newTableName) { - _this._tableName = newTableName; + _._tableName = newTableName; } }, - "_primaryKey": { - enumerable: false, - writable: true - }, - "primaryKey": { get: function get() { - return _this._primaryKey || "id"; + return _._primaryKey || "id"; }, set: function set(newPrimaryKey) { - _this._primaryKey = newPrimaryKey; + _._primaryKey = newPrimaryKey; } }, - "_softDelete": { - enumerable: false, - writable: true, - value: false - }, - "softDelete": { get: function get() { - _this._softDelete = true; + _._softDelete = true; } } }); @@ -192,7 +176,8 @@ var Model = (function () { }, { key: "ensure", value: function ensure(attributeName, validatorFunction, validatorMessage) { - this._validations[attributeName] = this._validations[attributeName] || []; + var _ = (0, _incognito2["default"])(this); + _._validations[attributeName] = _._validations[attributeName] || []; var validatorDetails = { validator: validatorFunction }; @@ -200,10 +185,8 @@ var Model = (function () { validatorDetails.message = validatorMessage; } - this._validations[attributeName].push(validatorDetails); + _._validations[attributeName].push(validatorDetails); } - }, { - key: "isValid", /** * Return a boolean indicating whether the model is valid or not. @@ -211,13 +194,13 @@ var Model = (function () { * @method isValid * @param {Function(boolean)} callback Callback returning the boolean. */ + }, { + key: "isValid", value: function isValid(callback) { this.invalidAttributes(function (invalidAttributeList) { callback(Object.keys(invalidAttributeList).length === 0); }); } - }, { - key: "invalidAttributes", /** * Return an object containing all invalid attributes and their errors. @@ -232,10 +215,13 @@ var Model = (function () { * @method invalidAttributes * @param {Function(invalidAttributeList)} callback Callback returning the invalid attribute list. */ + }, { + key: "invalidAttributes", value: function invalidAttributes(callback) { var _this2 = this; - var attributeNamesWithValidators = Object.keys(this._validations); + var _ = (0, _incognito2["default"])(this); + var attributeNamesWithValidators = Object.keys(_._validations); var compileInvalidAttributeList = function compileInvalidAttributeList(errors, validatorMessages) { if (errors) { @@ -257,7 +243,7 @@ var Model = (function () { }; var performValidationsForAttribute = function performValidationsForAttribute(attributeName, done) { - var attributeValidations = _this2._validations[attributeName]; + var attributeValidations = _._validations[attributeName]; var performValidation = function performValidation(validation, returnValue) { var validator = validation.validator; @@ -314,7 +300,7 @@ var Model = (function () { associationNames[_key] = arguments[_key]; } - this._includeAssociations = associationNames; + (0, _incognito2["default"])(this)._includeAssociations = associationNames; return this; } }, { @@ -322,7 +308,7 @@ var Model = (function () { value: function _delete(callback) { var _this3 = this; - if (this._softDelete) { + if ((0, _incognito2["default"])(this)._softDelete) { if (!this.constructor.database) { throw new Error("Cannot delete without Model.database set."); } @@ -355,10 +341,10 @@ var Model = (function () { throw new Error("Not implemented."); } } - }, { - key: "beforeValidation", /* Stubbed methods for hooks */ + }, { + key: "beforeValidation", value: function beforeValidation(callback) { callback(); } @@ -386,20 +372,20 @@ var Model = (function () { value: function toJSON() { return this.attributes; } - }, { - key: _symbols2["default"].setAttributes, /** * Private Functionality */ + }, { + key: _symbols2["default"].setAttributes, value: function value(newAttributes) { this[_symbols2["default"].parseAttributesFromFields](newAttributes); } }, { key: _symbols2["default"].associations, value: function value() { - return this._associations; + return (0, _incognito2["default"])(this)._associations; } }, { key: _symbols2["default"].properties, @@ -409,7 +395,7 @@ var Model = (function () { }, { key: _symbols2["default"].validations, value: function value() { - return this._validations; + return (0, _incognito2["default"])(this)._validations; } }, { key: _symbols2["default"].attributes, @@ -418,7 +404,7 @@ var Model = (function () { var attributes = {}; this.properties.forEach(function (propertyName) { - if (!_this4._associations[propertyName]) { + if (!(0, _incognito2["default"])(_this4)._associations[propertyName]) { attributes[propertyName] = _this4[propertyName]; } }); @@ -433,8 +419,6 @@ var Model = (function () { return true; } } - }, { - key: _symbols2["default"].callDeep, /** * Call a function deeply through all associations @@ -444,6 +428,8 @@ var Model = (function () { * @param {String} functionName The name of the function that you want to fire deeply. * @param {function(errors, results)} Function called at the end of the operation. */ + }, { + key: _symbols2["default"].callDeep, value: function value(methodName, predicate, callback) { var _this5 = this; @@ -516,11 +502,13 @@ var Model = (function () { } }); + var _ = (0, _incognito2["default"])(this); + //add belongsTo associations and remove others Object.keys(this.associations).forEach(function (associationName) { var relatedModel = _this6[associationName]; var foreignKeyField = (0, _jargon2["default"])(associationName).foreignKey.toString(); - if (_this6._associations[associationName].type === "belongsTo") { + if (_._associations[associationName].type === "belongsTo") { //try with relatedModel and relatedModel.id if (relatedModel && relatedModel.id) { fieldAttributes[foreignKeyField] = relatedModel.id; diff --git a/es5/spec/model.spec.js b/es5/spec/model.spec.js index 4899af9..71837da 100644 --- a/es5/spec/model.spec.js +++ b/es5/spec/model.spec.js @@ -13,7 +13,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } var _sinon = require("sinon"); @@ -366,14 +366,14 @@ describe("Model(attributes, options)", function () { describe(".deleted", function () { var SoftUser = (function (_Model) { + _inherits(SoftUser, _Model); + function SoftUser() { _classCallCheck(this, SoftUser); _get(Object.getPrototypeOf(SoftUser.prototype), "constructor", this).apply(this, arguments); } - _inherits(SoftUser, _Model); - _createClass(SoftUser, [{ key: "initialize", value: function initialize() { @@ -412,14 +412,14 @@ describe("Model(attributes, options)", function () { associateSpy = undefined; var Post = (function (_Model2) { + _inherits(Post, _Model2); + function Post() { _classCallCheck(this, Post); _get(Object.getPrototypeOf(Post.prototype), "constructor", this).apply(this, arguments); } - _inherits(Post, _Model2); - _createClass(Post, [{ key: "initialize", value: function initialize() { @@ -477,62 +477,62 @@ describe("Model(attributes, options)", function () { describe("(Associations)", function () { var Street = (function (_Model3) { + _inherits(Street, _Model3); + function Street() { _classCallCheck(this, Street); _get(Object.getPrototypeOf(Street.prototype), "constructor", this).apply(this, arguments); } - _inherits(Street, _Model3); - return Street; })(_2["default"]); var Driver = (function (_Model4) { + _inherits(Driver, _Model4); + function Driver() { _classCallCheck(this, Driver); _get(Object.getPrototypeOf(Driver.prototype), "constructor", this).apply(this, arguments); } - _inherits(Driver, _Model4); - return Driver; })(_2["default"]); var Truck = (function (_Model5) { + _inherits(Truck, _Model5); + function Truck() { _classCallCheck(this, Truck); _get(Object.getPrototypeOf(Truck.prototype), "constructor", this).apply(this, arguments); } - _inherits(Truck, _Model5); - return Truck; })(_2["default"]); var Wheel = (function (_Model6) { + _inherits(Wheel, _Model6); + function Wheel() { _classCallCheck(this, Wheel); _get(Object.getPrototypeOf(Wheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(Wheel, _Model6); - return Wheel; })(_2["default"]); var SteeringWheel = (function (_Model7) { + _inherits(SteeringWheel, _Model7); + function SteeringWheel() { _classCallCheck(this, SteeringWheel); _get(Object.getPrototypeOf(SteeringWheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(SteeringWheel, _Model7); - return SteeringWheel; })(_2["default"]); @@ -1083,7 +1083,9 @@ describe("Model(attributes, options)", function () { }); it("should return the model to support chaining", function () { - user.include.apply(user, _toConsumableArray(associationNames)).should.equal(user); + var _user; + + (_user = user).include.apply(_user, _toConsumableArray(associationNames)).should.equal(user); }); it("should fetch belongsTo associations", function (done) { @@ -1162,9 +1164,9 @@ describe("Model(attributes, options)", function () { var Post = function Post() { _classCallCheck(this, Post); - }; + } // Needed to mock a through association that is incomplete - // Needed to mock a through association that is incomplete + ; (function () { user.hasMany("posts", Post).through("photos"); @@ -1216,14 +1218,14 @@ describe("Model(attributes, options)", function () { describe(".delete(callback)", function () { describe("(when dependent is declared on the association)", function () { var Account = (function (_Model8) { + _inherits(Account, _Model8); + function Account() { _classCallCheck(this, Account); _get(Object.getPrototypeOf(Account.prototype), "constructor", this).apply(this, arguments); } - _inherits(Account, _Model8); - _createClass(Account, [{ key: "initialize", value: function initialize() { @@ -1240,14 +1242,14 @@ describe("Model(attributes, options)", function () { })(_2["default"]); var ForumUser = (function (_Model9) { + _inherits(ForumUser, _Model9); + function ForumUser() { _classCallCheck(this, ForumUser); _get(Object.getPrototypeOf(ForumUser.prototype), "constructor", this).apply(this, arguments); } - _inherits(ForumUser, _Model9); - _createClass(ForumUser, [{ key: "initialize", value: function initialize() { @@ -1265,14 +1267,14 @@ describe("Model(attributes, options)", function () { })(_2["default"]); var Post = (function (_Model10) { + _inherits(Post, _Model10); + function Post() { _classCallCheck(this, Post); _get(Object.getPrototypeOf(Post.prototype), "constructor", this).apply(this, arguments); } - _inherits(Post, _Model10); - _createClass(Post, [{ key: "initialize", value: function initialize() { @@ -1351,14 +1353,14 @@ describe("Model(attributes, options)", function () { describe("(when .softDelete is not called)", function () { var Post = (function (_Model11) { + _inherits(Post, _Model11); + function Post() { _classCallCheck(this, Post); _get(Object.getPrototypeOf(Post.prototype), "constructor", this).apply(this, arguments); } - _inherits(Post, _Model11); - return Post; })(_2["default"]); @@ -1379,14 +1381,14 @@ describe("Model(attributes, options)", function () { var post = undefined; var Post = (function (_Model12) { + _inherits(Post, _Model12); + function Post() { _classCallCheck(this, Post); _get(Object.getPrototypeOf(Post.prototype), "constructor", this).apply(this, arguments); } - _inherits(Post, _Model12); - _createClass(Post, [{ key: "initialize", value: function initialize() { diff --git a/es5/spec/model/fetch.spec.js b/es5/spec/model/fetch.spec.js index 96cd009..9f83f47 100644 --- a/es5/spec/model/fetch.spec.js +++ b/es5/spec/model/fetch.spec.js @@ -8,7 +8,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } var _sinon = require("sinon"); @@ -89,14 +89,14 @@ describe(".fetch(callback)", function () { deleteQuerySpy = undefined; var Post = (function (_Model) { + _inherits(Post, _Model); + function Post() { _classCallCheck(this, Post); _get(Object.getPrototypeOf(Post.prototype), "constructor", this).apply(this, arguments); } - _inherits(Post, _Model); - _createClass(Post, [{ key: "initialize", value: function initialize() { diff --git a/es5/spec/model/save.spec.js b/es5/spec/model/save.spec.js index 07eca71..b6c13b7 100644 --- a/es5/spec/model/save.spec.js +++ b/es5/spec/model/save.spec.js @@ -8,7 +8,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } @@ -97,14 +97,14 @@ describe(".save(callback)", function () { steeringWheel = undefined; var TruckOwner = (function (_Model) { + _inherits(TruckOwner, _Model); + function TruckOwner() { _classCallCheck(this, TruckOwner); _get(Object.getPrototypeOf(TruckOwner.prototype), "constructor", this).apply(this, arguments); } - _inherits(TruckOwner, _Model); - _createClass(TruckOwner, [{ key: "associate", value: function associate() { @@ -117,14 +117,14 @@ describe(".save(callback)", function () { })(_2["default"]); var Truck = (function (_Model2) { + _inherits(Truck, _Model2); + function Truck() { _classCallCheck(this, Truck); _get(Object.getPrototypeOf(Truck.prototype), "constructor", this).apply(this, arguments); } - _inherits(Truck, _Model2); - _createClass(Truck, [{ key: "associate", value: function associate() { @@ -139,14 +139,14 @@ describe(".save(callback)", function () { })(_2["default"]); var Owner = (function (_Model3) { + _inherits(Owner, _Model3); + function Owner() { _classCallCheck(this, Owner); _get(Object.getPrototypeOf(Owner.prototype), "constructor", this).apply(this, arguments); } - _inherits(Owner, _Model3); - _createClass(Owner, [{ key: "associate", value: function associate() { @@ -159,14 +159,14 @@ describe(".save(callback)", function () { })(_2["default"]); var Wheel = (function (_Model4) { + _inherits(Wheel, _Model4); + function Wheel() { _classCallCheck(this, Wheel); _get(Object.getPrototypeOf(Wheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(Wheel, _Model4); - _createClass(Wheel, [{ key: "associate", value: function associate() { @@ -184,14 +184,14 @@ describe(".save(callback)", function () { })(_2["default"]); var SteeringWheel = (function (_Model5) { + _inherits(SteeringWheel, _Model5); + function SteeringWheel() { _classCallCheck(this, SteeringWheel); _get(Object.getPrototypeOf(SteeringWheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(SteeringWheel, _Model5); - _createClass(SteeringWheel, [{ key: "associate", value: function associate() { diff --git a/es5/spec/testClasses.js b/es5/spec/testClasses.js index b7a2668..381077e 100644 --- a/es5/spec/testClasses.js +++ b/es5/spec/testClasses.js @@ -12,7 +12,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } var _ = require("../../"); @@ -25,14 +25,14 @@ var _2 = _interopRequireDefault(_); /* Simple Example */ var User = (function (_Model) { + _inherits(User, _Model); + function User() { _classCallCheck(this, User); _get(Object.getPrototypeOf(User.prototype), "constructor", this).apply(this, arguments); } - _inherits(User, _Model); - _createClass(User, [{ key: "associate", value: function associate() { @@ -67,14 +67,14 @@ var User = (function (_Model) { exports.User = User; var Address = (function (_Model2) { + _inherits(Address, _Model2); + function Address() { _classCallCheck(this, Address); _get(Object.getPrototypeOf(Address.prototype), "constructor", this).apply(this, arguments); } - _inherits(Address, _Model2); - _createClass(Address, [{ key: "associate", value: function associate() { @@ -94,14 +94,14 @@ var Address = (function (_Model2) { exports.Address = Address; var PostalCode = (function (_Model3) { + _inherits(PostalCode, _Model3); + function PostalCode() { _classCallCheck(this, PostalCode); _get(Object.getPrototypeOf(PostalCode.prototype), "constructor", this).apply(this, arguments); } - _inherits(PostalCode, _Model3); - _createClass(PostalCode, [{ key: "associate", value: function associate() { @@ -139,14 +139,14 @@ var PhotoLike = (function () { exports.PhotoLike = PhotoLike; var Photo = (function (_Model4) { + _inherits(Photo, _Model4); + function Photo() { _classCallCheck(this, Photo); _get(Object.getPrototypeOf(Photo.prototype), "constructor", this).apply(this, arguments); } - _inherits(Photo, _Model4); - _createClass(Photo, [{ key: "associate", value: function associate() { @@ -173,14 +173,14 @@ var Photo = (function (_Model4) { exports.Photo = Photo; var Comment = (function (_Model5) { + _inherits(Comment, _Model5); + function Comment() { _classCallCheck(this, Comment); _get(Object.getPrototypeOf(Comment.prototype), "constructor", this).apply(this, arguments); } - _inherits(Comment, _Model5); - _createClass(Comment, [{ key: "associate", value: function associate() { diff --git a/es5/spec/validation/isPresent.spec.js b/es5/spec/validation/isPresent.spec.js index 1aee2a6..267c2f9 100644 --- a/es5/spec/validation/isPresent.spec.js +++ b/es5/spec/validation/isPresent.spec.js @@ -8,7 +8,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -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) subClass.__proto__ = superClass; } +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; } var _libValidationIsPresentJs = require("../../lib/validation/isPresent.js"); @@ -36,38 +36,38 @@ var databaseConfig = { describe("isPresent(item, callback)", function () { var Wheel = (function (_Model) { + _inherits(Wheel, _Model); + function Wheel() { _classCallCheck(this, Wheel); _get(Object.getPrototypeOf(Wheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(Wheel, _Model); - return Wheel; })(_2["default"]); var SteeringWheel = (function (_Model2) { + _inherits(SteeringWheel, _Model2); + function SteeringWheel() { _classCallCheck(this, SteeringWheel); _get(Object.getPrototypeOf(SteeringWheel.prototype), "constructor", this).apply(this, arguments); } - _inherits(SteeringWheel, _Model2); - return SteeringWheel; })(_2["default"]); var Street = (function (_Model3) { + _inherits(Street, _Model3); + function Street() { _classCallCheck(this, Street); _get(Object.getPrototypeOf(Street.prototype), "constructor", this).apply(this, arguments); } - _inherits(Street, _Model3); - _createClass(Street, [{ key: "associate", value: function associate() { @@ -79,14 +79,14 @@ describe("isPresent(item, callback)", function () { })(_2["default"]); var Truck = (function (_Model4) { + _inherits(Truck, _Model4); + function Truck() { _classCallCheck(this, Truck); _get(Object.getPrototypeOf(Truck.prototype), "constructor", this).apply(this, arguments); } - _inherits(Truck, _Model4); - _createClass(Truck, [{ key: "associate", value: function associate() { diff --git a/es6/lib/model/addAssociation.js b/es6/lib/model/addAssociation.js index 98f64a6..4aa2be7 100644 --- a/es6/lib/model/addAssociation.js +++ b/es6/lib/model/addAssociation.js @@ -2,6 +2,7 @@ import inflect from "jargon"; import AssociationSetter from "../associationSetter.js"; import Collection from "../collection.js"; +import privateData from "incognito"; //private functions function getSetterFunctionHasOne(association, privateAssociationName, privateImplicitAssociationName) { @@ -202,7 +203,7 @@ export default function addAssociation (associationDetails) { this[associationDetails.name] = associationDetails.value; - this._associations[associationDetails.name] = association; + privateData(this)._associations[associationDetails.name] = association; return associationSetter; } diff --git a/es6/lib/model/fetch.js b/es6/lib/model/fetch.js index 75e2ca8..ad46495 100644 --- a/es6/lib/model/fetch.js +++ b/es6/lib/model/fetch.js @@ -1,6 +1,6 @@ import flowsync from "flowsync"; import inflect from "jargon"; - +import privateData from "incognito"; import ModelFinder from "../modelFinder.js"; import symbols from "./symbols"; @@ -176,7 +176,9 @@ function fetchBy(fields = [this.primaryKey], callback = undefined) { } }, this); - if(this._softDelete) { + const _ = privateData(this); + + if(_._softDelete) { chain = chain.whereNull(inflect("deletedAt").snake.toString()); } @@ -188,13 +190,13 @@ function fetchBy(fields = [this.primaryKey], callback = undefined) { } else { this[symbols.parseAttributesFromFields](records[0]); - if (this._includeAssociations.length > 0) { + if (_._includeAssociations.length > 0) { const associations = this.associations; /* We'll be putting all of our Async tasks into this */ const fetchTasks = []; - this._includeAssociations.forEach((associationName) => { + _._includeAssociations.forEach((associationName) => { const association = associations[associationName]; diff --git a/es6/lib/model/index.js b/es6/lib/model/index.js index fb96ae6..bc817f2 100644 --- a/es6/lib/model/index.js +++ b/es6/lib/model/index.js @@ -4,6 +4,7 @@ import flowsync from "flowsync"; import Datetime from "fleming"; import inflect from "jargon"; import Quirk from "quirk"; +import privateData from "incognito"; import symbols from "./symbols"; @@ -17,25 +18,18 @@ export default class Model { * @constructor */ constructor(initialAttributes) { - [ - "_validations", - "_associations" - ].forEach((privatePropertyName) => { - Object.defineProperty(this, privatePropertyName, { - writable: true, - enumerable: false, - value: {} - }); - }); + const _ = privateData(this); + _._validations = {}; + _._associations = {}; + _._includeAssociations = []; + _._tableName = null; + _._primaryKey = null; + _._softDelete = null; + /** * Define dynamic properties */ Object.defineProperties(this, { - "_includeAssociations": { - enumerable: false, - writable: true, - value: [] - }, "additionalAttributes": { enumerable: false, //this fix db related issues @@ -65,43 +59,27 @@ export default class Model { get: this[symbols.validations] }, - "_tableName": { - enumerable: false, - writable: true - }, - "tableName": { get: () => { - return this._tableName || inflect(this.constructor.name).plural.snake.toString(); + return _._tableName || inflect(this.constructor.name).plural.snake.toString(); }, set: (newTableName) => { - this._tableName = newTableName; + _._tableName = newTableName; } }, - "_primaryKey": { - enumerable: false, - writable: true - }, - "primaryKey": { get: () => { - return this._primaryKey || "id"; + return _._primaryKey || "id"; }, set: (newPrimaryKey) => { - this._primaryKey = newPrimaryKey; + _._primaryKey = newPrimaryKey; } }, - "_softDelete": { - enumerable: false, - writable: true, - value: false - }, - "softDelete": { get: () => { - this._softDelete = true; + _._softDelete = true; } } }); @@ -142,13 +120,14 @@ export default class Model { } ensure(attributeName, validatorFunction, validatorMessage) { - this._validations[attributeName] = this._validations[attributeName] || []; + const _ = privateData(this); + _._validations[attributeName] = _._validations[attributeName] || []; let validatorDetails = {validator: validatorFunction}; if (validatorMessage) { validatorDetails.message = validatorMessage; } - this._validations[attributeName].push(validatorDetails); + _._validations[attributeName].push(validatorDetails); } /** @@ -177,7 +156,8 @@ export default class Model { * @param {Function(invalidAttributeList)} callback Callback returning the invalid attribute list. */ invalidAttributes(callback) { - const attributeNamesWithValidators = Object.keys(this._validations); + const _ = privateData(this); + const attributeNamesWithValidators = Object.keys(_._validations); const compileInvalidAttributeList = (errors, validatorMessages) => { if (errors) { @@ -199,7 +179,7 @@ export default class Model { }; const performValidationsForAttribute = (attributeName, done) => { - const attributeValidations = this._validations[attributeName]; + const attributeValidations = _._validations[attributeName]; const performValidation = (validation, returnValue) => { const validator = validation.validator; @@ -237,12 +217,12 @@ export default class Model { } include(...associationNames) { - this._includeAssociations = associationNames; + privateData(this)._includeAssociations = associationNames; return this; } delete(callback) { - if(this._softDelete) { + if(privateData(this)._softDelete) { if (!this.constructor.database) { throw new Error("Cannot delete without Model.database set."); } if(this[this.primaryKey]) { @@ -314,7 +294,7 @@ export default class Model { } [symbols.associations]() { - return this._associations; + return privateData(this)._associations; } [symbols.properties]() { @@ -322,13 +302,13 @@ export default class Model { } [symbols.validations]() { - return this._validations; + return privateData(this)._validations; } [symbols.attributes]() { var attributes = {}; this.properties.forEach((propertyName) => { - if(!this._associations[propertyName]) { + if(!privateData(this)._associations[propertyName]) { attributes[propertyName] = this[propertyName]; } }); @@ -424,11 +404,13 @@ export default class Model { } }); + const _ = privateData(this); + //add belongsTo associations and remove others Object.keys(this.associations).forEach((associationName) => { let relatedModel = this[associationName]; let foreignKeyField = inflect(associationName).foreignKey.toString(); - if(this._associations[associationName].type === "belongsTo") { + if(_._associations[associationName].type === "belongsTo") { //try with relatedModel and relatedModel.id if(relatedModel && relatedModel.id) { fieldAttributes[foreignKeyField] = relatedModel.id;