Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema;
  3.  
  4. module.exports = (cfg) => {
  5. console.log('Initializing MongoDB module')
  6.  
  7. const uri = `mongodb://${cfg.host}:${cfg.port}/${cfg.name}`;
  8.  
  9. mongoose.connect(uri, {
  10. promiseLibrary: global.Promise
  11. }).then(() => {
  12. console.log('MongoDB connection is initialized');
  13. }).catch(e => {
  14. console.log('Couldn\'t connect to MongoDB');
  15.  
  16. process.exit();
  17. });
  18.  
  19. const userSchema = new Schema({
  20. username: { type: String, required: true, index: { unique: true } },
  21. password: { type: String, required: true },
  22. avatar: { type: String },
  23. firstLogin: { type: Date, default: Date.now }
  24. });
  25.  
  26. const chatSchema = new Schema({
  27. username: String,
  28. message: String,
  29. date: Date
  30. });
  31.  
  32. this.User = mongoose.model('User', userSchema);
  33. this.Chat = mongoose.model('Chat', chatSchema);
  34. this.db = mongoose.connection;
  35.  
  36. return this;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement