index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const mongoose = require('mongoose');
  3. const bluebird = require('bluebird');
  4. mongoose.Promise = bluebird;
  5. let lib = {
  6. connection: null,
  7. schemas: {},
  8. models: {},
  9. init: (url, cb) => {
  10. lib.connection = mongoose.connect(url).connection;
  11. lib.connection.on('error', err => {
  12. console.error('Database error: ' + err.message)
  13. process.exit();
  14. });
  15. lib.connection.once('open', _ => {
  16. lib.schemas = {
  17. song: new mongoose.Schema(require(`./schemas/song`)),
  18. queueSong: new mongoose.Schema(require(`./schemas/queueSong`)),
  19. station: new mongoose.Schema(require(`./schemas/station`)),
  20. user: new mongoose.Schema(require(`./schemas/user`)),
  21. playlist: new mongoose.Schema(require(`./schemas/playlist`)),
  22. news: new mongoose.Schema(require(`./schemas/news`)),
  23. report: new mongoose.Schema(require(`./schemas/report`))
  24. };
  25. lib.schemas.station.path('name').validate((id) => {
  26. return /^[a-z]+$/.test(id);
  27. }, 'The id can only have the letters a-z.');
  28. lib.models = {
  29. song: mongoose.model('song', lib.schemas.song),
  30. queueSong: mongoose.model('queueSong', lib.schemas.queueSong),
  31. station: mongoose.model('station', lib.schemas.station),
  32. user: mongoose.model('user', lib.schemas.user),
  33. playlist: mongoose.model('playlist', lib.schemas.playlist),
  34. news: mongoose.model('news', lib.schemas.news),
  35. report: mongoose.model('report', lib.schemas.report)
  36. };
  37. cb();
  38. });
  39. }
  40. };
  41. module.exports = lib;