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

equelize-one-to-many #10

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
10 changes: 10 additions & 0 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
stree1,711-2880 Nulla St, Mississippi, 96522,1
2,8562 Fusce Rd, Nebraska, 20620,2
3,606-3727 Ullamcorper Street, Roseville, 11523,1
4,867-859 Sit Rd, New York, 39531,2
5,7292 Dictum Av, San Antonio, 47096,3
6,651-8679 Sodales Av, Tamuning, 10855,3
7,191-103 Integer Rd, Corona New Mexico, 08219,3
8,2508 Dolor. Av, Muskegon KY, 12482,4
9,666-4366 Lacinia Avenue, Ohio, 19253,1
10,Lacinia Road, San Bernardino, 09289,2
9 changes: 9 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "sequelize_exercise",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
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
111 changes: 111 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const { Address } = require('../models');

class Controller {

static parsingData(task, data) {
console.log(task, data)
if (task === 'list') {
this.showList()
} else if (task === 'add') {
this.addData(data)
} else if (task === 'update') {
this.updateData(data)
} else if (task === 'delete') {
this.deleteData(data)
} else if (task === 'full_address') {
this.full_addresses(data)
}
}

static full_addresses(data) {
Address.findOne({ where:{id:data[0]} })
.then(data => {
data.full_address();
process.exit()
})
.catch(err => {
console.log(err);
process.exit()
})
}

static showList() {
Address.findAll({raw:true})
.then(data => {
console.log(data);
process.exit()
})
.catch(err => {
console.log(err);
process.exit()
})
}

static addData(data) {
Address.create({
street: data[0],
city: data[1],
zip_code: data[2]
})
.then(data => {
console.log(`Success Insert Data`)
process.exit()
})
.catch(err => {
console.log(err);
process.exit()
})
}

static updateData(data) {
Address.findById(data[0])
.then(getData => {
let getObject = Object.keys(getData.dataValues)
for (let j = 1; j < data.length; j++) {
let matchObj = data[j].split(':')
if (matchObj[0] === 'street') {
getData.dataValues.street = matchObj[1]
} else if (matchObj[0] === 'city') {
getData.dataValues.city = matchObj[1]
} else if (matchObj[0] === 'zip_code') {
getData.dataValues.zip_code = matchObj[1]
}
}
Address.update({
street: getData.dataValues.street,
city: getData.dataValues.city,
zip_code: getData.dataValues.zip_code
}, {
where: { id: getData.dataValues.id }
})
.then(data => {
console.log('Success Update Data');
process.exit()
})
.catch(err => {
console.log(err)
process.exit()
})
})
.catch(err => {
console.log(err)
})
}

static deleteData(data) {
Address.destroy({
where: { id: data[0] }
})
.then(data => {
console.log(`Success Delete Data`)
process.exit()
})
.catch(err => {
console.log(err)
process.exit()
})
}

}

module.exports = Controller
119 changes: 119 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const { Contact, Address } = require('../models');

class Controller {

static parsingData(task, data) {
if (task === 'list') {
this.showList()
} else if (task === 'add') {
this.addData(data)
} else if (task === 'update') {
this.updateData(data)
} else if (task === 'delete') {
this.deleteData(data)
} else if (task === 'find_one') {
this.findOne(data)
}
}

//Find All Release 2 - 1
static showList() {
Contact.findAll({
include: Address
})
.then(data => {
let convertData = JSON.parse(JSON.stringify(data))
console.log(convertData);
process.exit()
})
.catch(err => {
console.log(err);
process.exit()
})
}

//Find ById Release 2 - 2
static findOne(data) {
Contact.findOne({
where: { id: data[0] }, include: {model: Address}
})
.then(data => {
let convertData = JSON.parse(JSON.stringify(data))
console.log(convertData)
process.exit()
})
.catch(err => {
console.log(err)
process.exit()
})
}

static addData(data) {
Contact.create({
name: data[0],
email: data[1],
phone: data[2]
})
.then(data => {
console.log(`Success Insert Data`)
process.exit()
})
.catch(err => {
console.log(err);
process.exit()
})
}

static updateData(data) {
Contact.findById(data[0])
.then(getData => {
let getObject = Object.keys(getData.dataValues)
for (let j = 1; j < data.length; j++) {
let matchObj = data[j].split(':')
if (matchObj[0] === 'name') {
console.log('masuk ke name')
getData.dataValues.name = matchObj[1]
} else if (matchObj[0] === 'email') {
getData.dataValues.email = matchObj[1]
} else if (matchObj[0] === 'phone') {
getData.dataValues.phone = matchObj[1]
}
}
Contact.update({
name: getData.dataValues.name,
email: getData.dataValues.email,
phone: getData.dataValues.phone
}, {
where: { id: getData.dataValues.id }
})
.then(data => {
console.log('Success Update Data');
process.exit()
})
.catch(err => {
console.log(err)
process.exit()
})
})
.catch(err => {
console.log(err)
})
}

static deleteData(data) {
Contact.destroy({
where : {id: data[0]}
})
.then(data => {
console.log(`Success Delete Data`)
process.exit()
})
.catch(err => {
console.log(err)
process.exit()
})
}

}

module.exports = Controller
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const ContactController = require('./controllers/contact');
const AddressesController = require('./controllers/address');

const command = process.argv
const category = process.argv[2].split(':')[0];
const task = process.argv[2].split(':')[1];
const data = command.splice(3, command.length);

if (category === 'contacts') {
ContactController.parsingData(task, data)
} else if (category === 'addresses') {
AddressesController.parsingData(task, data)
}

33 changes: 33 additions & 0 deletions migrations/20180222095029-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');
}
};
36 changes: 36 additions & 0 deletions migrations/20180222095242-create-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'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
},
id_contact: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
20 changes: 20 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.STRING,
id_contact: DataTypes.INTEGER
}, {});

Address.associate = function (models) {
Address.belongsTo(models.Contact, { foreignKey: 'id_contact' })
}

//Instance Method
Address.prototype.full_address = function () {
console.log(`Alamat Lengkap : ${this.street} - ${this.city} - ${this.zip_code}`)
}

return Address;
};
13 changes: 13 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});

Contact.associate = function (models) {
models.Contact.hasMany(models.Address, { foreignKey: 'id_contact' })
}
return Contact;
};
Loading