index.js 752 B

123456789101112131415161718192021222324252627282930313233343536
  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.log('Database error: ' + err.message));
  10. lib.connection.once('open', _ => {
  11. lib.schemas = {
  12. song: new mongoose.Schema(require(`./schemas/song`)),
  13. station: new mongoose.Schema(require(`./schemas/station`)),
  14. user: new mongoose.Schema(require(`./schemas/user`))
  15. };
  16. lib.models = {
  17. song: mongoose.model('song', lib.schemas.song),
  18. station: mongoose.model('station', lib.schemas.station),
  19. user: mongoose.model('user', lib.schemas.user)
  20. };
  21. cb();
  22. });
  23. }
  24. };
  25. module.exports = lib;