Skip to content

Instantly share code, notes, and snippets.

@oakinogundeji
Last active April 7, 2023 11:02
Show Gist options
  • Save oakinogundeji/dd36aa05972fb5d59b3dc3bd5834891d to your computer and use it in GitHub Desktop.
Save oakinogundeji/dd36aa05972fb5d59b3dc3bd5834891d to your computer and use it in GitHub Desktop.
naive user model
'use strict';
/**
* Module dependencies
*/
const
Mongoose = require('mongoose'),
argon2 = require('argon2');
/**
* User Schema
*/
const UserSchema = new Mongoose.Schema({
username: {
type: String,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
hasPhotos: {
type: Boolean,
required: true,
default: false
},
photos: [{
filename: {
type: String,
default: ''
},
createdAt: {
type: Date,
default: Date.now,
required: true
},
fullsize: {
type: String
},
thumbnail: {
type: String
}
}]
});
/**
* User Schema Methods
*/
UserSchema.methods.generateHash = async function (password) {
return await argon2.hash(password);
};
UserSchema.methods.validatePassword = async function (candidate) {
return await argon2.verify(this.password, candidate);
};
/**
* Create Schema Secondary Indexes
*/
UserSchema.index({"photos.filename": 1});
/**
* Compile Schema to Model
*/
const UserModel = Mongoose.model('User', UserSchema);
/**
* Export UserModel
*/
module.exports = UserModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment