including CAIT data, models and seeds
This commit is contained in:
54
models/country.js
Normal file
54
models/country.js
Normal file
@@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
var Database = require("./../config/database");
|
||||
|
||||
class Country {
|
||||
|
||||
static associate(){
|
||||
var Datum = require("./datum");
|
||||
this.sql.hasOne(Datum.sql, {as: "Data", foreignKey: "id", constraints: false});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Country.sql = Database.sequelize.define('Country', {
|
||||
id: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
name: {
|
||||
type: Database.Sequelize.STRING,
|
||||
unique: true,
|
||||
allowNull: false
|
||||
}
|
||||
}, {tableName: "countries"});
|
||||
|
||||
Country.graphql = new GraphQLObjectType({
|
||||
name: "Country",
|
||||
description: "A world country",
|
||||
fields: ()=>{
|
||||
id: {
|
||||
type: new GraphQLNonNull(GraphQLInteger)
|
||||
},
|
||||
name: {
|
||||
type: new GraphQLNonNull(GraphQLString)
|
||||
},
|
||||
data: {
|
||||
type: new GraphQLList(Datum.graphql),
|
||||
args: {
|
||||
year: {
|
||||
type: GraphQLList(GraphQLInteger)
|
||||
}
|
||||
},
|
||||
resolve: (country, args)=>{
|
||||
debugger
|
||||
if (args.year){
|
||||
country.getData({where: args});
|
||||
} else {
|
||||
country.getData();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Country;
|
||||
78
models/datum.js
Normal file
78
models/datum.js
Normal file
@@ -0,0 +1,78 @@
|
||||
"use strict";
|
||||
var Database = require("./../config/database");
|
||||
|
||||
class Datum {
|
||||
static associate(){
|
||||
var Country = require("./country");
|
||||
this.sql.belongsTo(Country.sql, {foreignKey: "country_id", targetKey: "id", constraints: false});
|
||||
}
|
||||
}
|
||||
|
||||
Datum.sql = Database.sequelize.define('Datum', {
|
||||
id: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
country_id: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
unique: "country_year",
|
||||
references: {
|
||||
model: "countries",
|
||||
key: "id",
|
||||
allowNull: false
|
||||
}
|
||||
},
|
||||
year: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
unique: "country_year",
|
||||
allowNull: false
|
||||
},
|
||||
population: Database.Sequelize.BIGINT,
|
||||
gdp: Database.Sequelize.FLOAT,
|
||||
total_emissions: Database.Sequelize.FLOAT,
|
||||
energy_emissions: Database.Sequelize.FLOAT,
|
||||
industrial_emissions: Database.Sequelize.FLOAT,
|
||||
agriculture_emissions: Database.Sequelize.FLOAT,
|
||||
waste_emissions: Database.Sequelize.FLOAT,
|
||||
lucf_emissions: Database.Sequelize.FLOAT,
|
||||
energy: Database.Sequelize.FLOAT
|
||||
}, {tableName: "data"});
|
||||
|
||||
Datum.graphql = new GraphQLObjectType({
|
||||
name: "Datum",
|
||||
description: "A world country",
|
||||
fields: ()=>{
|
||||
year: {
|
||||
type: new GraphQLNonNull(GraphQLString)
|
||||
},
|
||||
population: {
|
||||
type: GraphQLInteger
|
||||
},
|
||||
gdp: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
total_emissions: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
energy_emissions: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
industrial_emissions: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
agriculture_emissions: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
waste_emissions: {
|
||||
type: GraphQLFloat
|
||||
},
|
||||
lucf_emissions: {
|
||||
type: GraphQLFloat
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
module.exports = Datum;
|
||||
49
models/house.js
Normal file
49
models/house.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
GraphQLString,
|
||||
GraphQLNonNull,
|
||||
GraphQLObjectType
|
||||
} from 'graphql';
|
||||
|
||||
import {
|
||||
fromGlobalId,
|
||||
globalIdField,
|
||||
nodeDefinitions,
|
||||
} from 'graphql-relay';
|
||||
|
||||
import Database from "./../config/database";
|
||||
import PowerData from "./power_datum"
|
||||
|
||||
/**
|
||||
* Define your own types here
|
||||
*/
|
||||
|
||||
var House = Database.sequelize.define('House', {
|
||||
id: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
name: Database.Sequelize.STRING
|
||||
}, {
|
||||
tableName: "houses",
|
||||
instanceMethods: {
|
||||
|
||||
},
|
||||
classMethods: {
|
||||
associate: function(){
|
||||
House.hasMany(PowerDatum, {as: 'PowerData'})
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
House.graphql_type = new GraphQLObjectType({
|
||||
name: 'House',
|
||||
description: 'A house',
|
||||
fields: () => ({
|
||||
id: globalIdField('House'),
|
||||
name: GraphQLNonNull(GraphQLInt)
|
||||
}),
|
||||
interfaces: [nodeInterface],
|
||||
});
|
||||
|
||||
export House;
|
||||
53
models/power_datum.js
Normal file
53
models/power_datum.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
GraphQLFloat,
|
||||
GraphQLInt,
|
||||
GraphQLNonNull,
|
||||
GraphQLObjectType
|
||||
} from 'graphql';
|
||||
|
||||
import {
|
||||
fromGlobalId,
|
||||
globalIdField,
|
||||
nodeDefinitions,
|
||||
} from 'graphql-relay';
|
||||
|
||||
import Database from "./../config/database";
|
||||
import PowerData from "./house"
|
||||
|
||||
/**
|
||||
* Define your own types here
|
||||
*/
|
||||
|
||||
var PowerDatum = Database.sequelize.define('Datum', {
|
||||
id: {
|
||||
type: Database.Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
time: Database.Sequelize.FLOAT,
|
||||
power: Database.Sequelize.FLOAT
|
||||
}, {
|
||||
tableName: "power_data",
|
||||
instanceMethods: {
|
||||
|
||||
},
|
||||
classMethods: {
|
||||
associate: function(){
|
||||
PowerDatum.belongsTo(House);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
PowerDatum.graphql_type = new GraphQLObjectType({
|
||||
name: 'Power Datum',
|
||||
description: 'A person who uses our app',
|
||||
fields: () => ({
|
||||
id: globalIdField('PowerDatum'),
|
||||
time: GraphQLInt,
|
||||
power: GraphQLFloat
|
||||
}),
|
||||
interfaces: [nodeInterface],
|
||||
});
|
||||
|
||||
export PowerDatum;
|
||||
Reference in New Issue
Block a user