Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release 3 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# sequelize-one-to-many
# sequelize-migration-seed
10 changes: 10 additions & 0 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1,711-2880 Nulla St, Mississippi, 96522
2,8562 Fusce Rd, Nebraska, 20620
3,606-3727 Ullamcorper Street, Roseville, 11523
4,867-859 Sit Rd, New York, 39531
5,7292 Dictum Av, San Antonio, 47096
6,651-8679 Sodales Av, Tamuning, 10855
7,191-103 Integer Rd, Corona New Mexico, 08219
8,2508 Dolor. Av, Muskegon KY, 12482
9,666-4366 Lacinia Avenue, Ohio, 19253
10,Lacinia Road, San Bernardino, 09289
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "werkbau",
"password": "pokemon",
"database": "database_migration_seed",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "werkbau",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "werkbau",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
5 changes: 5 additions & 0 deletions contacts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1,Lani Rollins,[email protected],1-633-389-7173
2,McKenzie Burris,[email protected],1-906-235-0832
3,Amethyst Morgan,[email protected],1-548-366-6273
4,Lamar Hardin,[email protected],1-519-693-8091
5,Keegan Coleman,[email protected],1-998-626-8896
214 changes: 214 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
const View = require('../view/index.js');
const db = require('../models/index.js');


class Controller {
constructor() {

}

static readCommands(argv) {
// node index.js <identifier:command> <options>
// list add update delete

let code = argv[2].split(':');
if (code.length < 2) {
return console.log('please specify command with <tableName>:<command> format.');
}
console.log(code);
let identifier = code[0];
let command = code[1];
let options = argv.slice(3);

// console.log(code);
if (identifier === 'addresses') {
switch (command) {
case 'add':
Controller.addToAddresses(options);
break;
case 'list': // if had options for id list one
Controller.listAddresses(options);
break;
case 'update':
Controller.updateAddresses(options);
break;
case 'delete':
Controller.deleteAddresses(options);
break;
case 'north_area':
Controller.northAreaAddresses(options);
break;
case 'south_area':
Controller.southAreaAddresses(options);
break;
default: console.log('No command specified');

}
} else if (identifier === 'contacts') {
switch (command) {
case 'add':
Controller.addToContacts(options);
break;
case 'list': // if had options for id list one
Controller.listContacts(options);
break;
case 'update':
Controller.updateContacts(options);
break;
case 'delete':
Controller.deleteContacts(options);
break;
case 'houses': // contacts:houses <id>
Controller.joinContactsAndGetHouses(options);
break;
default: console.log('No command specified');

}
} else {
// error message
console.log('command error');
}

}

// ########### Addresses #########

static addToAddresses(options){
db.Address.create({
street: options[0],
city: options[1],
zip_code: options[2]
}).then(newAddress => {
View.displayAddData(newAddress);
});
}

static listAddresses(options){
if (options[0]) {
db.Address.findOne({
where:{
id : options[0]
}
}).then(foundAddress => {
foundAddress.full_address(function(props){
View.displayAlamatLengkap(props);
})
View.displayOneFound(foundAddress);

})
} else {
db.Address.findAll({
}).then(foundAddress => {
View.displayManyFound(foundAddress);
});
}
}

static updateAddresses(options){
db.Address.findOne({
where:{
id: options[0]
}
}).then(foundAddress => {
let newProp = {};
newProp[options[1]] = options[2];
foundAddress.update(newProp);
View.displayUpdate(foundAddress);
});
}

static deleteAddresses(options){
db.Address.findOne({
where:{
id : options[0]
}
}).then(foundAddress => {
View.displayDestroyed(foundAddress);
return foundAddress.destroy();
});
}


static northAreaAddresses(){
db.Address.north_area().then(foundAddresses => {
View.displayManyFound(foundAddresses)
})
}

static southAreaAddresses(){
db.Address.south_area().then(foundAddresses => {
View.displayManyFound(foundAddresses)
})
}

// ########### Contacts ##########

static addToContacts(options){
db.Contact.create({
name: options[0],
email: options[1],
phone: options[2]
}).then(newContact => {
View.displayAddData(newContact);
});
}

static listContacts(options){
if (options[0]) {
db.Contact.findOne({
where:{
id : options[0]
}
}).then(foundContact => {
View.displayOneFound(foundContact);
})
} else {
db.Contact.findAll({
}).then(foundContacts => {
View.displayManyFound(foundContacts);
});
}
}

static updateContacts(options){
db.Contact.findOne({
where:{
id: options[0]
}
}).then(foundContact => {
let newProp = {};
newProp[options[1]] = options[2];
foundContact.update(newProp);
View.displayUpdate(foundContact);
});
}

static deleteContacts(options){
db.Contact.findOne({
where:{
id : options[0]
}
}).then(foundContact => {
View.displayDestroyed(foundContact);
return foundContact.destroy();
});
}

static joinContactsAndGetHouses(options){
db.Contact.findOne({
where:{
id : options[0]
},
include: [{model: db.Address, as:'Houses'}]
}).then(foundContact => {
foundContact.getHouses().then(houses=>{
View.displayContactHouses(foundContact,houses)
console.log(foundContact);
});
})
}

}


module.exports = Controller;
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// sequelize model:generate --name Contact --attributes name:string,email:string,phone:STRING
// sequelize model:generate --name Address --attributes street:string,city:string,zip_code:STRING
const Controller = require('./controller/index.js')

let argv = process.argv;

Controller.readCommands(argv);
33 changes: 33 additions & 0 deletions migrations/20180222050816-create-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Contacts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
phone: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contacts');
}
};
33 changes: 33 additions & 0 deletions migrations/20180222050842-create-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Addresses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
street: {
type: Sequelize.STRING
},
city: {
type: Sequelize.STRING
},
zip_code: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
26 changes: 26 additions & 0 deletions migrations/20180222093815-AddFkToAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
// add fk to addresses
return queryInterface.addColumn('Addresses', 'ContactId', Sequelize.INTEGER)
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.removeColumn('Addresses', 'ContactId')
}
};
23 changes: 23 additions & 0 deletions migrations/20180222095341-AddValueToContactId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
}
};
Loading