From 52c284f02ad1a3fabfc40df40e7b608552fa5c7f Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sat, 16 Mar 2019 21:55:50 -0700 Subject: [PATCH] using the sequelize auto index file for model/db config --- app/config/associate.js | 15 ++-------- models/donor.model.js | 2 +- models/index.js | 36 +++++++++++++---------- models/organization.model.js | 8 ++++++ models/project.model.js | 56 ++++++++++++++++++++++++++++++++++++ server.js | 4 ++- 6 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 models/project.model.js diff --git a/app/config/associate.js b/app/config/associate.js index ee12ef2..ec03f8f 100644 --- a/app/config/associate.js +++ b/app/config/associate.js @@ -7,23 +7,12 @@ module.exports = models => { Organization, Charge, PaymentPlan, - GrantsOrganizations + GrantsOrganizations, + Project } = models; Donor.hasMany(Grant, { foreignKey: 'donor_id' }); - Grant.belongsToMany(Cause, { - through: 'grants_causes', - foreignKey: 'grant_id', - otherKey: 'cause_id' - }); - - Grant.belongsToMany(Region, { - through: 'grants_regions', - foreignKey: 'grant_id', - otherKey: 'region_id' - }); - Grant.belongsToMany(Organization, { through: 'grants_organizations', foreignKey: 'grant_id', diff --git a/models/donor.model.js b/models/donor.model.js index 5c66b9d..60bddd3 100644 --- a/models/donor.model.js +++ b/models/donor.model.js @@ -36,7 +36,7 @@ module.exports = (sequelize, DataTypes) => { allowNull: false }, age: DataTypes.INTEGER, - phone: DataTypes.DataTypes.STRING(20), + phone: DataTypes.STRING(20), address: DataTypes.STRING, city: DataTypes.STRING, diff --git a/models/index.js b/models/index.js index c1a3d6d..299671d 100644 --- a/models/index.js +++ b/models/index.js @@ -5,30 +5,36 @@ const path = require('path'); const Sequelize = require('sequelize'); const basename = path.basename(__filename); const env = process.env.NODE_ENV || 'development'; -const config = require(__dirname + '/../config/config.json')[env]; +const config = require('../app/config/migrations.config.js')[env]; const db = {}; let sequelize; if (config.use_env_variable) { - sequelize = new Sequelize(process.env[config.use_env_variable], config); + sequelize = new Sequelize(process.env[config.use_env_variable], config); } else { - sequelize = new Sequelize(config.database, config.username, config.password, config); + sequelize = new Sequelize( + config.database, + config.username, + config.password, + config + ); } -fs - .readdirSync(__dirname) - .filter(file => { - return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); - }) - .forEach(file => { - const model = sequelize['import'](path.join(__dirname, file)); - db[model.name] = model; - }); +fs.readdirSync(__dirname) + .filter(file => { + return ( + file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' + ); + }) + .forEach(file => { + const model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); Object.keys(db).forEach(modelName => { - if (db[modelName].associate) { - db[modelName].associate(db); - } + if (db[modelName].associate) { + db[modelName].associate(db); + } }); db.sequelize = sequelize; diff --git a/models/organization.model.js b/models/organization.model.js index c76bbbf..75da769 100644 --- a/models/organization.model.js +++ b/models/organization.model.js @@ -10,6 +10,14 @@ module.exports = (sequelize, DataTypes) => { }, name: { type: DataTypes.STRING, allowNull: false }, short_description: { type: DataTypes.STRING, allowNull: false }, + total_received: { + type: DataTypes.INTEGER, + defaultValue: 0 + }, + total_donors: { + type: DataTypes.INTEGER, + defaultValue: 0 + }, primary_contact_email: { type: DataTypes.STRING, diff --git a/models/project.model.js b/models/project.model.js new file mode 100644 index 0000000..1d52199 --- /dev/null +++ b/models/project.model.js @@ -0,0 +1,56 @@ +'use strict'; + +const Organization = require('./organization.model'), + Cause = require('./cause.model'), + Region = require('./region.model'); + +module.exports = (sequelize, DataTypes) => { + const Project = sequelize.define('projects', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + total_received: { + type: DataTypes.INTEGER, + defaultValue: 0 + }, + total_donors: { + type: DataTypes.INTEGER, + defaultValue: 0 + }, + + description: DataTypes.STRING, + + cause: { + type: DataTypes.STRING, + allowNull: false, + references: { + model: Cause(sequelize, DataTypes), + key: 'name' + } + }, + region: { + type: DataTypes.STRING, + allowNull: false, + references: { + model: Region(sequelize, DataTypes), + key: 'name' + } + }, + organization_id: { + type: DataTypes.UUID, + allowNull: false, + references: { + model: Organization(sequelize, DataTypes), + key: 'id' + } + }, + number_people_impacted: { + type: DataTypes.STRING, + defaultValue: 0 + } + }); + + return Project; +}; diff --git a/server.js b/server.js index 9442f45..3b87e2f 100644 --- a/server.js +++ b/server.js @@ -3,9 +3,11 @@ const express = require('express'), app = express(), bodyParser = require('body-parser'), port = process.env.PORT, - db = require('./app/config/db.config.js'), + db = require('./models'), morgan = require('morgan'); +console.log(db); + app.use(morgan('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));