Skip to content

Commit

Permalink
add bithound
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Aug 5, 2015
1 parent 2d50c89 commit 05bac3b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 74 deletions.
1 change: 1 addition & 0 deletions es5/lib/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ function fetchBy() {
}

fetchTasks.push(function (finished) {
//call the fetch function for the correct association type
fetchByAssociations[association.type].call(_this5, associationName, associations, finished);
});
});
Expand Down
166 changes: 92 additions & 74 deletions es5/lib/model/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,96 +23,114 @@ var _symbols = require("./symbols");

var _symbols2 = _interopRequireDefault(_symbols);

function save(callback) {
//private methods
function propagate(callback) {
//disabling this rule because break is not necessary when return is present
/* eslint-disable no-fallthrough */
this[_symbols2["default"].callDeep]("save", function (associationDetails) {
switch (associationDetails.type) {
case "hasOne":
return true;
case "hasMany":
if (associationDetails.through === undefined) {
return true;
} else {
return false;
}
case "belongsTo":
return false;
}
}, callback);
}

function saveOrUpdate(callback) {
var _this = this;

if (!this.constructor.database) {
throw new Error("Cannot save without Model.database set.");
}
var now = new _fleming2["default"]();
if (this.isNew) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

_flowsync2["default"].series([function (next) {
_this.beforeValidation(next);
}, function (next) {
_this.isValid(function (valid) {
if (valid) {
next();
this.constructor.database.insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
_this.invalidAttributes(function (invalidAttributeList) {
var hasInvalidAttributes = Object.keys(invalidAttributeList).length > 0;

if (hasInvalidAttributes) {
var errorPrefix = _this.constructor.name + " is invalid";
var multiError = new _blunder2["default"]([], errorPrefix);
for (var invalidAttributeName in invalidAttributeList) {
var invalidAttributeMessages = invalidAttributeList[invalidAttributeName];

for (var index in invalidAttributeMessages) {
var invalidAttributeMessage = invalidAttributeMessages[index];
var error = new Error(invalidAttributeName + " " + invalidAttributeMessage);
multiError.push(error);
}
}
next(multiError);
} else {
next();
}
});
_this[_this.primaryKey] = ids[0];
callback();
}
});
}, function (next) {
_this.beforeSave(next);
}, function (next) {
if (_this.isNew) {
var now = new _fleming2["default"]();
_this.createdAt = now.toDate();
var fieldAttributes = _this[_symbols2["default"].getFieldAttributes]();

_this.constructor.database.insert(fieldAttributes).into(_this.tableName).results(function (error, ids) {
if (error) {
next(error);
} else {
this.updatedAt = now.toDate();
var attributes = this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};

for (var attributeName in attributes) {
if (attributeName !== this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
}
}

this.constructor.database.update(updateAttributes).into(this.tableName).where(this.primaryKey, "=", this[this.primaryKey]).results(callback);
}
}

function validate(callback) {
var _this2 = this;

this.isValid(function (valid) {
if (valid) {
callback();
} else {
_this2.invalidAttributes(function (invalidAttributeList) {
var hasInvalidAttributes = Object.keys(invalidAttributeList).length > 0;

if (hasInvalidAttributes) {
var errorPrefix = _this2.constructor.name + " is invalid";
var multiError = new _blunder2["default"]([], errorPrefix);
for (var invalidAttributeName in invalidAttributeList) {
var invalidAttributeMessages = invalidAttributeList[invalidAttributeName];

for (var index in invalidAttributeMessages) {
var invalidAttributeMessage = invalidAttributeMessages[index];
var error = new Error(invalidAttributeName + " " + invalidAttributeMessage);
multiError.push(error);
}
}
callback(multiError);
} else {
_this[_this.primaryKey] = ids[0];
next();
callback();
}
});
} else {
var now = new _fleming2["default"]();
_this.updatedAt = now.toDate();
var attributes = _this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};

for (var attributeName in attributes) {
if (attributeName !== _this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
}
}

_this.constructor.database.update(updateAttributes).into(_this.tableName).where(_this.primaryKey, "=", _this[_this.primaryKey]).results(next);
}
});
}

//public save method

function save(callback) {
var _this3 = this;

if (!this.constructor.database) {
throw new Error("Cannot save without Model.database set.");
}

_flowsync2["default"].series([function (next) {
_this3.beforeValidation(next);
}, function (next) {
//disabling this rule because break is not necessary when return is present
/* eslint-disable no-fallthrough */
_this[_symbols2["default"].callDeep]("save", function (associationDetails) {
switch (associationDetails.type) {
case "hasOne":
return true;
case "hasMany":
if (associationDetails.through === undefined) {
return true;
} else {
return false;
}
case "belongsTo":
return false;
}
}, next);
validate.call(_this3, next);
}, function (next) {
_this3.beforeSave(next);
}, function (next) {
saveOrUpdate.call(_this3, next);
}, function (next) {
propagate.call(_this3, next);
}, function (next) {
_this.afterSave(next);
_this3.afterSave(next);
}], function (errors) {
if (errors) {
callback(errors);
} else {
callback(undefined, _this);
callback(undefined, _this3);
}
});
}
Expand Down

0 comments on commit 05bac3b

Please sign in to comment.