integration with migrations/seeds/etc

This commit is contained in:
Arjun Patel
2019-01-21 22:49:54 -08:00
parent 33afb962ae
commit 6e6eba3de4
9 changed files with 267 additions and 0 deletions
+8
View File
@@ -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')
}
+25
View File
@@ -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'
}
};
@@ -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
});
}
};
+37
View File
@@ -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;
+23
View File
@@ -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'
});
}
};
+24
View File
@@ -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');
}
};
@@ -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' });
}
};
+48
View File
@@ -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" }
]
});
}
};
+65
View File
@@ -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, {});
}
};