index.js 1.4 KB

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