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

sequelize practice #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
sequelize practice
  • Loading branch information
agnynureza committed Feb 21, 2018
commit 7d3cccc8c68c175fad1a79aa69380c4d845dc46a
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": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
54 changes: 54 additions & 0 deletions controllerArticles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const sequelize = require ('sequelize')
const {articles} = require ('./models')

class Articles{
static commandManage(command){
if(command[1] === 'add'){
let addData = command.slice(2)
let obj ={
title : addData[0],
body : addData[1],
id_author : addData[2],
id_tag : addData[3]
}
articles.create(obj).then(result=>{
console.log(result.dataValues)
process.exit()
})
}else if (command[1] === 'read_one'){
let id = Number(command.slice(2))
articles.findById(id).then(result=>{
console.log(result.dataValues)
process.exit()
})
}else if(command[1] === 'read_all'){
articles.findAll().then(data =>{
data.map(result=>{
console.log(result.dataValues)
})
process.exit()
})
}else if(command[1] === 'update'){
let id = command [2]
let newData = command.slice(3)
articles.findById(id).then(data=>{
articles.update({
first_name: newData[0] === '.' ? data.title : newData[0],
last_name : newData[1] === '.' ? data.body : newData[1],
religion : newData[2] === '.' ? data.id_author : newData[2],
gender : newData[3] === '.' ? data.id_tag : newData[3]
},{where:{id:id}}).then(data2=>{
console.log(`data berhasil di update`)
process.exit()
})
})
}else if (command[1] === 'delete'){
let id = command [2]
articles.destroy({where:{id:id}}).then(result=>{
process.exit()
})
}
}
}

module.exports = Articles
77 changes: 77 additions & 0 deletions controllerAuthors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const sequelize = require ('sequelize')
const {authors} = require ('./models')
const chalk = require('chalk');
const Table = require('cli-table');


class Authors{
static commandManage(command){
if(command[1] === 'add'){
let addData = command.slice(2)
let obj ={
first_name : addData[0],
last_name : addData[1],
religion : addData[2],
gender : addData[3],
age : addData[4]
}
authors.create(obj).then(result=>{
console.log(result.dataValues)
process.exit()
})
}else if (command[1] === 'read_one'){
let id = Number(command.slice(2))
authors.findById(id).then(result=>{
var table = new Table({
head: ['id','First Name','Last Name','Religion','Gender','Age']
, colWidths: [20, 20,20,20,20,20]
});

table.push(
[result.dataValues.id,result.dataValues.first_name, result.dataValues.last_name,
result.dataValues.religion,result.dataValues.gender,result.dataValues.age]
);

console.log(table.toString());
process.exit()
})
}else if(command[1] === 'read_all'){
authors.findAll().then(data =>{
var table = new Table({
head: ['id','First Name','Last Name','Religion','Gender','Age']
, colWidths: [20, 20,20,20,20,20]
});
data.map(result=>{
table.push(
[result.dataValues.id,result.dataValues.first_name, result.dataValues.last_name,
result.dataValues.religion,result.dataValues.gender,result.dataValues.age]
);
})
console.log(table.toString());
process.exit()
})
}else if(command[1] === 'update'){
let id = command [2]
let newData = command.slice(3)
authors.findById(id).then(data=>{
authors.update({
first_name: newData[0] === '.' ? data.first_name : newData[0],
last_name : newData[1] === '.' ? data.last_name : newData[1],
religion : newData[2] === '.' ? data.religion : newData[2],
gender : newData[3] === '.' ? data.gender : newData[3],
age : newData[4] === '.' ? data.age : newData[4]
},{where:{id:id}}).then(data2=>{
console.log(`data berhasil di update`)
process.exit()
})
})
}else if (command[1] === 'delete'){
let id = command [2]
authors.destroy({where:{id:id}}).then(result=>{
process.exit()
})
}
}
}

module.exports = Authors
30 changes: 30 additions & 0 deletions controllerHelp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const sequelize = require ('sequelize')
const chalk = require('chalk');
const Table = require('cli-table');

class Help{
static commandManage(){
const table = new Table({
head: ['No', 'Command', 'Task'],
colWidths: [5, 30, 80]
})
table.push([`1`, `author add`, `add<space"data yang ingin dimasukkan`],
[`2`, `author read_one`, `read_one<space>"masukkan id author"`],
[`3`, `author rad_all`, `read_all`],
[`4`, `author update`, `update<space>"masukkan id yang ingin di-update dan datanya"`],
[`5`, `author delete`, `delete<space>"masukkan id author"`],
[`6`, `tag add`, `add<space"data yang ingin dimasukkan`],
[`7`, `tag read_one`, `read_one<space>"masukkan id tag"`],
[`8`, `tag rad_all`, `read_all`],
[`9`, `tag update`, `update<space>"masukkan id yang ingin di-update dan datanya"`],
[`10`, `tag delete`, `delete<space>"masukkan id tag"`],
[`11`, `article add`, `add<space"data yang ingin dimasukkan`],
[`12`, `article read_one`, `read_one<space>"masukkan id article"`],
[`13`, `article read_all`, `read_all`],
[`14`, `article update`, `update<space>"masukkan id yang ingin di-update dan datanya"`],
[`15`, `article delete`, `delete<space>"masukkan id article"`])
console.log(table.toString())
}
}

module.exports = Help
49 changes: 49 additions & 0 deletions controllerTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const sequelize = require ('sequelize')
const {tags} = require ('./models')


class Tags{
static commandManage(command){
if(command[1] === 'add'){
let addData = command.slice(2)
let obj ={
name : addData[0]
}
tags.create(obj).then(result=>{
console.log(result.dataValues)
process.exit()
})
}else if (command[1] === 'read_one'){
let id = Number(command.slice(2))
tags.findById(id).then(result=>{
console.log(result.dataValues)
process.exit()
})
}else if(command[1] === 'read_all'){
tags.findAll().then(data =>{
data.map(result=>{
console.log(result.dataValues)
process.exit()
})
})
}else if(command[1] === 'update'){
let id = command [2]
let newData = command.slice(3)
tags.findById(id).then(data=>{
tags.update({
name: newData[0] === '.' ? data.name : newData[0]
},{where:{id:id}}).then(data2=>{
console.log(`data berhasil di update`)
process.exit()
})
})
}else if (command[1] === 'delete'){
let id = command [2]
tags.destroy({where:{id:id}}).then(result=>{
process.exit()
})
}
}
}

module.exports = Tags
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Tags = require('./controllerTags')
const Authors = require('./controllerAuthors')
const Articles = require('./controllerArticles')
const Help = require('./controllerHelp')

let command = process.argv.slice(2)

if(command[0] === 'tag'){
Tags.commandManage(command)
}else if (command[0] === 'author'){
Authors.commandManage(command)
}else if(command[0] === 'article'){
Articles.commandManage(command)
}else{
Help.commandManage()
}
9 changes: 9 additions & 0 deletions models/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (sequelize,DataTypes)=>{
const articles = sequelize.define('articles',{
title : DataTypes.STRING,
body : DataTypes.STRING,
id_author : DataTypes.INTEGER,
id_tag : DataTypes.INTEGER,
})
return articles
}
10 changes: 10 additions & 0 deletions models/authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = (sequelize,DataTypes)=>{
const authors = sequelize.define('authors',{
first_name : DataTypes.STRING,
last_name : DataTypes.STRING,
religion : DataTypes.STRING,
gender : DataTypes.STRING,
age : DataTypes.INTEGER,
})
return authors
}
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;
6 changes: 6 additions & 0 deletions models/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (sequelize,DataTypes)=>{
const tags = sequelize.define('tags',{
name : DataTypes.STRING
})
return tags
}
Loading