Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
web-api-development/models/users.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
110 lines (96 sloc)
2.12 KB
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
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; | |
} | |
} |