mongoose.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Connects to MongoDB using the mongoose driver
  2. // Handles related routes
  3. var h = require('../helper');
  4. var Promise = require('bluebird');
  5. // Can treat mongoose library as one that supports Promises
  6. // these methods will then have "-Async" appended to them.
  7. var Mongoose = Promise.promisifyAll(require('mongoose'));
  8. var connection = Mongoose.connect('mongodb://127.0.0.1/hello_world');
  9. var WorldSchema = new Mongoose.Schema({
  10. id : Number,
  11. randomNumber: Number
  12. }, {
  13. collection: 'world'
  14. });
  15. var FortuneSchema = new Mongoose.Schema({
  16. id: Number,
  17. message: String
  18. }, {
  19. collection: 'fortune'
  20. });
  21. var Worlds = connection.model('World', WorldSchema);
  22. var Fortunes = connection.model('Fortune', FortuneSchema);
  23. function randomWorldPromise() {
  24. var id = h.randomTfbNumber();
  25. var promise = Worlds
  26. .findOneAsync({
  27. id: id
  28. })
  29. .then(function (world) {
  30. return world;
  31. })
  32. .catch(function (err) {
  33. process.exit(1);
  34. });
  35. return promise;
  36. }
  37. function promiseAllFortunes() {
  38. var promise = Fortunes
  39. .findAsync({})
  40. .then(function (fortunes) {
  41. return fortunes;
  42. })
  43. .catch(function (err) {
  44. process.exit(1);
  45. });
  46. return promise;
  47. }
  48. function updateWorld(world) {
  49. var promise = Worlds
  50. .updateAsync({
  51. id: world.randomNumber
  52. }, {
  53. randomNumber: world.randomNumber
  54. })
  55. .then(function (result) {
  56. return world;
  57. })
  58. .catch(function (err) {
  59. process.exit(1);
  60. });
  61. return promise;
  62. }
  63. module.exports = {
  64. SingleQuery: function(req, reply) {
  65. randomWorldPromise()
  66. .then(function (world) {
  67. reply(world)
  68. .header('Server', 'hapi');
  69. });
  70. },
  71. MultipleQueries: function(req, reply) {
  72. var queries = h.getQueries(req);
  73. var worldPromises = h.fillArray(randomWorldPromise(), queries);
  74. Promise
  75. .all(worldPromises)
  76. .then(function (worlds) {
  77. reply(worlds)
  78. .header('Server', 'hapi');
  79. });
  80. },
  81. Fortunes: function(req, reply) {
  82. promiseAllFortunes()
  83. .then(function (fortunes) {
  84. fortunes.push(h.ADDITIONAL_FORTUNE);
  85. fortunes.sort(function (a, b) {
  86. return a.message.localeCompare(b.message);
  87. });
  88. reply.view('fortunes', {
  89. fortunes: fortunes
  90. })
  91. .header('Content-Type', 'text/html')
  92. .header('Server', 'hapi');
  93. });
  94. },
  95. Updates: function(req, reply) {
  96. var queries = h.getQueries(req);
  97. var worldPromises = [];
  98. for (var i = 0; i < queries; i++) {
  99. worldPromises.push(randomWorldPromise());
  100. }
  101. Promise
  102. .all(worldPromises)
  103. .map(function (world) {
  104. world.randomNumber = h.randomTfbNumber();
  105. return updateWorld(world);
  106. })
  107. .then(function (worlds) {
  108. reply(worlds)
  109. .header('Server', 'hapi');
  110. });
  111. }
  112. };