Skip to content

Commit

Permalink
rename Models to models (linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
Florin Braghis committed Sep 14, 2011
1 parent ddf9f50 commit 0d2d0fc
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example/models/Airplane.js
@@ -0,0 +1,9 @@
var moose = require("../../lib");

//assume airplane has been loaded
module.exports = exports = (Airplane = moose.addModel(moose.getSchema("airplane")));

var fetchType = Airplane.fetchType;
Airplane.manyToOne("type", {model : "airplane_type",fetchType : fetchType.EAGER,key : {type_id : "id"}});
Airplane.oneToMany("legs", {model : "leg_instance",key : {type_id : "id"}});

13 changes: 13 additions & 0 deletions example/models/AirplaneType.js
@@ -0,0 +1,13 @@
var moose = require("../../lib");

module.exports = exports =(AirPlaneType = moose.addModel(moose.getSchema("airplane_type")));

var fetchType = AirPlaneType.fetchType;
AirPlaneType.manyToMany("supportedAirports", {
joinTable : "can_land",
fetchType : fetchType.EAGER,
model : "airport",
key : {airportCode : "typeId"}
});

AirPlaneType.oneToMany("airplanes", {model : "airplane", key : {type_id : "id"}});
13 changes: 13 additions & 0 deletions example/models/Airport.js
@@ -0,0 +1,13 @@
var moose = require("../../lib"),
expressPlugin = require("../plugins/ExpressPlugin");

module.exports = exports = (Airport = moose.addModel(moose.getSchema("airport"), {
plugins : [moose.plugins.CachePlugin, expressPlugin]
}));

var fetchType = Airport.fetchType;
Airport.manyToMany("supportedAirplaneTypes", {
joinTable : "can_land",
model : "airplane_type",
key : {airportCode : "typeId"}
});
3 changes: 3 additions & 0 deletions example/models/CanLand.js
@@ -0,0 +1,3 @@
var moose = require("../../lib");

module.exports = exports = moose.addModel(moose.getSchema("can_land"));
25 changes: 25 additions & 0 deletions example/models/Flight.js
@@ -0,0 +1,25 @@
var moose = require("../../lib"),
expressPlugin = require("../plugins/ExpressPlugin");

module.exports = exports = (Flight = moose.addModel(moose.getSchema("flight"), {
plugins : [ expressPlugin],
instance :{
toObject : function() {
var obj = this.super(arguments);
obj.legs = this.legs.map(function(l) {
return l.toObject();
});
return obj;
}
}
}));

var fetchType = Flight.fetchType;
Flight.oneToMany("legs", {
model : "flight_leg",
orderBy : "scheduledDepartureTime",
key : {id : "flightId"},
fetchType :fetchType.EAGER

});

33 changes: 33 additions & 0 deletions example/models/FlightLeg.js
@@ -0,0 +1,33 @@
var moose = require("../../lib");

module.exports = exports = (FlightLeg = moose.addModel(moose.getSchema("flight_leg"), {
plugins : [moose.plugins.CachePlugin],
instance : {
toObject : function(){
var obj = this.super(arguments);
delete obj.departureCode;
delete obj.arrivalCode;
obj.departs = this.departs.toObject();
obj.arrives = this.arrives.toObject();
return obj;
}
}
}));

var fetchType = FlightLeg.fetchType;
FlightLeg.oneToMany("legInstances", {
model : "leg_instance",
key : {id : "flightLegId"}
});

FlightLeg.manyToOne("departs", {
model : "airport",
fetchType : fetchType.EAGER,
key : {departureCode : "airportCode"}
});

FlightLeg.manyToOne("arrives", {
model : "airport",
fetchType : fetchType.EAGER,
key : {arrivalCode : "airportCode"}
});
16 changes: 16 additions & 0 deletions example/models/LegInstance.js
@@ -0,0 +1,16 @@
var moose = require("../../lib");

module.exports = exports = (LegInstance = moose.addModel(moose.getSchema("leg_instance")));

var fetchType = LegInstance.fetchType;
LegInstance.manyToOne("airplane", {
model : "airplane",
fetchType : fetchType.EAGER,
key : {id : "type_id"}
});

LegInstance.manyToOne("flightLeg", {
model : "flight_leg",
fetchType : fetchType.EAGER,
key : {flightLegId : "id"}
});
25 changes: 25 additions & 0 deletions example/models/index.js
@@ -0,0 +1,25 @@
var moose = require("../../lib"), comb = require("comb");



//all needed tables
var tables = ["leg_instance","flight_leg","flight","airplane","airplane_type","can_land","airport"];
/*
* Helper function to load our models
* */
exports.load = function(){
var ret = new comb.Promise();
//load the tables so they are directly available for our models
moose.loadSchemas(tables).then(function(){
//now that the tables are loaded load the models!
require("./Airport");
require("./CanLand");
require("./AirplaneType");
require("./AirPlane");
require("./FlightLeg");
require("./LegInstance");
require("./Flight");
ret.callback();
}, comb.hitch(ret, "errback"));
return ret;
};

0 comments on commit 0d2d0fc

Please sign in to comment.