Files
old-spike/models/house.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-01-26 19:42:30 -06:00
import {
GraphQLNonNull,
2016-01-31 17:18:27 -06:00
GraphQLObjectType,
GraphQLInt,
GraphQLString
2016-01-26 19:42:30 -06:00
} from 'graphql';
import {
globalIdField,
2016-01-31 17:18:27 -06:00
connectionDefinitions,
connectionArgs
2016-01-26 19:42:30 -06:00
} from 'graphql-relay';
2016-01-31 17:18:27 -06:00
import extend from 'extend';
2016-01-27 20:15:09 -06:00
import DB from "./../config/database";
2016-01-31 17:18:27 -06:00
import {nodeInterface} from './../config/graphql/node';
const NAME = 'House';
2016-01-26 19:42:30 -06:00
/**
2016-01-31 17:18:27 -06:00
* Sequelize Definition
2016-01-26 19:42:30 -06:00
*/
2016-01-31 17:18:27 -06:00
var House = DB.sequelize.define(NAME, {
2016-01-26 19:42:30 -06:00
id: {
2016-01-27 20:15:09 -06:00
type: DB.Sequelize.INTEGER,
2016-01-26 19:42:30 -06:00
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
2016-01-27 20:15:09 -06:00
name: DB.Sequelize.STRING
2016-01-26 19:42:30 -06:00
}, {
2016-01-31 17:18:27 -06:00
paranoid: true,
underscored: true,
tableName: "houses",
2016-01-26 19:42:30 -06:00
instanceMethods: {
},
classMethods: {
2016-01-31 17:18:27 -06:00
set: ()=>{
2016-01-27 20:15:09 -06:00
House.hasMany(DB.PowerDatum, {as: 'PowerData'});
2016-01-31 17:18:27 -06:00
House.hasMany(DB.User, {as: 'Habitants'});
House.graphql_type = new GraphQLObjectType({
name: NAME,
description: 'A house',
fields: () => ({
id: globalIdField(NAME),
name: {
type: new GraphQLNonNull(GraphQLString)
},
power_data: {
type: connectionDefinitions({name: DB.PowerDatum.name, nodeType: DB.PowerDatum.graphql_type}).connectionType,
description: "Returns house's power data.",
args: connectionArgs,
resolve: (house, args) => {
return house.getPowerDataByTime(args);
}
},
habitants: {
type: connectionDefinitions({name: DB.User.name, nodeType: DB.User.graphql_type}).connectionType,
description: "Returns list of house's habitants.",
args: connectionArgs,
resolve: (house, args) => {
var params = extend({
order: 'name ASC',
limit: 50,
offset: 0,
}, args);
delete params.where; // don't allow any additional query params.
return house.getHabitants(params);
}
}
}),
interfaces: [nodeInterface]
});
},
getPowerDataByTime: (start_date, end_date, page)=>{
var params = extend({
order: 'time ASC',
limit: 500
}, args.page);
params.where = {time: {}};
if (start_date) params.where.time.$gt = moment.utc(start_date).toDate();
if (end_date) params.where.time.$lt = moment.utc(end_date).toDate();
return House.getPowerData(params);
2016-01-26 19:42:30 -06:00
}
}
});
2016-01-31 17:18:27 -06:00
House.name = NAME;
2016-01-27 20:15:09 -06:00
module.exports = House;