|
1 | | -import { DbConnectionError, errorHandler } from '@chronosrx/common'; |
2 | | -const mongoose = require('mongoose'); |
3 | | -require('dotenv').config(); |
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import bcrypt from 'bcryptjs'; |
| 3 | +//define attrs |
| 4 | +interface UserAttrs { |
| 5 | + username: string; |
| 6 | + password: string; |
| 7 | +} |
| 8 | +// add a method 'build' to the UserModel |
| 9 | +// mongoose has built-in Model class that takes 'UserDoc' |
| 10 | +interface UserModel extends mongoose.Model<UserDoc> { |
| 11 | + build(attrs: UserAttrs): UserDoc; |
| 12 | +} |
| 13 | +//create user data in the database in this shape |
| 14 | +interface UserDoc extends mongoose.Document { |
| 15 | + username: string; |
| 16 | + password: string; |
| 17 | +} |
| 18 | +//create the Schema in mongoose with defined requirements |
| 19 | +const userSchema = new mongoose.Schema( |
| 20 | + { |
| 21 | + username: { |
| 22 | + type: String, |
| 23 | + required: true, |
| 24 | + unique: true, |
| 25 | + }, |
| 26 | + password: { |
| 27 | + type: String, |
| 28 | + required: true, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + //anytime we create Json formatted data, transform the user document as following |
| 33 | + toJSON: { |
| 34 | + transform(doc, ret) { |
| 35 | + ret.id = ret._id; |
| 36 | + delete ret._id; |
| 37 | + delete ret.password; |
| 38 | + delete ret.__v; |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | +); |
| 43 | +//pre is built-in moogoose function that runs before the function 'save' takes place |
| 44 | +userSchema.pre('save', async function () { |
| 45 | + // Check if password has been created or modified |
| 46 | + if (!this.isModified('password')) return; |
| 47 | + //if the password is modified, hash the password |
| 48 | + const salt = await bcrypt.genSalt(10); |
| 49 | + const hashedPassword = await bcrypt.hash(this.password, salt); |
| 50 | + this.password = hashedPassword; |
| 51 | +}); |
| 52 | +//schema has property: "method", custom define "comparePassword" |
| 53 | +userSchema.methods.comparePassword = async function (providedPassword: string) { |
| 54 | + const isMatch = await bcrypt.compare(providedPassword, this.password); |
| 55 | + return isMatch; |
| 56 | +}; |
4 | 57 |
|
5 | | -const MONGO_URI = process.env.MONGO_URI; |
| 58 | +userSchema.statics.build = (attrs: UserAttrs) => { |
| 59 | + //returning user document with (attrs) passed in |
| 60 | + return new User(attrs); |
| 61 | +}; |
6 | 62 |
|
7 | | -mongoose |
8 | | - .connect(MONGO_URI, { |
9 | | - useNewUrlParser: true, |
10 | | - useUnifiedTopology: true, |
11 | | - }) |
12 | | - .then(() => console.log(`Connected to MongoDB Database`)) |
13 | | - .catch(err => { |
14 | | - throw new DbConnectionError(); |
15 | | - }); |
| 63 | +const User = mongoose.model<UserDoc, UserModel>('User', userSchema); |
16 | 64 |
|
17 | | -const Schema = mongoose.Schema; |
18 | | -const UserSchema = new Schema({ |
19 | | - username: { type: String, require: true }, |
20 | | - password: { type: String, require: true }, |
21 | | -}); |
| 65 | +export { User }; |
| 66 | + |
| 67 | +// const newUser = User.create({ |
| 68 | +// poo: 'doo', |
| 69 | +// doo: 'doo' |
| 70 | +// }) |
| 71 | + |
| 72 | +// const testUser = User.build({ |
| 73 | +// username: 'derp', |
| 74 | +// password: 'derp', |
| 75 | +// doo: 'doo' |
| 76 | +// }); |
| 77 | +// await testUser.save() |
22 | 78 |
|
23 | | -const User = mongoose.model('User', UserSchema); |
| 79 | +// const thisUser = await User.findById(userId) |
| 80 | +// res.send(thisUser) || res.json(thisUser) |
24 | 81 |
|
25 | | -module.exports = { User }; |
| 82 | +// testUser.password = "derpdeedoo" |
0 commit comments