1 const mongoose = require('mongoose')
3 const customUsersValidators = require('../helpers/custom-validators').users
4 const modelUtils = require('./utils')
6 // ---------------------------------------------------------------------------
8 const UserSchema = mongoose.Schema({
18 UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
19 UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
20 UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
22 UserSchema.methods = {
23 toFormatedJSON: toFormatedJSON
26 UserSchema.statics = {
27 countTotal: countTotal,
28 getByUsernameAndPassword: getByUsernameAndPassword,
29 listForApi: listForApi,
31 loadByUsername: loadByUsername
34 mongoose.model('User', UserSchema)
36 // ---------------------------------------------------------------------------
38 function countTotal (callback) {
39 return this.count(callback)
42 function getByUsernameAndPassword (username, password) {
43 return this.findOne({ username: username, password: password })
46 function listForApi (start, count, sort, callback) {
48 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
51 function loadById (id, callback) {
52 return this.findById(id, callback)
55 function loadByUsername (username, callback) {
56 return this.findOne({ username: username }, callback)
59 function toFormatedJSON () {
62 username: this.username,