model set up and associations
This commit is contained in:
+19
-1
@@ -1,5 +1,23 @@
|
||||
module.exports = models => {
|
||||
const { Donors, Grants } = models;
|
||||
const { Donors, Grants, Causes, Regions, Organizations } = models;
|
||||
|
||||
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'
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
const Sequelize = require('sequelize'),
|
||||
DonorsModel = require('../models/donors.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');
|
||||
|
||||
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
||||
@@ -32,8 +35,14 @@ const db = {};
|
||||
//creating tables/models from imported functions
|
||||
const Donors = DonorsModel(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.Grants = Grants;
|
||||
db.Causes = Causes;
|
||||
db.Regions = Regions;
|
||||
db.Organizations = Organizations;
|
||||
|
||||
// make associations with created models
|
||||
associate(db);
|
||||
|
||||
@@ -22,6 +22,7 @@ exports.findByDonorId = (req, res, next) => {
|
||||
}
|
||||
})
|
||||
.then(grants => {
|
||||
const grants_full_info = grants.map(grant => {});
|
||||
res.status(200).json({
|
||||
grants,
|
||||
number_grants: grants.length
|
||||
|
||||
@@ -16,7 +16,9 @@ module.exports = (sequelize, DataTypes) => {
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
isEmail: true,
|
||||
validate: {
|
||||
isEmail: true
|
||||
},
|
||||
allowNull: false
|
||||
},
|
||||
password: {
|
||||
|
||||
@@ -18,9 +18,18 @@ module.exports = (sequelize, DataTypes) => {
|
||||
allowNull: false,
|
||||
default: new Date()
|
||||
},
|
||||
monthly: DataTypes.BOOLEAN,
|
||||
num_causes: DataTypes.INTEGER,
|
||||
num_regions: DataTypes.INTEGER
|
||||
monthly: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false
|
||||
},
|
||||
num_causes: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
num_regions: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
}
|
||||
});
|
||||
return Grants;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -11,7 +11,7 @@ router.post('/', donors.create);
|
||||
// Retrieve all Donors
|
||||
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);
|
||||
|
||||
/** Create grants with following body:
|
||||
|
||||
Reference in New Issue
Block a user