Skip to content

Commit

Permalink
Merge 23fe0ab into 82ba824
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Aug 4, 2015
2 parents 82ba824 + 23fe0ab commit ea2d692
Show file tree
Hide file tree
Showing 32 changed files with 4,251 additions and 3,986 deletions.
95 changes: 95 additions & 0 deletions es5/lib/associationSetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"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; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var ambiguous = Symbol(),
dependent = Symbol();

var AssociationSetter = (function () {
function AssociationSetter(association) {
_classCallCheck(this, AssociationSetter);

this.association = association;

switch (association.type) {
case "belongsTo":
Object.defineProperties(this, {
"ambiguous": {
get: this[ambiguous]
}
});
break;
case "hasOne":
case "hasMany":
Object.defineProperties(this, {
"dependent": {
get: this[dependent]
}
});
break;
}
}

_createClass(AssociationSetter, [{
key: "foreignName",
value: function foreignName(name) {
this.association.foreignName = name;
return this;
}
}, {
key: "where",
value: function where() {
for (var _len = arguments.length, options = Array(_len), _key = 0; _key < _len; _key++) {
options[_key] = arguments[_key];
}

this.association.where = options;
return this;
}
}, {
key: "andWhere",
value: function andWhere() {
this.association.andWhere = this.association.andWhere || [];

for (var _len2 = arguments.length, options = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
options[_key2] = arguments[_key2];
}

this.association.andWhere.push(options);
return this;
}
}, {
key: "through",
value: function through(associationName) {
this.association.through = associationName;
return this;
}
}, {
key: "as",
value: function as(associationName) {
this.association.as = associationName;
return this;
}
}, {
key: ambiguous,
value: function value() {
this.association.ambiguous = true;
}
}, {
key: dependent,
value: function value() {
this.association.dependent = true;
}
}]);

return AssociationSetter;
})();

exports["default"] = AssociationSetter;
module.exports = exports["default"];
74 changes: 34 additions & 40 deletions es5/lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ var _modelFinderJs = require("./modelFinder.js");

var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);

var _modelJs = require("./model.js");

var _modelJs2 = _interopRequireDefault(_modelJs);

var Collection = (function (_Array) {
function Collection(initialData) {
_classCallCheck(this, Collection);
Expand Down Expand Up @@ -90,7 +86,7 @@ var Collection = (function (_Array) {
modelName = model;
}

throw TypeError("The model " + modelName + " is not an instance of " + _this._modelConstructor.name + ", therefore, it cannot be pushed to this collection.");
throw new TypeError("The model " + modelName + " is not an instance of " + _this._modelConstructor.name + ", therefore, it cannot be pushed to this collection.");
}
}
}
Expand All @@ -101,48 +97,46 @@ var Collection = (function (_Array) {
value: function fetch(callback) {
var _this2 = this;

if (this.association) {
(function () {
var processWhereCondition = function processWhereCondition(value) {
if (typeof value === "string") {
var snakeCasedValue = (0, _jargon2["default"])(value).snake.toString();
return snakeCasedValue;
} else {
return value;
}
};

var modelFinder = new _modelFinderJs2["default"](_this2.association.constructor.database);
function processWhereCondition(value) {
if (typeof value === "string") {
var snakeCasedValue = (0, _jargon2["default"])(value).snake.toString();
return snakeCasedValue;
} else {
return value;
}
}

var query = modelFinder.find(_this2.association.constructor).where(_this2.association.foreignKey, "=", _this2.association.parent.id);
if (this.association) {
var modelFinder = new _modelFinderJs2["default"](this.association.constructor.database);

if (_this2.association.where) {
(function () {
var processedWhereConditions = _this2.association.where.map(processWhereCondition);
var self = _this2;
query.andWhere(function () {
var _this3 = this;
var query = modelFinder.find(this.association.constructor).where(this.association.foreignKey, "=", this.association.parent.id);

this.where.apply(this, _toConsumableArray(processedWhereConditions));
if (this.association.where) {
(function () {
var processedWhereConditions = _this2.association.where.map(processWhereCondition);
var self = _this2;
query.andWhere(function () {
var _this3 = this;

if (Array.isArray(self.association.andWhere)) {
self.association.andWhere.forEach(function (whereConditions) {
var processedAndWhereItem = whereConditions.map(processWhereCondition);
_this3.andWhere.apply(_this3, _toConsumableArray(processedAndWhereItem));
});
}
});
})();
}
this.where.apply(this, _toConsumableArray(processedWhereConditions));

query.results(function (error, models) {
_this2.splice(0, _this2.length);
models.forEach(function (model) {
_this2.push(model);
if (Array.isArray(self.association.andWhere)) {
self.association.andWhere.forEach(function (whereConditions) {
var processedAndWhereItem = whereConditions.map(processWhereCondition);
_this3.andWhere.apply(_this3, _toConsumableArray(processedAndWhereItem));
});
}
});
callback(error);
})();
}

query.results(function (error, models) {
_this2.splice(0, _this2.length);
models.forEach(function (model) {
_this2.push(model);
});
})();
callback(error);
});
} else {
throw new Error("Cannot fetch collection without an association set. Call Model.all instead.");
}
Expand Down
Loading

0 comments on commit ea2d692

Please sign in to comment.