api: fix more mongoose stuff

This commit is contained in:
Paul Bienkowski 2021-09-27 20:35:51 +02:00
parent 50d6697b5b
commit 0784f916b6
7 changed files with 24 additions and 20 deletions

View file

@ -38,6 +38,7 @@ if (!isProduction) {
} }
require('./db'); require('./db');
require('./models');
app.use(require('./routes')); app.use(require('./routes'));

View file

@ -15,7 +15,7 @@ const schema = new mongoose.Schema(
schema.plugin(uniqueValidator, { message: 'reused token' }); schema.plugin(uniqueValidator, { message: 'reused token' });
class AccessToken extends mongoose.Model { class AccessTokenClass extends mongoose.Model {
toJSON() { toJSON() {
return { return {
token: this.token, token: this.token,
@ -24,7 +24,7 @@ class AccessToken extends mongoose.Model {
} }
isValid() { isValid() {
return this.expiresAt < new Date() return this.expiresAt < new Date();
} }
toHeaderString() { toHeaderString() {
@ -42,6 +42,8 @@ class AccessToken extends mongoose.Model {
} }
} }
mongoose.model(AccessToken, schema); schema.loadClass(AccessTokenClass);
const AccessToken = mongoose.model('AccessToken', schema);
module.exports = AccessToken; module.exports = AccessToken;

View file

@ -14,7 +14,7 @@ const schema = new mongoose.Schema(
{ timestamps: true }, { timestamps: true },
); );
class AuthorizationCode extends mongoose.Model { class AuthorizationCodeClass extends mongoose.Model {
static generate(options, expiresInSeconds = 60) { static generate(options, expiresInSeconds = 60) {
const code = crypto.randomBytes(8).toString('hex'); const code = crypto.randomBytes(8).toString('hex');
@ -26,6 +26,7 @@ class AuthorizationCode extends mongoose.Model {
} }
} }
mongoose.model(AuthorizationCode, schema); schema.loadClass(AuthorizationCodeClass)
module.exports = AuthorizationCode; const AuthorizationCode = mongoose.model('AuthorizationCode', schema);
module.exports = AuthorizationCode

View file

@ -9,7 +9,7 @@ const schema = new mongoose.Schema(
{ timestamps: true }, { timestamps: true },
); );
class Comment extends mongoose.Model { class CommentClass extends mongoose.Model {
toJSONFor(user) { toJSONFor(user) {
return { return {
id: this._id, id: this._id,
@ -20,6 +20,5 @@ class Comment extends mongoose.Model {
} }
} }
mongoose.model(Comment, schema); schema.loadClass(CommentClass)
module.exports = mongoose.model('Comment', schema);
module.exports = Comment;

View file

@ -17,7 +17,7 @@ const schema = new mongoose.Schema(
schema.plugin(uniqueValidator, { message: 'reused token' }); schema.plugin(uniqueValidator, { message: 'reused token' });
class RefreshToken extends mongoose.Model { class RefreshTokenClass extends mongoose.Model {
toJSON() { toJSON() {
return { return {
token: this.token, token: this.token,
@ -40,6 +40,7 @@ class RefreshToken extends mongoose.Model {
} }
} }
mongoose.model(RefreshToken, schema); schema.loadClass(RefreshTokenClass);
module.exports = RefreshToken; const RefreshToken = mongoose.model('RefreshToken', schema);
module.exports = RefreshToken

View file

@ -142,7 +142,7 @@ function getDaytime(dateTime) {
return DAYTIMES[Math.floor((dateTime.hour % 24) / 2)]; return DAYTIMES[Math.floor((dateTime.hour % 24) / 2)];
} }
class Track extends mongoose.Model { class TrackClass extends mongoose.Model {
slugify() { slugify() {
this.slug = slug(this.title || 'track') + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36); this.slug = slug(this.title || 'track') + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36);
} }
@ -301,6 +301,6 @@ class Track extends mongoose.Model {
} }
} }
mongoose.model(Track, schema); schema.loadClass(TrackClass);
const Track = mongoose.model('Track', schema);
module.exports = Track; module.exports = Track;

View file

@ -39,7 +39,7 @@ const schema = new mongoose.Schema(
schema.plugin(uniqueValidator, { message: 'ist bereits vergeben. Sorry!' }); schema.plugin(uniqueValidator, { message: 'ist bereits vergeben. Sorry!' });
class User extends mongoose.Model { class UserClass extends mongoose.Model {
validPassword(password) { validPassword(password) {
const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
return this.hash === hash; return this.hash === hash;
@ -86,6 +86,6 @@ class User extends mongoose.Model {
} }
} }
mongoose.model(User, schema); schema.loadClass(UserClass);
const User = mongoose.model('User', schema);
module.exports = User; module.exports = User