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

Davidyunius #20

Open
wants to merge 2 commits 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": "davidyunius",
"password": null,
"database": "mvc",
"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"
}
}
4 changes: 4 additions & 0 deletions controllers/controller-addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'
const Addresses = require('../models/')
const ViewAddresses = require('../views/view-addresses')

60 changes: 60 additions & 0 deletions controllers/controller-contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'
const Contacts = require('../models/')
const ViewContacts = require('../views/view-contacts')

class ControllerContacts{
constructor(){}
static command(input, data){
if (input === 'list') {
this.list()
}else if(input === 'add') {
this.add(data)
}else if(input === 'update') {
let id = data[0]
this.update(id, data)
}else if(input === 'delete') {
this.delete(data)
}
}

static add(data){
let obj = {
name: data[0],
email: data[1],
phone: data[2]
}
Contacts.Contacts.create(obj).then(data => {
console.log(data.dataValues)
process.exit()
})
}

static list(){
Contacts.Contacts.findAll({ raw: true }).then(contacts => {
console.log(contacts)
process.exit()
})
}

static update(id, data){
Contacts.Contacts.findById(id).then(oldData => {
Contacts.Contacts.update({
name: data[1] === '.' ? oldData.name : data[1],
email: data[2] === '.' ? oldData.email : data[2],
phone: data[3] === '.' ? oldData.phone : data[3]
}, {where: {id:id}}).then(updatedData => {
console.log('Data updated!')
process.exit()
})
})
}

static delete(id) {
Contacts.Contacts.destroy({ where: { id: id } }).then(data => {
console.log('Data deleted');
process.exit()
})
}
}

module.exports = ControllerContacts
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const ControllerContacts = require('./controllers/controller-contacts')
const ControllerAddresses = require('./controllers/controller-addresses')

let argv = process.argv.slice(2)

if (argv[0].split(':')[0] === 'contacts') {
ControllerContacts.command(argv[0].split(':')[1], argv.slice(1))
// console.log(argv[0].split(':')[1]);
}else if (argv[0] === 'addresses') {
// ControllerAddresses.command()
}
33 changes: 33 additions & 0 deletions migrations/20180222055654-create-contacts.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/20180222055657-create-addresses.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.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
15 changes: 15 additions & 0 deletions models/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Addresses = sequelize.define('Addresses', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Addresses;
};
15 changes: 15 additions & 0 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contacts = sequelize.define('Contacts', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Contacts;
};
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;
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "sequelize-migration-seed",
"version": "1.0.0",
"main": "index.js",
"repository": "https://github.com/platinum-fox-2017/sequelize-migration-seed.git",
"author": "davidyunius <[email protected]>",
"license": "MIT",
"dependencies": {
"pg": "^7.4.1",
"sequelize": "^4.33.4",
"sequelize-cli": "^4.0.0"
}
}
42 changes: 42 additions & 0 deletions seeders/20180222062403-demo-contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const fs = require('fs')

const contactCsv = fs.readFileSync('contacts.csv', 'utf-8').trim().split('\n')
const addressesCsv = fs.readFileSync('addresses.csv', 'utf-8').trim().split('\n')
const arrData = []
function convertCsv() {
let arr = []
for (let i = 0; i < contactCsv.length; i++) {
arr.push(String(contactCsv[i]).split(','))
}

for (let j = 0; j < contactCsv.length; j++) {
let obj = {
name: arr[j][1],
email: arr[j][2],
phone: arr[j][3],
createdAt: new Date(),
updatedAt: new Date()
}
arrData.push(obj)
}
return arrData
}

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Contacts', convertCsv(), {});
},

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

Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Contacts', null, {});
}
};
42 changes: 42 additions & 0 deletions seeders/20180222062409-demo-addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const fs = require('fs')

const contactCsv = fs.readFileSync('contacts.csv', 'utf-8').trim().split('\n')
const addressesCsv = fs.readFileSync('addresses.csv', 'utf-8').trim().split('\n')
const arrData = []
function convertCsv() {
let arr = []
for (let i = 0; i < addressesCsv.length; i++) {
arr.push(String(addressesCsv[i]).split(','))
}

for (let j = 0; j < addressesCsv.length; j++) {
let obj = {
street: arr[j][1],
city: arr[j][2],
zip_code: arr[j][3],
createdAt: new Date(),
updatedAt: new Date()
}
arrData.push(obj)
}
return arrData
}

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Addresses', convertCsv(), {});
},

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

Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Addresses', null, {});
}
};
Empty file added views/view-addresses.js
Empty file.
Empty file added views/view-contacts.js
Empty file.
Loading