-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/refactor/models'
- Loading branch information
Showing
9 changed files
with
189 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mongoose from 'mongoose'; | ||
import { createSchema } from 'mongoose-model-decorators'; | ||
|
||
const Types = mongoose.Schema.Types; | ||
|
||
export default () => { | ||
class Authentication { | ||
static schema = { | ||
user: { type: Types.ObjectId, ref: 'User' }, | ||
email: { type: String, max: 254, required: true, unique: true }, | ||
hash: { type: String, required: true }, | ||
salt: { type: String, required: true }, | ||
validated: { type: Boolean, default: false } | ||
}; | ||
} | ||
|
||
return createSchema({ minimize: false })(Authentication); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import mongoose from 'mongoose'; | ||
import { createSchema } from 'mongoose-model-decorators'; | ||
|
||
const Types = mongoose.Schema.Types; | ||
|
||
const listOfUsers = [{ type: Types.ObjectId, ref: 'User' }]; | ||
|
||
export default () => { | ||
class History { | ||
static collection = 'historyentries'; | ||
|
||
static schema = { | ||
user: { type: Types.ObjectId, ref: 'User', required: true }, | ||
playlist: { type: Types.ObjectId, ref: 'Playlist' }, | ||
item: { type: Types.ObjectId, ref: 'PlaylistItem' }, | ||
media: { | ||
media: { type: Types.ObjectId, ref: 'Media', required: true }, | ||
artist: String, | ||
title: String, | ||
start: { type: Number, default: 0 }, | ||
end: { type: Number, default: 0 } | ||
}, | ||
played: { type: Date, default: Date.now }, | ||
upvotes: listOfUsers, | ||
downvotes: listOfUsers, | ||
favorites: listOfUsers | ||
}; | ||
} | ||
|
||
return createSchema({ minimize: false })(History); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createSchema } from 'mongoose-model-decorators'; | ||
|
||
export default () => { | ||
class Media { | ||
static schema = { | ||
artist: { type: String, max: 128, required: true }, | ||
title: { type: String, max: 128, required: true }, | ||
duration: { type: Number, min: 0, default: 0 }, | ||
thumbnail: { type: String, max: 256, default: '' }, | ||
sourceID: { type: String, max: 128, required: true }, | ||
sourceType: { type: String, max: 128, required: true }, | ||
nsfw: { type: Boolean, default: false }, | ||
restricted: [{ type: String, max: 2 }] | ||
}; | ||
} | ||
|
||
return createSchema({ minimize: false })(Media); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import mongoose from 'mongoose'; | ||
import { createSchema } from 'mongoose-model-decorators'; | ||
|
||
const Types = mongoose.Schema.Types; | ||
|
||
export default () => { | ||
class Playlist { | ||
static schema = { | ||
created: { type: Date, default: Date.now }, | ||
name: { type: String, min: 0, max: 128, required: true }, | ||
description: { type: String, min: 0, max: 512 }, | ||
author: { type: Types.ObjectId, ref: 'User', required: true }, | ||
shared: { type: Boolean, default: false }, | ||
nsfw: { type: Boolean, default: false }, | ||
media: [{ type: Types.ObjectId, ref: 'PlaylistItem' }] | ||
}; | ||
|
||
get size(): number { | ||
return this.media.length; | ||
} | ||
|
||
getItemAt(index): Promise { | ||
return this.model('PlaylistItem').findOne(this.media[index]); | ||
} | ||
} | ||
|
||
return createSchema({ minimize: false })(Playlist); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mongoose from 'mongoose'; | ||
import { createSchema } from 'mongoose-model-decorators'; | ||
|
||
const Types = mongoose.Schema.Types; | ||
|
||
export default () => { | ||
class PlaylistItem { | ||
static schema = { | ||
media: { type: Types.ObjectId, ref: 'Media', required: true }, | ||
artist: { type: String, max: 128, required: true }, | ||
title: { type: String, max: 128, required: true }, | ||
start: { type: Number, min: 0, default: 0 }, | ||
end: { type: Number, min: 0, default: 0 } | ||
}; | ||
} | ||
|
||
return createSchema({ minimize: false })(PlaylistItem); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createSchema, pre } from 'mongoose-model-decorators'; | ||
|
||
export default uw => { | ||
class User { | ||
static schema = { | ||
joined: { type: Date, default: Date.now }, | ||
username: { type: String, min: 3, max: 32, required: true, unique: true }, | ||
language: { type: String, min: 2, max: 2, default: 'en' }, | ||
role: { type: Number, min: 0, max: 5, default: 0 }, | ||
avatar: { type: String, min: 0, max: 256, default: '' }, | ||
slug: { type: String, min: 3, max: 256, required: true }, | ||
level: { type: Number, min: 0, max: 9001, default: 0 }, | ||
lastSeen: { type: Date, default: Date.now }, | ||
exiled: { type: Boolean, default: false }, | ||
banned: { type: Date, default: null } | ||
}; | ||
|
||
getActivePlaylistID(): Promise<string> { | ||
return uw.redis.get(`playlist:${this.id}`); | ||
} | ||
|
||
async getActivePlaylist(): Promise { | ||
const playlistID = await this.getActivePlaylistID(); | ||
return await this.model('Playlist').findOne({ _id: playlistID }); | ||
} | ||
|
||
@pre('validate') | ||
makeSlug() { | ||
this.slug = this.username.toLowerCase(); | ||
} | ||
} | ||
|
||
return createSchema({ minimize: true })(User); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import AuthenticationSchema from './Authentication'; | ||
import HistorySchema from './History'; | ||
import MediaSchema from './Media'; | ||
import PlaylistSchema from './Playlist'; | ||
import PlaylistItemSchema from './PlaylistItem'; | ||
import UserSchema from './User'; | ||
|
||
function model(uw, name, schemaCreator) { | ||
const Schema = schemaCreator(uw); | ||
uw.mongo.model(name, new Schema); | ||
} | ||
|
||
export default function models() { | ||
return uw => { | ||
model(uw, 'Authentication', AuthenticationSchema); | ||
model(uw, 'History', HistorySchema); | ||
model(uw, 'Media', MediaSchema); | ||
model(uw, 'Playlist', PlaylistSchema); | ||
model(uw, 'PlaylistItem', PlaylistItemSchema); | ||
model(uw, 'User', UserSchema); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters