diff --git a/.sequelizerc b/.sequelizerc new file mode 100644 index 0000000..a03cf42 --- /dev/null +++ b/.sequelizerc @@ -0,0 +1,8 @@ +const path = require('path'); + +module.exports = { + 'config': path.resolve('./app/config', 'config.js'), + 'models-path': path.resolve('./app', 'models'), + 'seeders-path': path.resolve('./app', './seeders'), + 'migrations-path': path.resolve('./app', './migrations') +} \ No newline at end of file diff --git a/app/config/config.js b/app/config/config.js new file mode 100644 index 0000000..707ed36 --- /dev/null +++ b/app/config/config.js @@ -0,0 +1,25 @@ +require('dotenv/config'); + +module.exports = { + development: { + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_DB, + host: process.env.DB_HOST, + dialect: process.env.DB_DIALECT + }, + test: { + username: 'root', + password: null, + database: 'database_test', + host: '127.0.0.1', + dialect: 'mysql' + }, + production: { + username: 'root', + password: null, + database: 'database_production', + host: '127.0.0.1', + dialect: 'mysql' + } +}; diff --git a/app/migrations/20190122064034-orgs-default-rating.js b/app/migrations/20190122064034-orgs-default-rating.js new file mode 100644 index 0000000..a669009 --- /dev/null +++ b/app/migrations/20190122064034-orgs-default-rating.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.changeColumn('organizations', 'rating', { + type: Sequelize.INTEGER, + defaultValue: 5, + allowNull: false + }); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.changeColumn('organizations', 'rating', { + type: Sequelize.INTEGER, + defaultValue: 0, + allowNull: false + }); + } +}; diff --git a/app/models/index.js b/app/models/index.js new file mode 100644 index 0000000..c1a3d6d --- /dev/null +++ b/app/models/index.js @@ -0,0 +1,37 @@ +'use strict'; + +const fs = require('fs'); +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 db = {}; + +let sequelize; +if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); +} else { + 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; + }); + +Object.keys(db).forEach(modelName => { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/app/seeders/20190122051334-base-user.js b/app/seeders/20190122051334-base-user.js new file mode 100644 index 0000000..537b15f --- /dev/null +++ b/app/seeders/20190122051334-base-user.js @@ -0,0 +1,23 @@ +'use strict'; + +// DOESNT WORK AS NEED TO HASH PASSWORD TO DB +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert('donors', [ + { + first_name: 'Arjun', + email: 'patel.arjun50@gmail.com', + password: 'test', + last_name: 'Patel', + created_at: new Date(), + updated_at: new Date() + } + ]); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('donors', { + email: 'patel.arjun50@gmail.com' + }); + } +}; diff --git a/app/seeders/20190122052354-base-causes.js b/app/seeders/20190122052354-base-causes.js new file mode 100644 index 0000000..ed69644 --- /dev/null +++ b/app/seeders/20190122052354-base-causes.js @@ -0,0 +1,24 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert('causes', [ + { + id: 1, + name: 'Animal Care', + created_at: new Date(), + updated_at: new Date() + }, + { + id: 2, + name: 'Water', + created_at: new Date(), + updated_at: new Date() + } + ]); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('causes'); + } +}; diff --git a/app/seeders/20190122053404-base-regions.js b/app/seeders/20190122053404-base-regions.js new file mode 100644 index 0000000..37f4837 --- /dev/null +++ b/app/seeders/20190122053404-base-regions.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert('regions', [ + { + id: 1, + name: 'North India', + created_at: new Date(), + updated_at: new Date() + } + ]); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('regions', { name: 'North India' }); + } +}; diff --git a/app/seeders/20190122053544-base-orgs.js b/app/seeders/20190122053544-base-orgs.js new file mode 100644 index 0000000..6ba8c29 --- /dev/null +++ b/app/seeders/20190122053544-base-orgs.js @@ -0,0 +1,48 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.bulkInsert('organizations', [ + { + id: 1, + name: 'Gopala Goshala', + short_description: 'We are committed to animal shelter!', + primary_cause: 'Animal Care', + primary_region: 'North India', + rating: 9.2, + created_at: new Date(), + updated_at: new Date() + }, + { + id: 2, + name: 'Water Organization India', + short_description: 'We love water! So should you!', + primary_cause: 'Water', + primary_region: 'North India', + rating: 8, + created_at: new Date(), + updated_at: new Date() + }, + { + id: 3, + name: "Pete's Animals", + short_description: 'Help us protect animals!', + primary_cause: 'Animal Care', + primary_region: 'North India', + rating: 7.5, + created_at: new Date(), + updated_at: new Date() + } + ]); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('organizations', { + [Sequelize.Op.or]: [ + { name: 'Gopala Goshala' }, + { name: 'Water Organization India' }, + { name: "Pete's Animals" } + ] + }); + } +}; diff --git a/app/seeders/20190122054316-base-grants.js b/app/seeders/20190122054316-base-grants.js new file mode 100644 index 0000000..cec55c4 --- /dev/null +++ b/app/seeders/20190122054316-base-grants.js @@ -0,0 +1,65 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + var sequelize = queryInterface.sequelize; + const causes = [1, 2], + regions = [1], + organizations = [1, 2, 3]; + + return Promise.all([ + sequelize.query( + `INSERT INTO grants (id, name, amount, monthly, last_payment, num_causes, num_regions, created_at, updated_at, donor_id) + VALUES (1, 'Max Impact in the South', 1000, 1, :last_payment, 2, 1, :created_at, :updated_at, (select id from donors where email='patel.arjun50@gmail.com'))`, + { + replacements: { + last_payment: new Date(), + created_at: new Date(), + updated_at: new Date() + }, + type: sequelize.QueryTypes.INSERT + } + ), + sequelize.query( + `INSERT INTO grants_causes (grant_id, cause_id, created_at, updated_at) + VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), + ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, + { + replacements: { + created_at: new Date(), + updated_at: new Date() + }, + type: sequelize.QueryTypes.INSERT + } + ), + sequelize.query( + `INSERT INTO grants_regions (grant_id, cause_id, created_at, updated_at) + VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), + ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, + { + replacements: { + created_at: new Date(), + updated_at: new Date() + }, + type: sequelize.QueryTypes.INSERT + } + ), + sequelize.query( + `INSERT INTO grants_causes (grant_id, cause_id, created_at, updated_at) + VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), + ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, + { + replacements: { + created_at: new Date(), + updated_at: new Date() + }, + type: sequelize.QueryTypes.INSERT + } + ) + ]); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.bulkDelete('grants', null, {}); + } +};