using the sequelize auto index file for model/db config

This commit is contained in:
Arjun Patel
2019-03-16 21:55:50 -07:00
parent 661828bab2
commit 52c284f02a
6 changed files with 91 additions and 30 deletions
+2 -13
View File
@@ -7,23 +7,12 @@ module.exports = models => {
Organization, Organization,
Charge, Charge,
PaymentPlan, PaymentPlan,
GrantsOrganizations GrantsOrganizations,
Project
} = models; } = models;
Donor.hasMany(Grant, { foreignKey: 'donor_id' }); 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, { Grant.belongsToMany(Organization, {
through: 'grants_organizations', through: 'grants_organizations',
foreignKey: 'grant_id', foreignKey: 'grant_id',
+1 -1
View File
@@ -36,7 +36,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false allowNull: false
}, },
age: DataTypes.INTEGER, age: DataTypes.INTEGER,
phone: DataTypes.DataTypes.STRING(20), phone: DataTypes.STRING(20),
address: DataTypes.STRING, address: DataTypes.STRING,
city: DataTypes.STRING, city: DataTypes.STRING,
+21 -15
View File
@@ -5,30 +5,36 @@ const path = require('path');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const basename = path.basename(__filename); const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development'; 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 = {}; const db = {};
let sequelize; let sequelize;
if (config.use_env_variable) { 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 { } else {
sequelize = new Sequelize(config.database, config.username, config.password, config); sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
} }
fs fs.readdirSync(__dirname)
.readdirSync(__dirname) .filter(file => {
.filter(file => { return (
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'
}) );
.forEach(file => { })
const model = sequelize['import'](path.join(__dirname, file)); .forEach(file => {
db[model.name] = model; const model = sequelize['import'](path.join(__dirname, file));
}); db[model.name] = model;
});
Object.keys(db).forEach(modelName => { Object.keys(db).forEach(modelName => {
if (db[modelName].associate) { if (db[modelName].associate) {
db[modelName].associate(db); db[modelName].associate(db);
} }
}); });
db.sequelize = sequelize; db.sequelize = sequelize;
+8
View File
@@ -10,6 +10,14 @@ module.exports = (sequelize, DataTypes) => {
}, },
name: { type: DataTypes.STRING, allowNull: false }, name: { type: DataTypes.STRING, allowNull: false },
short_description: { 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: { primary_contact_email: {
type: DataTypes.STRING, type: DataTypes.STRING,
+56
View File
@@ -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;
};
+3 -1
View File
@@ -3,9 +3,11 @@ const express = require('express'),
app = express(), app = express(),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
port = process.env.PORT, port = process.env.PORT,
db = require('./app/config/db.config.js'), db = require('./models'),
morgan = require('morgan'); morgan = require('morgan');
console.log(db);
app.use(morgan('dev')); app.use(morgan('dev'));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));