1
0
Fork 0
peertube/server/models/user.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

const mongoose = require('mongoose')
const customUsersValidators = require('../helpers/custom-validators').users
2016-08-16 20:31:45 +00:00
const modelUtils = require('./utils')
// ---------------------------------------------------------------------------
const UserSchema = mongoose.Schema({
2016-08-16 20:31:45 +00:00
createdDate: {
type: Date,
default: Date.now
},
password: String,
username: String,
role: String
})
UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
UserSchema.methods = {
toFormatedJSON: toFormatedJSON
}
UserSchema.statics = {
2016-08-16 20:31:45 +00:00
countTotal: countTotal,
2016-07-20 14:23:58 +00:00
getByUsernameAndPassword: getByUsernameAndPassword,
2016-08-16 20:31:45 +00:00
listForApi: listForApi,
loadById: loadById,
loadByUsername: loadByUsername
}
mongoose.model('User', UserSchema)
// ---------------------------------------------------------------------------
2016-08-16 20:31:45 +00:00
function countTotal (callback) {
return this.count(callback)
}
function getByUsernameAndPassword (username, password) {
return this.findOne({ username: username, password: password })
}
2016-08-16 20:31:45 +00:00
function listForApi (start, count, sort, callback) {
const query = {}
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
}
function loadById (id, callback) {
return this.findById(id, callback)
}
function loadByUsername (username, callback) {
return this.findOne({ username: username }, callback)
}
function toFormatedJSON () {
return {
id: this._id,
username: this.username,
role: this.role
}
}