model set up and associations

This commit is contained in:
Arjun Patel
2019-01-15 23:14:05 -08:00
parent a4e4394329
commit 86dd4c8cdb
8 changed files with 59 additions and 6 deletions
+19 -1
View File
@@ -1,5 +1,23 @@
module.exports = models => { module.exports = models => {
const { Donors, Grants } = models; const { Donors, Grants, Causes, Regions, Organizations } = models;
Donors.hasMany(Grants, { foreignKey: 'donor_id' }); Donors.hasMany(Grants, { foreignKey: 'donor_id' });
Grants.belongsToMany(Causes, {
through: 'grants_causes',
foreignKey: 'grant_id',
otherKey: 'cause_id'
});
Grants.belongsToMany(Regions, {
through: 'grants_regions',
foreignKey: 'grant_id',
otherKey: 'region_id'
});
Grants.belongsToMany(Organizations, {
through: 'grants_organizations',
foreignKey: 'grant_id',
otherKey: 'organization_id'
});
}; };
+9
View File
@@ -1,6 +1,9 @@
const Sequelize = require('sequelize'), const Sequelize = require('sequelize'),
DonorsModel = require('../models/donors.model.js'), DonorsModel = require('../models/donors.model.js'),
GrantsModel = require('../models/grants.model.js'), GrantsModel = require('../models/grants.model.js'),
CausesModel = require('../models/causes.model.js'),
RegionsModel = require('../models/regions.model.js'),
OrganizationsModel = require('../models/organizations.model.js'),
associate = require('./associate'); associate = require('./associate');
const connection_uri = process.env.JAWSDB_MARIA_URL; const connection_uri = process.env.JAWSDB_MARIA_URL;
@@ -32,8 +35,14 @@ const db = {};
//creating tables/models from imported functions //creating tables/models from imported functions
const Donors = DonorsModel(sequelize, Sequelize); const Donors = DonorsModel(sequelize, Sequelize);
const Grants = GrantsModel(sequelize, Sequelize); const Grants = GrantsModel(sequelize, Sequelize);
const Causes = CausesModel(sequelize, Sequelize);
const Regions = RegionsModel(sequelize, Sequelize);
const Organizations = OrganizationsModel(sequelize, Sequelize);
db.Donors = Donors; db.Donors = Donors;
db.Grants = Grants; db.Grants = Grants;
db.Causes = Causes;
db.Regions = Regions;
db.Organizations = Organizations;
// make associations with created models // make associations with created models
associate(db); associate(db);
@@ -22,6 +22,7 @@ exports.findByDonorId = (req, res, next) => {
} }
}) })
.then(grants => { .then(grants => {
const grants_full_info = grants.map(grant => {});
res.status(200).json({ res.status(200).json({
grants, grants,
number_grants: grants.length number_grants: grants.length
+3 -1
View File
@@ -16,7 +16,9 @@ module.exports = (sequelize, DataTypes) => {
}, },
email: { email: {
type: DataTypes.STRING, type: DataTypes.STRING,
isEmail: true, validate: {
isEmail: true
},
allowNull: false allowNull: false
}, },
password: { password: {
+12 -3
View File
@@ -18,9 +18,18 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false, allowNull: false,
default: new Date() default: new Date()
}, },
monthly: DataTypes.BOOLEAN, monthly: {
num_causes: DataTypes.INTEGER, type: DataTypes.BOOLEAN,
num_regions: DataTypes.INTEGER allowNull: false
},
num_causes: {
type: DataTypes.INTEGER,
allowNull: false
},
num_regions: {
type: DataTypes.INTEGER,
allowNull: false
}
}); });
return Grants; return Grants;
}; };
View File
+14
View File
@@ -0,0 +1,14 @@
module.exports = (sequelize, DataTypes) => {
const Organizations = sequelize.define('organizations', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: { type: DataTypes.STRING, allowNull: false },
short_description: DataTypes.STRING,
primary_cause: { type: DataTypes.STRING, allowNull: false },
primary_region: { type: DataTypes.STRING, allowNull: false }
});
return Organizations;
};
+1 -1
View File
@@ -11,7 +11,7 @@ router.post('/', donors.create);
// Retrieve all Donors // Retrieve all Donors
router.get('/', checkAuth, donors.findAll); router.get('/', checkAuth, donors.findAll);
// Retrieve grants with cause and region and charity details by donor_id // Retrieve grants with causes and regions and charities details by donor_id
router.get('/grants/:donor_id', checkAuth, grants.findByDonorId); router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
/** Create grants with following body: /** Create grants with following body: