Browse Source

Updated Mongoose package on backend.

KrisVos130 7 years ago
parent
commit
143c49959d
3 changed files with 176 additions and 187 deletions
  1. 156 159
      backend/logic/db/index.js
  2. 19 27
      backend/package-lock.json
  3. 1 1
      backend/package.json

+ 156 - 159
backend/logic/db/index.js

@@ -24,175 +24,172 @@ let lockdown = false;
 
 let lib = {
 
-	connection: null,
 	schemas: {},
 	models: {},
 
 	init: (url, errorCb,  cb) => {
-		lib.connection = mongoose.connect(url).connection;
-
-		lib.connection.on('error', err => {
-			errorCb('Database connection error.', err, 'DB');
-		});
-
-		lib.connection.once('open', _ => {
-
-			lib.schemas = {
-				song: new mongoose.Schema(require(`./schemas/song`)),
-				queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
-				station: new mongoose.Schema(require(`./schemas/station`)),
-				user: new mongoose.Schema(require(`./schemas/user`)),
-				playlist: new mongoose.Schema(require(`./schemas/playlist`)),
-				news: new mongoose.Schema(require(`./schemas/news`)),
-				report: new mongoose.Schema(require(`./schemas/report`)),
-				punishment: new mongoose.Schema(require(`./schemas/punishment`))
-			};
-
-			lib.models = {
-				song: mongoose.model('song', lib.schemas.song),
-				queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
-				station: mongoose.model('station', lib.schemas.station),
-				user: mongoose.model('user', lib.schemas.user),
-				playlist: mongoose.model('playlist', lib.schemas.playlist),
-				news: mongoose.model('news', lib.schemas.news),
-				report: mongoose.model('report', lib.schemas.report),
-				punishment: mongoose.model('punishment', lib.schemas.punishment)
-			};
-
-			lib.schemas.user.path('username').validate((username) => {
-				return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
-			}, 'Invalid username.');
-
-			lib.schemas.user.path('email.address').validate((email) => {
-				if (!isLength(email, 3, 254)) return false;
-				if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
-				return regex.emailSimple.test(email);
-			}, 'Invalid email.');
-
-			lib.schemas.station.path('name').validate((id) => {
-				return (isLength(id, 2, 16) && regex.az09_.test(id));
-			}, 'Invalid station name.');
-
-			lib.schemas.station.path('displayName').validate((displayName) => {
-				return (isLength(displayName, 2, 32) && regex.azAZ09_.test(displayName));
-			}, 'Invalid display name.');
-
-			lib.schemas.station.path('description').validate((description) => {
-				if (!isLength(description, 2, 200)) return false;
-				let characters = description.split("");
-				return characters.filter((character) => {
-					return character.charCodeAt(0) === 21328;
-				}).length === 0;
-			}, 'Invalid display name.');
-
-
-			lib.schemas.station.path('owner').validate((owner, callback) => {
-				lib.models.station.count({owner: owner}, (err, c) => {
-					callback(!(err || c >= 3));
-				});
-			}, 'User already has 3 stations.');
-
-			/*
-			lib.schemas.station.path('queue').validate((queue, callback) => {
-				let totalDuration = 0;
-				queue.forEach((song) => {
-					totalDuration += song.duration;
-				});
-				return callback(totalDuration <= 3600 * 3);
-			}, 'The max length of the queue is 3 hours.');
-
-			lib.schemas.station.path('queue').validate((queue, callback) => {
-				if (queue.length === 0) return callback(true);
-				let totalDuration = 0;
-				const userId = queue[queue.length - 1].requestedBy;
-				queue.forEach((song) => {
-					if (userId === song.requestedBy) {
+		mongoose.connect(url, {useMongoClient: true}).then(
+			() => {
+				lib.schemas = {
+					song: new mongoose.Schema(require(`./schemas/song`)),
+					queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
+					station: new mongoose.Schema(require(`./schemas/station`)),
+					user: new mongoose.Schema(require(`./schemas/user`)),
+					playlist: new mongoose.Schema(require(`./schemas/playlist`)),
+					news: new mongoose.Schema(require(`./schemas/news`)),
+					report: new mongoose.Schema(require(`./schemas/report`)),
+					punishment: new mongoose.Schema(require(`./schemas/punishment`))
+				};
+
+				lib.models = {
+					song: mongoose.model('song', lib.schemas.song),
+					queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
+					station: mongoose.model('station', lib.schemas.station),
+					user: mongoose.model('user', lib.schemas.user),
+					playlist: mongoose.model('playlist', lib.schemas.playlist),
+					news: mongoose.model('news', lib.schemas.news),
+					report: mongoose.model('report', lib.schemas.report),
+					punishment: mongoose.model('punishment', lib.schemas.punishment)
+				};
+
+				lib.schemas.user.path('username').validate((username) => {
+					return (isLength(username, 2, 32) && regex.azAZ09_.test(username));
+				}, 'Invalid username.');
+
+				lib.schemas.user.path('email.address').validate((email) => {
+					if (!isLength(email, 3, 254)) return false;
+					if (email.indexOf('@') !== email.lastIndexOf('@')) return false;
+					return regex.emailSimple.test(email);
+				}, 'Invalid email.');
+
+				lib.schemas.station.path('name').validate((id) => {
+					return (isLength(id, 2, 16) && regex.az09_.test(id));
+				}, 'Invalid station name.');
+
+				lib.schemas.station.path('displayName').validate((displayName) => {
+					return (isLength(displayName, 2, 32) && regex.azAZ09_.test(displayName));
+				}, 'Invalid display name.');
+
+				lib.schemas.station.path('description').validate((description) => {
+					if (!isLength(description, 2, 200)) return false;
+					let characters = description.split("");
+					return characters.filter((character) => {
+						return character.charCodeAt(0) === 21328;
+					}).length === 0;
+				}, 'Invalid display name.');
+
+
+				lib.schemas.station.path('owner').validate((owner, callback) => {
+					lib.models.station.count({owner: owner}, (err, c) => {
+						callback(!(err || c >= 3));
+					});
+				}, 'User already has 3 stations.');
+
+				/*
+				lib.schemas.station.path('queue').validate((queue, callback) => {
+					let totalDuration = 0;
+					queue.forEach((song) => {
 						totalDuration += song.duration;
-					}
-				});
-				return callback(totalDuration <= 900);
-			}, 'The max length of songs per user is 15 minutes.');
-
-			lib.schemas.station.path('queue').validate((queue, callback) => {
-				if (queue.length === 0) return callback(true);
-				let totalSongs = 0;
-				const userId = queue[queue.length - 1].requestedBy;
-				queue.forEach((song) => {
-					if (userId === song.requestedBy) {
-						totalSongs++;
-					}
-				});
-				if (totalSongs <= 2) return callback(true);
-				if (totalSongs > 3) return callback(false);
-				if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
-				return callback(false);
-			}, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
-			*/
-
-			let songTitle = (title) => {
-				return (isLength(title, 1, 64) && regex.ascii.test(title));
-			};
-			lib.schemas.song.path('title').validate(songTitle, 'Invalid title.');
-			lib.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
-
-			lib.schemas.song.path('artists').validate((artists) => {
-				return !(artists.length < 1 || artists.length > 10);
-			}, 'Invalid artists.');
-			lib.schemas.queueSong.path('artists').validate((artists) => {
-				return !(artists.length < 0 || artists.length > 10);
-			}, 'Invalid artists.');
-
-			let songArtists = (artists) => {
-				return artists.filter((artist) => {
+					});
+					return callback(totalDuration <= 3600 * 3);
+				}, 'The max length of the queue is 3 hours.');
+
+				lib.schemas.station.path('queue').validate((queue, callback) => {
+					if (queue.length === 0) return callback(true);
+					let totalDuration = 0;
+					const userId = queue[queue.length - 1].requestedBy;
+					queue.forEach((song) => {
+						if (userId === song.requestedBy) {
+							totalDuration += song.duration;
+						}
+					});
+					return callback(totalDuration <= 900);
+				}, 'The max length of songs per user is 15 minutes.');
+
+				lib.schemas.station.path('queue').validate((queue, callback) => {
+					if (queue.length === 0) return callback(true);
+					let totalSongs = 0;
+					const userId = queue[queue.length - 1].requestedBy;
+					queue.forEach((song) => {
+						if (userId === song.requestedBy) {
+							totalSongs++;
+						}
+					});
+					if (totalSongs <= 2) return callback(true);
+					if (totalSongs > 3) return callback(false);
+					if (queue[queue.length - 2].requestedBy !== userId || queue[queue.length - 3] !== userId) return callback(true);
+					return callback(false);
+				}, 'The max amount of songs per user is 3, and only 2 in a row is allowed.');
+				*/
+
+				let songTitle = (title) => {
+					return (isLength(title, 1, 64) && regex.ascii.test(title));
+				};
+				lib.schemas.song.path('title').validate(songTitle, 'Invalid title.');
+				lib.schemas.queueSong.path('title').validate(songTitle, 'Invalid title.');
+
+				lib.schemas.song.path('artists').validate((artists) => {
+					return !(artists.length < 1 || artists.length > 10);
+				}, 'Invalid artists.');
+				lib.schemas.queueSong.path('artists').validate((artists) => {
+					return !(artists.length < 0 || artists.length > 10);
+				}, 'Invalid artists.');
+
+				let songArtists = (artists) => {
+					return artists.filter((artist) => {
 						return (isLength(artist, 1, 32) && regex.ascii.test(artist) && artist !== "NONE");
 					}).length === artists.length;
-			};
-			lib.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
-			lib.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
+				};
+				lib.schemas.song.path('artists').validate(songArtists, 'Invalid artists.');
+				lib.schemas.queueSong.path('artists').validate(songArtists, 'Invalid artists.');
 
-			let songGenres = (genres) => {
-				return genres.filter((genre) => {
+				let songGenres = (genres) => {
+					return genres.filter((genre) => {
 						return (isLength(genre, 1, 16) && regex.az09_.test(genre));
 					}).length === genres.length;
-			};
-			lib.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
-			lib.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
-
-			lib.schemas.song.path('thumbnail').validate((thumbnail) => {
-				return isLength(thumbnail, 8, 256);
-			}, 'Invalid thumbnail.');
-			lib.schemas.queueSong.path('thumbnail').validate((thumbnail) => {
-				return isLength(thumbnail, 0, 256);
-			}, 'Invalid thumbnail.');
-
-			lib.schemas.playlist.path('displayName').validate((displayName) => {
-				return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
-			}, 'Invalid display name.');
-
-			lib.schemas.playlist.path('createdBy').validate((createdBy, callback) => {
-				lib.models.playlist.count({createdBy: createdBy}, (err, c) => {
-					callback(!(err || c >= 10));
-				});
-			}, 'Max 10 playlists per user.');
-
-			lib.schemas.playlist.path('songs').validate((songs) => {
-				return songs.length <= 2000;
-			}, 'Max 2000 songs per playlist.');
-
-			lib.schemas.playlist.path('songs').validate((songs) => {
-				if (songs.length === 0) return true;
-				return songs[0].duration <= 10800;
-			}, 'Max 3 hours per song.');
-
-			lib.schemas.report.path('description').validate((description) => {
-				return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
-			}, 'Invalid description.');
-
-			initialized = true;
-
-			if (lockdown) return this._lockdown();
-			cb();
-		});
+				};
+				lib.schemas.song.path('genres').validate(songGenres, 'Invalid genres.');
+				lib.schemas.queueSong.path('genres').validate(songGenres, 'Invalid genres.');
+
+				lib.schemas.song.path('thumbnail').validate((thumbnail) => {
+					return isLength(thumbnail, 8, 256);
+				}, 'Invalid thumbnail.');
+				lib.schemas.queueSong.path('thumbnail').validate((thumbnail) => {
+					return isLength(thumbnail, 0, 256);
+				}, 'Invalid thumbnail.');
+
+				lib.schemas.playlist.path('displayName').validate((displayName) => {
+					return (isLength(displayName, 1, 16) && regex.ascii.test(displayName));
+				}, 'Invalid display name.');
+
+				lib.schemas.playlist.path('createdBy').validate((createdBy, callback) => {
+					lib.models.playlist.count({createdBy: createdBy}, (err, c) => {
+						callback(!(err || c >= 10));
+					});
+				}, 'Max 10 playlists per user.');
+
+				lib.schemas.playlist.path('songs').validate((songs) => {
+					return songs.length <= 2000;
+				}, 'Max 2000 songs per playlist.');
+
+				lib.schemas.playlist.path('songs').validate((songs) => {
+					if (songs.length === 0) return true;
+					return songs[0].duration <= 10800;
+				}, 'Max 3 hours per song.');
+
+				lib.schemas.report.path('description').validate((description) => {
+					return (!description || (isLength(description, 0, 400) && regex.ascii.test(description)));
+				}, 'Invalid description.');
+
+				initialized = true;
+
+				if (lockdown) return this._lockdown();
+				cb();
+			},
+			err => {
+				errorCb('Database connection error.', err, 'DB');
+			}
+		);
 	},
 
 	passwordValid: (password) => {

+ 19 - 27
backend/package-lock.json

@@ -1008,9 +1008,9 @@
       }
     },
     "kareem": {
-      "version": "1.4.1",
-      "resolved": "https://registry.npmjs.org/kareem/-/kareem-1.4.1.tgz",
-      "integrity": "sha1-7XYgAET6BB7zK02oJh4lU/EXNTE="
+      "version": "1.5.0",
+      "resolved": "https://registry.npmjs.org/kareem/-/kareem-1.5.0.tgz",
+      "integrity": "sha1-4+QQHZ3P3imXadr0tNtk2JXRdEg="
     },
     "levn": {
       "version": "0.3.0",
@@ -1133,20 +1133,20 @@
       }
     },
     "mongoose": {
-      "version": "4.11.1",
-      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-4.11.1.tgz",
-      "integrity": "sha1-JWC22J50SwWFfQJMq4sxYGZxbj4=",
+      "version": "4.11.7",
+      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-4.11.7.tgz",
+      "integrity": "sha512-tRYcThB+qx8yzVdG7rY8mlBUrF5CEJADl4Vlp3MMI9vVpgyFtOJfC2/IZiGmNFNThJjNIESBUxgoFrCRVDcLCw==",
       "requires": {
         "async": "2.1.4",
         "bson": "1.0.4",
         "hooks-fixed": "2.0.0",
-        "kareem": "1.4.1",
-        "mongodb": "2.2.27",
+        "kareem": "1.5.0",
+        "mongodb": "2.2.31",
         "mpath": "0.3.0",
         "mpromise": "0.5.5",
         "mquery": "2.3.1",
         "ms": "2.0.0",
-        "muri": "1.2.1",
+        "muri": "1.2.2",
         "regexp-clone": "0.0.1",
         "sliced": "1.0.1"
       },
@@ -1160,19 +1160,19 @@
           }
         },
         "mongodb": {
-          "version": "2.2.27",
-          "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.27.tgz",
-          "integrity": "sha1-NBIgNNtm2YO89qta2yaiSnD+9uY=",
+          "version": "2.2.31",
+          "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.31.tgz",
+          "integrity": "sha1-GUBEXGYeGSF7s7+CRdmFSq71SNs=",
           "requires": {
             "es6-promise": "3.2.1",
-            "mongodb-core": "2.1.11",
+            "mongodb-core": "2.1.15",
             "readable-stream": "2.2.7"
           }
         },
         "mongodb-core": {
-          "version": "2.1.11",
-          "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-2.1.11.tgz",
-          "integrity": "sha1-HDh3bOsXSZepnCiGDu2QKNqbPho=",
+          "version": "2.1.15",
+          "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-2.1.15.tgz",
+          "integrity": "sha1-hB9TuH//9MdFgYnDXIroJ+EWl2Q=",
           "requires": {
             "bson": "1.0.4",
             "require_optional": "1.0.1"
@@ -1227,9 +1227,9 @@
       "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
     },
     "muri": {
-      "version": "1.2.1",
-      "resolved": "https://registry.npmjs.org/muri/-/muri-1.2.1.tgz",
-      "integrity": "sha1-7H6lzmympSPrGrNbrNpfqBbJqjw="
+      "version": "1.2.2",
+      "resolved": "https://registry.npmjs.org/muri/-/muri-1.2.2.tgz",
+      "integrity": "sha1-YxmBMmUNsIoEzHnM0A3Tia/SYxw="
     },
     "nan": {
       "version": "2.3.5",
@@ -1368,14 +1368,6 @@
         "pause": "0.0.1"
       }
     },
-    "passport-discord": {
-      "version": "0.1.2",
-      "resolved": "https://registry.npmjs.org/passport-discord/-/passport-discord-0.1.2.tgz",
-      "integrity": "sha1-W4k6IAdgPGyC7IJOZ6NaRijUeVU=",
-      "requires": {
-        "passport-oauth2": "1.4.0"
-      }
-    },
     "passport-github": {
       "version": "1.1.0",
       "resolved": "https://registry.npmjs.org/passport-github/-/passport-github-1.1.0.tgz",

+ 1 - 1
backend/package.json

@@ -25,7 +25,7 @@
     "i18next-sync-fs-backend": "^1.0.0",
     "mailgun-js": "^0.8.0",
     "moment": "^2.15.2",
-    "mongoose": "^4.9.0",
+    "mongoose": "^4.11.7",
     "oauth": "^0.9.14",
     "passport": "^0.3.2",
     "passport-github": "^1.1.0",