Skip to content

Commit

Permalink
Merge branch 'feature/refactor/models'
Browse files Browse the repository at this point in the history
  • Loading branch information
René Kooi committed Feb 28, 2016
2 parents 4ab876d + 3570ad7 commit b4ce0da
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 16 deletions.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"engines": {
"node": ">= 4.0"
},
"dependencies": {
"bluebird": "^2.10.0",
"body-parser": "^1.13.3",
"debug": "^2.2.0",
"express": "^4.13.3",
"ioredis": "^1.9.0",
"mongoose": "^4.1.6",
"mongoose-model-decorators": "^0.2.1",
"ws": "^0.8.0"
},
"devDependencies": {
"babel-cli": "^6.3.17",
"babel-eslint": "^5.0.0-beta10",
Expand All @@ -27,14 +37,5 @@
"test:lint": "eslint src",
"test": "npm run test:mocha && npm run test:lint",
"build": "babel --out-dir lib src"
},
"dependencies": {
"bluebird": "^2.10.0",
"body-parser": "^1.13.3",
"debug": "^2.2.0",
"express": "^4.13.3",
"ioredis": "^1.9.0",
"mongoose": "^4.1.6",
"ws": "^0.8.0"
}
}
18 changes: 18 additions & 0 deletions src/models/Authentication.js
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);
};
31 changes: 31 additions & 0 deletions src/models/History.js
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);
};
18 changes: 18 additions & 0 deletions src/models/Media.js
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);
};
28 changes: 28 additions & 0 deletions src/models/Playlist.js
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);
};
18 changes: 18 additions & 0 deletions src/models/PlaylistItem.js
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);
};
34 changes: 34 additions & 0 deletions src/models/User.js
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);
};
22 changes: 22 additions & 0 deletions src/models/index.js
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);
};
}
17 changes: 10 additions & 7 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Redis from 'ioredis';
import debug from 'debug';
import http from 'http';

import models from './models';

mongoose.Promise = Promise;

export default class UWaveServer extends EventEmitter {
Expand All @@ -24,9 +26,11 @@ export default class UWaveServer extends EventEmitter {
this.app = express();
this.server = http.createServer(this.app);

this.mongo = null;
this.mongo = mongoose.createConnection();
this.redis = null;

models()(this);

this.log = debug('uwave:server');
this.mongoLog = debug('uwave:mongo');
this.redisLog = debug('uwave:redis');
Expand All @@ -35,11 +39,7 @@ export default class UWaveServer extends EventEmitter {
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.use((req, res, next) => {
/* eslint-disable no-param-reassign */
req.uwave = {
redis: this.redis,
mongo: this.mongo,
keys: this.config.keys
};
req.uwave = this;
/* eslint-enable no-param-reassign */
next();
});
Expand All @@ -56,6 +56,10 @@ export default class UWaveServer extends EventEmitter {
process.on('SIGINT', () => { this.stop(); });
}

model(name) {
return this.mongo.model(name);
}

/**
* Registers middleware on a route
*
Expand Down Expand Up @@ -119,7 +123,6 @@ export default class UWaveServer extends EventEmitter {
}

_createMongoConnection() {
this.mongo = mongoose.createConnection();
const promise = this.mongo.open(
`mongodb://${this.config.mongo.host}:${this.config.mongo.port}/uwave`,
this.config.mongo.options
Expand Down

0 comments on commit b4ce0da

Please sign in to comment.