diff --git a/api/src/index.js b/api/src/index.js index 11eba8e..5a21905 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -38,6 +38,7 @@ if (!isProduction) { } require('./db'); +require('./models'); app.use(require('./routes')); diff --git a/api/src/models/AccessToken.js b/api/src/models/AccessToken.js index d8e7899..d471c58 100644 --- a/api/src/models/AccessToken.js +++ b/api/src/models/AccessToken.js @@ -15,7 +15,7 @@ const schema = new mongoose.Schema( schema.plugin(uniqueValidator, { message: 'reused token' }); -class AccessToken extends mongoose.Model { +class AccessTokenClass extends mongoose.Model { toJSON() { return { token: this.token, @@ -24,7 +24,7 @@ class AccessToken extends mongoose.Model { } isValid() { - return this.expiresAt < new Date() + return this.expiresAt < new Date(); } 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; diff --git a/api/src/models/AuthorizationCode.js b/api/src/models/AuthorizationCode.js index ea91043..41859a2 100644 --- a/api/src/models/AuthorizationCode.js +++ b/api/src/models/AuthorizationCode.js @@ -14,7 +14,7 @@ const schema = new mongoose.Schema( { timestamps: true }, ); -class AuthorizationCode extends mongoose.Model { +class AuthorizationCodeClass extends mongoose.Model { static generate(options, expiresInSeconds = 60) { 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 diff --git a/api/src/models/Comment.js b/api/src/models/Comment.js index 9273542..8a40212 100644 --- a/api/src/models/Comment.js +++ b/api/src/models/Comment.js @@ -9,7 +9,7 @@ const schema = new mongoose.Schema( { timestamps: true }, ); -class Comment extends mongoose.Model { +class CommentClass extends mongoose.Model { toJSONFor(user) { return { id: this._id, @@ -20,6 +20,5 @@ class Comment extends mongoose.Model { } } -mongoose.model(Comment, schema); - -module.exports = Comment; +schema.loadClass(CommentClass) +module.exports = mongoose.model('Comment', schema); diff --git a/api/src/models/RefreshToken.js b/api/src/models/RefreshToken.js index 7847c9b..8eaf247 100644 --- a/api/src/models/RefreshToken.js +++ b/api/src/models/RefreshToken.js @@ -17,7 +17,7 @@ const schema = new mongoose.Schema( schema.plugin(uniqueValidator, { message: 'reused token' }); -class RefreshToken extends mongoose.Model { +class RefreshTokenClass extends mongoose.Model { toJSON() { return { 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 diff --git a/api/src/models/Track.js b/api/src/models/Track.js index 33513c9..ef700f8 100644 --- a/api/src/models/Track.js +++ b/api/src/models/Track.js @@ -142,7 +142,7 @@ function getDaytime(dateTime) { return DAYTIMES[Math.floor((dateTime.hour % 24) / 2)]; } -class Track extends mongoose.Model { +class TrackClass extends mongoose.Model { slugify() { 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; diff --git a/api/src/models/User.js b/api/src/models/User.js index 41d22f0..f49c483 100644 --- a/api/src/models/User.js +++ b/api/src/models/User.js @@ -39,7 +39,7 @@ const schema = new mongoose.Schema( schema.plugin(uniqueValidator, { message: 'ist bereits vergeben. Sorry!' }); -class User extends mongoose.Model { +class UserClass extends mongoose.Model { validPassword(password) { const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); return this.hash === hash; @@ -86,6 +86,6 @@ class User extends mongoose.Model { } } -mongoose.model(User, schema); - -module.exports = User; +schema.loadClass(UserClass); +const User = mongoose.model('User', schema); +module.exports = User