Guest User

Untitled

a guest
Dec 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var async = require('async');
  3.  
  4. mongoose.connect("mongodb://localhost/so_quotes");
  5.  
  6. // ===================================================
  7. // Quote schema
  8.  
  9. var QuoteSchema = new mongoose.Schema({
  10. quote: String
  11. });
  12.  
  13. // Finds a random quote based on how many there are in the collection
  14.  
  15. QuoteSchema.statics.random = function(cb) {
  16. this.count(function(err, count) {
  17. if (err) return cb(err);
  18. var rand = Math.floor(Math.random() * count);
  19. this.findOne().skip(rand).exec(cb);
  20. }.bind(this));
  21. };
  22.  
  23. // Finds the 'next quote'
  24.  
  25. QuoteSchema.methods.next = function(cb) {
  26. var model = this.model("Quote");
  27. model.findOne().where('_id').gt(this._id).exec(function(err, quote) {
  28. if (err) throw err;
  29.  
  30. if (quote) {
  31. cb(null, quote);
  32. } else {
  33. // If quote is null, we've wrapped around.
  34. model.findOne(cb);
  35. }
  36. });
  37. };
  38.  
  39. var Quote = mongoose.model("Quote", QuoteSchema);
  40.  
  41. // ===================================================
  42. // User schema
  43.  
  44. var UserSchema = new mongoose.Schema({
  45. user: String,
  46. lastQuote: { type: mongoose.Schema.Types.ObjectId, ref: 'Quote' }
  47. });
  48.  
  49. var User = mongoose.model("User", UserSchema);
  50.  
  51. // ===================================================
  52. // Clear out old data and re-initialize each time the program runs
  53.  
  54. function reset(callback) {
  55. async.parallel([
  56. Quote.remove.bind(Quote),
  57. User.remove.bind(User)
  58. ], function(err) {
  59. if (err) throw err;
  60. callback();
  61. });
  62. }
  63.  
  64. // ===================================================
  65. // Seed the database with initial data.
  66.  
  67. function seed(callback) {
  68. quotes = [
  69. "Chuck Norris counted to infinity - twice.",
  70. "When the Boogeyman goes to sleep every night he checks his closet for Chuck Norris.",
  71. "Chuck Norris has already been to Mars; that's why there are no signs of life there.",
  72. "The chief export of Chuck Norris is pain.",
  73. "There is no chin behind Chuck Norris' beard. There is only another fist.",
  74. "When Chuck Norris had surgery, the anesthesia was applied to the doctors.",
  75. "Chuck Norris is what Willis was talking about.",
  76. "If you have five dollars and Chuck Norris has five dollars, Chuck Norris has more money than you.",
  77. "Ironically, Chuck Norris' idden talent is invisibility."
  78. ];
  79.  
  80. async.parallel([
  81. function(cb) {
  82. async.forEachSeries(quotes, function(quote, eachCallback) {
  83. var q = new Quote({quote: quote});
  84. q.save(eachCallback);
  85. }, cb);
  86. },
  87. function(cb) {
  88. var user = new User({user: "JohnDoe"});
  89. user.save(cb);
  90. }
  91. ], function(err, results) {
  92. if (err) throw err;
  93. callback();
  94. })
  95. }
  96.  
  97. module.exports = {
  98. Quote: Quote,
  99. User: User,
  100. reset: reset,
  101. seed: seed
  102. };
Add Comment
Please, Sign In to add comment