feat(database): add users model

This commit is contained in:
frab1t 2019-07-04 08:51:08 +02:00
parent 6dbb59ba94
commit 591810d484
1 changed files with 35 additions and 0 deletions

35
src/models/users.js Normal file
View File

@ -0,0 +1,35 @@
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const saltRounds = 10;
// Define a schema
const { Schema } = mongoose;
const UserSchema = new Schema({
name: {
type: String,
trim: true,
required: true,
},
email: {
type: String,
trim: true,
required: true,
},
password: {
type: String,
trim: true,
required: true,
},
});
// hash
// eslint-disable-next-line func-names
UserSchema.pre('save', function (next) {
const user = this;
user.password = bcrypt.hashSync(user.password, saltRounds);
next();
});
module.exports = mongoose.model('User', UserSchema);