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

migration-seed #24

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
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": "yosa",
"password": "123456",
"database": "mvcsequelize",
"host": "127.0.0.1",
"dialect": "postgres"
},
"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"
}
}
10 changes: 10 additions & 0 deletions controllers/controller-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ControllerAddresses {
constructor(){

}
static manageAction(act, value){
console.log('ini controller Addresses')
}
}

module.exports = ControllerAddresses
46 changes: 46 additions & 0 deletions controllers/controller-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const model = require('../models')
const view = require('../views/views-contacts.js')

class ControllerContacts {
constructor(){

}
static manageAction(act, value){
if(act === 'list'){
model.Contact.findAll()
.then(contact => {
// console.log(contact)
view.list(contact)
})
} else if(act === 'add'){
model.Contact.create({
name: value[0],
email: value[1],
phone: value[2]
})
.then(addContact =>{
view.notifAdd(addContact)
})
} else if(act === 'update'){
model.Contact.update({
name: value[1],
email: value[2],
phone: value[3]
}, {where: {id: value[0]}
})
.then(updateContact => {
view.notifUpdate(updateContact)
})
} else if(act === 'delete'){
model.Contact.destroy({
where: { id: value[0] }
})
.then(deleteContact =>{
view.notifDelete(deleteContact)
})

}
}
}

module.exports = ControllerContacts
17 changes: 17 additions & 0 deletions controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let controlContact = require('./controller-contact.js')
let controlAddress = require('./controller-address.js')

class Controller {
constructor(){

}
static manageOption(option, act, value){
if(option === 'contacts'){
controlContact.manageAction(act, value)
} else if(option === 'addresses'){
controlAddress.manageAction(act, value)
}
}
}

module.exports = Controller
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let control = require('./controllers/controller.js')
let argv = process.argv;

if(argv[2] === null){
console.log('contacts list --- untuk menampilkan daftar kontak')
console.log('contacts add --- untuk menambahkan data kontak')
console.log('contacts update --- untuk mengubah kontak')
console.log('contacts delete --- untuk menghapus data kontak')
} else {
// kirim option, act, dan value
control.manageOption(argv[2], argv[3], argv.slice(4))
}
33 changes: 33 additions & 0 deletions migrations/20180222051108-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/20180222051255-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.INTEGER
},
zip_code: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
14 changes: 14 additions & 0 deletions migrations/20180222081435-ubahTypeData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {

return queryInterface.changeColumn('Addresses', 'city', {type: Sequelize.STRING});

},

down: (queryInterface, Sequelize) => {

// return queryInterface.changeColumn('Addresses', 'city', {type: Sequelize.STRING});
}
};
12 changes: 12 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.INTEGER,
zip_code: DataTypes.STRING
}, {});
Address.associate = function(models) {
// associations can be defined here
};
return Address;
};
12 changes: 12 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});
Contact.associate = function(models) {
// associations can be defined here
};
return Contact;
};
36 changes: 36 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var 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 => {
var 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;
Loading