Skip to content

Commit

Permalink
Merge 4a17a05 into 39d3367
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Aug 25, 2015
2 parents 39d3367 + 4a17a05 commit 39a72fd
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 56 deletions.
4 changes: 2 additions & 2 deletions es5/lib/model/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _jargon2 = _interopRequireDefault(_jargon);
function deleteSelf(callback) {
var _this = this;

if (!this.constructor.database) {
if (!this[_symbols2["default"].getDatabase]()) {
throw new Error("Cannot delete without Model.database set.");
}

Expand Down Expand Up @@ -80,7 +80,7 @@ function softDelete(callback) {
var now = new _fleming2["default"]();
var attributesToUpdate = {};
attributesToUpdate[(0, _jargon2["default"])("deletedAt").snake.toString()] = now.toDate();
_this2.constructor.database.update(attributesToUpdate).into(_this2.tableName).where(_this2.primaryKey, "=", _this2[_this2.primaryKey]).results(function (error, results) {
_this2[_symbols2["default"].getDatabase]().update(attributesToUpdate).into(_this2.tableName).where(_this2.primaryKey, "=", _this2[_this2.primaryKey]).results(function (error, results) {
if (error) {
next(error);
} else if (results === 0) {
Expand Down
14 changes: 7 additions & 7 deletions es5/lib/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var fetchByAssociations = {
function fetchByHasOne(associationName, associations, callback) {
var _this = this;

var modelFinder = new _modelFinderJs2["default"](this.constructor.database);
var modelFinder = new _modelFinderJs2["default"](this[_symbols2["default"].getDatabase]());
var association = associations[associationName];
var ModelClass = association.constructor;

Expand Down Expand Up @@ -99,7 +99,7 @@ function fetchByHasOne(associationName, associations, callback) {
}

function fetchWhere(modelClass, key, conditionType, ids, target, callback) {
var modelFinder = new _modelFinderJs2["default"](this.constructor.database);
var modelFinder = new _modelFinderJs2["default"](this[_symbols2["default"].getDatabase]());
modelFinder.find(modelClass).where(key, conditionType, ids).results(function (findErrors, resultModels) {
resultModels.forEach(function (model) {
target.push(model);
Expand All @@ -111,14 +111,13 @@ function fetchWhere(modelClass, key, conditionType, ids, target, callback) {
function fetchByHasMany(associationName, associations, callback) {
var _this3 = this;

var modelFinder = new _modelFinderJs2["default"](this.constructor.database);
var association = associations[associationName];

if (association.through) {
(function () {
var throughAssociation = associations[association.through];

modelFinder.find(throughAssociation.constructor).where(association.foreignId, _this3[_this3.primaryKey]).results(function (errors, models) {
throughAssociation.constructor.find.where(association.foreignId, _this3[_this3.primaryKey]).results(function (errors, models) {
if (models.length > 0) {
(function () {
var foreignAssociationName = association.as || associationName;
Expand Down Expand Up @@ -167,7 +166,7 @@ function fetchByHasMany(associationName, associations, callback) {
function fetchByBelongsTo(associationName, associations, callback) {
var _this4 = this;

var modelFinder = new _modelFinderJs2["default"](this.constructor.database);
var modelFinder = new _modelFinderJs2["default"](this[_symbols2["default"].getDatabase]());
var association = associations[associationName];

if (!this[association.foreignId]) {
Expand All @@ -188,11 +187,12 @@ function fetchBy() {
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) {
var database = this[_symbols2["default"].getDatabase]();
if (!database) {
throw new Error("Cannot fetch without Model.database set.");
}

var chain = this.constructor.database.select("*").from(this.tableName);
var chain = database.select("*").from(this.tableName);
fields.forEach(function (field, index) {
if (!_this5[field]) {
throw new Error("Cannot fetch this model by the '" + field + "' field because it is not set.");
Expand Down
30 changes: 27 additions & 3 deletions es5/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Model = (function () {
* @constructor
*/

function Model(initialAttributes) {
function Model(initialAttributes, options) {
var _this = this;

_classCallCheck(this, Model);
Expand All @@ -67,6 +67,10 @@ var Model = (function () {
_._primaryKey = null;
_._softDelete = null;

if (options !== undefined) {
_._database = options.database;
}

/**
* Define dynamic properties
*/
Expand Down Expand Up @@ -328,7 +332,15 @@ var Model = (function () {
/**
* Private Functionality
*/

}, {
key: _symbols2["default"].getDatabase,
value: function value() {
var database = (0, _incognito2["default"])(this)._database;
if (!database) {
database = this.constructor.database;
}
return database;
}
}, {
key: _symbols2["default"].setAttributes,
value: function value(newAttributes) {
Expand Down Expand Up @@ -487,9 +499,21 @@ Object.assign(Model.prototype, _defineProperty({
}, _symbols2["default"].addAssociation, require("./addAssociation.js")));

Object.defineProperties(Model, {
"database": {
get: function getDatabase() {
var database = this._database;
if (!database) {
database = Model._database;
}
return database;
},
set: function setDatabase(value) {
this._database = value;
}
},
"find": {
get: function modelFind() {
var modelQuery = new _modelFinderJs2["default"](Model.database);
var modelQuery = new _modelFinderJs2["default"](this.database);
return modelQuery.find(this);
}
}
Expand Down
10 changes: 7 additions & 3 deletions es5/lib/model/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var _fleming = require("fleming");

var _fleming2 = _interopRequireDefault(_fleming);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var _symbols = require("./symbols");

var _symbols2 = _interopRequireDefault(_symbols);
Expand Down Expand Up @@ -51,7 +55,7 @@ function saveOrUpdate(callback) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

this.constructor.database.insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
this[_symbols2["default"].getDatabase]().insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
Expand All @@ -70,7 +74,7 @@ function saveOrUpdate(callback) {
}
}

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

Expand Down Expand Up @@ -110,7 +114,7 @@ function validate(callback) {
function save(callback) {
var _this3 = this;

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

Expand Down
3 changes: 2 additions & 1 deletion es5/lib/model/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
properties: Symbol(),
validations: Symbol(),
isNew: Symbol(),
fetchBy: Symbol()
fetchBy: Symbol(),
getDatabase: Symbol()
};
74 changes: 62 additions & 12 deletions es5/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,56 @@ describe("Model(attributes, options)", function () {
users.length.should.equal(5);
});
});

describe("(with a different database for a model)", function () {
var Car = (function (_Model2) {
_inherits(Car, _Model2);

function Car() {
_classCallCheck(this, Car);

_get(Object.getPrototypeOf(Car.prototype), "constructor", this).apply(this, arguments);
}

return Car;
})(_2["default"]);

var car = undefined,
database = undefined,
query = undefined;

describe("(static way)", function () {
beforeEach(function () {
database = new _almaden2["default"](_databaseConfigJson2["default"]);
Car.database = database;
query = database.spy("select * from `cars`", []);
car = new Car();
});

it("should use the specific model class database", function (done) {
Car.find.all.results(function () {
query.callCount.should.equal(1);
done();
});
});
});

describe("(instance way)", function () {
beforeEach(function () {
database = new _almaden2["default"](_databaseConfigJson2["default"]);
Car.database = null;
car = new Car({ id: 2 }, { database: database });
query = database.spy("select * from `cars` where `id` = 2 limit 1", []);
});

it("should use the specific model instance database", function (done) {
car.fetch(function () {
query.callCount.should.equal(1);
done();
});
});
});
});
});
});

Expand All @@ -347,8 +397,8 @@ describe("Model(attributes, options)", function () {
validateSpy = undefined,
associateSpy = undefined;

var Post = (function (_Model2) {
_inherits(Post, _Model2);
var Post = (function (_Model3) {
_inherits(Post, _Model3);

function Post() {
_classCallCheck(this, Post);
Expand Down Expand Up @@ -412,8 +462,8 @@ describe("Model(attributes, options)", function () {
});

describe("(Associations)", function () {
var Street = (function (_Model3) {
_inherits(Street, _Model3);
var Street = (function (_Model4) {
_inherits(Street, _Model4);

function Street() {
_classCallCheck(this, Street);
Expand All @@ -424,8 +474,8 @@ describe("Model(attributes, options)", function () {
return Street;
})(_2["default"]);

var Driver = (function (_Model4) {
_inherits(Driver, _Model4);
var Driver = (function (_Model5) {
_inherits(Driver, _Model5);

function Driver() {
_classCallCheck(this, Driver);
Expand All @@ -436,8 +486,8 @@ describe("Model(attributes, options)", function () {
return Driver;
})(_2["default"]);

var Truck = (function (_Model5) {
_inherits(Truck, _Model5);
var Truck = (function (_Model6) {
_inherits(Truck, _Model6);

function Truck() {
_classCallCheck(this, Truck);
Expand All @@ -448,8 +498,8 @@ describe("Model(attributes, options)", function () {
return Truck;
})(_2["default"]);

var Wheel = (function (_Model6) {
_inherits(Wheel, _Model6);
var Wheel = (function (_Model7) {
_inherits(Wheel, _Model7);

function Wheel() {
_classCallCheck(this, Wheel);
Expand All @@ -460,8 +510,8 @@ describe("Model(attributes, options)", function () {
return Wheel;
})(_2["default"]);

var SteeringWheel = (function (_Model7) {
_inherits(SteeringWheel, _Model7);
var SteeringWheel = (function (_Model8) {
_inherits(SteeringWheel, _Model8);

function SteeringWheel() {
_classCallCheck(this, SteeringWheel);
Expand Down
2 changes: 1 addition & 1 deletion es5/spec/model/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe("Model(attributes, options)", function () {

describe("(Model.database not set)", function () {
beforeEach(function () {
delete _2["default"].database;
delete _2["default"]._database;
});

it("should throw an error", function () {
Expand Down
2 changes: 1 addition & 1 deletion es5/spec/model/destroy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
//
// describe("(Model.database not set)", () => {
// beforeEach(() => {
// delete Model.database;
// delete Model._database;
// });
//
// it("should throw an error", () => {
Expand Down
6 changes: 3 additions & 3 deletions es5/spec/model/fetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe(".fetch(callback)", function () {

describe("(Model.database not set)", function () {
beforeEach(function () {
delete _2["default"].database;
delete _2["default"]._database;
});

it("should throw an error", function () {
Expand Down Expand Up @@ -228,7 +228,7 @@ describe(".fetch(callback)", function () {

describe("(Model.database not set)", function () {
beforeEach(function () {
delete _2["default"].database;
delete _2["default"]._database;
});

it("should throw an error", function () {
Expand Down Expand Up @@ -305,7 +305,7 @@ describe(".fetch(callback)", function () {

describe("(Model.database not set)", function () {
beforeEach(function () {
delete _2["default"].database;
delete _2["default"]._database;
});

it("should throw an error", function () {
Expand Down
2 changes: 1 addition & 1 deletion es5/spec/model/save.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ describe("Model(attributes, options)", function () {

describe("(Model.database is NOT set)", function () {
beforeEach(function () {
delete _2["default"].database;
delete _2["default"]._database;
});

it("should call back with an error", function () {
Expand Down
4 changes: 2 additions & 2 deletions es6/lib/model/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import inflect from "jargon";
* @param {Function} callback
*/
export default function deleteSelf(callback) {
if (!this.constructor.database) { throw new Error("Cannot delete without Model.database set."); }
if (!this[symbols.getDatabase]()) { throw new Error("Cannot delete without Model.database set."); }

flowsync.series([
(done) => {
Expand Down Expand Up @@ -57,7 +57,7 @@ function softDelete(callback) {
let now = new Datetime();
let attributesToUpdate = {};
attributesToUpdate[inflect("deletedAt").snake.toString()] = now.toDate();
this.constructor.database
this[symbols.getDatabase]()
.update(attributesToUpdate)
.into(this.tableName)
.where(this.primaryKey, "=", this[this.primaryKey])
Expand Down
Loading

0 comments on commit 39a72fd

Please sign in to comment.