Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
1,186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// database ORM | ||
const Sequelize = require("sequelize"); | ||
|
||
const sequelize = new Sequelize( | ||
'mysql', | ||
'root', | ||
'codio', | ||
{ | ||
host: 'localhost', | ||
dialect: 'mysql' | ||
} | ||
); | ||
|
||
sequelize.authenticate().then(() => { | ||
console.log('Connection has been established successfully.'); | ||
}).catch((error) => { | ||
console.error('Unable to connect to the database: ', error); | ||
}); | ||
|
||
module.exports = sequelize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
const sequelize = require('./db'); | ||
const { Sequelize, DataTypes } = require("sequelize"); | ||
|
||
const Article = sequelize.define("articles", { | ||
ID: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER | ||
}, | ||
title: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
allText: { | ||
type: DataTypes.TEXT, | ||
allowNull: false | ||
}, | ||
summary: { | ||
type: DataTypes.TEXT, | ||
}, | ||
dateCreated: { | ||
type: DataTypes.DATE, | ||
}, | ||
dateModified: { | ||
type: DataTypes.DATE, | ||
}, | ||
imageURL: { | ||
type: DataTypes.TEXT, | ||
}, | ||
published: { | ||
type: DataTypes.TINYINT, | ||
}, | ||
authorID: { | ||
type: DataTypes.INTEGER, | ||
} | ||
}, { | ||
timestamps: false | ||
}); | ||
|
||
sequelize.sync().then(() => { | ||
console.log('Article table created successfully!'); | ||
}).catch((error) => { | ||
console.error('Unable to create table : ', error); | ||
}); | ||
|
||
Article.bulkCreate([ | ||
{ title: "Title 1", allText: "Text 1" }, | ||
{ title: "Title 2", allText: "Text 2" }, | ||
{ title: "Title 3", allText: "Text 3" }, | ||
{ title: "Title 4", allText: "Text 4" }, | ||
{ title: "Title 5", allText: "Text 5" }, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,110 @@ | ||
const db = require('../helpers/database'); | ||
const bcrypt = require('bcrypt'); | ||
const saltRounds = 10; | ||
|
||
const { Sequelize, DataTypes } = require("sequelize"); | ||
|
||
const sequelize = require('../db'); | ||
const User = sequelize.define("users", { | ||
ID: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER | ||
}, | ||
firstName: { | ||
type: DataTypes.STRING, | ||
}, | ||
lastName: { | ||
type: DataTypes.STRING, | ||
}, | ||
username: { | ||
type: DataTypes.STRING, | ||
}, | ||
about: { | ||
type: DataTypes.TEXT, | ||
}, | ||
dateRegistered: { | ||
type: DataTypes.DATE, | ||
}, | ||
password: { | ||
type: DataTypes.STRING(255), | ||
}, | ||
passwordSalt: { | ||
type: DataTypes.STRING, | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
avatarURL: { | ||
type: DataTypes.STRING, | ||
} | ||
}, { | ||
timestamps: false | ||
}); | ||
|
||
sequelize.sync().then(() => { | ||
console.log('User table created successfully!'); | ||
}).catch((error) => { | ||
console.error('Unable to create table : ', error); | ||
}); | ||
|
||
//get a single user by the (unique) username | ||
exports.findByUsername = async function findByUsername(username) { | ||
const query = "SELECT * FROM users WHERE username = ?;"; | ||
const user = await db.run_query(query, username); | ||
return user; | ||
} | ||
|
||
//get all users | ||
exports.getAll = async function getAll() { | ||
let data = User.findAll() | ||
return data; | ||
} | ||
|
||
//create a user | ||
exports.add = async function add(user) { | ||
const hash = bcrypt.hashSync(user.password, saltRounds); | ||
user.password = hash; | ||
console.log(user); | ||
try { | ||
|
||
const data = await User.create(user); | ||
return data; | ||
} | ||
catch (err) { | ||
console.log(err); | ||
return err; | ||
} | ||
} | ||
|
||
exports.updatePassword = async function updatePassword() { | ||
|
||
const hash = bcrypt.hashSync("asd", saltRounds); | ||
|
||
let data = await User.update({ password: hash }, { | ||
where: { | ||
ID: [1, 2, 3, 4, 6, 7, 8, 9] | ||
} | ||
}); | ||
return data; | ||
|
||
} | ||
|
||
//delete a user - not working - foreign key constraint | ||
exports.deleteUser = async function deleteUser(id) { | ||
try { | ||
|
||
let data = await User.destroy({ | ||
where: { | ||
ID: id | ||
}}); | ||
return data; | ||
} | ||
catch (err) { | ||
console.log(err); | ||
return err; | ||
} | ||
|
||
} |
Oops, something went wrong.